-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
580 lines (515 loc) · 19.7 KB
/
main.lua
File metadata and controls
580 lines (515 loc) · 19.7 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
function love.load()
--Libraries and other files that are required
push = require 'push'
Class = require 'class'
bitser = require 'bitser'
local moonshine = require 'moonshine'
require 'Card'
require 'Button'
require 'Slider'
require 'Text'
require 'Characters/Character stats'
require 'StateMachine'
require 'states/BaseState'
require 'states/HomeState'
require 'states/GameState'
require 'states/SettingsState'
require 'states/CampaignState'
require 'states/DeckeditState'
require 'states/SandboxState'
require 'states/ExitState'
require 'levels'
require 'other functions'
require 'WeaponManager'
require 'Weapon'
require 'ProjectileManager'
require 'Projectile'
require 'Card editor'
require 'Remove card'
require 'Card viewer'
--Operating System
OS = love.system.getOS()
VIRTUALWIDTH = 1920
VIRTUALHEIGHT = 1080
-- initialise virtual resolution
push.setupScreen(VIRTUALWIDTH, VIRTUALHEIGHT, {
upscale = 'normal',
})
love.window.maximize()
-- load fonts
font50 = love.graphics.newFont(50)
font60 = love.graphics.newFont(60)
font70 = love.graphics.newFont(70)
font80 = love.graphics.newFont(80)
font100 = love.graphics.newFont(100)
font40SW = love.graphics.newFont('Fonts/Distant Galaxy.ttf',40)
font50SW = love.graphics.newFont('Fonts/Distant Galaxy.ttf',50)
font60SW = love.graphics.newFont('Fonts/Distant Galaxy.ttf',60)
font90SW = love.graphics.newFont('Fonts/Distant Galaxy.ttf',90)
font80SWrunes = love.graphics.newFont('Fonts/Aurebesh Bold.ttf',80)
love.graphics.setFont(font80)
blur = moonshine(moonshine.effects.fastgaussianblur).chain(moonshine.effects.vignette)
blur.fastgaussianblur.sigma = 5
blur.vignette.radius = 1
imageDecoderThreads = {}
if OS == "Android" then --For some reason Android and ChromeOS devices
imageDecoderThreads[1] = love.thread.newThread("ImageDecoderThread.lua")
else
for i = 1, math.max(love.system.getProcessorCount()-1,1) do --Creates as many threads as the system has minus 1, but at least 1.
imageDecoderThreads[i] = love.thread.newThread("ImageDecoderThread.lua")
end
end
love.thread.getChannel("imageDecoderQueue"):push("Graphics/Evolution")
love.thread.getChannel("imageDecoderQueue"):push("Graphics/Evolution Max")
love.thread.getChannel("imageDecoderOutput")
imageDecoderThreads[1]:start()
if imageDecoderThreads[2] then
imageDecoderThreads[2]:start() --No point starting more than 2 as there's only 2 images being decoded
end
gui = {}
songs = {}
background = {}
paused = false
UserData = {}
love.keyboard.keysDown = {}
love.mouse.buttonsPressed = {}
love.mouse.buttonsReleased = {}
mouseDown = false
touchDown = false
mouseTouching = false
mouseTrapped = false
mouseTrapped2 = false
mouseLastX = 0
mouseLastY = 0
mousePressedX = 0
mousePressedY = 0
mouseX = 0
mouseY = 0
mouseButtonX = 0
mouseButtonY = 0
lastClickIsTouch = false
focus = true
keyHoldTimer = 0
keyPressedTimer = love.timer.getTime()
mouseLocked = false
sandbox = true
yscroll = 0
rawyscroll = 0
if love.filesystem.getInfo('Settings.txt') == nil then
Settings = {
['pause_on_loose_focus'] = true,
['volume_level'] = 0.5,
['FPS_counter'] = false,
['videos'] = true,
['active_deck'] = 'Player 1 deck.txt',
['P1_selection'] = 1,
['P2_selection'] = 4,
['background_selection'] = 1,
['music_selection'] = 1
}
else
Settings = bitser.loadLoveFile('Settings.txt')
end
love.audio.setVolume(Settings['volume_level'])
if love.filesystem.getInfo('Player 1 cards.txt') == nil and love.filesystem.getInfo('User Data.txt') == nil then
tutorial()
else
if love.filesystem.getInfo('User Data.txt') == nil then
UserData['Credits'] = 0
if Settings['videos'] == nil then Settings['videos'] = true end
bitser.dumpLoveFile('User Data.txt',UserData)
--If any save data is from pre 0.11 (doesn't contain userdata, character levels or evolutions), delete it to avoid crashing
if love.filesystem.getInfo('Player 1 deck.txt') ~= nil and bitser.loadLoveFile('Player 1 deck.txt') ~= nil then
P1deckCards = bitser.loadLoveFile('Player 1 deck.txt')
for k, pair in pairs(P1deckCards) do
if P1deckCards[k] ~= nil and not Characters[P1deckCards[k][1]] then
P1deckCards[k] = nil
end
end
bitser.dumpLoveFile('Player 1 deck.txt',P1deckCards)
end
if love.filesystem.getInfo('Player 1 cards.txt') ~= nil and bitser.loadLoveFile('Player 1 cards.txt') ~= nil then
P1cards = bitser.loadLoveFile('Player 1 cards.txt')
for k, pair in pairs(P1cards) do
if P1cards[k] ~= nil and not Characters[P1cards[k][1]] then
P1cards[k] = nil
end
end
bitser.dumpLoveFile('Player 1 cards.txt',P1cards)
P1cards = nil
end
end
if love.filesystem.getInfo('Player 1 deck.txt') == nil then
P1deckCards = {}
else
P1deckCards = bitser.loadLoveFile('Player 1 deck.txt') or {} --In case save file has corrupted, or is a pre-bitser file
end
if love.filesystem.getInfo('Player 1 cards.txt') == nil or bitser.loadLoveFile('Player 1 cards.txt') == nil then
bitser.dumpLoveFile('Player 1 cards.txt',{})
end
end
if Settings['active_deck'] == nil then Settings['active_deck'] = 'Player 1 deck.txt' end
if Settings['P1_selection'] == nil then Settings['P1_selection'] = 1 end
if Settings['P2_selection'] == nil then Settings['P2_selection'] = 4 end
if Settings['background_selection'] == nil then Settings['background_selection'] = 1 end
if Settings['music_selection'] == nil then Settings['music_selection'] = 1 end
local function loadDeck(deck)
if love.filesystem.getInfo(deck) == nil or bitser.loadLoveFile(deck) == nil then
bitser.dumpLoveFile(deck,{})
if Settings['active_deck'] == deck then
P1deckCards = {}
end
elseif Settings['active_deck'] == deck then
P1deckCards = bitser.loadLoveFile(deck)
end
end
loadDeck('Player 1 deck 2.txt')
loadDeck('Player 1 deck 3.txt')
-- initialize state machine with all state-returning functions
gStateMachine = StateMachine {
['HomeState'] = function() return HomeState() end,
['GameState'] = function() return GameState() end,
['SettingsState'] = function() return SettingsState() end,
['CampaignState'] = function() return CampaignState() end,
['DeckeditState'] = function() return DeckeditState() end,
['SandboxState'] = function() return SandboxState() end,
['ExitState'] = function() return ExitState() end,
}
gStateMachine:change('HomeState')
end
function love.resize(w, h)
push.resize(w, h)
end
function love.focus(InFocus)
focus = InFocus
if Settings['pause_on_loose_focus'] and not (paused and gStateMachine.state == 'GameState' and not winner) then pause(not focus) end --Pause/play game if pause_on_loose_focus setting is on
end
function love.lowmemory()
if toggleSetting('videos',false) ~= false then
if GameState == "SettingsState" then
gui[3]:toggle()
end
updateBackground()
end
end
function love.joystickadded()
joysticks = love.joystick.getJoysticks()
end
function love.joystickremoved()
joysticks = love.joystick.getJoysticks()
end
function love.keypressed(key,scancode,isrepeat)
--Stop mute/fullscreen being repeatedly toggled if you hold keys
if not isrepeat then
love.keyboard.keysDown[key] = true
lastPressed = key
keyHoldTimer = 0
if key == 'return' or key == 'kpenter' then
love.mousepressed(love.mouse.getPosition(),1,false)
--F11 toggles between fullscreen and maximised
elseif key == 'f11' then
love.window.setFullscreen(not love.window.getFullscreen())
--M mutes/unmutes
elseif key == 'm' then
if love.audio.getVolume() == 0 then
love.audio.setVolume(0.5)
else
love.audio.setVolume(0)
end
Settings['volume_level'] = love.audio.getVolume()
bitser.dumpLoveFile('Settings.txt', Settings)
end
end
if gStateMachine:keypressed(key,isrepeat) == nil then --Allow state to override up/down behaviour if desired
if key == 'up' or key == 'down' then
if mouseTouching == false then
repositionMouse(1)
else
for k, v in ipairs(gui) do
if v == mouseTouching then
if key == 'up' then
if gui[k-1] and (gui[k-1].visible or gui[k-1].visible == nil) then
repositionMouse(k-1)
end
end
if key == 'down' then
if gui[k+1] and (gui[k+1].visible or gui[k+1].visible == nil) then
repositionMouse(k+1)
end
end
break
end
end
end
end
end
-- for k, pair in pairs(gui) do
-- if pair.keypressed then
-- pair:keypressed(key,isrepeat)
-- end
-- end
end
function love.keyreleased(key)
love.keyboard.keysDown[key] = false
if key == 'return' or key == 'kpenter' then
love.mouse.buttonsReleased[1] = true
if not (love.mouse.isDown(1) or love.keyboard.wasDown('return') or love.keyboard.wasDown('kpenter')) then mouseDown = false end
elseif key == 'escape' then
gStateMachine:back()
end
-- gStateMachine:keyreleased(key)
for k, pair in pairs(gui) do
if pair.keyreleased then
pair:keyreleased(key)
end
end
end
function love.keyboard.wasDown(key)
return love.keyboard.keysDown[key]
end
function love.touchpressed()
touchDown = true
end
function love.touchreleased()
touchDown = false
end
function love.mousepressed(x,y,button,istouch)
love.mouse.buttonsPressed[button] = true
mouseDown = true
mousePressedX, mousePressedY = push.toGame(love.mouse.getPosition())
mousePressedTime = love.timer.getTime()
end
function love.mousereleased(x,y,button,istouch)
love.mouse.buttonsReleased[button] = true
if button == 4 then love.keyreleased('escape') end
lastClickIsTouch = istouch
if not (love.keyboard.wasDown('return') or love.keyboard.wasDown('kpenter')) then mouseDown = false end
end
function love.gamepadpressed(joystick,button)
local key = controllerBinds(button)
love.keypressed(key)
lastClickIsTouch = false
end
function love.gamepadreleased(joystick,button)
local key = controllerBinds(button)
love.keyreleased(key)
end
function love.mousemoved(x,y,dx,dy,istouch)
mouseX,mouseY = push.toGame(x,y)
if mouseX == false or mouseY == false then
mouseX = -1
mouseY = -1
end
if math.abs(x - mouseButtonX) < 1 and math.abs(y - mouseButtonY) < 1 then
return
end
love.mouse.setVisible(true)
lastClickIsTouch = istouch
end
function love.touchmoved(id,x,y,dx,dy)
if (yscroll > -maxScroll or dy > 0) and (yscroll < 0 or dy < 0) and (math.abs(dy) > 1.5 or touchLocked) then
touchLocked = true
yscroll = yscroll + dy * 1.5
rawyscroll = dy * 150/12
lastScrollIsTouch = true
end
end
function love.wheelmoved(x,y)
if (yscroll > -maxScroll or y > 0) and (yscroll < 0 or y < 0) then
rawyscroll = rawyscroll + y * 50
lastScrollIsTouch = false
end
end
function love.update(dt)
--Handle joystick inputs
if joysticks and focus then
local leftx = 0
local lefty = 0
local rightx = 0
for k, v in pairs(joysticks) do
leftx = leftx + v:getGamepadAxis('leftx')
lefty = lefty + v:getGamepadAxis('lefty')
rightx = rightx + v:getGamepadAxis('rightx')
end
if math.abs(leftx) > 0.2 or math.abs(lefty) > 0.2 then --Deadzone because otherwise joysticks are so sensitive they trap mouse inside game unless you alt-tab
if math.abs(leftx) > math.abs(lefty) then
if leftx < 0 then
direction = 'left'
else
direction = 'right'
end
else
if lefty < 0 then
direction = 'up'
else
direction = 'down'
end
end
if direction ~= lastPressed then
if love.keyboard.wasDown('up') and direction ~= 'up' then
love.keyreleased('up')
end
if love.keyboard.wasDown('down') and direction ~= 'down' then
love.keyreleased('down')
end
if love.keyboard.wasDown('left') and direction ~= 'left' then
love.keyreleased('left')
end
if love.keyboard.wasDown('right') and direction ~= 'right' then
love.keyreleased('right')
end
if love.timer.getTime() > keyPressedTimer + 0.1 then
love.keypressed(direction)
keyPressedTimer = love.timer.getTime()
end
end
elseif direction then
love.keyreleased(direction)
direction = nil
lastPressed = nil
keyHoldTimer = 0
end
if math.abs(rightx) > 0.2 then
if rightx < 0 then
direction2 = 'dpleft'
else
direction2 = 'dpright'
end
if direction2 ~= lastPressed then
if love.keyboard.wasDown('dpleft') and direction ~= 'dpleft' then
love.keyreleased('left')
end
if love.keyboard.wasDown('dpright') and direction ~= 'dpright' then
love.keyreleased('dpright')
end
if love.timer.getTime() > keyPressedTimer + 0.1 then
love.keypressed(direction2)
keyPressedTimer = love.timer.getTime()
end
end
elseif direction2 then
love.keyreleased(direction2)
direction2 = nil
lastPressed = nil
keyHoldTimer = 0
end
end
--Handle mouse inputs
if mouseDown then
mouseLastX,mouseLastY = push.toGame(love.mouse.getPosition())
if mouseLastX == false or mouseLastY == false then
mouseLastX = -1
mouseLastY = -1
mouseDown = false
end
end
--Handle holding down keys
if lastPressed then
if love.keyboard.wasDown(lastPressed) then
keyPressedTimer = love.timer.getTime()
keyHoldTimer = keyHoldTimer + dt
if keyHoldTimer > 0.5 then
love.keypressed(lastPressed,nil,true)
keyHoldTimer = keyHoldTimer - 0.08
end
else
keyHoldTimer = 0
end
end
--Smooth scrolling
if (yscroll > -maxScroll or rawyscroll > 0) and (yscroll < 0 or rawyscroll < 0) and not touchDown then
yscroll = yscroll + rawyscroll * dt * 12
if lastScrollIsTouch then
rawyscroll = rawyscroll - rawyscroll * math.min(dt*3,0.1)
else
rawyscroll = rawyscroll - rawyscroll * math.min(dt*10,1)
end
else
if yscroll > 0 then yscroll = 0 rawyscroll = 0 end
if yscroll < -maxScroll then yscroll = -maxScroll rawyscroll = 0 end
end
--Manage song queue and background video looping
if not paused then
if songs[1] and not songs[currentSong]:isPlaying() then --Check if a song is currently playing
if songs[currentSong]:tell() == 0 then --Check that the current song isn't playing because it's finished rather than because the system is lagging a lot
if songs[currentSong+1] then
currentSong = currentSong+1
else
currentSong = 1
end
end
songs[currentSong]:play() --We want this to happen regardless of whether we switch to the next song, as at this point the game is not paused but no song is playing (presumably stopped due to lag)
end
if background['Video'] and not background['Background']:isPlaying() then
background['Background']:seek(background['Seek'])
background['Background']:play()
end
end
if lastClickIsTouch and mouseDown == false and mouseTrapped == false then
mouseX = -1
mouseY = -1
touchLocked = false
end
mouseTouching = false
--Update GUI elements
for k, pair in pairs(gui) do
pair:update(dt)
end
for k, pair in pairs(gui) do --for functions which rely on mouseTouching being calculated first
if pair.update2 then
pair:update2(dt)
end
end
--Update state machine
gStateMachine:update(dt)
--Reset tables of clicked keys so last frame's inputs aren't used next frame
love.mouse.buttonsPressed = {}
love.mouse.buttonsReleased = {}
if mouseDown == false and mouseLocked == false then mouseTrapped = false mouseLastX = -1 mouseLastY = -1 end
end
function love.draw()
push.start()
--In case a state wants to draw the background itself, eg for effects or draw batching using a canvas
if gStateMachine:renderBackground() == nil then
love.graphics.draw(background['Background'])
end
gStateMachine:renderNormal()
for k, pair in pairs(gui) do
if mouseTouching ~= pair and mouseTrapped ~= pair then
pair:render()
end
end
gStateMachine:renderForeground()
if mouseTrapped then
if mouseTouching and mouseTouching ~= mouseTrapped then
mouseTouching:render()
end
mouseTrapped:render()
elseif mouseTouching then
mouseTouching:render()
end
--For GUI elements that need rendering in front of mouseTouching (eg evolution icons)
for k, pair in pairs(gui) do
if pair.renderInFront then
pair:renderInFront()
end
end
if Settings['FPS_counter'] == true then
love.graphics.print({{0,255,0,255}, 'FPS: ' .. tostring(love.timer.getFPS())}, font50, 1697, 1027)
end
-- for k, v in pairs(joysticks) do
-- love.graphics.print(tostring(v),0,300+k*100)
-- love.graphics.print(v:getName(),1880-font80:getWidth(v:getName())-font80:getWidth(tostring(v:isConnected()))-font80:getWidth(tostring(v:isGamepad())),k*100-100)
-- love.graphics.print(tostring(v:isConnected()),1900-font80:getWidth(tostring(v:isConnected()))-font80:getWidth(tostring(v:isGamepad())),k*100-100)
-- love.graphics.print(tostring(v:isGamepad()),1920-font80:getWidth(tostring(v:isGamepad())),k*100-100)
-- end
-- stats = love.graphics.getStats()
-- stats = {love.graphics.getRendererInfo()}
-- y = 0
-- for k, pair in pairs(stats) do
-- love.graphics.print(k .. ' ' .. pair,0,y)
-- y = y + 100
-- end
-- love.graphics.print(tostring(mouseTrapped) .. ' ' .. tostring(mouseTrapped2))
push.finish()
end