diff --git a/defold_game/.editor_settings b/defold_game/.editor_settings index abdccd1..262c3c6 100644 --- a/defold_game/.editor_settings +++ b/defold_game/.editor_settings @@ -22,10 +22,6 @@ "/main/player.script" :code ] - [ - "/main/main.script" - :code - ] [ "/game.project" :cljfx-form-view @@ -54,24 +50,24 @@ :scene ] [ - "/game.project" - :cljfx-form-view + "/main/main.atlas" + :scene ] [ "/main/player.sprite" :scene ] - [ - "/main/main.script" - :code - ] [ "/input/game.input_binding" :cljfx-form-view ] [ - "/main/main.atlas" - :scene + "/main/main.script" + :code + ] + [ + "/game.project" + :cljfx-form-view ] [ "/main/main.collection" diff --git a/defold_game/input/game.input_binding b/defold_game/input/game.input_binding index d789024..438c772 100644 --- a/defold_game/input/game.input_binding +++ b/defold_game/input/game.input_binding @@ -1,18 +1,18 @@ key_trigger { input: KEY_A - action: "left_wheel_fwd" + action: "a_key" } key_trigger { input: KEY_Q - action: "left_wheel_bwk" + action: "q_key" } key_trigger { input: KEY_D - action: "right_wheel_fwd" + action: "d_key" } key_trigger { input: KEY_E - action: "right_wheel_bwk" + action: "e_key" } mouse_trigger { input: MOUSE_BUTTON_LEFT diff --git a/defold_game/main/main.collection b/defold_game/main/main.collection index b5548d1..0f528cb 100644 --- a/defold_game/main/main.collection +++ b/defold_game/main/main.collection @@ -1,42 +1,5 @@ name: "main" scale_along_z: 0 -embedded_instances { - id: "go" - data: "components {\n" - " id: \"main\"\n" - " component: \"/main/main.script\"\n" - "}\n" - "embedded_components {\n" - " id: \"logo\"\n" - " type: \"sprite\"\n" - " data: \"default_animation: \\\"logo\\\"\\n" - "material: \\\"/builtins/materials/sprite.material\\\"\\n" - "textures {\\n" - " sampler: \\\"texture_sampler\\\"\\n" - " texture: \\\"/main/main.atlas\\\"\\n" - "}\\n" - "\"\n" - "}\n" - "embedded_components {\n" - " id: \"background\"\n" - " type: \"sprite\"\n" - " data: \"default_animation: \\\"background\\\"\\n" - "material: \\\"/builtins/materials/sprite.material\\\"\\n" - "textures {\\n" - " sampler: \\\"texture_sampler\\\"\\n" - " texture: \\\"/main/main.atlas\\\"\\n" - "}\\n" - "\"\n" - " position {\n" - " z: -0.5\n" - " }\n" - "}\n" - "" - position { - x: 640.0 - y: 360.0 - } -} embedded_instances { id: "camera" data: "embedded_components {\n" @@ -61,11 +24,6 @@ embedded_instances { data: "components {\n" " id: \"body\"\n" " component: \"/main/player.sprite\"\n" - " scale {\n" - " x: 2.0\n" - " y: 2.0\n" - " z: 2.0\n" - " }\n" "}\n" "components {\n" " id: \"player1\"\n" @@ -73,7 +31,7 @@ embedded_instances { "}\n" "" position { - x: 189.0 - y: 164.0 + x: 649.0 + y: 503.0 } } diff --git a/defold_game/main/player.script b/defold_game/main/player.script index 9caa1d5..fb9fb6a 100644 --- a/defold_game/main/player.script +++ b/defold_game/main/player.script @@ -1,54 +1,54 @@ -go.property("speed", 100) -go.property("rotation_speed", 3) +go.property("acceleration", 100) +go.property("deceleration", 200) +go.property("max_speed", 400) +go.property("wheel_base", 40) -- pixels between left and right wheels + +local UP = vmath.vector3(0, 1, 0) function init(self) - msg.post(".", "acquire_input_focus") - self.rotation = 0 - self.rotating_left = false - self.rotating_right = false - self.moving_forward = false + msg.post(".", "acquire_input_focus") + + self.left_speed = 0 + self.right_speed = 0 + self.left_input = false + self.right_input = 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 + -- each wheel accelerates when its key is held, decelerates otherwise + if self.left_input then + self.left_speed = math.min(self.left_speed + self.acceleration * dt, self.max_speed) + else + self.left_speed = math.max(self.left_speed - self.deceleration * dt, 0) + end - local pos = go.get_position() - pos.x = pos.x + vx * dt - pos.y = pos.y + vy * dt - go.set_position(pos) - end + if self.right_input then + self.right_speed = math.min(self.right_speed + self.acceleration * dt, self.max_speed) + else + self.right_speed = math.max(self.right_speed - self.deceleration * dt, 0) + end + -- differential drive: speed difference creates rotation, average creates forward motion + local linear_speed = (self.left_speed + self.right_speed) / 2 + local angular_vel = (self.right_speed - self.left_speed) / self.wheel_base + local rot = go.get_rotation() + rot = rot * vmath.quat_rotation_z(angular_vel * dt) + go.set_rotation(rot) + + local p = go.get_position() + p = p + vmath.rotate(rot, UP * linear_speed * dt) + go.set_position(p) + + -- reset each frame (on_input re-sets if key is still held) + self.left_input = false + self.right_input = false 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 + if action_id == hash("a_key") then + self.left_input = true + elseif action_id == hash("d_key") then + self.right_input = true + end end