local platypus = require "platypus.platypus" local LEFT = hash("left") local RIGHT = hash("right") local JUMP = hash("jump") local TOGGLE_DEBUG = hash("toggle_debug") local TOGGLE_PROFILE = hash("toggle_profile") local ANIM_RUN = hash("run") local ANIM_JUMP = hash("jump") local ANIM_IDLE = hash("idle") --local function play_animation(self, animation) --if self.current_animation ~= animation then --msg.post("#sprite", "play_animation", { id = animation }) --self.current_animation = animation --end --end function init(self) msg.post(".", "acquire_input_focus") self.canmove = true self.input_state = {} self.platypus = platypus.create({ collisions = { --separation = platypus.SEPARATION_RAYS, separation = platypus.SEPARATION_SHAPES, groups = { [hash("ground")] = platypus.DIR_ALL, --[hash("onewayplatform")] = platypus.DIR_ALL, [hash("onewayplatform")] = platypus.DIR_DOWN, [hash("onewaydoor")] = platypus.DIR_LEFT, }, left = 29, right = 29, top = 50, bottom = 10, offset = vmath.vector3(0, 7, 0) }, gravity = -950, max_velocity = 780, allow_double_jump = true, allow_wall_jump = true, allow_wall_slide = true, wall_slide_gravity = -50 --debug = true, }) --self.current_animation = nil --msg.post("@render:", "clear_color", { color = vmath.vector4(0.008, 0.090, 0.129, 1.0) }) end function update(self, dt) if not self.canmove then return end local ground_contact = self.platypus.has_ground_contact() if self.input_state[LEFT] then self.platypus.left(ground_contact and 250 or 250) --play_animation(self, ground_contact and ANIM_RUN or ANIM_JUMP) sprite.set_hflip("#sprite", true) elseif self.input_state[RIGHT] then self.platypus.right(ground_contact and 250 or 250) --play_animation(self, ground_contact and ANIM_RUN or ANIM_JUMP) sprite.set_hflip("#sprite", false) else --self.platypus.abort_wall_slide() --play_animation(self, ground_contact and ANIM_IDLE or ANIM_JUMP) end self.platypus.update(dt) end local function handle_action(self, action_id, action) if not action_id then return end if action.pressed then self.input_state[action_id] = true elseif action.released then self.input_state[action_id] = nil end if action_id == hash("toggle_profile") then if action.released then msg.post("@system:", "toggle_profile") end return end if action_id == JUMP and canmove then if action.pressed then self.platypus.jump(800) elseif action.released then self.platypus.abort_jump(0.5) end elseif action_id == TOGGLE_DEBUG and action.released then self.platypus.toggle_debug() msg.post("@system:", "toggle_physics_debug") end end function on_message(self, message_id, message, sender) self.platypus.on_message(message_id, message, sender) if message_id == hash("frozen") then self.platypus.right(0); self.input_state = {} self.canmove = false end if message_id == hash("unfreeze") then self.canmove = true end if message_id == platypus.GROUND_CONTACT then print("Ground contact") elseif message_id == platypus.WALL_CONTACT then print("Wall contact") elseif message_id == platypus.FALLING then print("Falling") elseif message_id == platypus.WALL_JUMP then sprite.set_hflip("#sprite", self.platypus.velocity.x < 0) elseif message_id == hash("onscreen") then if message.id == hash("left") and canmove then handle_action(self, LEFT, message) elseif message.id == hash("right") and canmove then handle_action(self, RIGHT, message) elseif message.id == hash("jump") and canmove then handle_action(self, JUMP, message) end end end function on_input(self, action_id, action) if self.canmove then handle_action(self, action_id, action) end end function on_reload(self) -- Add reload-handling code here -- Remove this function if not needed end