robot_cleaning_game/defold_game/main/player.script
2026-05-13 22:34:46 +01:00

54 lines
1.5 KiB
Text

go.property("speed", 100)
go.property("rotation_speed", 3)
function init(self)
msg.post(".", "acquire_input_focus")
self.rotation = 0
self.rotating_left = false
self.rotating_right = false
self.moving_forward = false
end
function update(self, dt)
if self.rotating_left then
self.rotation = self.rotation + self.rotation_speed * dt
end
if self.rotating_right then
self.rotation = self.rotation - self.rotation_speed * dt
end
print("Rotation" .. self.rotation)
if self.moving_forward then
local vx = math.cos(self.rotation) * self.speed
local vy = math.sin(self.rotation) * self.speed
local pos = go.get_position()
pos.x = pos.x + vx * dt
pos.y = pos.y + vy * dt
go.set_position(pos)
end
end
function on_input(self, action_id, action)
-- Rotate with A/D keys
if action_id == hash("left_wheel_fwd") and action.pressed then
self.rotating_left = true
elseif action_id == hash("left_wheel_fwd") and action.released then
self.rotating_left = false
end
if action_id == hash("right_wheel_fwd") and action.pressed then
self.rotating_right = true
elseif action_id == hash("right_wheel_fwd") and action.released then
self.rotating_right = false
end
-- Move forward with W
if (action_id == hash("left_wheel_fwd") or action_id == hash("right_wheel_fwd")) and action.pressed then
self.moving_forward = true
elseif (action_id == hash("left_wheel_fwd") or action_id == hash("right_wheel_fwd")) and action.released then
self.moving_forward = false
end
end