local platypus = require "platypus.platypus" local fps = require("fps.fps") 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 facingright = true local facingleft = false local jumping = false local onground = true local canattack = true 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 = 580, 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) }) facingright = true onground = true jumping = false end function update(self, dt) --fps.update(dt) --print(fps.average_ms .. " ms" , fps.fps .. " FPS", fps.raw_average_time) 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 then --particlefx.play("/particle test#test") jumping = true onground = false print("jumping true") print("onground false") if action.pressed then self.platypus.jump(800) --particlefx.play("/particle test#test") elseif action.released then --particlefx.play("/particle test#test") self.platypus.abort_jump(0.5) jumping = false --onground = true print("jumping false") print("onground true") 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") onground = true jumping = false print("onground true") 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) end if message_id == hash("onscreen") then if message.id == hash("left") then facingleft = true facingright = false handle_action(self, LEFT, message) elseif message.id == hash("right") then facingright = true facingleft = false handle_action(self, RIGHT, message) elseif message.id == hash("jump") then handle_action(self, JUMP, message) jumping = true onground = false end end if message.id == hash("attack") and message.released and facingright and onground and canattack then print("attack recieved") print("attackright") local pos = go.get_position() + vmath.vector3(160, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x + 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end if message.id == hash("attack") and message.released and facingleft and onground then print("attackleft") local pos = go.get_position() + vmath.vector3(160, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x - 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end if message.id == hash("attack") and message.released and facingleft and jumping and canattack then print("attackleft in the air") local pos = go.get_position() + vmath.vector3(0, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x - 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end if message.id == hash("attack") and message.released and facingright and jumping and canattack then print("attackright") local pos = go.get_position() + vmath.vector3(0, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x + 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end end function on_input(self, action_id, action) handle_action(self, action_id, action) if action_id == hash("right") then facingright = true facingleft = false end if action_id == hash("left") then facingleft = true facingright = false end if action_id == hash("jump") then jumping = true onground = false end if action_id == hash("key_x") then print("keyx") particlefx.play("/particle test#test") end if action_id == hash("key_z") and action.released and facingright and onground and canattack then print("attackright") local pos = go.get_position() + vmath.vector3(160, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x + 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end if action_id == hash("key_z") and action.released and facingright and jumping and canattack then print("attackright") local pos = go.get_position() + vmath.vector3(0, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x + 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end if action_id == hash("key_z") and action.released and facingleft and onground and canattack then print("attackleft") local pos = go.get_position() + vmath.vector3(160, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x - 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end if action_id == hash("key_z") and action.released and facingleft and jumping and canattack then print("attackleft") local pos = go.get_position() + vmath.vector3(0, 30, 0) local id = factory.create("#attack", pos) go.animate(id, "position.x", go.PLAYBACK_ONCE_FORWARD, pos.x - 1500, go.EASING_LINEAR, 0.8, 0, function() go.delete(id) end) canattack = false timer.delay(.5, false, function() canattack = true end) end end function on_reload(self) -- Add reload-handling code here -- Remove this function if not needed end