epervier-old/examples/gameplay/plateform/actors/player.lua
Kazhnuz 6f59f1e732 refactor(examples): use updateStart(dt) and updateEnd(dt)
Allow to drop easily some instance of calling the parent's same function
2019-06-13 18:44:13 +02:00

68 lines
1.4 KiB
Lua

local Base = require "gamecore.modules.world.actors.actor2D"
local Player = Base:extend()
function Player:new(world, x, y, id)
Player.super.new(self, world, "player", x, y, 16, 24, true)
self:setSprite("player", 8, 12)
self:cloneSprite()
self:setYGravity(480)
end
function Player:updateStart(dt)
self.xfrc = 480*3
if self.keys["up"].isPressed and (self.onGround) then
self.ysp = -280
end
if self.keys["down"].isDown then
--self.ysp = 120
end
if self.keys["left"].isDown then
self.xsp = -120
end
if self.keys["right"].isDown then
self.xsp = 120
end
if self.keys["start"].isPressed then
self.world:switchActivity()
self.assets:switchActivity()
self.scene:flushKeys()
end
self:setDirection(self.xsp)
end
function Player:updateEnd(dt)
self:setAnimation()
end
function Player:setAnimation()
self:setCustomSpeed(math.abs(self.xsp) / 12)
if (self.onGround) then
if math.abs(self.xsp) > 0 then
self:changeAnimation("walk", false)
else
self:changeAnimation("idle", true)
end
else
self:changeAnimation("jump", true)
end
end
function Player:setDirection(direction)
direction = direction or 0
if direction ~= 0 then
direction = utils.math.sign(direction)
self:setSpriteScallingX(direction)
end
end
function Player:collisionResponse(collision)
if collision.other.type == "coin" then
collision.other:takeCoin(self)
end
end
return Player