project/main/mummy.script
2025-08-29 12:59:38 +01:00

92 lines
3.2 KiB
Text

function init(self)
go.set_position(vmath.vector3(950,700,0.2))
self.velocity = vmath.vector3(0, 0, 0)
self.gravity = -5000 -- gravity force
self.grounded = false
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)
local pos = go.get_position()
pos = pos + self.velocity * dt
local centre = vmath.vector3(pos.x, pos.y, 0)
local platform = pos + vmath.vector3(0, -45, 0)
-- msg.post("@render:", "draw_line", { start_point = centre, end_point = platform, color = vmath.vector4(1,0,0,1) })
local platform_result = physics.raycast(centre, platform, { hash("platform") })
if platform_result then
local point = platform_result.position - pos
--msg.post("@render:", "draw_debug_text", { text = "Platform", position = vmath.vector3(20, 400, 0), color = vmath.vector4(1, 0, 0, 1) } )
--msg.post("@render:", "draw_debug_text", { text = "X : " .. tostring(point.x), position = vmath.vector3(20, 380, 0), color = vmath.vector4(1, 0, 0, 1) } )
--msg.post("@render:", "draw_debug_text", { text = "Y : " .. tostring(point.y), position = vmath.vector3(20, 360, 0), color = vmath.vector4(1, 0, 0, 1) } )
self.grounded = true
self.velocity.y = 0
if point.y > -45 then
pos.y = pos.y + (point.y + 45)
end
else
self.grounded = false
end
if not self.grounded then
self.velocity.y = self.velocity.y + self.gravity * dt
end
go.set_position(pos)
if pos.x >= 1100 then
msg.post("main:/controller#main", "LevelCompleted")
end
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)
if message_id == hash("explode") then
msg.post("level3:/controller#level3", "manual_pause")
go.delete("mummy")
-- sprite.play_flipbook("#still", hash("explosion"))
-- msg.post(self.creator, "mummy_deleted")
--msg.post("mummy", "disable")
end
if message_id == hash("play_anim") then
sprite.play_flipbook("/mummy#still", "walking_mummy")
local flip_y = vmath.quat_rotation_y(math.rad(180))
go.set_rotation(flip_y)
go.set_scale(vmath.vector3(1.3, 1.3, 1))
self.velocity.x = 250
msg.post(self.creator, "manual_pause")
end
if message_id == hash("creator") then
self.creator = message.creator
end
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