-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene_game.lua
396 lines (315 loc) · 11.7 KB
/
scene_game.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
Gamestate = require "hump.gamestate"
require 'point'
require 'camera'
require 'player'
require 'radar'
local game = {}
function game:init()
game.pSight = 175
game.pSense = 200
game.rSight = 12
game.rSense = 30
game.music = love.audio.newSource("sounds/horror_background.mp3")
game.music:setLooping(true)
game.engineSound = love.audio.newSource("sounds/ghost-yacht.wav")
game.engineSound:setLooping(true)
game.breathing = love.audio.newSource("sounds/breathing.wav")
game.breathing:setLooping(true)
love.math.setRandomSeed(love.math.getRandomSeed())
end
function game:enter(previous, shouldKeepState)
game.volume = 0.0
--game.music:setPitch(0.3)
game.music:play()
game.engineSound:play()
game.breathing:play()
if not shouldKeepState then
game.sum = 0
game.rotDirection = 0
game.goDirection = 0
game.inertia = 0
game.spaceWidth = 8000
game.world = {}
game.player = player
game.player:init()
game.player.air = 100
game.player.power = 100
game.player.gold = 0
game.radar = radar
game.radar:init(400, 100)
game.radarPeriod = 5
game.powerAmount = 500
game.airAmount = 500
game.goldAmount = 10
game.dronesAmount = 20
game.scrapAmount = 500
game.player.airDelay = 0.5
game.camera = camera
game.currentRadarTime = game.radarPeriod
createWorld()
end
end
function game:leave()
game.music:stop()
game.engineSound:stop()
game.breathing:stop()
end
function game:update(dt)
game.player:rotate(game.rotDirection*3*dt)
local forward = game.goDirection*15*dt
if game.player.power == 0 then forward = 0 end
game.inertia = game.inertia * (1 - 3*dt) + forward
game.player:forward(game.inertia)
checkForCollision(dt)
game.volume = math.min(0.1, game.volume+dt/5)
game.music:setVolume(game.volume)
game.camera:setRotation(game.player.rot+math.pi/2)
game.camera:setRotationCenter(game.player.x, player.y)
local width, height = love.window.getDimensions()
game.camera:setPosition(game.player.x - width/2, game.player.y - height/2)
--[[currentRadarTime = currentRadarTime - dt
if currentRadarTime < 0 then
currentRadarTime = currentRadarTime + radarPeriod
radar:resetRadius()
end
radar:update(dt)
radar:setCenter(player.x, player.y)
---]]
game.player.airDelay = game.player.airDelay - dt
if (game.player.airDelay < 0) then
game.player.air = math.max(0, game.player.air-dt*2)
end
game.player.power = math.max(0, game.player.power - dt*math.abs(game.inertia)*0.8 )
game.engineSound:setVolume(math.max(game.inertia/20, math.min(0.15, game.player.power/100)))
local breathVolume = (110-game.player.air)/100
if game.player.air < 20 then
breathVolume = 0.8 * math.max(0, game.player.air-3) / 18
game.music:setVolume(0.0)
end
game.breathing:setVolume(breathVolume)
if game.player.air == 0 then
Gamestate.switch(game_end, false)
elseif game.player.gold == game.goldAmount then
Gamestate.switch(game_end, true)
end
end
function checkForCollision(dt)
local p = Point(game.player.x, game.player.y)
local r = Point(game.radar.x, game.radar.y)
for k,v in pairs(game.world) do
local x,y = v.p:get()
local mX = math.abs(game.player.x - x)
local mY = math.abs(game.player.y - y)
if mX + mY < 16 then
local length = (v.p - p):len()
if length < 16 then
--collision happen
if v.type == "air" then
game.player.air = math.min(100, game.player.air + 10)
table.remove(game.world, k)
elseif v.type == "power" then
game.player.power = math.min(100, game.player.power + 10)
table.remove(game.world, k)
elseif v.type == "gold" then
game.player.gold = game.player.gold + 1
table.remove(game.world, k)
elseif v.type == "scrap" then
game.inertia = game.inertia * (1 - 10* dt)
elseif v.type == "drone" then
game.player.power = game.player.power - dt*70
end
end
end
if mX + mY < 400 and v.type == "drone" then
local dir = v.p - p
local len = dir:len()
if len > 0 then
dir:normalize()
dir = 100 * dt *((400-len)/400)* dir
v.p = v.p - dir
end
end
end
end
function createWorld()
for i=1,game.airAmount do
local x = love.math.random() * game.spaceWidth - game.spaceWidth/2
local y = love.math.random() * game.spaceWidth - game.spaceWidth/2
local hp = {}
hp.p = Point(x, y)
hp.type = "air"
table.insert(game.world, hp)
end
for i=1,game.powerAmount do
local x = love.math.random() * game.spaceWidth - game.spaceWidth/2
local y = love.math.random() * game.spaceWidth - game.spaceWidth/2
local hp = {}
hp.p = Point(x, y)
hp.type = "power"
table.insert(game.world, hp)
end
for i=1,game.goldAmount do
local x = love.math.random() * game.spaceWidth - game.spaceWidth/2
local y = love.math.random() * game.spaceWidth - game.spaceWidth/2
local hp = {}
hp.p = Point(x, y)
hp.type = "gold"
table.insert(game.world, hp)
end
for i=1,game.dronesAmount do
local x = love.math.random() * game.spaceWidth - game.spaceWidth/2
local y = love.math.random() * game.spaceWidth - game.spaceWidth/2
local dist = Point(game.player.x-x, game.player.y-y)
while dist:len() < 300 do
x = love.math.random() * game.spaceWidth - game.spaceWidth/2
y = love.math.random() * game.spaceWidth - game.spaceWidth/2
dist = Point(game.player.x-x, game.player.y-y)
end
local hp = {}
hp.p = Point(x, y)
hp.type = "drone"
table.insert(game.world, hp)
end
for i=1,game.scrapAmount do
local x = love.math.random() * game.spaceWidth - game.spaceWidth/2
local y = love.math.random() * game.spaceWidth - game.spaceWidth/2
local hp = {}
hp.p = Point(x, y)
hp.type = "scrap"
table.insert(game.world, hp)
end
end
function game:draw()
local w, h = love.window.getDimensions( )
game.camera:set()
love.graphics.setBackgroundColor(70, 107, 119)
local color = {255, 0, 0, 255}
love.graphics.setColor(color)
for k,v in pairs(game.world) do
local x,y = v.p:get()
if v.type == "air" then color = {100, 100, 255, 255}
elseif v.type == "power" then color = {100, 255, 100, 255}
elseif v.type == "drone" then color = {255, 100, 100, 255}
elseif v.type == "scrap" then color = {230, 230, 230, 255}
else color = {255, 255, 100, 255} end
love.graphics.setColor(color)
love.graphics.circle("line", x, y, 5, 8)
end
love.graphics.push()
love.graphics.translate(player.x, player.y)
love.graphics.rotate(player.rot+math.pi/2)
love.graphics.translate(-player.x, -player.y)
color = {230,230,230,255}
love.graphics.setColor(color)
vert = {game.player.x, game.player.y-8, game.player.x-6, game.player.y+6,game.player.x+6, game.player.y+6}
love.graphics.polygon("line", vert)
love.graphics.pop()
love.graphics.rectangle("line", -game.spaceWidth/2, -game.spaceWidth/2, game.spaceWidth, game.spaceWidth)
if game.radar.currentRadius ~= game.radar.maxRadius then
color[4] = (1 - game.radar.currentRadius/game.radar.maxRadius) * 255;
if game.radar.currentRadius < 40 then
color[4] = (game.radar.currentRadius/40)*color[4];
end
love.graphics.setColor(color)
love.graphics.circle("line", game.radar.x, game.radar.y, game.radar.currentRadius, 100)
end
game.camera:unset()
--HUD
color = {230,230,230,255}
hey = 12
local barWidth = 150
local barHeight = 10
local margin = 5
local airX = 20
local barY = 20
love.graphics.setColor(color)
love.graphics.rectangle("line", airX, barY, barWidth+margin*2, barHeight+margin*2)
local airWidth = (game.player.air/100) * barWidth
love.graphics.rectangle("fill", airX+margin, barY+margin, airWidth, barHeight)
local powerX = w - barWidth - 20
love.graphics.rectangle("line", powerX, barY, barWidth+margin*2, barHeight+margin*2)
local powerWidth = (game.player.power/100) * barWidth
love.graphics.rectangle("fill", powerX+margin, barY+margin, powerWidth, barHeight)
love.graphics.printf( game.player.gold.."/"..game.goldAmount, w/2-250, barY+barHeight+margin, 500, "center")
end
function movePixels(dt, allies, food, enemy)
local width, height = love.window.getDimensions( )
for k,v in pairs(allies) do
p = Point(0, 0)
p = calc(p, v.point, food, -5)
p = calc(p, v.point, enemy, 5)
p = calc(p, v.point, allies, -0.1)
outerRing = v.point
local len = outerRing:len()
if len ~= 0 then
p = p - (outerRing / ((len-game.spaceWidth)*(len-game.spaceWidth)))
end
local x,y = p:get()
if x ~= 0 or y ~= 0 then
p = 300*dt*p
v.point = v.point + p
if v.point:len() > game.spaceWidth then
allies[k] = nil
addPixel()
end
end
end
end
function calc(direction, startPoint, others, gravitation)
for k1,v1 in pairs(others) do
local diff = startPoint - v1.point
local len = diff:len()
if len ~= 0 then
direction = direction + gravitation*(diff / (len*len))
end
end
return direction
end
function drawPixels(pixels, color)
local p = Point(player.x, player.y)
local r = Point(radar.x, radar.y)
for k,v in pairs(pixels) do
local x,y = v.point:get()
local pLen = (Point(x, y) - p):len()
local rLen = (Point(x, y) - r):len()
local alpha = color[4]
if game.pLen < game.pSight then alpha = 255
elseif pLen < pSense then alpha = (1-((game.pLen - game.pSight)/(game.pSense-game.pSight))) * 255
else alpha = 0 end
local distToRadar = math.abs(rLen - game.radar.currentRadius)
if distToRadar < game.rSense and game.radar.currentRadius ~= game.radar.maxRadius then
if distToRadar < game.rSight then alpha = 255
else alpha = math.max(1 - (distToRadar-game.rSight)/(game.rSense-game.rSight) * 255, alpha)
end
end
color[4] = alpha
love.graphics.setColor(color)
love.graphics.circle("line",x, y, 5, 10)
--love.graphics.point(v.point:get())
end
end
function game:keypressed(k)
if k == "a" or k == "left" then
game.rotDirection = game.rotDirection - 1
elseif k == "d" or k == "right" then
game.rotDirection = game.rotDirection + 1
elseif k == "w" or k == "up" then
game.goDirection = game.goDirection + 1
elseif k == "s" or k == "down" then
game.goDirection = game.goDirection - 1
end
end
function game:keyreleased(k)
if k == "escape" then
Gamestate.switch(menu)
elseif k == "a" or k == "left" then
game.rotDirection = game.rotDirection + 1
elseif k == "d" or k == "right" then
game.rotDirection = game.rotDirection - 1
elseif k == "w" or k == "up" then
game.goDirection = game.goDirection - 1
elseif k == "s" or k == "down" then
game.goDirection = game.goDirection + 1
end
end
return game