-
Notifications
You must be signed in to change notification settings - Fork 0
/
Air.lua
345 lines (300 loc) · 10.5 KB
/
Air.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
Enemy = require("Enemy")
function itemAmmo(x, y, volume)
volume = volume or 50
return {
x = x,
y = y,
volume = volume,
width = 50,
height = 50,
update = function(self, dt)
self.y = self.y + ground.speed * dt
end,
draw = function(self, dt)
cx = self.x + self.width / 2
cy = self.y + self.height / 2
love.graphics.setColor(1, 0, 0)
love.graphics.circle("fill", cx, cy, self.width / 2)
love.graphics.setColor(0.8, 0, 0)
love.graphics.circle("fill", cx, cy, self.width / 2 - 8)
love.graphics.setColor(0, 0, 0)
love.graphics.circle("line", cx, cy, self.width / 2)
end,
apply = function(self)
player.ammo = player.ammo + self.volume
end
}
end
function itemHealth(x, y, volume)
volume = volume or 5
return {
x = x,
y = y,
volume = 5,
width = 50,
height = 50,
update = function(self, dt)
self.y = self.y + ground.speed * dt
end,
draw = function(self, dt)
cx = self.x + self.width / 2
cy = self.y + self.height / 2
love.graphics.setColor(0, 1, 0)
love.graphics.circle("fill", cx, cy, self.width / 2)
love.graphics.setColor(0, 0.8, 0)
love.graphics.circle("fill", cx, cy, self.width / 2 - 8)
love.graphics.setColor(0, 0, 0)
love.graphics.circle("line", cx, cy, self.width / 2)
end,
apply = function(self)
player.health = math.min(player.health + self.volume, 10)
end
}
end
function itemWeapon(x, y)
return {
x = x,
y = y,
volume = 5,
width = 50,
height = 50,
update = function(self, dt)
self.y = self.y + ground.speed * dt
end,
draw = function(self, dt)
cx = self.x + self.width / 2
cy = self.y + self.height / 2
love.graphics.setColor(1, 0, 1)
love.graphics.circle("fill", cx, cy, self.width / 2)
love.graphics.setColor(0.8, 0, 0.8)
love.graphics.circle("fill", cx, cy, self.width / 2 - 8)
love.graphics.setColor(0, 0, 0)
love.graphics.circle("line", cx, cy, self.width / 2)
end,
apply = function(self)
player.weapon = RandomWeapon()
end
}
end
function Air()
local boomSprite = {
image = love.graphics.newImage("sprites/boom.png"),
quads = {},
width = 128,
height = 32,
quadSize = 32,
}
for i = 1, 4 do
boomSprite.quads[i] = love.graphics.newQuad(
(i - 1) * boomSprite.quadSize,
0,
boomSprite.quadSize,
boomSprite.quadSize,
boomSprite.width,
boomSprite.height
)
end
local screenWidth = love.graphics.getWidth()
local screenHeight = love.graphics.getHeight()
local clouds = {}
local cloudSprite = {
image = love.graphics.newImage("sprites/cloud.png"),
width = 64,
height = 64,
}
local cloudCount = 8
for i = 1, cloudCount do
local c = {
x = math.random(screenWidth),
y = math.random(screenHeight),
speed = math.random(512, 1536),
scale = math.random(2, 4),
}
table.insert(clouds, c)
end
local explosions = {}
local missiles = {}
local enemies = {}
local items = {}
return {
player = player,
enemies = enemies,
missiles = missiles,
items = items,
update = function(self, dt)
for key, cloud in pairs(clouds) do
cloud.y = cloud.y + cloud.speed * dt
if cloud.y > screenHeight then
cloud.x = math.random(screenWidth - cloudSprite.width * cloud.scale)
cloud.y = math.random(-screenHeight, - cloudSprite.height * cloud.scale)
end
end
activeCount = 0
for key, enemy in pairs(enemies) do
if enemy and enemy.active then
activeCount = activeCount + 1
enemy:update(dt)
else
enemies[key] = nil
end
end
-- no more enemies left, respawn
if activeCount == 0 then
self:addEnemies(math.random(1, 10))
end
-- move existing missiles
for key, msl in pairs(missiles) do
if msl and msl.active then
msl.y = msl.y + msl.speed * dt
-- enemy missiles can hurt other enemies, too
for key, enemy in pairs(enemies) do
if enemy and enemy.active and msl.owner ~= enemy then
if collision(msl, enemy) then
enemy:hit(msl)
break
end
end
end
-- only enemy missiles can hurt the player
if not msl.ours and player.active and collision(msl, player) then
player:hit(msl)
end
if msl.y < 0 or msl.y > screenHeight then
msl.active = false
end
else
missiles[key] = nil
end
end
-- update items, which move at the same speed as the ground
local itemCount = 0
for key, item in pairs(items) do
if item then
itemCount = itemCount + 1
item:update(dt)
if player.active and collision(item, player) then
item:apply()
items[key] = nil
end
if item.y > love.graphics.getHeight() then
items[key] = nil
end
end
end
--
-- Give the player more ammo if they've run out
--
if itemCount == 0 and player.ammo <= 0 and math.random() > 0.99 then
self:addItem(math.random(love.graphics.getWidth()), 0, "ammo")
end
-- update explosions
for key, expl in pairs(explosions) do
if expl and expl.active then
expl.frame = expl.frame + expl.speed * dt
if expl.frame > #boomSprite.quads then
-- show's over, hide and disable whatever exploded
expl.active = false
expl.thing.active = false
end
else
explosions[key] = nil
end
end
end,
draw = function(self)
love.graphics.setColor(1, 1, 1)
for key, cloud in pairs(clouds) do
love.graphics.draw(cloudSprite.image, cloud.x, cloud.y, 0, cloud.scale)
end
-- cloud shadows
--[[
for i = 1, #clouds do
local c = clouds[i]
local width = clouds[i].scale * cloud.width
local cx, cy = c.x + width / 2, c.y + width / 2
local offset = clouds[i].scale * cloud.width / 16
love.graphics.setColor(75/255, 105/255, 47/255)
love.graphics.circle("fill", cx - offset, c.x + offset / 2, 15 * c.scale)
end
--]]
if player.active then
player:draw()
end
for key, enemy in pairs(enemies) do
if enemy and enemy.active then
enemy:draw()
end
end
for key, msl in pairs(missiles) do
if msl and msl.active and msl.ours then
love.graphics.setColor(1, 0, 0)
love.graphics.rectangle("fill", msl.x, msl.y, msl.width, msl.height)
elseif msl and msl.active then
love.graphics.setColor(0, 0, 1)
love.graphics.rectangle("fill", msl.x, msl.y, msl.width, msl.height)
end
end
-- Nb. this must happen _after_ the player etc. have been drawn
for key, expl in pairs(explosions) do
if expl and expl.active then
love.graphics.setColor(expl.color[1], expl.color[2], expl.color[3])
love.graphics.draw(
boomSprite.image,
boomSprite.quads[math.floor(expl.frame)],
expl.x - 5 * 32,
expl.y - 5 * 32,
0,
10
)
end
end
for key, item in pairs(items) do
if item then
item:draw()
end
end
end,
addExplosion = function(self, x, y, color)
expl = boom({x=x, y=y})
expl.speed = 5 + math.random() * 5
expl.color = color or {1, 1, 0.85}
table.insert(explosions, expl)
end,
addEnemy = function(self)
local enemy = Enemy()
table.insert(enemies, enemy)
return enemy
end,
addEnemies = function(self, number)
--
-- TODO: pick a safe place to add the new enemy
--
local spacing = love.graphics.getWidth() / (number + 1)
for i = 1, number do
local enemy = self.addEnemy()
enemy.x = i * spacing + math.random(-32, 32)
enemy.triggerHappiness = love.math.random()
end
end,
addMissile = function(self, x, y, ours)
local msl = fire(x, y)
msl.ours = ours
msl.speed = 1000
if ours then
msl.speed = -1000
end
table.insert(missiles, msl)
return msl
end,
addItem = function(self, x, y, kind)
if kind == "ammo" then
table.insert(items, itemAmmo(x, y))
elseif kind == "health" then
table.insert(items, itemHealth(x, y))
elseif kind == "weapon" then
table.insert(items, itemWeapon(x, y))
end
end,
}
end
return Air