-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
281 lines (241 loc) · 7.02 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
local BLACK = { love.math.colorFromBytes(0, 0, 0, 255) }
local WHITE = { love.math.colorFromBytes(255, 255, 255, 255) }
local RED = { love.math.colorFromBytes(255, 0, 0, 255) }
local BLUE = { love.math.colorFromBytes(0, 0, 255, 255) }
local GREEN = { love.math.colorFromBytes(0, 255, 0, 255) }
local YELLOW = { love.math.colorFromBytes(255, 255, 0, 255) }
local GRAY = { love.math.colorFromBytes(200, 200, 200, 255) }
local WIDTH = 1000
local HEIGHT = 1000
local BASEHEIGHT = 250
local BASEY = HEIGHT - BASEHEIGHT
local GUNLENGTH = BASEHEIGHT / 4
local GUNX = WIDTH / 2
local GUNY = BASEY + GUNLENGTH * 2
local MISSILE_SPEED = 500
local MISSILE_RADIUS = 5
local MOUSE_RADIUS = 10
local FONT_SIZE = 45
local FONT_PADDING = 20
local EXPLOSION_TIME = 3
local BASE_EXPLOSION_RADIUS = 35
local ENEMY_SPEED = 50
local ENEMY_WIDTH = 5
local lifes = 6
local score = 0
local mousex, mousey = 0, 0
local function drawEnd()
local font = love.graphics.getFont()
love.graphics.setColor(WHITE)
local text = "You're dead."
local w = font:getWidth(text)
local h = font:getHeight(text)
love.graphics.print(text, (WIDTH - w) / 2, HEIGHT / 2 - h - FONT_PADDING)
local text = "Final score: " .. score
local w = font:getWidth(text)
local h = font:getHeight(text)
love.graphics.print(text, (WIDTH - w) / 2, HEIGHT / 2 + FONT_PADDING)
end
local function newEnemy(e)
local x = math.random() * WIDTH
local targetX = math.random() * (WIDTH - 10)
e.x = x
e.y = 0
e.begx = x
e.targetX = targetX
e.alive = true
end
local function deployMissile(m, x, y)
m.targetX = x
m.targetY = y
m.curX = GUNX
m.curY = GUNY
m.explosionTime = 0
m.deployed = true
end
local function undeployMissile(m)
m.deployed = false
end
local enemies = {{}, {}, {}, {}, {}}
newEnemy(enemies[1])
newEnemy(enemies[2])
newEnemy(enemies[3])
newEnemy(enemies[4])
newEnemy(enemies[5])
local missiles = {{}, {}, {}}
undeployMissile(missiles[1])
undeployMissile(missiles[2])
undeployMissile(missiles[3])
local function updateEnemy(e, dt)
for i, m in ipairs(missiles) do
if m.deployed and m.curY < m.targetY then
if (e.x - m.targetX)^2 + (e.y - m.targetY)^2 < (m.explosionTime * BASE_EXPLOSION_RADIUS)^2 then
score = score + 1
newEnemy(e)
return
end
elseif e.y > BASEY then
lifes = lifes - 1
if lifes <= 0 then
love.update = nil
love.draw = drawEnd
return
end
newEnemy(e)
return
end
end
local eVec = { e.targetX - e.x, BASEY - e.y }
if eVec[1] < 0 then
eVec[1] = e.x - e.targetX
end
local hip = math.sqrt(eVec[1]^2 + eVec[2]^2)
local sin = eVec[2] / hip
local cos = eVec[1] / hip
local x = ENEMY_SPEED * dt * cos
if e.targetX < e.x then
x = e.x - x
else
x = x + e.x
end
local y = e.y + ENEMY_SPEED * dt * sin
e.x = x
e.y = y
end
local function updateMissile(m, dt)
if m.curY < m.targetY then
m.explosionTime = m.explosionTime + dt
if m.explosionTime > EXPLOSION_TIME then
undeployMissile(m)
end
return
end
local mVec = { m.targetX - m.curX, m.targetY - m.curY }
if mVec[1] < 0 then
mVec[1] = m.curX - m.targetX
end
if mVec[2] < 0 then
mVec[2] = m.curY - m.targetY
end
local hip = math.sqrt(mVec[1]^2 + mVec[2]^2)
local sin = mVec[2] / hip
local cos = mVec[1] / hip
local x = MISSILE_SPEED * dt * cos
if m.targetX < m.curX then
x = m.curX - x
else
x = x + m.curX
end
local y = m.curY - MISSILE_SPEED * dt * sin
m.curX = x
m.curY = y
end
local function drawMouse()
love.graphics.setColor(GREEN)
love.graphics.circle("line", mousex, mousey, MOUSE_RADIUS)
love.graphics.line(mousex, mousey - MOUSE_RADIUS, mousex, mousey + MOUSE_RADIUS)
love.graphics.line(mousex - MOUSE_RADIUS, mousey, mousex + MOUSE_RADIUS, mousey)
end
local function drawGun()
love.graphics.setColor(BLUE)
local restore = love.graphics.getLineWidth()
love.graphics.setLineWidth(MOUSE_RADIUS * 2)
local gunVec = { mousex - GUNX, mousey - GUNY }
if gunVec[1] < 0 then
gunVec[1] = GUNX - mousex
end
if gunVec[2] < 0 then
gunVec[2] = GUNY - mousey
end
local hip = math.sqrt(gunVec[1]^2 + gunVec[2]^2)
local sin = gunVec[2] / hip
local cos = gunVec[1] / hip
local x = GUNLENGTH * cos
if mousex < GUNX then
x = GUNX - x
else
x = x + GUNX
end
local y = GUNY - GUNLENGTH * sin
love.graphics.line(GUNX, GUNY, x, y)
love.graphics.setLineWidth(restore)
love.graphics.setColor(BLACK)
love.graphics.circle("fill", GUNX, GUNY, MOUSE_RADIUS * 2)
end
local function drawBase()
love.graphics.setColor(GRAY)
love.graphics.rectangle("fill", 0, BASEY, WIDTH, HEIGHT)
end
local function drawLifes()
love.graphics.setColor(BLACK)
love.graphics.print("Lifes: " .. lifes, FONT_PADDING, HEIGHT - (FONT_SIZE + FONT_PADDING) * 2)
end
local function drawScore()
love.graphics.setColor(BLACK)
love.graphics.print("Score: " .. score, FONT_PADDING, HEIGHT - FONT_SIZE - FONT_PADDING)
end
local function drawMissiles()
for i, m in ipairs(missiles) do
if m.deployed then
if m.explosionTime == 0 then
love.graphics.setColor(RED)
love.graphics.circle("fill", m.curX, m.curY, MISSILE_RADIUS)
else
love.graphics.setColor(YELLOW)
love.graphics.circle("fill", m.targetX, m.targetY, m.explosionTime * BASE_EXPLOSION_RADIUS)
end
end
end
end
local function drawEnemies()
love.graphics.setColor(RED)
local restore = love.graphics.getLineWidth()
love.graphics.setLineWidth(ENEMY_WIDTH)
for i, e in ipairs(enemies) do
love.graphics.line(e.begx, 0, e.x, e.y)
end
love.graphics.setLineWidth(restore)
end
function love.load()
love.window.updateMode(WIDTH, HEIGHT, {resizable = false})
love.graphics.setNewFont("Monocraft.ttf", FONT_SIZE)
love.graphics.setBackgroundColor(BLACK)
love.graphics.setColor(WHITE)
mousex, mousey = love.mouse.getPosition()
love.mouse.setVisible(false)
love.window.setTitle("Lunar Command")
end
function love.mousemoved(x, y, dx, dy, istouch)
mousex = x
mousey = y
if mousey > BASEY then
mousey = BASEY
end
end
function love.mousepressed(x, y, button, istouch, presses)
for i, m in ipairs(missiles) do
if not m.deployed then
deployMissile(missiles[i], x, y)
return
end
end
end
function love.update(dt)
for i, m in ipairs(missiles) do
if m.deployed then
updateMissile(m, dt)
end
end
for i, e in ipairs(enemies) do
updateEnemy(e, dt)
end
end
function love.draw()
drawBase()
drawGun()
drawLifes()
drawScore()
drawMissiles()
drawEnemies()
drawMouse()
end