try and get player steering working
" " " " " " " " " " " "
This commit is contained in:
parent
26e88c8f03
commit
8e54b39b18
4 changed files with 66 additions and 42 deletions
|
|
@ -61,10 +61,6 @@ embedded_instances {
|
|||
data: "components {\n"
|
||||
" id: \"body\"\n"
|
||||
" component: \"/main/player.sprite\"\n"
|
||||
" position {\n"
|
||||
" x: 109.0\n"
|
||||
" y: 90.0\n"
|
||||
" }\n"
|
||||
" scale {\n"
|
||||
" x: 2.0\n"
|
||||
" y: 2.0\n"
|
||||
|
|
@ -76,4 +72,8 @@ embedded_instances {
|
|||
" component: \"/main/player.script\"\n"
|
||||
"}\n"
|
||||
""
|
||||
position {
|
||||
x: 189.0
|
||||
y: 164.0
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +1,54 @@
|
|||
go.property("speed", 100)
|
||||
go.property("rotation_speed", 3)
|
||||
|
||||
function init(self)
|
||||
msg.post(".", "acquire_input_focus") -- <1>
|
||||
self.vel = vmath.vector3() -- <2>
|
||||
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)
|
||||
local pos = go.get_position() -- <3>
|
||||
pos = pos + self.vel * dt -- <4>
|
||||
go.set_position(pos) -- <5>
|
||||
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
|
||||
|
||||
self.vel.x = 0 -- <6>
|
||||
self.vel.y = 0
|
||||
local pos = go.get_position()
|
||||
pos.x = pos.x + vx * dt
|
||||
pos.y = pos.y + vy * dt
|
||||
go.set_position(pos)
|
||||
end
|
||||
|
||||
-- sprite.play_flipbook("#body", "s")
|
||||
rot = go.get_rotation()
|
||||
print(rot)
|
||||
|
||||
end
|
||||
|
||||
|
||||
function on_input(self, action_id, action)
|
||||
if action_id == hash("up") then
|
||||
self.vel.y = 150 -- <7>
|
||||
elseif action_id == hash("down") then
|
||||
self.vel.y = -150
|
||||
elseif action_id == hash("left") then
|
||||
self.vel.x = -150 -- <8>
|
||||
elseif action_id == hash("right") then
|
||||
self.vel.x = 150
|
||||
-- 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue