epervier-framework/examples/scenes/gameplay/plateform/actors/player.lua

102 lines
2.3 KiB
Lua
Raw Normal View History

local Player = actor {
type = "player",
dimensions = { w = 16, h = 24 },
friction = { x = 480 * 3 },
isSolid = true,
visuals = {
mode = "sprite",
assetName = "monkey_lad",
clone = true,
origin = {
x = 8,
y = 12
},
getHitboxes = true
},
gravity = { axis = "y", value = 480 },
drawHitboxes = true
}
function Player:onInit()
2019-09-08 12:34:16 +02:00
self.isDead = false
self.start = self.position:clone()
self.isPunching = false
self.direction = 1
self.coin = 0
self.punchName = ""
2019-04-29 16:13:09 +02:00
end
function Player:update(dt)
if love.keyboard.isDown("up") and (self.onGround) then
self.speed.y = -280
assets:playSFX("gameplay.jump")
2019-04-29 16:13:09 +02:00
end
if love.keyboard.isDown("down") then
self.mainHitbox:modify({x = 0, y = 8}, {w = 16, h = 16})
else
self.mainHitbox:modify({x = 0, y = 0}, {w = 16, h = 24})
2019-04-29 16:13:09 +02:00
end
if love.keyboard.isDown("left") and (not self.isPunching) then
self.speed.x = -120
2019-04-29 16:13:09 +02:00
end
if love.keyboard.isDown("right") and (not self.isPunching) then
self.speed.x = 120
2019-04-29 16:13:09 +02:00
end
if love.keyboard.isDown("a") then
self.isPunching = true
else
self.isPunching = false
end
self:setDirection(self.speed.x)
2019-05-05 15:23:04 +02:00
self:setAnimation()
2019-09-08 12:34:16 +02:00
local _, dimensions = self.world:getBox()
2019-09-08 12:34:16 +02:00
if (self.position.y > (dimensions.h + self.dimensions.h)) and (self.isDead == false) then
self.timers:newTimer(1, "respawn")
2019-09-08 12:34:16 +02:00
self.isDead = true
end
2019-05-05 15:23:04 +02:00
end
function Player:setAnimation()
self.visual:setCustomSpeed(math.abs(self.speed.x) / 12)
if (self.isPunching) then
self.visual:changeAnimation("punch", false)
else
if (self.onGround) then
if math.abs(self.speed.x) > 0 then
self.visual:changeAnimation("walk", false)
else
self.visual:changeAnimation("idle", true)
end
2019-05-05 15:23:04 +02:00
else
self.visual:changeAnimation("jump", true)
2019-05-05 15:23:04 +02:00
end
end
2019-04-29 16:13:09 +02:00
end
function Player:setDirection(direction)
direction = direction or 0
if direction ~= 0 then
direction = utils.math.sign(direction)
self.direction = direction
self.visual:setScalling({x = direction})
end
end
2019-09-08 12:34:16 +02:00
function Player:timerResponse(timer)
if timer == "respawn" then
self.position.x, self.position.y = self.start.x, self.start.y
self.speed.x, self.speed.y = 0, 0
2019-09-08 12:34:16 +02:00
self.isDead = false
end
end
function Player:drawHUD()
assets:print("medium", "Coins : " .. self.coin, 8, 8)
end
2019-04-29 16:13:09 +02:00
return Player