-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
309 lines (309 loc) · 8.81 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
local insert
insert = table.insert
local event, graphics, keyboard, mouse, physics, window
do
local _obj_0 = love
event, graphics, keyboard, mouse, physics, window = _obj_0.event, _obj_0.graphics, _obj_0.keyboard, _obj_0.mouse, _obj_0.physics, _obj_0.window
end
local cos, floor, min, pi, random, sin, sqrt
do
local _obj_0 = math
cos, floor, min, pi, random, sin, sqrt = _obj_0.cos, _obj_0.floor, _obj_0.min, _obj_0.pi, _obj_0.random, _obj_0.sin, _obj_0.sqrt
end
local Assets
Assets = require('assets').Assets
local Blur
Blur = require('shader').Blur
local Splash
Splash = require('ui').Splash
local Moon
Moon = require('moon').Moon
local Garbage
Garbage = require('garbage').Garbage
local Robot
Robot = require('robot').Robot
local BulletOne
BulletOne = require('bullet').BulletOne
local Explosion
Explosion = require('ps').Explosion
local PlasmaOne
PlasmaOne = require('weapon').PlasmaOne
local VERSION = 0.1
local DIFFICULTY = 5
local MAINMENU, NEWGAME, GAME, PAUSE, GAMEOVER = 1, 2, 3, 4, 5
local nope
nope = function()
return 0
end
local randomFloat
randomFloat = function(lower, greater)
return lower + random() * (greater - lower)
end
local cleanPhysicsUp
cleanPhysicsUp = function()
local _list_0 = objects.garbage
for _index_0 = 1, #_list_0 do
local object = _list_0[_index_0]
object.body:destroy()
end
objects.garbage = { }
local _list_1 = objects.bullets
for _index_0 = 1, #_list_1 do
local object = _list_1[_index_0]
object.body:destroy()
end
objects.bullets = { }
return objects.robot:reset()
end
local setStage
setStage = function(state, stage)
state.stage = stage
local _exp_0 = stage
if MAINMENU == _exp_0 then
state.time = 0
state.contam = 0
state.target_contam = 0
elseif NEWGAME == _exp_0 then
state.rate = 100
state.time = 0
state.contam = 0
state.target_contam = 0
cleanPhysicsUp()
state.weapon = PlasmaOne(assets, world, objects.robot.width)
state.stage = GAME
elseif GAME == _exp_0 then
nope()
elseif PAUSE == _exp_0 then
nope()
end
return state
end
local gravitate
gravitate = function(object, moon, gravity)
gravity = gravity or GRAVITY
local mx, my = moon.body:getPosition()
local x, y = object.body:getPosition()
local dx, dy = mx - x, my - y
local len = math.sqrt(dx * dx + dy * dy)
local fx, fy = dx / len * GRAVITY, dy / len * GRAVITY
return object.body:applyForce(fx, fy)
end
love.load = function()
state = { }
WIDTH, HEIGHT = window.getMode()
GRAVITY = 50
math.randomseed(os.time())
window.setTitle("MoonBot! " .. tostring(VERSION))
assets = Assets()
back = graphics.newQuad(0, 0, WIDTH, HEIGHT, WIDTH, HEIGHT)
splash = {
go = Splash(assets, "ENTER", {
255,
215,
71
}),
gameover = Splash(assets, "CONTAMINATED", {
255,
71,
71
}),
pause = Splash(assets, "PAUSE", {
255,
215,
71
})
}
shader = {
blur = Blur()
}
physics.setMeter(64)
world = physics.newWorld(0, 0, true)
objects = { }
objects.moon = Moon(assets, world, WIDTH / 2, HEIGHT / 2)
objects.robot = Robot(assets, world, WIDTH / 2, HEIGHT / 2 - objects.moon.radius - 30)
objects.garbage = { }
objects.bullets = { }
explosions = { }
return setStage(state, MAINMENU)
end
local hitABox
hitABox = function(fixture, damage)
if fixture:getGroupIndex() == Garbage.PH_GROUP then
local garbage = fixture:getUserData()
garbage:hit(damage)
local _ = true
end
return false
end
love.update = function(dt)
if state.stage == GAME then
world:update(dt)
local alive_garbage = { }
local _list_0 = objects.garbage
for _index_0 = 1, #_list_0 do
local garbage = _list_0[_index_0]
if garbage:isDestroyed() then
garbage:destroy()
else
gravitate(garbage, objects.moon)
insert(alive_garbage, garbage)
end
end
objects.garbage = alive_garbage
gravitate(objects.robot, objects.moon, GRAVITY * 10)
if random(1, state.rate) == 1 then
local angle = randomFloat(0, pi * 2)
local x, y = WIDTH / 2 + cos(angle) * (WIDTH + 100), HEIGHT / 2 + sin(angle) * (WIDTH + 100)
local garbage = Garbage(assets, world, x, y, random(0, pi * 2))
garbage.body:setLinearVelocity(random(-GRAVITY * 2, GRAVITY * 2), random(-GRAVITY * 2, GRAVITY * 2))
insert(objects.garbage, garbage)
end
local bullet = state.weapon:update(dt)
if bullet ~= nil then
insert(objects.bullets, bullet)
end
local alive_bullets = { }
local _list_1 = objects.bullets
for _index_0 = 1, #_list_1 do
bullet = _list_1[_index_0]
bullet:update(dt)
local _list_2 = bullet.body:getContactList()
for _index_1 = 1, #_list_2 do
local coll = _list_2[_index_1]
if coll:isTouching() then
local a, b = coll:getFixtures()
if a:getGroupIndex() ~= Robot.PH_GROUP and b:getGroupIndex() ~= Robot.PH_GROUP then
hitABox(a, bullet.damage)
hitABox(b, bullet.damage)
local explosion = Explosion(assets, bullet:getX(), bullet:getY(), 1.0)
insert(explosions, explosion)
bullet:makeDead()
break
end
end
end
if bullet:isDead() then
bullet:destroy()
else
insert(alive_bullets, bullet)
end
end
objects.bullets = alive_bullets
local alive_explosions = { }
local _list_2 = explosions
for _index_0 = 1, #_list_2 do
local explosion = _list_2[_index_0]
explosion:update(dt)
if explosion:isDead() then
explosion:destroy()
else
insert(alive_explosions, explosion)
end
end
local explosions = alive_explosions
objects.robot:update(dt, objects.moon)
if keyboard.isScancodeDown('a', 'left') then
objects.robot:left(80)
end
if keyboard.isScancodeDown('d', 'right') then
objects.robot:right(80)
end
if keyboard.isScancodeDown('w', 'up') then
objects.robot:up(110)
end
if keyboard.isScancodeDown('s', 'down') then
objects.robot:down(90)
end
if mouse.isDown(1) then
state.weapon:trigger(objects.robot:getX(), objects.robot:getY(), mouse.getX(), mouse.getY())
end
state.target_contam = #objects.moon.body:getContactList() * DIFFICULTY
state.contam = state.contam + ((state.target_contam - state.contam) * dt)
state.time = state.time + dt
if state.contam >= 100 then
return setStage(state, GAMEOVER)
end
end
end
love.keypressed = function(key, scancode, isrepeat)
if state.stage == GAME then
if key == "escape" or key == "p" then
return setStage(state, PAUSE)
end
else
if state.stage == PAUSE then
if key == "p" or key == "return" then
return setStage(state, GAME)
else
if key == "escape" then
return setStage(state, MAINMENU)
end
end
else
local _exp_0 = key
if "return" == _exp_0 then
return setStage(state, NEWGAME)
elseif "escape" == _exp_0 then
return event.quit()
end
end
end
end
love.mousepressed = function(x, y, button, istouch)
return nope()
end
local renderWorld
renderWorld = function()
objects.moon:draw()
objects.robot:draw()
local _list_0 = objects.garbage
for _index_0 = 1, #_list_0 do
local garbage = _list_0[_index_0]
garbage:draw()
end
local _list_1 = objects.bullets
for _index_0 = 1, #_list_1 do
local bullet = _list_1[_index_0]
bullet:draw()
end
graphics.setBlendMode("add")
local _list_2 = explosions
for _index_0 = 1, #_list_2 do
local explosion = _list_2[_index_0]
explosion:draw()
end
return graphics.setBlendMode("alpha")
end
love.draw = function()
graphics.setColor(255, 255, 255)
graphics.draw(assets.tex.back, back, 0, 0)
if state.stage ~= GAME then
shader.blur:draw(renderWorld)
else
renderWorld()
end
graphics.setColor(255, 255, 255)
graphics.setFont(assets.font.basic)
if state.stage == GAME then
graphics.print(state.weapon.name, 20, HEIGHT - 50)
local bar
if state.weapon:isReloading() then
bar = "[" .. ("."):rep(state.weapon:magazineSize()) .. "]"
else
bar = "[" .. ("-"):rep(state.weapon:currentMagazine()) .. (" "):rep(state.weapon:magazineSize() - state.weapon:currentMagazine()) .. "]"
end
graphics.print(bar, WIDTH - assets.font.basic:getWidth(bar) - 20, HEIGHT - 50)
end
graphics.print(os.date("%M:%S", state.time), 20, 20)
local percent = min(state.contam, 100)
graphics.setColor(227 + percent * 0.28, 255 - percent * 1.34, 121)
graphics.print(tostring(floor(state.contam)) .. " %", WIDTH - assets.font.basic:getWidth(tostring(floor(state.contam)) .. " %") - 20, 20)
local _exp_0 = state.stage
if MAINMENU == _exp_0 then
return splash.go:draw()
elseif GAMEOVER == _exp_0 then
return splash.gameover:draw()
elseif PAUSE == _exp_0 then
return splash.pause:draw()
end
end