get player dir sprite swapping working

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
This commit is contained in:
Travis Shears 2026-05-14 11:13:07 +01:00
parent fe99c7a4a6
commit 883c28363e
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
6 changed files with 38 additions and 73 deletions

View file

@ -11,7 +11,7 @@
:scene :scene
] ]
[ [
"/main/zoom.script" "/main/player/player.script"
:code :code
] ]
] ]
@ -77,6 +77,10 @@
"/main/main.collection" "/main/main.collection"
:scene :scene
] ]
[
"/main/player/player.script"
:code
]
] ]
} }
} }

View file

@ -2,11 +2,7 @@ name: "main"
scale_along_z: 0 scale_along_z: 0
embedded_instances { embedded_instances {
id: "camera" id: "camera"
data: "components {\n" data: "embedded_components {\n"
" id: \"zoom\"\n"
" component: \"/main/zoom.script\"\n"
"}\n"
"embedded_components {\n"
" id: \"camera\"\n" " id: \"camera\"\n"
" type: \"camera\"\n" " type: \"camera\"\n"
" data: \"aspect_ratio: 1.0\\n" " data: \"aspect_ratio: 1.0\\n"
@ -25,13 +21,10 @@ embedded_instances {
} }
embedded_instances { embedded_instances {
id: "player" id: "player"
children: "body"
data: "components {\n" data: "components {\n"
" id: \"body\"\n" " id: \"player_controller\"\n"
" component: \"/main/player.sprite\"\n" " component: \"/main/player/player.script\"\n"
"}\n"
"components {\n"
" id: \"player1\"\n"
" component: \"/main/player.script\"\n"
"}\n" "}\n"
"" ""
position { position {
@ -39,3 +32,11 @@ embedded_instances {
y: 503.0 y: 503.0
} }
} }
embedded_instances {
id: "body"
data: "components {\n"
" id: \"body\"\n"
" component: \"/main/player/player.sprite\"\n"
"}\n"
""
}

View file

@ -5,11 +5,22 @@ local WHEEL_BASE = 80
local UP = vmath.vector3(0, 1, 0) 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) function init(self)
msg.post(".", "acquire_input_focus") msg.post(".", "acquire_input_focus")
self.left_speed = 0 self.left_speed = 0
self.right_speed = 0 self.right_speed = 0
self.current_anim = nil
self.left_fwd = false self.left_fwd = false
self.left_rev = false self.left_rev = false
self.right_fwd = false self.right_fwd = false
@ -46,14 +57,23 @@ function update(self, dt)
local linear_speed = (self.left_speed + self.right_speed) / 2 local linear_speed = (self.left_speed + self.right_speed) / 2
local angular_vel = (self.right_speed - self.left_speed) / WHEEL_BASE local angular_vel = (self.right_speed - self.left_speed) / WHEEL_BASE
-- pprint({rot_changes = angular_vel * dt})
local rot = go.get_rotation() local rot = go.get_rotation()
rot = rot * vmath.quat_rotation_z(angular_vel * dt) rot = rot * vmath.quat_rotation_z(angular_vel * dt)
go.set_rotation(rot) go.set_rotation(rot)
go.set_rotation(vmath.conj(rot), "body")
local p = go.get_position() local p = go.get_position()
p = p + vmath.rotate(rot, UP * linear_speed * dt) p = p + vmath.rotate(rot, UP * linear_speed * dt)
go.set_position(p) 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 end
function on_input(self, action_id, action) function on_input(self, action_id, action)

View file

@ -2,5 +2,5 @@ default_animation: "n"
material: "/builtins/materials/sprite.material" material: "/builtins/materials/sprite.material"
textures { textures {
sampler: "texture_sampler" sampler: "texture_sampler"
texture: "/main/player.tilesource" texture: "/main/player/player.tilesource"
} }

View file

@ -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