-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
executable file
·363 lines (312 loc) · 10.5 KB
/
main.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
local global = require "global"
local Zombie = require "zombie"
local Human = require "human"
local Bullet = require "bullet"
local Gate = require "gate"
local LevelLoader = require "levelloader"
local HumanInfo = require "humaninfo"
local Trap = require "trap"
local Button = require "button"
local ModeButton = require "modebutton"
local Collidable = require "collidable"
local Tank = require "tank"
local Static = require "static"
local Barricade = require "barricade"
local ZombieGrid = require "zombiegrid"
local lastdt = 0
function love.load()
love.window.setMode(1280, 720)
love.window.setTitle("Zombie Simulator 2014")
love.graphics.setBackgroundColor(255, 248, 220)
reset()
end
function reset()
global.reset()
global.zg = ZombieGrid(86, 48, 15)
global.addDrawable(HumanInfo(0, 0))
LevelLoader("level")
-- Define mode buttons
global.addDrawable(ModeButton(984, 646,
love.graphics.newImage("button_normal.png"),
"normal"))
global.addDrawable(ModeButton(1058, 646,
love.graphics.newImage("button_heal.png"),
"heal"))
global.addDrawable(ModeButton(1132, 646,
love.graphics.newImage("button_trap.png"),
"trap"))
global.addDrawable(ModeButton(1206, 646,
love.graphics.newImage("button_barricade.png"),
"barricade"))
end
function love.update(dt)
if global.showHelp then
return
end
global.totalTime = global.totalTime + dt
lastdt = dt
global.zg:compute()
-- update pathfinding
global.grid:clear()
for e, _ in pairs(global.entities) do
if e:isInstanceOf(Static) and e.stopsHumans then
-- hope that all things with area also have these properties...
if e.w and e.h then
global.grid:fillRegion(e.x, e.y, e.w, e.h)
else
global.grid:fill(e:center())
end
end
end
for o, _ in pairs(global.entities) do
o:update(dt)
end
global.collider:update(dt)
if love.math.random() <= global.zSpawnRate * math.abs(math.cos(math.rad(global.totalTime))) * dt then
global.zSpawnRate = math.min(global.zSpawnRate * 1.01, global.maxZRate)
Zombie.spawn()
end
local humancount = 0
for _, _ in pairs(global.humans) do
humancount = humancount + 1
end
if humancount == 0 then
global.endGame = true
global.showHelp = false
end
end
function drawHelp(x, y, w, h)
love.graphics.setColor(105, 105, 105, 220)
love.graphics.rectangle("fill", x, y, w - 2*x, h - 2*y)
local text = [[Welcome to Zombie Simulator 2014
Use the mouse to direct your human survivors.
Left-click (and drag) to select, right-click to direct.
When a survivor is in a relevant 'hotzone', they can:]]
local tasks = [[ - radio the military for help
- call for reinforcements at the helipad
- repair outer doors
- increase turret accuracy
- recover in a rest area]]
love.graphics.setColor(255, 255, 255)
love.graphics.printf(text, x, y + 10, w - 2*x, "center")
love.graphics.printf(tasks, x + 490, y + 165, w)
love.graphics.printf("Good luck!", x, y + 300, w - 2*x, "center")
love.graphics.printf("(Press Esc or ? to begin)", x, y + 315, w - 2*x, "center")
end
function drawEndgame(x, y, w, h)
love.graphics.setColor(178, 34, 34, 220)
love.graphics.rectangle("fill", x, y, w - 2*x, h - 2*y)
love.graphics.setColor(0, 0, 0)
love.graphics.printf("Oh no, everyone died", x, y + 10, w - 2*x, "center")
love.graphics.printf("Here is some text which mocks/praises you based on how many zombies you managed to kill:",
x, y + 30, w - 2*x, "center")
-- :(
local texts = {
[100] = "what? you suck",
[500] = "meh",
[1000] = "not bad",
[5000] = "nice work",
[10000] = "excellent!"
}
local textkeys = {}
for k, _ in pairs(texts) do
textkeys[#textkeys+1] = k
end
table.sort(textkeys)
local text = "i think you broke the game"
for _, count in ipairs(textkeys) do
if global.killedZombies <= count then
text = texts[count]
break
end
end
love.graphics.setNewFont(50)
love.graphics.printf(text, x, y + 245, w - 2*x, "center")
love.graphics.setNewFont(24)
love.graphics.printf("Press [r] to restart", x, h - 2*y, w - 2*x, "center")
love.graphics.setNewFont(12)
end
function love.draw()
local winW = love.graphics.getWidth()
local winH = love.graphics.getHeight()
local mouseX = love.mouse.getX()
local mouseY = love.mouse.getY()
global.correctDrawables()
for _, d in pairs(global.drawables) do
d:draw()
end
if dragging then
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("line", dragging.x, dragging.y,
mouseX - dragging.x, mouseY - dragging.y)
end
love.graphics.setNewFont(20)
love.graphics.setColor(0, 0, 0)
love.graphics.printf(string.format("Zombie kills: %05d", global.killedZombies),
0, 5, winW, "center")
love.graphics.setNewFont(12)
local msgstr = ""
for i = 1, global.messagenum do
msgstr = msgstr .. global.messages[i] .. "\n"
end
love.graphics.printf(msgstr, 5, winH - global.maxmessages * 18, 450)
if global.debug then
love.graphics.setColor(255, 0, 0)
global.drawHitboxes()
global.zg:drawGrid(global.zg.heights)
love.graphics.setColor(0, 0, 0)
love.graphics.printf(string.format("x: %d, y: %d", mouseX, mouseY), 0, 0, 130)
love.graphics.printf(string.format("i: %d, j: %d",
math.floor(mouseX / 15), math.floor(mouseY / 15)),
130, 0 ,130)
love.graphics.printf(string.format("dt: %f", lastdt),
260, 0 ,130)
end
if global.showHelp then
drawHelp(40, 40, winW, winH)
elseif global.endGame then
drawEndgame(40, 40, winW, winH)
end
end
function love.keypressed(key)
if global.endGame then
if key == "r" then
reset()
else
return
end
end
if key == "d" then
global.debug = not global.debug
elseif key == 'escape' and global.showHelp then
global.showHelp = false
elseif key == 'escape' then
unselectAll()
elseif key == 'a' then
for e, _ in pairs(global.humans) do
e:setSelected()
end
elseif key == 'kp0' then
for e, _ in pairs(global.humans) do
if e.selected then
e:setMode("normal")
end
end
elseif key == 'kp1' then
for e, _ in pairs(global.humans) do
if e.selected then
e:setMode("heal")
end
end
elseif key == 'kp2' then
for e, _ in pairs(global.humans) do
if e.selected then
e:setMode("trap")
end
end
elseif key == 'kp3' then
for e, _ in pairs(global.humans) do
if e.selected then
e:setMode("barricade")
end
end
elseif (key == '/' or key == '?') and not global.endGame then
global.showHelp = not global.showHelp
end
if global.debug then
if key == "z" then
global.zSpawnRate = global.maxZRate
elseif key == "h" then
local x, y = love.mouse.getPosition()
global.addDrawable(Human:new(x, y, 10, 0.1, 1))
elseif key == "q" then
global.addDrawable(Zombie:new(love.mouse.getPosition()))
elseif key == "b" then
global.addDrawable(Barricade:new(love.mouse.getPosition()))
elseif key == 'c' then
for e, _ in pairs(global.humans) do
e:destroy()
end
for e, _ in pairs(global.zombies) do
e:destroy()
end
elseif key == 'b' then
local b = Bullet(love.mouse.getX(), love.mouse.getY(),
love.math.random(-250,250),
love.math.random(-250,250), 50)
global.addDrawable(b)
elseif key == 't' then
global.addDrawable(Trap(love.mouse.getX(), love.mouse.getY()))
elseif key == 'm' then
global.addDrawable(Tank(love.mouse.getX(), love.mouse.getY()))
end
end
end
function love.keyreleased(key)
end
-- When holding shift, toggle the selection state of entities in the
-- set (but don't unselect things we have already selected), if not
-- holding shift, same as before: one-unit selections.
function click(entities, isdragging)
for e, _ in pairs(entities) do
if e:isInstanceOf(Button) and e.depressed then
e:onClick()
e.depressed = false
return
end
end
for e, _ in pairs(global.entities) do
if e:isInstanceOf(Human) then
if love.keyboard.isDown("lshift") then
if entities[e] then
e:toggleSelected()
end
else
if entities[e] then
e:setSelected()
else
e:setUnselected()
end
end
elseif e:isInstanceOf(Gate) and not isdragging and entities[e] then
e:toggle()
end
end
end
-- Unselect all selected entities
function unselectAll()
for e, _ in pairs(global.humans) do
e:setUnselected()
end
end
function love.mousepressed(x, y, button)
if button == "l" then
dragging = {x=x, y=y}
for _, o in pairs(global.collidablesAt(x, y)) do
if o:isInstanceOf(Button) then
o.depressed = true
end
end
end
end
function love.mousereleased(x, y, button)
if button == "l" then
if dragging and not (dragging.x == x and dragging.y == y) then
if dragging.x == x then
x = x + 1
elseif dragging.y == y then
y = y + 1
end
click(global.collidablesUnder(dragging.x, dragging.y, x, y), true)
else
click(global.collidablesAt(x, y), false)
end
dragging = nil
elseif button == "r" then
for e, _ in pairs(global.humans) do
if e.selected then
e:setDest(x, y)
end
end
end
end