I was talking about something like that.
movement.lua
local M = {}
function M.directional_movement(position, direction, speed)
return coroutine.create(function(dt)
while true do
position = position + (dt * speed) * direction
coroutine.yield(position)
end
end)
end
return M
Ooops, updated code, reread if you already did it. input.script:
function init(self)
msg.post(".", "acquire_input_focus")
self.direction = vmath.vector3(0,0,0)
self.new_input = false
end
function on_input(self, action_id, action)
if action_id == hash("left") and action.pressed then
self.direction.x = self.direction.x - 1
self.new_input = true
end
if action_id == hash("right") and action.pressed then
self.direction.x = self.direction.x + 1
self.new_input = true
end
if action_id == hash("up") and action.pressed then
self.direction.y = self.direction.y + 1
self.new_input = true
end
if action_id == hash("down") and action.pressed then
self.direction.y = self.direction.y - 1
self.new_input = true
end
if self.new_input then
self.direction = vmath.normalize(self.direction)
msg.post("#player", "input", { direction = self.direction })
self.new_input = false
end
end
player.script
local movement = require("main.movement")
function init(self)
self.speed = 50
self.move = nil
end
function update(self, dt)
if self.move then
local new_position = select(2, coroutine.resume(self.move, dt))
go.set_position(new_position)
end
end
function on_message(self, message_id, message)
if message_id == hash("input") then
local position = go.get_position()
self.move = movement.directional_movement(position, message.direction, self.speed)
end
end
bot.script
local movement = require("main.movement")
function init(self)
self.speed = 10
self.move = nil
end
local function direction_to_player(self)
-- here find direction to player in form of normalized vector3
end
local function follow_player(self)
-- call this function when you want bot start following player
-- collision, msg whaterver you want
-- then
self.move = movement.directional_movement(go.get_position(), direction_to_player(self), self.speed)
end
function update(self, dt)
if self.move then
local new_position = select(2, coroutine.resume(self.move, dt))
go.set_position(new_position)
end
end