-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
311 lines (286 loc) · 8.84 KB
/
main.lua
File metadata and controls
311 lines (286 loc) · 8.84 KB
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
local vec2 = require("lib/vec2")
local uniform = require("lib/uniform")
local ser = require("lib/ser")
local utils = require("utils")
local assets = require("scripts/assets")
local player = require("scripts/player")
Interface = require("scripts/interface")
ParticleManager = require("scripts/particleManager")
local camera = require("scripts/camera")
local weaponDrop = require("scripts/weaponDrop")
local weaponData = require("scripts/weaponData")
EnemyManager = require("scripts/enemyManager")
WaveManager = require("scripts/waveManager")
Logger = require("lib/logger")
local fullscreen = table.contains(arg, "--fullscreen")
CurrentShader = nil
ControlType = "keyboard"
Joystick = nil
JRightStick = {xAxis = 0 ; yAxis = 0}
JLeftStick = {xAxis = 0 ; yAxis = 0}
local starPositions = {}
local starCanvas = nil
-- Massive credits to Bigfoot71 for helping with infinite stars - You a real one fr
-- Forum link for the curious: https://love2d.org/forums/viewtopic.php?p=254398#p254398
local repeatShader = love.graphics.newShader[[
extern vec2 tex_size;
extern vec2 cam_pos;
vec4 effect(vec4 color, Image tex, vec2 tex_coords, vec2 screen_coords) {
vec2 wrapped_coords = (screen_coords + cam_pos) / tex_size;
return Texel(tex, fract(wrapped_coords)); // Get the fractional part (xy%1)
}
]]
local function genStars(nStars)
local canvas = love.graphics.newCanvas()
love.graphics.setCanvas(canvas)
for _ = 1, nStars do
local size = uniform(1.7, 3.45)
love.graphics.rectangle(
"fill",
uniform(0, SC_WIDTH),
uniform(0, SC_HEIGHT),
size, size
)
end
love.graphics.setCanvas()
return canvas
end
local function dropWeapon(weapon, position)
local newWeapon = weapon.new()
local drop = weaponDrop.new()
drop.weapon = newWeapon
drop.position = position
WeaponDrops[#WeaponDrops+1] = drop
end
function love.keypressed(key, unicode)
ControlType = "keyboard"
-- Pause key
if key == "escape" and GameState == "game" and not CurrentShader and not Player.dead then
GamePaused = not GamePaused end
-- Fullscreen key
if key == "f11" then
fullscreen = not fullscreen
love.window.setFullscreen(fullscreen, "desktop")
-- Set window dimensions to default
if not fullscreen and false then
love.window.setMode(960, 540, {resizable=true}) end
end
-- Toggle debug menu
if key == "f1" and GameState == "game" then
Interface.debug.enabled = not Interface.debug.enabled
end
end
function love.joystickadded(joystick)
Joystick = joystick
JRightStick = {xAxis = 0 ; yAxis = 0}
JLeftStick = {xAxis = 0 ; yAxis = 0}
end
function GameLoad()
GameState = "game"
-- Globals
MotionSpeed = 1
Time = 0
EnemyBullets = {}
StatNames = {"Time", "Waves", "Accuracy", "Kills", "Dash Kills"}
Stats = {0, 0, 0, 0, 0}
Score = 0
-- Setup player
Player = player.new()
Player.load()
-- Weapon drops
WeaponDrops = {}
--dropWeapon(weaponData.pistol, vec2.new(100, 100))
--dropWeapon(weaponData.assaultRifle, vec2.new(150, 100))
--dropWeapon(weaponData.shotgun, vec2.new(100, 150))
-- Enemies
EnemyManager.load()
WaveManager.load()
-- Setup interface
-- Setup camera
Camera = camera.new()
Camera.lockedTarget = Player
ParticleManager.particles = {}
end
local function updateJoystickData()
if ControlType == "keyboard" then return end
-- Right Stick
local xAxis = Joystick:getAxis(3)
local yAxis = Joystick:getAxis(4)
-- Deadzone
if math.abs(xAxis) < 0.1 then xAxis = 0 end
if math.abs(yAxis) < 0.1 then yAxis = 0 end
JRightStick.xAxis = xAxis
JRightStick.yAxis = yAxis
-- Left Stick
xAxis = Joystick:getAxis(1)
yAxis = Joystick:getAxis(2)
-- Deadzone
if math.abs(xAxis) < 0.1 then xAxis = 0 end
if math.abs(yAxis) < 0.1 then yAxis = 0 end
JLeftStick.xAxis = xAxis
JLeftStick.yAxis = yAxis
end
function SaveGame()
love.filesystem.write("save", ser(Save))
end
local function updateWeaponDrops(delta)
for i, v in ipairs(WeaponDrops) do
v.update(delta, i)
end
end
local function drawWeaponDrops()
for _, v in ipairs(WeaponDrops) do
v.draw()
end
end
local function updateEBullets(delta)
for i, v in ipairs(EnemyBullets) do
v.update(delta, i)
end
end
local function drawEBullets(delta)
for _, v in ipairs(EnemyBullets) do
v.draw()
end
end
local function createNewSave()
-- Create new save file
Save = {
version = Version;
settings = {};
highScores = {};
playerColorSlot = 1;
playerAccSlot = 1;
highScore = {0, 0, 0};
}
for i = 1, #SettingNames do
Save.settings[i] = true
end
-- Write to save
love.filesystem.write("save", ser(Save))
end
local function loadSave()
-- SAVE FILE MANAGEMENT
SettingNames = {"Sounds", "Auto Reload", "Aim Line", "Screen Shake", "Bullet Particles"}
Save = nil
if love.filesystem.getInfo("save") then
Logger:verboseLog("Existing save detected, reading file...")
-- Read from save
local data = love.filesystem.load("save")()
Save = data
-- Check if the save is up to date
if Save.version ~= Version or not Save.version then
Logger:verboseLog("Recent save file is outdated, creating a new save file...")
createNewSave()
end
else
Logger:verboseLog("Creating a new save file...")
createNewSave()
end
Logger:verboseLog("Loading save sequence complete.")
end
local function setStats(delta)
-- Waves
Stats[utils.indexOf(StatNames, "Waves")] = WaveManager.wave - 1
-- Accuracy
local num = math.floor((Player.hitBullets / (Player.hitBullets + Player.missedBullets))*100)
if tostring(num) == "nan" then
num = 0
end
Stats[utils.indexOf(StatNames, "Accuracy")] = "%" .. num
-- Time
local min = math.floor(Time / 60)
local sec = math.floor(Time - min*60)
local index = utils.indexOf(StatNames, "Time")
Stats[index] = min .. "m" .. sec .. "s"
end
function love.load()
love.graphics.setDefaultFilter("nearest", "nearest")
starCanvas = genStars(60)
-- Fullscreen
if table.contains(arg, "--fullscreen") then
love.window.setFullscreen(fullscreen, "desktop") end
PlayerColors = {
{0.13, 0.34, 0.8}, {1, 0, 0.5}, {1, 0.5, 0}, {1, 0.8, 0}, {0, 0.4, 0.4}
}
assets.load()
assets.gameLoad()
GameLoad()
GameState = "intro"
MotionSpeed = 0.25
-- Skip intro sequence
if table.contains(arg, "--skip-intro") then GameState = "menu" ; MotionSpeed = 1 end
loadSave()
Interface:load()
GamePaused = false
-- Create simulation enemies
Difficulty = 3
for i = 1, 10 do
EnemyManager.newEnemy(vec2.new(uniform(0, SC_WIDTH), uniform(0, SC_HEIGHT)))
end
end
function love.update(delta)
setStats(delta)
SC_WIDTH, SC_HEIGHT = love.graphics.getDimensions()
updateJoystickData()
-- Set cursor
if GameState ~= "game" or GamePaused then
love.mouse.setCursor(assets.cursorDefault)
if GameState ~= "game" then
Camera.updateMenu(delta)
EnemyManager.spawnSimEnemies(delta)
end
else
if Player.dead then love.mouse.setCursor(assets.cursorDefault)
else
if CurrentShader then
love.mouse.setCursor(assets.cursorCombatI) else
love.mouse.setCursor(assets.cursorCombat) end
end
end
Interface:update(delta)
EnemyManager.update(delta)
ParticleManager.update(delta)
updateEBullets(delta)
if GameState == "game" then
Player.update(delta)
Camera.update(delta)
updateWeaponDrops(delta)
WaveManager.update(delta)
if Score > Save.highScore[Difficulty] then
Save.highScore[Difficulty] = Score
end
end
-- Hide debug menu if no in-game
if GameState ~= "game" then
Interface.debug.enabled = false
end
end
function love.draw()
if CurrentShader then
love.graphics.setBackgroundColor(0.93, 0.93, 0.93, 1)
else
love.graphics.setBackgroundColor(0.05, 0.05, 0.05, 1)
end
love.graphics.setShader(CurrentShader)
-- Draw the stars
repeatShader:send("cam_pos", {Camera.position.x, Camera.position.y})
repeatShader:send("tex_size", {960, 540})
local sx = SC_WIDTH/960
local sy = SC_HEIGHT/540
love.graphics.setShader(repeatShader)
love.graphics.draw(starCanvas,0,0,0,sx,sy)
love.graphics.setShader(CurrentShader)
ParticleManager.draw()
EnemyManager.draw()
drawEBullets()
--drawStars()
if GameState == "game" then
drawWeaponDrops()
Player.draw()
end
love.graphics.push()
love.graphics.scale(SC_WIDTH/960, SC_HEIGHT/540)
Interface:draw()
love.graphics.pop()
end