-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arena.lua
executable file
·250 lines (224 loc) · 5.49 KB
/
Arena.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
Color = require('Color')
Paddle = require('Paddle')
Ball = require('Ball')
physics = require('physics')
physics.start()
physics.setGravity(0, 0)
local Arena = {
height = display.contentHeight - 50,
width = display.contentWidth,
thickness = 20,
displayGroup = display.newGroup(),
physics = physics,
lives = 2,
shapes = {}
}
function Arena:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
-- draw arena background
function Arena:drawBg()
local bg =
display.newRect(0,50,self.width, self.height)
bg.anchorX = 0
bg.anchorY = 0
bg:setFillColor(unpack(Color.teal))
self.bg = bg
self.displayGroup:insert(bg)
end
-- draw arena scoreboard
function Arena:drawScoreboard()
local livesText = display.newText(
'Lives: ' .. self.lives,
self.width / 2, 0,
native.systemFontBold, 72)
self.scoreboard = livesText
end
-- update current score
function Arena:updateScoreboard()
self.scoreboard.text = 'Lives: ' .. self.lives
end
-- remove scoreboard
function Arena:removeScoreboard()
self.scoreboard:removeSelf()
self.scoreboard = nil
end
-- swap red and blues. remove red and replace with blue, and vice versa
function Arena:swapRedBlueShapes(redBlockClass, blueBlockClass)
local len = #self.shapes
for i = 1, len do
local x = self.shapes[i].x
local y = self.shapes[i].y
if(self.shapes[i].color == 'red') then
self.shapes[i]:removeSelf()
self.shapes[i] = nil
blueBlock = blueBlockClass:new({
xPos = x,
yPos = y
})
blueBlock:init()
local shape = blueBlock.shape
self.shapes[i] = shape
timer.performWithDelay(
10,
function()
self.physics.addBody(shape, 'dynamic', {density = 100})
end
)
elseif(self.shapes[i].color == 'blue') then
self.shapes[i]:removeSelf()
self.shapes[i] = nil
redBlock = redBlockClass:new({
xPos = x,
yPos = y
})
redBlock:init()
local shape = redBlock.shape
self.shapes[i] = shape
timer.performWithDelay(
10,
function()
self.physics.addBody(shape, 'dynamic', {density = 100})
end
)
end
end
end
-- draw arena walls
function Arena:drawWalls()
local top =
display.newRect(0, 50,self.width, self.thickness)
local left =
display.newRect(0, 50,self.thickness, self.height)
local right =
display.newRect(self.width-self.thickness, 50,self.thickness,self.height)
local bottom =
display.newRect(0,self.height + 50, self.width, self.thickness)
top.anchorX = 0
top.anchorY = 0
left.anchorX = 0
left.anchorY = 0
right.anchorX = 0
right.anchorY = 0
bottom.anchorX = 0
bottom.anchorY = 0
top:setFillColor(unpack(Color.gray))
left:setFillColor(unpack(Color.gray))
right:setFillColor(unpack(Color.gray))
bottom:setFillColor(unpack(Color.gray))
self.displayGroup:insert(top)
self.displayGroup:insert(left)
self.displayGroup:insert(right)
self.displayGroup:insert(bottom)
self.physics.addBody(top, 'static')
self.physics.addBody(left, 'static')
self.physics.addBody(right, 'static')
self.physics.addBody(bottom, 'static')
-- add listener for bottom collision
bottom:addEventListener(
'collision',
function()
event = {
name = 'bottomCollision'
}
Runtime:dispatchEvent(event)
end
)
end
-- add physics to shape
function Arena:addBlockShapePhysics(shape)
self.physics.addBody(shape, 'dynamic', {density = 100.0})
end
-- add shape to arena
function Arena:addShape(shape)
timer.performWithDelay(100, function()
self:addBlockShapePhysics(shape)
shape.isFixedRotation = true
self.shapes[#self.shapes + 1] = shape
end)
end
-- draw paddle
function Arena:drawPaddle()
paddle = Paddle:new()
paddle:init()
self.displayGroup:insert(paddle.shape)
self.physics.addBody(paddle.shape, 'static')
self.paddle = paddle
end
-- remove paddle
function Arena:removePaddle()
self.paddle:deactivate()
self.paddle.shape:removeSelf()
self.paddle = nil
end
-- change scoreboard to game over
function Arena:gameOver()
self.scoreboard.text = "GAME OVER"
end
-- flash color when hitting bottom wall
function Arena:wallHit()
self.bg:setFillColor(unpack(Color.black))
timer.performWithDelay(
50,
function()
self.bg:setFillColor(unpack(Color.teal))
end
)
end
-- handle losing a life ,remove ball, decrement lives, redraw ball
function Arena:loseLife()
self:removeBall()
self.lives = self.lives - 1
self:wallHit()
if(self.lives <= 0) then
self:gameOver()
else
self:updateScoreboard()
timer.performWithDelay(
5000,
function()
self:drawBall()
end)
end
end
-- draw background and walls
function Arena:drawSetup()
self:drawBg()
self:drawWalls()
end
-- draw ball and add physics to move
function Arena:drawBall()
ball = Ball:new()
ball:init()
self.displayGroup:insert(ball.shape)
self.physics.addBody(
ball.shape,
'dynamic',
{
bounce = 1,
radius = ball.radius
}
)
ball.shape:applyForce( 3, 3, ball.shape.x, ball.shape.y);
self.ball = ball
end
-- remove ball
function Arena:removeBall()
self.ball.shape:removeSelf()
self.ball = nil
end
-- set up display for arena
function Arena:init()
self:drawSetup()
end
-- draw paddle, add display handler, ball, and scoreboard
function Arena:startGame()
self:drawPaddle()
self.paddle:activate()
self:drawBall()
self:drawScoreboard()
end
return Arena