-
Notifications
You must be signed in to change notification settings - Fork 3
/
Player.lua
55 lines (46 loc) · 1.72 KB
/
Player.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
local pd <const> = playdate
local gfx <const> = Graphics
Player = {}
class("Player").extends(AnimatedSprite)
local player_it <const> = gfx.imagetable.new("/assets/images/character")
local particles = ParticleCircle(0, 0)
particles:setSpread(0, 90)
function Player:init(x, y, playerSlot)
Player.super.init(self, player_it)
self:remove() -- AnimatedSprite adds itself to the scene, we want to do that through Noble
local skinOffset = (playerSlot - 1) * 7
self:addState("idle", 1 + skinOffset, 1 + skinOffset, {}, true) -- Set as default state
self:addState("left", 2 + skinOffset, 4 + skinOffset, { tickStep = 4, flip = playdate.geometry.kFlippedX })
self:addState("right", 2 + skinOffset, 4 + skinOffset, { tickStep = 4 })
self:setGroups(COLLISION_LAYERS.PLAYER)
self:setCollidesWithGroups(COLLISION_LAYERS.ITEM)
self:setCollideRect(0, 0, self:getSize())
self:moveTo(x, y)
self.movement_speed = 2
self.x_velocity = 0
end
function Player:update()
if not self.active then
return
end
Player.super.update(self)
if pd.buttonIsPressed(pd.kButtonLeft) then
self.x_velocity = -1 * self.movement_speed
self:changeState("left")
particles:moveTo(self.x, self.y + 8)
particles:add(1)
elseif pd.buttonIsPressed(pd.kButtonRight) then
self.x_velocity = 1 * self.movement_speed
self:changeState("right")
particles:moveTo(self.x, self.y + 8)
particles:add(1)
else
self.x_velocity = 0
self:changeState("idle")
end
if self.x_velocity < 0 and self.x > 9 then
self:moveBy(self.x_velocity, 0)
elseif self.x_velocity > 0 and self.x < 191 then
self:moveBy(self.x_velocity, 0)
end
end