Ok, so here is the code for the camera movement:
function update(self, dt)
self.pos = go.get_position("hero")
self.pos.x = self.pos.x - self.window_width * 0.5 * self.upscale_factor
self.pos.y = self.pos.y - self.window_height * 0.5 * self.upscale_factor + 50
go.set_position(self.pos)
end
this is the player movement script:
local input_left = hash("move_left")
local input_right = hash("move_right")
-- max speed right/left
go.property("max_speed", 30)
-- the acceleration to move right/left
go.property("move_acceleration", 1000)
function init(self)
msg.post(".", "acquire_input_focus")
-- initial player velocity
self.velocity = vmath.vector3(0, 0, 0)
-- movement input in the range [-1,1]
self.move_input = 0
end
function update(self, dt)
-- determine the target speed based on input
local target_speed = self.move_input * self.max_speed
-- calculate the difference between our current speed and the target speed
local speed_diff = target_speed - self.velocity.x
-- the complete acceleration to integrate over this frame
local acceleration = vmath.vector3(0, 0, 0)
if speed_diff ~= 0 then
-- set the acceleration to work in the direction of the difference
if speed_diff < 0 then
acceleration.x = -self.move_acceleration
else
acceleration.x = self.move_acceleration
end
end
-- calculate the velocity change this frame (dv is short for delta-velocity)
local dv = acceleration * dt
-- check if dv exceeds the intended speed difference, clamp it in that case
if math.abs(dv.x) > math.abs(speed_diff) then
dv.x = speed_diff
end
-- save the current velocity for later use
-- (self.velocity, which right now is the velocity used the previous frame)
local v0 = self.velocity
-- calculate the new velocity by adding the velocity change
self.velocity = self.velocity + dv
-- calculate the translation this frame by integrating the velocity
self.dp = (v0 + self.velocity) * dt * 0.5
go.set_position(go.get_position() + self.dp)
end
function on_input(self, action_id, action)
if action_id == input_right then
self.move_input = action.value
elseif action_id == input_left then
self.move_input = -action.value
end
end
function final(self)
msg.post(".", "release_input_focus")
end