-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.lua
312 lines (263 loc) · 8.53 KB
/
world.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
World = {}
function World:new(orginX, orginY, width, height)
local new = {
entities = {},
speed = 1,
gameIsOver = false,
firstGame = true,
showTheMessage = true,
currentMessage = "",
currentTitle = "",
isPaused = false,
bulletTime = 5,
maxBulletTime = 5,
}
setmetatable(new, self)
self.__index = self
-- Where do the boundaries start and end
new.boundaries = {
["orginX"] = orginX,
["orginY"] = orginY,
["width"] = width,
["height"] = height,
}
return new
end
-- Add an entity to this world
function World:addEntity(entity, atBeginning)
-- TODO make sure this is an entity
if atBeginning then
table.insert(self.entities, 1, entity)
else
table.insert(self.entities, entity)
--self.entities[#self.entities + 1] = entity
end
return self
end
-- Sets the player
function World:setPlayer(player)
self.player = player
return self
end
-- Get the player
function World:getPlayer()
return self.player
end
-- Draw the worlds entities
function World:draw()
love.graphics.setBackgroundColor(20, 20, 20)
self:drawEntities()
self:drawBulletTime()
if self.showTheMessage then
love.graphics.draw(bloodyImage, 0, 0)
love.graphics.setColor(255, 255, 255)
love.graphics.setFont(bloodyFont35)
love.graphics.printf(self.currentTitle, 0, 150, 800, "center")
love.graphics.setFont(bloodyFont25)
love.graphics.printf(self.currentMessage, 0, 250, 800, "center")
end
end
-- Update the Worlds entites
function World:update(deltaTime)
if self.bulletTime <= 0 and not self.isPaused then
self.speed = 1
self.bulletTime = 0.1
elseif self.speed == 1 and self.bulletTime < 5 then
self.bulletTime = self.bulletTime + deltaTime
elseif self.speed ~= 1 and not self.isPaused then
self.bulletTime = self.bulletTime - deltaTime
end
for i,v in ipairs(self.entities) do
v:update(deltaTime)
if v:isRemoveable() then
table.remove(self.entities, i)
else
local collider = self:detectCollision(v, nil, true)
if collider and not collider:isRemoveable() then
-- Check if zombie hits bullet, or bullet hits zombie
if collider.getType() == "bullet" and v.getType() == "zombie" then
-- die, zombie, die!!
v:hit(collider:getDamage())
collider.remove = true
elseif collider.getType() == "zombie" and v.getType() == "bullet" then
collider:hit(v:getDamage())
v.remove = true
end
-- Check if zombie hits player or player hits zombie
if (collider.getType() == "player" and v.getType() == "zombie")
or (collider.getType() == "zombie" and v.getType() == "player") then
world:gameOver()
end
end
end
end
end
-- Draw the entities
function World:drawEntities()
for i=1, #self.entities do
self.entities[i]:draw()
end
end
-- Check if specific coordinates are out of the worlds boundaries
function World:outOfBoundaries(x, y)
return not (x > self.boundaries.orginX
and x < (self.boundaries.orginX + self.boundaries.width)
and y > self.boundaries.orginY
and y < (self.boundaries.orginY + self.boundaries.height))
end
-- Detect a collision
function World:detectCollision(collider, exclude, biggerBox)
-- collision detection
for i, entity in ipairs(self.entities) do
if collider ~= entity and (not exclude or entity ~= exclude) and entity:isCollidable() then
local distance = math.sqrt(
math.pow(math.abs(collider.x - entity.x), 2)
+ math.pow(math.abs(collider.y - entity.y), 2)
)
if distance < entity.size + collider.size + (biggerBox and 1 or 0) then
return entity
end
end
end
return false
end
-- get the current speed (is needed for bullettime)
function World:getSpeed()
return self.speed or 1
end
-- toggle the bullettime
function World:toggleBulletTime()
if world.isPaused then
return
end
if self:getSpeed() == 1 and self.bulletTime > 0 then
self.speed = 0.3
else
self.speed = 1
end
end
-- Adds an specific amount of zombies
function World:addZombies(count, speed, attachToEntity)
for i=1,count do
while (true) do
local x = math.random() * world.boundaries.width
local y = math.random() * world.boundaries.height + 600
local size = 8
if not self:detectCollision({x = x, y = y, size = size}) then
local zomb = Zombie:new(
x, y, speed, size
);
-- Check to which entity the zombies should be attached
if attachToEntity then
zomb:attachTo(attachToEntity)
else
zomb:attachTo(player)
end
world:addEntity(zomb)
break
end
end
end
end
-- get the entitiy count
function World:getEntityCount(type)
if not type then
return #self.entities
end
local count = 0
for i, entity in ipairs(self.entities) do
if entity.getType and entity.getType() == type then
count = count + 1
end
end
return count
end
function World:removeEntities(entityType)
for i, entity in ipairs(self.entities) do
if not entityType or (entity.getType and entity.getType() == entityType) then
entity.remove = true
end
end
end
-- pause the team
function World:pause()
self.speed = 0
self.isPaused = true
end
-- unPause the game
function World:unPause()
self.speed = 1
self.isPaused = false
end
-- the game is over
function World:gameOver()
self:pause()
self.gameIsOver = true
local message = "You got " .. Highscore:getKills() .. " zombies killed"
if Highscore:getKills() < 40 then
message = message .. "\nThats pretty lame ...)"
elseif Highscore:getKills() < 60 then
message = message .. "\nYou are doing well ..."
elseif Highscore:getKills() < 80 then
message = message .. "\nKeep up the good work ..."
elseif Highscore:getKills() < 100 then
message = message .. "\nNice one, dude ..."
elseif Highscore:getKills() < 120 then
message = message .. "\nAwsome!!"
elseif Highscore:getKills() < 140 then
message = message .. "\nUnstoppable!"
elseif Highscore:getKills() < 160 then
message = message .. "\nYou are insane!"
elseif Highscore:getKills() < 180 then
message = message .. "\nThats fucking impossible!!!"
elseif Highscore:getKills() < 200 then
message = message .. "\nDude, get a life! Thats just a little game xD"
elseif Highscore:getKills() >= 200 then
message = message .. "\nSome would say your cheating :)"
end
self:showMessage(
"GAME OVER",
"Arghhh, Brainz! Eatin' ya brainz makes me a happy zombiee\n\n"
.. message
.. "\n\nStart over with (space)"
)
end
-- start the game
function World:startGame()
Highscore:reset()
-- need to remove all the zombies
self:removeEntities("zombie")
self:removeEntities("bullet")
self.gameIsOver = false
self:togglePause()
end
-- show the message
function World:showMessage(title, message)
love.graphics.clear ()
self.currentMessage = message
self.currentTitle = title
self.showTheMessage = true
end
-- hide the message
function World:hideMessage()
self.showTheMessage = false
end
-- toggle the pause state
function World:togglePause()
if self.isPaused then
self:hideMessage()
self:unPause()
else
self:showMessage("Pause", "Unpause with [space]")
self:pause()
end
end
-- Draw the bulletTime indicator
function World:drawBulletTime()
if self.isPaused then
return
end
love.graphics.setColor(255, 255, 255, 80)
love.graphics.rectangle("line", 375, 550, 50, 15)
love.graphics.rectangle("fill", 375, 550, 50 * self.bulletTime / self.maxBulletTime, 15)
end