-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
2531 lines (2232 loc) · 80.7 KB
/
main.lua
File metadata and controls
2531 lines (2232 loc) · 80.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Cybersafe Rogue - Roguelike MVP
-- Controles:
-- - Movimento: W/A/S/D
-- - Atirar: Mouse esquerdo (segurar) ou Espaço
-- - Reiniciar (quando game over): R
local Player = require('player')
local Enemy = require('enemy')
local Bullet = require('bullet')
local Questions = require('questions')
local Theme = require('theme')
local state = 'title' -- 'title' | 'play' | 'question' | 'upgrade' | 'gameover'
local player
local enemies = {}
local bullets = {}
local wave = 0
local completedWaves = 0
local waveActive = false
local toSpawn = 0
local spawnTimer = 0
local spawnInterval = 0.6
local difficulty = 1.0
local question
local lastAnswerCorrect = false
local baseEnemies = 5
local upgradeChoices = {
{ key = 'atk_speed', label = 'Velocidade de ataque' },
{ key = 'laser', label = 'Laser perfurante' },
{ key = 'damage', label = 'Dano aumentado' },
{ key = 'scatter', label = 'Tiro espalhado' },
}
local currentUpgradeChoices = nil
local hudFont, titleFont, questionFont, optionFont, smallFont, bigTitleFont
local cheatBtn = { x = 0, y = 0, w = 170, h = 28 }
local laser = {
active = false,
interval = 0.4,
timer = 0.4,
width = 10,
length = 900,
dx = 1,
dy = 0,
}
local feedback = { text = nil, timer = 0, color = {1, 1, 1} }
local laserHits = {}
local areaHits = {}
local showHowTo = false
local paused = false
local pausedFromState = nil -- Guarda estado anterior ao pause
-- Músicas de fundo
local bgMusic = nil
local menuMusic = nil
local bossMusic = nil
local musicVolume = 0.075 -- Volume baixo
local menuMusicVolume = 0.10 -- Volume para menu
local bossMusicVolume = 0.075 -- Volume para boss
-- Sistema de Sons Procedurais (versão suave)
local sounds = {}
local soundEnabled = true
local masterVolume = 0.12 -- Volume bem baixo
-- Gerador de som mais suave com filtro
local function generateSoftTone(frequency, duration, waveType, envelope, options)
options = options or {}
local sampleRate = 44100
local samples = math.floor(sampleRate * duration)
local soundData = love.sound.newSoundData(samples, sampleRate, 16, 1)
local freqSlide = options.freqSlide or 0 -- Deslize de frequência
local vibrato = options.vibrato or 0
local softness = options.softness or 1 -- Quanto maior, mais suave
local prevSample = 0 -- Para filtro passa-baixa
for i = 0, samples - 1 do
local t = i / sampleRate
local progress = i / samples
-- Frequência com slide e vibrato
local freq = frequency + (freqSlide * progress) + math.sin(t * 30) * vibrato
-- Envelope suave
local env = 1
if envelope == 'pluck' then
env = math.exp(-progress * 6 * softness)
elseif envelope == 'fade' then
env = (1 - progress) ^ 1.5
elseif envelope == 'pulse' then
env = math.sin(progress * math.pi) ^ 0.7
elseif envelope == 'smooth' then
-- Attack + decay suave
local attack = math.min(1, progress * 10)
local decay = math.max(0, 1 - (progress - 0.1) * 1.2)
env = attack * decay
end
-- Forma de onda
local sample = 0
if waveType == 'sine' then
sample = math.sin(2 * math.pi * freq * t)
elseif waveType == 'triangle' then
-- Triângulo é mais suave que quadrada
local phase = (freq * t) % 1
sample = 4 * math.abs(phase - 0.5) - 1
elseif waveType == 'softsaw' then
-- Serra suavizada
local phase = (freq * t) % 1
sample = 2 * phase - 1
sample = math.sin(sample * math.pi / 2) -- Suaviza
elseif waveType == 'noise' then
-- Ruído filtrado (mais suave)
local noise = math.random() * 2 - 1
sample = prevSample * 0.7 + noise * 0.3 -- Filtro passa-baixa
elseif waveType == 'laser' then
-- Som de laser suave (frequência descendente)
local laserFreq = freq * (1 - progress * 0.5)
sample = math.sin(2 * math.pi * laserFreq * t) * 0.6
sample = sample + math.sin(2 * math.pi * laserFreq * 2 * t) * 0.2
end
-- Filtro passa-baixa para suavizar
sample = prevSample * 0.3 + sample * 0.7
prevSample = sample
sample = sample * env * masterVolume
sample = math.max(-1, math.min(1, sample))
soundData:setSample(i, sample)
end
return love.audio.newSource(soundData)
end
local function initSounds()
-- Sons muito mais suaves
-- Tiro: som curto e suave tipo "pew" abafado
sounds.shoot = generateSoftTone(400, 0.06, 'triangle', 'pluck', {freqSlide = -200, softness = 2})
-- Hit: som suave de impacto
sounds.hit = generateSoftTone(150, 0.05, 'noise', 'pluck', {softness = 3})
-- Inimigo morrendo: som descendente suave
sounds.enemyDie = generateSoftTone(300, 0.12, 'triangle', 'fade', {freqSlide = -200})
-- Laser: som de laser suave e contínuo
sounds.laser = generateSoftTone(600, 0.08, 'laser', 'smooth', {freqSlide = -300})
-- Upgrade: som agradável ascendente
sounds.upgrade = generateSoftTone(400, 0.25, 'sine', 'pulse', {freqSlide = 300, vibrato = 5})
-- Resposta correta: acorde suave
sounds.correct = generateSoftTone(523, 0.2, 'sine', 'fade', {vibrato = 2})
-- Resposta errada: som grave descendente
sounds.wrong = generateSoftTone(200, 0.3, 'triangle', 'fade', {freqSlide = -100})
-- Clique de botão: som muito curto
sounds.buttonClick = generateSoftTone(350, 0.03, 'sine', 'pluck', {softness = 3})
-- Início de wave: som suave de alerta
sounds.waveStart = generateSoftTone(300, 0.35, 'sine', 'pulse', {freqSlide = 100})
-- Boss levando dano
sounds.bossHit = generateSoftTone(80, 0.1, 'softsaw', 'pluck', {softness = 2})
-- Player levando dano
sounds.playerHit = generateSoftTone(120, 0.12, 'noise', 'fade', {softness = 2})
end
local function playSound(name)
if not soundEnabled or not sounds[name] then return end
sounds[name]:stop()
sounds[name]:play()
end
local function showQuestionScreen()
Questions.setWave(wave)
question = Questions.getForWave(wave)
if not question then
-- Todas as perguntas esgotadas, continua sem pergunta
return false
end
state = 'question'
laser.active = false
laser.timer = laser.interval
playSound('question')
return true
end
local function clearBattlefield()
enemies = {}
bullets = {}
waveActive = false
toSpawn = 0
spawnTimer = 0
laser.active = false
laserHits = {}
end
local function pushFeedback(text, color)
feedback.text = text
feedback.timer = 1.5
feedback.color = color or {1, 1, 1}
end
local function cheatJumpToQuestion()
if state == 'upgrade' then return end
clearBattlefield()
completedWaves = completedWaves + 2
wave = wave + 1
showQuestionScreen()
end
local function cheatJumpToBoss()
if state == 'upgrade' then return end
clearBattlefield()
wave = 20 -- Próxima será 21 (boss) - startWave incrementa
completedWaves = 40
waveActive = false -- Força iniciar nova wave no próximo update
-- Dar upgrades equivalentes a jogar até wave 20
-- 10 perguntas respondidas = 10 upgrades
-- 2x laser, 2x scatter, resto em atk speed
player.hasLaser = true
player.laserLevel = 2
player.hasScatter = true
player.scatterLevel = 2
-- 6 upgrades de atk speed (10 - 2 laser - 2 scatter = 6)
for i = 1, 6 do
player.fireDelay = math.max(0.05, player.fireDelay * 0.5)
laser.interval = math.max(0.1, laser.interval * 0.5)
end
pushFeedback('Skip para Boss - Upgrades aplicados!', {1, 0.8, 0.2})
state = 'play' -- Garante que está no estado de jogo
end
local function updateFeedback(dt)
if feedback.timer > 0 then
feedback.timer = math.max(0, feedback.timer - dt)
end
end
local function addLaserHit(x, y)
table.insert(laserHits, { x = x, y = y, timer = 0.25, max = 0.25 })
end
local function updateLaserHits(dt)
for i = #laserHits, 1, -1 do
local hit = laserHits[i]
hit.timer = hit.timer - dt
if hit.timer <= 0 then
table.remove(laserHits, i)
end
end
end
local function addAreaHit(x, y, radius)
table.insert(areaHits, { x = x, y = y, radius = radius, timer = 0.3, max = 0.3 })
end
local function updateAreaHits(dt)
for i = #areaHits, 1, -1 do
local hit = areaHits[i]
hit.timer = hit.timer - dt
if hit.timer <= 0 then
table.remove(areaHits, i)
end
end
end
local function getTitleButtons()
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
local btnW, btnH = 240, 56
local playY = h * 0.55
local gap = 16
return {
play = { x = (w - btnW)/2, y = playY, w = btnW, h = btnH },
howto = { x = (w - btnW)/2, y = playY + btnH + gap, w = btnW, h = btnH },
}
end
local function getHowToLayout()
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
local boxW, boxH = 560, 360
local bx = (w - boxW)/2
local by = (h - boxH)/2 - 40
local closeW, closeH = 180, 46
local closeX = bx + (boxW - closeW)/2
local closeY = by + boxH - closeH - 24
return {
box = { x = bx, y = by, w = boxW, h = boxH },
close = { x = closeX, y = closeY, w = closeW, h = closeH },
}
end
-- Boss state
local boss = nil
local bossActive = false
local bossSpawnTimer = 0
local bossAttackTimer = 0
local bossPattern = 1
local bossProjectiles = {}
local function resetGame()
player = Player.new(love.graphics.getWidth()/2, love.graphics.getHeight()/2)
player.lives = 2 -- 2 vidas extras
enemies = {}
bullets = {}
wave = 0
completedWaves = 0
waveActive = false
toSpawn = 0
spawnTimer = 0
difficulty = 1.0
question = nil
lastAnswerCorrect = false
currentUpgradeChoices = nil
laser.active = false
laser.interval = 0.4
laser.timer = laser.interval
player.hasLaser = false
player.laserLevel = 0
player.hasScatter = false
player.scatterLevel = 0
player.lastUpgrade = nil
areaHits = {}
boss = nil
bossActive = false
bossSpawnTimer = 0
bossAttackTimer = 0
bossPattern = 1
bossProjectiles = {}
Questions.reset()
state = 'play'
end
-- Função para usar uma vida e continuar
local function useLife()
if player.lives > 0 then
player.lives = player.lives - 1
player.hp = player.maxHp
-- Limpa inimigos e projéteis para dar fôlego
enemies = {}
bossProjectiles = {}
-- Reposiciona player no centro
player.x = love.graphics.getWidth() / 2
player.y = love.graphics.getHeight() / 2
pushFeedback('Vida extra usada! (' .. player.lives .. ' restante' .. (player.lives == 1 and '' or 's') .. ')', {0.4, 1, 0.6})
playSound('upgrade')
state = 'play'
return true
end
return false
end
function love.load()
love.window.setTitle('Cybersafe Rogue - Roguelike MVP')
love.graphics.setBackgroundColor(Theme.colors.bgDark)
math.randomseed(os.time())
smallFont = love.graphics.newFont(12)
hudFont = love.graphics.newFont(14)
optionFont = love.graphics.newFont(15)
questionFont = love.graphics.newFont(17)
titleFont = love.graphics.newFont(22)
bigTitleFont = love.graphics.newFont(32)
initSounds() -- Inicializa sistema de sons
-- Carrega músicas
bgMusic = love.audio.newSource('main theme.mp3', 'stream')
bgMusic:setLooping(true)
bgMusic:setVolume(musicVolume)
menuMusic = love.audio.newSource('menu pause.mp3', 'stream')
menuMusic:setLooping(true)
menuMusic:setVolume(menuMusicVolume)
bossMusic = love.audio.newSource('boss.mp3', 'stream')
bossMusic:setLooping(true)
bossMusic:setVolume(bossMusicVolume)
-- Inicia com música do menu
menuMusic:play()
resetGame()
state = 'title'
showHowTo = false
end
local function startWave()
wave = wave + 1
-- Não avança além da wave 21 (boss)
if wave > 21 then
wave = 21
end
waveActive = true
playSound('waveStart')
-- Wave 21 é a boss wave
if wave == 21 then
-- Troca para música do boss
if bgMusic then bgMusic:stop() end
if bossMusic then bossMusic:play() end
-- Inicializa o boss (Trojan)
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
boss = {
x = w / 2,
y = 80,
hp = 5000,
maxHp = 5000,
r = 50,
speed = 40,
phase = 1,
targetX = w / 2,
moveTimer = 0,
attackCooldown = 0,
}
bossActive = true
bossSpawnTimer = 3 -- Espera 3 segundos antes de spawnar minions
bossAttackTimer = 0
bossPattern = 1
bossProjectiles = {}
toSpawn = 0
spawnTimer = 0
spawnInterval = 2.5 -- Minions aparecem mais devagar
return
end
-- sempre +5 inimigos por wave (com multiplicador de dificuldade)
local enemiesThisWave = baseEnemies + (wave - 1) * 5
toSpawn = math.floor(enemiesThisWave * difficulty)
spawnTimer = 0
spawnInterval = math.max(0.25, 0.7 - wave * 0.03)
end
local function spawnBossMinion()
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
local side = math.random(4)
local x, y
if side == 1 then x, y = -20, math.random(0, h)
elseif side == 2 then x, y = w + 20, math.random(0, h)
elseif side == 3 then x, y = math.random(0, w), -20
else x, y = math.random(0, w), h + 20 end
-- Minions do boss são mais fracos mas constantes
local hp = 8 + math.random(0, 4)
local speed = 70 + math.random(0, 20)
table.insert(enemies, Enemy.new(x, y, hp, speed, 'basic'))
end
local function fireBossProjectile(angle, speed, size)
-- Dano escala com a fase: 20 (fase 1), 30 (fase 2), 40 (fase 3)
local phaseDamage = 10 * (boss.phase + 1) -- 20, 30, 40
table.insert(bossProjectiles, {
x = boss.x,
y = boss.y,
vx = math.cos(angle) * speed,
vy = math.sin(angle) * speed,
r = size or 8,
damage = phaseDamage,
})
end
local function bossAttackPattern1()
-- Padrão circular: projéteis em círculo
local numProjectiles = 12
for i = 1, numProjectiles do
local angle = (i / numProjectiles) * math.pi * 2
fireBossProjectile(angle, 150, 8)
end
end
local function bossAttackPattern2()
-- Padrão direcionado: projéteis em direção ao player
local dx, dy = player.x - boss.x, player.y - boss.y
local len = math.sqrt(dx*dx + dy*dy)
if len > 0 then
dx, dy = dx/len, dy/len
end
local baseAngle = math.atan2(dy, dx)
-- 5 projéteis em cone
for i = -2, 2 do
local angle = baseAngle + i * 0.2
fireBossProjectile(angle, 200, 10)
end
end
local function bossAttackPattern3()
-- Padrão espiral: projéteis em espiral
local baseAngle = love.timer.getTime() * 3
for i = 0, 5 do
local angle = baseAngle + i * (math.pi / 3)
fireBossProjectile(angle, 120, 6)
end
end
local function updateBoss(dt)
if not boss then return end
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
-- Inicializa variáveis de movimento avançado se não existirem
if not boss.moveMode then
boss.moveMode = 'wander' -- wander, chase, zigzag, dash
boss.dashTimer = 0
boss.dashCooldown = 0
boss.zigzagDir = 1
boss.zigzagTimer = 0
boss.targetY = boss.y
boss.dashVx = 0
boss.dashVy = 0
end
-- Movimento baseado na fase
boss.moveTimer = boss.moveTimer - dt
-- Fase 1: Movimento horizontal simples (como antes)
if boss.phase == 1 then
if boss.moveTimer <= 0 then
boss.targetX = 100 + math.random() * (w - 200)
boss.moveTimer = 2 + math.random() * 2
end
if boss.x < boss.targetX then
boss.x = math.min(boss.x + boss.speed * dt, boss.targetX)
else
boss.x = math.max(boss.x - boss.speed * dt, boss.targetX)
end
-- Fase 2: Comportamento de Charger (dash attacks)
elseif boss.phase == 2 then
boss.dashCooldown = boss.dashCooldown - dt
if boss.moveMode == 'wander' then
-- Movimento normal até preparar dash
if boss.moveTimer <= 0 then
boss.targetX = 100 + math.random() * (w - 200)
boss.targetY = 80 + math.random() * (h * 0.3)
boss.moveTimer = 1.5 + math.random()
end
local dx = boss.targetX - boss.x
local dy = boss.targetY - boss.y
local len = math.sqrt(dx*dx + dy*dy)
if len > 5 then
boss.x = boss.x + (dx/len) * boss.speed * dt
boss.y = boss.y + (dy/len) * boss.speed * dt
end
-- Preparar dash quando cooldown acabar
if boss.dashCooldown <= 0 then
boss.moveMode = 'charging'
boss.dashTimer = 0.8 -- Tempo carregando
-- Direção do dash: em direção ao player
local toDx = player.x - boss.x
local toDy = player.y - boss.y
local toLen = math.sqrt(toDx*toDx + toDy*toDy)
if toLen > 0 then
boss.dashVx = (toDx/toLen) * boss.speed * 12 -- TRIPLO: 4 -> 12
boss.dashVy = (toDy/toLen) * boss.speed * 12
end
end
elseif boss.moveMode == 'charging' then
-- Tremer enquanto carrega (visual)
boss.dashTimer = boss.dashTimer - dt
if boss.dashTimer <= 0 then
boss.moveMode = 'dash'
boss.dashTimer = 1.2 -- Duração do dash TRIPLA: 0.6 -> 1.8
end
elseif boss.moveMode == 'dash' then
-- Dash rápido!
boss.x = boss.x + boss.dashVx * dt
boss.y = boss.y + boss.dashVy * dt
-- Limita à tela
boss.x = math.max(50, math.min(w - 50, boss.x))
boss.y = math.max(50, math.min(h - 50, boss.y))
boss.dashTimer = boss.dashTimer - dt
if boss.dashTimer <= 0 then
boss.moveMode = 'wander'
boss.dashCooldown = 3 + math.random() * 2
boss.moveTimer = 0.5
end
end
-- Fase 3: Comportamento híbrido Charger + Zigzag
else
boss.dashCooldown = boss.dashCooldown - dt
boss.zigzagTimer = boss.zigzagTimer - dt
if boss.moveMode == 'wander' then
-- Movimento zigzag MUITO mais intenso (glitch)
if boss.zigzagTimer <= 0 then
boss.zigzagDir = -boss.zigzagDir
boss.zigzagTimer = 0.08 + math.random() * 0.05 -- Muito mais rápido
end
if boss.moveTimer <= 0 then
boss.targetX = 50 + math.random() * (w - 100) -- Range maior
boss.targetY = 50 + math.random() * (h * 0.6) -- Desce mais
boss.moveTimer = 0.5 + math.random() * 0.5 -- Muda alvo mais rápido
end
-- Movimento em direção ao alvo + zigzag lateral MUITO MAIOR
local dx = boss.targetX - boss.x
local dy = boss.targetY - boss.y
local len = math.sqrt(dx*dx + dy*dy)
if len > 5 then
local ndx, ndy = dx/len, dy/len
-- Perpendicular
local perpX, perpY = -ndy, ndx
local moveSpeed = boss.speed * 2.5 -- MUITO mais rápido na fase 3
boss.x = boss.x + (ndx * 0.5 + perpX * 1.5 * boss.zigzagDir) * moveSpeed * dt -- Zigzag 3x maior
boss.y = boss.y + (ndy * 0.5 + perpY * 1.5 * boss.zigzagDir) * moveSpeed * dt
end
-- Dash mais frequente
if boss.dashCooldown <= 0 then
boss.moveMode = 'charging'
boss.dashTimer = 0.3 -- Carga MUITO mais rápida
local toDx = player.x - boss.x
local toDy = player.y - boss.y
local toLen = math.sqrt(toDx*toDx + toDy*toDy)
if toLen > 0 then
boss.dashVx = (toDx/toLen) * boss.speed * 15 -- Dash MUITO mais forte
boss.dashVy = (toDy/toLen) * boss.speed * 15
end
end
elseif boss.moveMode == 'charging' then
boss.dashTimer = boss.dashTimer - dt
if boss.dashTimer <= 0 then
boss.moveMode = 'dash'
boss.dashTimer = 0.5
end
elseif boss.moveMode == 'dash' then
boss.x = boss.x + boss.dashVx * dt
boss.y = boss.y + boss.dashVy * dt
boss.x = math.max(50, math.min(w - 50, boss.x))
boss.y = math.max(50, math.min(h - 50, boss.y))
boss.dashTimer = boss.dashTimer - dt
if boss.dashTimer <= 0 then
boss.moveMode = 'wander'
boss.dashCooldown = 2 + math.random()
boss.moveTimer = 0.3
end
end
end
-- Determina fase baseada no HP
if boss.hp <= boss.maxHp * 0.3 then
boss.phase = 3
elseif boss.hp <= boss.maxHp * 0.6 then
boss.phase = 2
end
-- Ataque baseado na fase
bossAttackTimer = bossAttackTimer - dt
if bossAttackTimer <= 0 then
if boss.phase == 1 then
bossAttackPattern1()
bossAttackTimer = 2.0
elseif boss.phase == 2 then
if bossPattern == 1 then
bossAttackPattern1()
bossPattern = 2
else
bossAttackPattern2()
bossPattern = 1
end
bossAttackTimer = 1.5
else
-- Fase 3: todos os padrões mais rápido
local pattern = math.random(1, 3)
if pattern == 1 then bossAttackPattern1()
elseif pattern == 2 then bossAttackPattern2()
else bossAttackPattern3() end
bossAttackTimer = 1.0
end
end
-- Update projectiles
for i = #bossProjectiles, 1, -1 do
local p = bossProjectiles[i]
p.x = p.x + p.vx * dt
p.y = p.y + p.vy * dt
-- Colisão com player
local dx, dy = p.x - player.x, p.y - player.y
local dist = math.sqrt(dx*dx + dy*dy)
if dist <= p.r + player.r then
player.hp = player.hp - p.damage
playSound('playerHit')
table.remove(bossProjectiles, i)
if player.hp <= 0 then
player.hp = 0
state = 'gameover'
end
-- Remove se fora da tela
elseif p.x < -50 or p.x > w + 50 or p.y < -50 or p.y > love.graphics.getHeight() + 50 then
table.remove(bossProjectiles, i)
end
end
-- Spawn minions periodicamente
bossSpawnTimer = bossSpawnTimer - dt
if bossSpawnTimer <= 0 then
spawnBossMinion()
bossSpawnTimer = math.max(1.5, 3.0 - boss.phase * 0.5) -- Mais rápido nas fases finais
end
-- Colisão do boss com player durante dash (causa 50% da vida máxima)
if boss.moveMode == 'dash' then
local dx, dy = boss.x - player.x, boss.y - player.y
local dist = math.sqrt(dx*dx + dy*dy)
local bossRadius = 40 -- Raio aproximado do boss
if dist <= bossRadius + player.r then
local chargeDamage = math.floor(player.maxHp * 0.5) -- 50% da vida máxima
player.hp = player.hp - chargeDamage
playSound('playerHit')
pushFeedback('CHARGE! -' .. chargeDamage .. ' HP', {1, 0.2, 0.2})
-- Empurra o player para longe
if dist > 0 then
local pushForce = 200
player.x = player.x - (dx/dist) * pushForce * 0.5
player.y = player.y - (dy/dist) * pushForce * 0.5
end
-- Cancela o dash após acertar
boss.moveMode = 'wander'
boss.dashCooldown = 2 + math.random()
if player.hp <= 0 then
player.hp = 0
state = 'gameover'
end
end
end
end
local function hitBoss(damage)
if not boss then return false end
boss.hp = boss.hp - damage
if boss.hp <= 0 then
boss = nil
bossActive = false
bossProjectiles = {}
return true -- Boss morreu
end
return false
end
local function spawnEnemy()
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
-- spawn nas bordas
local side = math.random(4)
local x, y
if side == 1 then x, y = -20, math.random(0, h)
elseif side == 2 then x, y = w + 20, math.random(0, h)
elseif side == 3 then x, y = math.random(0, w), -20
else x, y = math.random(0, w), h + 20 end
-- Escalonamento: vida inicial 3, +6 HP por wave (3x mais)
local hp = 3 + (wave - 1) * 6
local speed = 60 + (wave - 1) * 1
-- Determinar tipo de inimigo
local enemyKind = 'basic'
-- Charger aparece a partir da wave 2
local chargerChance = 0
if wave >= 2 then
chargerChance = math.min(0.12 + wave * 0.025, 0.55)
end
-- Zigzag aparece a partir da wave 3
local zigzagChance = 0
if wave >= 3 then
zigzagChance = math.min(0.10 + wave * 0.02, 0.40)
end
-- Rolar para determinar tipo
local roll = math.random()
if roll < zigzagChance then
enemyKind = 'zigzag'
-- Zigzag tem menos HP base (aplicado no Enemy.new)
elseif roll < zigzagChance + chargerChance then
enemyKind = 'charger'
hp = hp + 1 -- Charger tem +1 HP
end
table.insert(enemies, Enemy.new(x, y, hp, speed, enemyKind))
end
local function allEnemiesDefeated()
-- Na boss wave, verifica se o boss foi derrotado
if wave == 21 then
return waveActive and not bossActive and #enemies == 0
end
return waveActive and toSpawn <= 0 and #enemies == 0
end
local function endWave()
waveActive = false
-- Se derrotou o boss, mostra vitória
if wave == 21 then
-- Para música do boss, volta para música do menu (vitória)
if bossMusic then bossMusic:stop() end
if menuMusic then menuMusic:play() end
state = 'victory'
return
end
completedWaves = completedWaves + 1
if completedWaves % 2 == 0 then
local hasQuestion = showQuestionScreen()
if not hasQuestion then
-- Sem mais perguntas, vai direto pro boss
startWave()
end
end
end
local function fireTowardsMouse()
local mx, my = love.mouse.getPosition()
local dx, dy = mx - player.x, my - player.y
local len = math.sqrt(dx*dx + dy*dy)
if len == 0 then return end
dx, dy = dx/len, dy/len
if player.hasScatter then
-- Fire bullets in a cone: 3 at level 1, 6 at level 2
local spreadAngle = 0.26 -- ~15 degrees in radians
local baseAngle = math.atan2(dy, dx)
local angles
if (player.scatterLevel or 1) >= 2 then
-- Level 2: 6 projectiles with tighter spread
local halfSpread = spreadAngle * 1.2
angles = {
baseAngle - halfSpread,
baseAngle - halfSpread * 0.6,
baseAngle - halfSpread * 0.2,
baseAngle + halfSpread * 0.2,
baseAngle + halfSpread * 0.6,
baseAngle + halfSpread,
}
else
-- Level 1: 3 projectiles
angles = { baseAngle - spreadAngle, baseAngle, baseAngle + spreadAngle }
end
for _, angle in ipairs(angles) do
local adx = math.cos(angle)
local ady = math.sin(angle)
local b = Bullet.new(player.x, player.y, adx * player.bulletSpeed, ady * player.bulletSpeed, player.bulletDamage)
table.insert(bullets, b)
end
else
local b = Bullet.new(player.x, player.y, dx * player.bulletSpeed, dy * player.bulletSpeed, player.bulletDamage)
table.insert(bullets, b)
end
playSound('shoot')
end
local function distancePointToSegment(px, py, x1, y1, x2, y2)
local dx, dy = x2 - x1, y2 - y1
local len2 = dx*dx + dy*dy
if len2 == 0 then
local ddx, ddy = px - x1, py - y1
return math.sqrt(ddx*ddx + ddy*ddy)
end
local t = ((px - x1) * dx + (py - y1) * dy) / len2
t = math.max(0, math.min(1, t))
local projx = x1 + t * dx
local projy = y1 + t * dy
local ddx, ddy = px - projx, py - projy
return math.sqrt(ddx*ddx + ddy*ddy)
end
local function getLaserDirections()
-- Returns array of {dx, dy} for each laser beam
local baseAngle = math.atan2(laser.dy, laser.dx)
if player.hasScatter then
local spreadAngle = 0.26 -- ~15 degrees
if (player.scatterLevel or 1) >= 2 then
-- Level 2: 6 lasers
local halfSpread = spreadAngle * 1.2
return {
{ dx = math.cos(baseAngle - halfSpread), dy = math.sin(baseAngle - halfSpread) },
{ dx = math.cos(baseAngle - halfSpread * 0.6), dy = math.sin(baseAngle - halfSpread * 0.6) },
{ dx = math.cos(baseAngle - halfSpread * 0.2), dy = math.sin(baseAngle - halfSpread * 0.2) },
{ dx = math.cos(baseAngle + halfSpread * 0.2), dy = math.sin(baseAngle + halfSpread * 0.2) },
{ dx = math.cos(baseAngle + halfSpread * 0.6), dy = math.sin(baseAngle + halfSpread * 0.6) },
{ dx = math.cos(baseAngle + halfSpread), dy = math.sin(baseAngle + halfSpread) },
}
else
-- Level 1: 3 lasers
return {
{ dx = math.cos(baseAngle - spreadAngle), dy = math.sin(baseAngle - spreadAngle) },
{ dx = math.cos(baseAngle), dy = math.sin(baseAngle) },
{ dx = math.cos(baseAngle + spreadAngle), dy = math.sin(baseAngle + spreadAngle) },
}
end
else
return { { dx = laser.dx, dy = laser.dy } }
end
end
local function applyAreaDamage(centerX, centerY, radius)
-- Deal damage to all enemies within radius of the center point
local areaHitCount = 0
for i = #enemies, 1, -1 do
local e = enemies[i]
local ddx, ddy = e.x - centerX, e.y - centerY
local dist = math.sqrt(ddx*ddx + ddy*ddy)
if dist <= radius + e.r then
local dead = e:hit(player.bulletDamage * 0.5) -- Area damage is 50% of normal
if dead then
table.remove(enemies, i)
playSound('enemyDie')
end
areaHitCount = areaHitCount + 1
end
end
return areaHitCount
end
local function applyLaserDamage()
local directions = getLaserDirections()
local anyHit = false
local hitPositions = {} -- Store positions for area damage (laser level 2)
local totalHits = 0 -- Contador de hits para lifesteal
for _, dir in ipairs(directions) do
local x1, y1 = player.x, player.y
local x2 = player.x + dir.dx * laser.length
local y2 = player.y + dir.dy * laser.length
-- Dano no boss (verificação dupla para evitar nil)
if boss and bossActive and boss.x and boss.y then
local dist = distancePointToSegment(boss.x, boss.y, x1, y1, x2, y2)
if dist <= boss.r + laser.width * 0.5 then
local bossDied = hitBoss(player.bulletDamage)
if boss then -- Verificar novamente após hit
addLaserHit(boss.x, boss.y)
end
anyHit = true
totalHits = totalHits + 1
if bossDied then
pushFeedback('BOSS DERROTADO!', {1, 0.8, 0.2})
elseif boss and player.laserLevel >= 2 then
table.insert(hitPositions, { x = boss.x, y = boss.y })
end
end
end
for i = #enemies, 1, -1 do
local e = enemies[i]
local dist = distancePointToSegment(e.x, e.y, x1, y1, x2, y2)
if dist <= e.r + laser.width * 0.5 then
local dead = e:hit(player.bulletDamage)
addLaserHit(e.x, e.y)
anyHit = true
totalHits = totalHits + 1
-- Store hit position for area damage if laser level 2
if player.laserLevel >= 2 then
table.insert(hitPositions, { x = e.x, y = e.y })
end
if dead then
table.remove(enemies, i)
playSound('enemyDie')
end
end
end
end
-- Apply area damage for laser level 2
if player.laserLevel >= 2 then
local areaRadius = 60
for _, pos in ipairs(hitPositions) do
applyAreaDamage(pos.x, pos.y, areaRadius)
addAreaHit(pos.x, pos.y, areaRadius)
end
end
-- Aplicar lifesteal (roubo de vida)
if anyHit and player.laserLifesteal and player.laserLifesteal > 0 then
local healAmount = totalHits * player.laserLifesteal
player.hp = math.min(player.maxHp, player.hp + healAmount)
end