-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.lua
226 lines (184 loc) · 4.94 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
require('utils/class')
require('utils/math')
require('utils/string')
require('utils/table')
require('utils/color')
require('utils/json')
require('utils/msgpack')
require('utils/stack')
require('utils/timer')
require('utils/vector2')
require('utils/shaders/bloom')
require('utils/shaders/motionblur')
require('utils/shaders/phosphor')
require('utils/shaders/starfield')
require('gui/gui')
require('assetloader')
require('network')
require('settings')
require('soundmanager')
require('world')
phosphor_shader = true
bloom_shader = true
GameState = { Menu = 1, Game = 2 }
local game_state = GameState.Menu
function getGameState()
return game_state
end
function setGameState(new_game_state)
game_state = new_game_state
if game_state == GameState.Menu then
World.reset(true)
else
World.reset()
end
end
function love.load()
love.graphics.setFont(Assets.fonts.Hyperspace_Bold.default)
love.mouse.setVisible(false)
love.keyboard.setKeyRepeat(true)
Settings.load()
love.window.setFullscreen(Settings.get('fullscreen', false))
phosphor_shader = Settings.get('phosphor_shader', true)
bloom_shader = Settings.get('bloom_shader', true)
SoundManager.setChannelVolume('master', Settings.get('master_volume', 1))
SoundManager.setChannelVolume('menu', Settings.get('menu_volume', 1))
SoundManager.setChannelVolume('sfx', Settings.get('sfx_volume', 1))
GUI.pushMenu(MainMenu())
love.resize()
end
function love.resize()
Bloom.reset()
MotionBlur.reset()
Phosphor.reset()
StarField.reset()
end
function love.mousemoved(x, y, dx, dy)
GUI.mousemoved(x, y, dx, dy)
end
function love.mousepressed(x, y, button)
if getGameState() == GameState.Menu then
GUI.mousepressed(x, y, button)
elseif getGameState() == GameState.Game then
if Network.getState() ~= NetworkState.Client then
if button == 1 then
World.addEntity(
PhotonBeam(
Vector2(x, y),
Color.fromHSV(math.random(0, 360), 1, 1),
Vector2((math.random() * 2) - 1, (math.random() * 2) - 1) * 500
)
)
elseif button == 3 then
World.addEntity(Sun(Vector2(x, y)))
elseif button == 2 then
local peerID = Network.getID()
for ship in World.getEntities(Ship) do
if ship:isLocalPlayer() then
peerID = -1
end
end
World.addEntity(Ship(peerID, Vector2(x, y), Color.fromHSV(math.random(0, 360), 1, 1)))
end
end
end
end
function love.mousereleased(x, y, button)
if getGameState() == GameState.Menu then
GUI.mousereleased(x, y, button)
end
end
function love.textinput(text)
if getGameState() == GameState.Menu then
GUI.textinput(text)
end
end
function love.keypressed(key, scancode, isRepeat)
if getGameState() == GameState.Menu then
GUI.keypressed(key, scancode, isRepeat)
end
if not isRepeat then
if scancode == 'f11' then
love.window.setFullscreen(not love.window.getFullscreen())
else
if getGameState() == GameState.Game then
if scancode == 'r' or scancode == 'escape' then
setGameState(GameState.Menu)
elseif table.find({'w', 'a', 's', 'd', 'space'}, scancode) and Network.getState() == NetworkState.Client then
Network.send("InputChanged", {
key, 1
}, nil, 1)
end
end
end
end
end
function love.keyreleased(key, scancode)
if getGameState() == GameState.Game then
if table.find({'w', 'a', 's', 'd', 'space'}, scancode) and Network.getState() == NetworkState.Client then
Network.send("InputChanged", {
key, 0
}, nil, 1)
end
end
end
function love.gamepadpressed(joystick, button)
if getGameState() == GameState.Menu then
GUI.gamepadpressed(joystick, button)
elseif getGameState() == GameState.Game then
if button == 'back' or button == 'b' or button == 'start' then
setGameState(GameState.Menu)
end
end
end
function love.gamepadreleased(joystick, button)
if getGameState() == GameState.Menu then
GUI.gamepadreleased(joystick, button)
end
end
function love.gamepadaxis(joystick, axis, value)
if getGameState() == GameState.Menu then
GUI.gamepadaxis(joystick, axis, value)
end
end
function love.update(delta)
SoundManager.update()
if getGameState() == GameState.Menu then
GUI.update(delta)
elseif getGameState() == GameState.Game then
World.update(delta)
end
collectgarbage('step', 512)
end
function love.draw()
if phosphor_shader then
Phosphor.preDraw()
end
if bloom_shader then
Bloom.preDraw()
end
StarField.draw()
if getGameState() == GameState.Menu then
GUI.draw()
elseif getGameState() == GameState.Game then
MotionBlur.update()
World.draw()
MotionBlur.draw()
end
if bloom_shader then
Bloom.postDraw()
end
if phosphor_shader then
Phosphor.postDraw()
end
local stats = love.graphics.getStats()
local fps = love.timer.getFPS()
local ram = collectgarbage('count') / 1024
local vram = stats.texturememory / 1024 / 1024
local drawcalls = stats.drawcalls
local string = ("FPS: %i, RAM: %.2fMB, VRAM: %.2fMB, Drawcalls: %i"):format(fps, ram, vram, drawcalls)
love.graphics.print(string, 10, 10)
end
function love.quit()
Network.close()
end