-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPile.lua
More file actions
77 lines (64 loc) · 2.3 KB
/
Pile.lua
File metadata and controls
77 lines (64 loc) · 2.3 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-- Pile.lua
-- Piles contain a Stack of crates
Pile = class(Panel)
function Pile:init(x,y,config,screen)
Panel.init(self,x,y)
self.config = config
self.screen = screen
self.crates = Stack()
-- the base of the pile
local baseX = math.floor((self.config.w - self.config.base.w)/2)
local baseW,baseH = self.config.base.w,self.config.base.h
if self.config.shadows then
self.base = ShadowObj(baseX,0,baseW,baseH)
screen:doDraw(self.base,self.config.base.sprite,0,-8)
else
self.base = SpriteObj(baseX,0,baseW,baseH)
screen:doDraw(self.base,self.config.base.sprite)
end
self:add(self.base)
end
function Pile:push(args)
assert(args.imgName ~= nil, "Trying to push a crate with no imgName")
assert(args.colStr ~= nil, "Truing to push a crate with no colStr")
local imgName = args.imgName
local colStr = args.colStr
local dx = args.dx or 0
local inverted = args.inverted or false
-- calculate the coords of the crate
local y = self.config.base.h + self.config.base.borderY
y = y + self.crates:size() * (self.config.crate.h + self.config.crate.borderY)
local x = math.floor((self.config.w - self.config.crate.w)/2)
-- make the actual obj
local crate = Crate(x+dx,y,self.config.crate,colStr,imgName,self.screen)
if inverted then crate:flipX() end
self.crates:push(crate)
self:add(crate)
end
-- retrieves the dx of this crate in this pile based on its position
function Pile:crateDx(crate)
local x = math.floor((self.config.w - self.config.crate.w)/2)
return crate.x - x - self.x
end
-- push method for when we already have a crate object
function Pile:pushCrate(crate,dx)
crate:addToScreen(self.screen)
-- calculate the coords of the crate
local y = self.config.base.h + self.config.base.borderY
y = y + self.crates:size() * (self.config.crate.h + self.config.crate.borderY)
local x = math.floor((self.config.w - self.config.crate.w)/2)
x = x + dx
crate:translate(x - crate.x, y - crate.y)
self:add(crate)
self.crates:push(crate)
end
-- return the crate itself
function Pile:pop()
local crate = self.crates:pop()
self:remove(crate)
self.screen:undoDraw(crate.obj)
return crate
end
function Pile:size()
return self.crates:size()
end