-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
213 lines (178 loc) · 6.01 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
require('conf')
require('textures')
require('nodes')
require('player')
require('ghosts')
require('candles')
require('coins')
require('generator')
require("lib.console.console")
require('text')
require('menu')
require('triggers')
require('config')
require('client')
require('server')
require('log')
------------------------
-- Load love
------------------------
DEFAULT_SCALE = 2
function love.load()
math.randomseed(os.time())
log:load()
love.graphics.setDefaultFilter("nearest")
config:load()
textures.load()
nodes:load()
menu:main_menu()
end
------------------------
-- Update
------------------------
function love.update(dt)
ghosts:update(dt)
player:update(dt)
draw:updateFade(dt)
generator:generate();
triggers:update(dt)
server:update(dt)
client:update(dt)
end
------------------------
-- Draw
------------------------
draw = {}
draw.deathMessage = {}
draw.deathMessage.x = 0.5 + math.random()
draw.deathMessage.y = 0.5 + math.random()
draw.restartMessage = {}
draw.restartMessage.x = 0.5 + math.random()
draw.restartMessage.y = 0.5 + math.random()
draw.scale = DEFAULT_SCALE
draw.fade = 1
draw.fadeSpeed = 0
--(1 - math.sin(math.pi * (player.blind or 0) / BLINDNESS_DELAY))
function draw:fadeBegin()
-- debug: turn of the fade to stop eyes bleeding
--draw.fadeSpeed = -speed
end
function draw:fadeEnd(speed)
draw.fadeSpeed = speed
end
function draw:updateFade(dt)
draw.fade = draw.fade + draw.fadeSpeed * dt
if (draw.fade > 1) then
draw.fade = 1
end
if (draw.fade < 0) then
draw.fade = 0
end
end
-- Put origin to the center of the window
local function updateOrigin()
love.graphics.origin()
love.graphics.translate(
love.graphics.getWidth() / 2 - (player.x + 0.5) * SIZE * draw.scale,
love.graphics.getHeight() / 2 - (player.y + 0.5) * SIZE * draw.scale
)
end
function love.draw()
if (not player.dead) then
updateOrigin();
-- need this to render textures properly
love.graphics.setColor(255, 255, 255, 255 * draw.fade)
nodes:render()
text:render()
-- Draw candles
candles:forEach(function(x, y)
if (math.abs(x - player.x) * SIZE * draw.scale < love.graphics.getWidth()
and math.abs(y - player.y) * SIZE * draw.scale < love.graphics.getHeight()) then
love.graphics.draw(textures.candle.image, textures.candle.quad, x * SIZE * draw.scale, y * SIZE * draw.scale, 0, draw.scale, draw.scale)
end
end)
-- Draw coins
coins:forEach(function(x, y)
if (math.abs(x - player.x) * SIZE * draw.scale < love.graphics.getWidth()
and math.abs(y - player.y) * SIZE * draw.scale < love.graphics.getHeight()) then
love.graphics.draw(textures.coin.image, textures.coin.quad, x * SIZE * draw.scale, y * SIZE * draw.scale, 0, draw.scale, draw.scale)
end
end)
-- Draw ghosts
for _, ghost in ipairs(ghosts) do
if (math.abs(ghost.x - player.x) * SIZE * draw.scale < love.graphics.getWidth()
and math.abs(ghost.y - player.y) * SIZE * draw.scale < love.graphics.getHeight()) then
love.graphics.draw(ghost.texture.image, ghost.texture.quad, ghost.x * SIZE * draw.scale, ghost.y * SIZE * draw.scale, 0, draw.scale, draw.scale)
end
end
-- Draw remote players
players:render()
-- Draw player
love.graphics.draw(textures.player.image, textures.player.quad, player.x * SIZE * draw.scale, player.y * SIZE * draw.scale, 0, draw.scale, draw.scale)
else
love.graphics.setBackgroundColor(
50 * draw.deathMessage.x,
50 * draw.deathMessage.y,
50 * draw.restartMessage.x)
love.graphics.setColor(255, 0, 0, 255)
love.graphics.print(
"You died.",
love.graphics.getWidth() / 2 * draw.deathMessage.x,
love.graphics.getHeight() / 2 * draw.deathMessage.y,
0, 2, 2)
love.graphics.setColor(255, 50, 100, 255)
love.graphics.print(
"Press C to restart at a checkpoint.",
love.graphics.getWidth() / 2 * draw.restartMessage.x,
love.graphics.getHeight() / 2 * draw.restartMessage.y,
0, 2, 2)
love.graphics.print(
"Press R to restart at a random location.",
love.graphics.getWidth() / 2 * draw.restartMessage.x,
love.graphics.getHeight() / 2 * draw.restartMessage.y + 25,
0, 2, 2)
end
love.graphics.origin()
love.graphics.setColor(255, 90, 100, 255)
love.graphics.print("Score: " .. player.coinsCollected, 0, 2, 0, 2, 2)
end
------------------------
-- Keypressed
------------------------
function love.keypressed(key)
if key == "escape" or key == "q" then
love.event.quit()
elseif key == "f" then
love.window.setFullscreen(not love.window.getFullscreen())
end
if key == "-" then
draw.scale = draw.scale / 2
elseif key == "=" or key == "+" then
draw.scale = draw.scale * 2
elseif key == "0" then
draw.scale = DEFAULT_SCALE
end
if (key == "`") then console.Show() end
player.handleKey(key)
if (player.dead) then
draw.deathMessage.x = 0.5 + math.random()
draw.deathMessage.y = 0.5 + math.random()
draw.restartMessage.x = 0.5 + math.random()
draw.restartMessage.y = 0.5 + math.random()
if (key == "C" or key == "c") then
if (player.checkpoint) then
player.x = player.checkpoint.x
player.y = player.checkpoint.y
player:ressurect()
love.graphics.setBackgroundColor(0,0,0)
end
elseif (key == "R" or key == "r") then
local pos = candles:randomPosition()
assert(pos)
player.x = pos.x
player.y = pos.y
player:ressurect()
love.graphics.setBackgroundColor(0,0,0)
end
end
end