-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollectable.lua
More file actions
52 lines (42 loc) · 1.38 KB
/
Copy pathcollectable.lua
File metadata and controls
52 lines (42 loc) · 1.38 KB
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
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/animation"
class("Collectable").extends(playdate.graphics.sprite)
local gfx = playdate.graphics
local collectableTable = gfx.imagetable.new("images/collectable-table-24-24")
assert(collectableTable)
-- Global collectibles tracking table
collectibles = collectibles or {}
-- Constructor
function Collectable:init(x, y)
-- Load image table
local imageTable = collectableTable
assert(imageTable, "Missing collectable-table-24-24 image table!")
-- Set up animation (optional - animates through all frames)
self.animation = gfx.animation.loop.new(150, imageTable, true)
self:setImage(self.animation:image())
local w, h = self:getSize()
self:setSize(w, h)
self:setZIndex(50)
-- No collide rect; player can overlap freely
self:moveTo(x, y)
self:add()
-- Add self to global collectibles table
table.insert(collectibles, self)
end
-- Update for animation
function Collectable:update()
self:setImage(self.animation:image())
end
-- Destructor
function Collectable:remove()
-- Ensure proper cleanup from global collectibles table when removed
for i = #collectibles, 1, -1 do
if collectibles[i] == self then
table.remove(collectibles, i)
break
end
end
playdate.graphics.sprite.remove(self)
end