-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
226 lines (180 loc) · 5.12 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
local love = require 'love'
math.random, math.randomseed = love.math.random, nil
local utils = require 'utils'
local game = require'game'
local const = require 'const'
local g
local state = const.initialstate
if state ~= 'menu' then
g = game.Game()
end
local fontsize = 24
local font = love.graphics.newFont(const.fontfilename, fontsize)
love.graphics.setFont(font)
local gamecallback = {}
function gamecallback.update()
g:update()
end
function gamecallback.mousepressed(x, y, button)
if button == 1 then
g:fireMissleAt(x, y)
end
if button == 2 then
state = 'pause'
end
end
function gamecallback.keypressed(key, scancode, isrepeat)
if scancode == 'space' then
g:fireMissleAt(love.mouse.getPosition())
end
if scancode == 'escape' then
state = 'pause'
end
if const.dev and scancode == 'e' then
g:fireEnemyMissle()
end
end
function gamecallback.draw()
for _, base in ipairs(g.bases) do
if base.ok then
love.graphics.setColor(0x0, 0xff, 0x0)
else
love.graphics.setColor(0xff, 0x0, 0x0)
end
local bw, bh = 50, const.baseheight
love.graphics.rectangle('fill', base.x - bw / 2, base.y - bh / 2, bw, bh)
love.graphics.setColor(0xff, 0x0, 0x0)
if base.ok then
love.graphics.print(base.ammo, base.x - bw / 2, base.y - bh / 2)
end
end
for _, town in ipairs(g.towns) do
if town.ok then
love.graphics.setColor(0x0, 0x0, 0xff)
else
love.graphics.setColor(0xff, 0x0, 0x0)
end
local tw, th = const.townwidth, const.townheight
love.graphics.rectangle('fill', town.x - tw / 2, town.y - th / 2, tw, th)
end
for _, m in ipairs(g.missles) do
if m.enemy then
love.graphics.setColor(0xff, 0x0, 0x0)
else
love.graphics.setColor(0x00, 0xff, 0x0)
love.graphics.circle('line', m.gx, m.gy, 5)
end
love.graphics.line(m.sx, m.sy, m.x, m.y)
end
for _, e in ipairs(g.explosions) do
local g = const.explosionmaxframes - e.frames
love.graphics.setColor(0xff, 0xff, 0xff, g)
love.graphics.circle('fill', e.x, e.y, const.explosionradius)
end
if not g.lost then
love.graphics.setColor(0x0, 0x7f, 0x0, 0x7f)
local x, y = love.mouse.getPosition()
local b = g:getNearestWorkingBase(x, y)
if b then
love.graphics.line(b.x, b.y - const.baseheight / 2, x, y)
love.graphics.circle('fill', x, y, const.explosionradius)
end
end
love.graphics.setColor(0x33, 0x24, 0x1f)
love.graphics.rectangle('fill', 0, const.groundlevel, 800, 10^5)
love.graphics.setColor(0xff, 0xff, 0xff)
local texts
if g.lost then
texts = {"It's over. You lost.", g.score .. ' points. Level ' .. g.level .. '.'}
else
texts = {'Level: ' .. g.level, 'Score: ' .. g.score}
end
love.graphics.print(table.concat(texts, '\n'), 0, const.groundlevel)
end
local pausecallback = {}
function pausecallback.update() end
function pausecallback.mousepressed(x, y, button)
if button == 1 then
state = 'game'
end
if button == 2 then
state = 'menu'
g = nil
end
end
function pausecallback.keypressed(key, scancode, isrepeat)
if scancode == 'escape' then
state = 'menu'
g = nil
end
if scancode == 'space' then
state = 'game'
end
end
function pausecallback.draw()
local msg = [[
Game paused!
LMB or Space to go back to game.
RMB or Escape to go back to main menu.
]]
love.graphics.printf(msg, 0, 0, 800, 'center')
end
local menucallback = {}
function menucallback.update() end
function menucallback.mousepressed(x, y, button)
if button == 1 then
state = 'game'
g = game.Game()
end
if buttons == 2 then
love.event.push('quit')
end
end
function menucallback.keypressed(key, scancode, isrepeat)
if scancode == 'space' then
state = 'game'
g = game.Game()
end
if scancode == 'escape' then
love.event.push('quit')
end
end
function menucallback.draw()
local msg = [[
Missles in Lua made using LÖVE, SLAM and sfxr!
LMB or Space to start a new game.
RMB or Escape to quit.
In-game controls:
Mouse movement to aim.
LMB or Space to shoot a missle.
RMB or Escape to pause.
Game made by FRex
for
GameDev.net 2018 New Year Challenge: Missile Command
Source code: https://github.com/FRex/Missles
]]
love.graphics.printf(msg, 0, 0, 800, 'center')
end
local callbacks = {
game = gamecallback,
pause = pausecallback,
menu = menucallback,
}
local timesaved = 0
function love.update(dt)
timesaved = timesaved + dt
local steptime = 1 / 60
while timesaved >= steptime do
timesaved = timesaved - steptime
callbacks[state].update(dt)
end
end
function love.mousepressed(x, y, button)
callbacks[state].mousepressed(x, y, button)
end
function love.keypressed(key, scancode, isrepeat)
callbacks[state].keypressed(key, scancode, isrepeat)
end
function love.draw()
callbacks[state].draw()
end