From 883c28363e4d47c87165f4f508c5f1dc8c542a37 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Thu, 14 May 2026 11:13:07 +0100 Subject: [PATCH] get player dir sprite swapping working " " " " " " " " " " " " " " " " " " " " " " --- defold_game/.editor_settings | 6 +- defold_game/main/main.collection | 23 +++---- defold_game/main/{ => player}/player.script | 20 +++++++ defold_game/main/{ => player}/player.sprite | 2 +- .../main/{ => player}/player.tilesource | 0 defold_game/main/zoom.script | 60 ------------------- 6 files changed, 38 insertions(+), 73 deletions(-) rename defold_game/main/{ => player}/player.script (74%) rename defold_game/main/{ => player}/player.sprite (71%) rename defold_game/main/{ => player}/player.tilesource (100%) delete mode 100644 defold_game/main/zoom.script diff --git a/defold_game/.editor_settings b/defold_game/.editor_settings index 35625a4..7948eab 100644 --- a/defold_game/.editor_settings +++ b/defold_game/.editor_settings @@ -11,7 +11,7 @@ :scene ] [ - "/main/zoom.script" + "/main/player/player.script" :code ] ] @@ -77,6 +77,10 @@ "/main/main.collection" :scene ] + [ + "/main/player/player.script" + :code + ] ] } } \ No newline at end of file diff --git a/defold_game/main/main.collection b/defold_game/main/main.collection index 1b06cfa..a3c3b0b 100644 --- a/defold_game/main/main.collection +++ b/defold_game/main/main.collection @@ -2,11 +2,7 @@ name: "main" scale_along_z: 0 embedded_instances { id: "camera" - data: "components {\n" - " id: \"zoom\"\n" - " component: \"/main/zoom.script\"\n" - "}\n" - "embedded_components {\n" + data: "embedded_components {\n" " id: \"camera\"\n" " type: \"camera\"\n" " data: \"aspect_ratio: 1.0\\n" @@ -25,13 +21,10 @@ embedded_instances { } embedded_instances { id: "player" + children: "body" data: "components {\n" - " id: \"body\"\n" - " component: \"/main/player.sprite\"\n" - "}\n" - "components {\n" - " id: \"player1\"\n" - " component: \"/main/player.script\"\n" + " id: \"player_controller\"\n" + " component: \"/main/player/player.script\"\n" "}\n" "" position { @@ -39,3 +32,11 @@ embedded_instances { y: 503.0 } } +embedded_instances { + id: "body" + data: "components {\n" + " id: \"body\"\n" + " component: \"/main/player/player.sprite\"\n" + "}\n" + "" +} diff --git a/defold_game/main/player.script b/defold_game/main/player/player.script similarity index 74% rename from defold_game/main/player.script rename to defold_game/main/player/player.script index 39e64a7..cd7b341 100644 --- a/defold_game/main/player.script +++ b/defold_game/main/player/player.script @@ -5,11 +5,22 @@ local WHEEL_BASE = 80 local UP = vmath.vector3(0, 1, 0) +local DIRECTIONS = { "n", "ne", "e", "se", "s", "sw", "w", "nw" } + +local function get_direction(rot) + local forward = vmath.rotate(rot, UP) + local angle = math.deg(math.atan2(forward.x, forward.y)) + if angle < 0 then angle = angle + 360 end + local index = math.floor((angle + 22.5) / 45) % 8 + 1 + return DIRECTIONS[index] +end + function init(self) msg.post(".", "acquire_input_focus") self.left_speed = 0 self.right_speed = 0 + self.current_anim = nil self.left_fwd = false self.left_rev = false self.right_fwd = false @@ -46,14 +57,23 @@ function update(self, dt) local linear_speed = (self.left_speed + self.right_speed) / 2 local angular_vel = (self.right_speed - self.left_speed) / WHEEL_BASE + -- pprint({rot_changes = angular_vel * dt}) local rot = go.get_rotation() rot = rot * vmath.quat_rotation_z(angular_vel * dt) go.set_rotation(rot) + go.set_rotation(vmath.conj(rot), "body") + local p = go.get_position() p = p + vmath.rotate(rot, UP * linear_speed * dt) go.set_position(p) + -- switch animation when facing direction changes + local dir = get_direction(rot) + if dir ~= self.current_anim then + self.current_anim = dir + msg.post("body#body", "play_animation", { id = hash(dir) }) + end end function on_input(self, action_id, action) diff --git a/defold_game/main/player.sprite b/defold_game/main/player/player.sprite similarity index 71% rename from defold_game/main/player.sprite rename to defold_game/main/player/player.sprite index 27c84f9..327d49c 100644 --- a/defold_game/main/player.sprite +++ b/defold_game/main/player/player.sprite @@ -2,5 +2,5 @@ default_animation: "n" material: "/builtins/materials/sprite.material" textures { sampler: "texture_sampler" - texture: "/main/player.tilesource" + texture: "/main/player/player.tilesource" } diff --git a/defold_game/main/player.tilesource b/defold_game/main/player/player.tilesource similarity index 100% rename from defold_game/main/player.tilesource rename to defold_game/main/player/player.tilesource diff --git a/defold_game/main/zoom.script b/defold_game/main/zoom.script deleted file mode 100644 index 5b87f22..0000000 --- a/defold_game/main/zoom.script +++ /dev/null @@ -1,60 +0,0 @@ -function init(self) - -- Add initialization code here - -- Learn more: https://defold.com/manuals/script/ - -- Remove this function if not needed - msg.post("@render:", "use_fixed_projection", { near = -1, far = 1, zoom = 5 }) - -end - -function final(self) - -- Add finalization code here - -- Learn more: https://defold.com/manuals/script/ - -- Remove this function if not needed -end - -function update(self, dt) - -- Add update code here - -- Learn more: https://defold.com/manuals/script/ - -- Remove this function if not needed - msg.post("@render:", "use_fixed_projection", { near = -1, far = 1, zoom = 5 }) - -end - -function late_update(self, dt) - -- This function is called at the end of update cycle but before render - -- Learn more: https://defold.com/manuals/script/ - -- Remove this function if not needed -end - -function fixed_update(self, dt) - -- This function is called if 'Fixed Update Frequency' is enabled in the Engine section of game.project - -- Can be coupled with fixed updates of the physics simulation if 'Use Fixed Timestep' is enabled in - -- Physics section of game.project - -- Add update code here - -- Learn more: https://defold.com/manuals/script/ - -- Remove this function if not needed -end - -function on_message(self, message_id, message, sender) - -- Add message-handling code here - -- Learn more: https://defold.com/manuals/message-passing/ - -- Remove this function if not needed -end - -function on_input(self, action_id, action) - -- Add input-handling code here. The game object this script is attached to - -- must have acquired input focus: - -- - -- msg.post(".", "acquire_input_focus") - -- - -- All mapped input bindings will be received. Mouse and touch input will - -- be received regardless of where on the screen it happened. - -- Learn more: https://defold.com/manuals/input/ - -- Remove this function if not needed -end - -function on_reload(self) - -- Add reload-handling code here - -- Learn more: https://defold.com/manuals/hot-reload/ - -- Remove this function if not needed -end