aboutsummaryrefslogtreecommitdiff
path: root/assets/scripts/local2P.lua
blob: 48bce2cbe1af852a9b59d02650ba84e6b6e74da0 (plain)
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
---@diagnostic disable: undefined-global, lowercase-global

backgrounds = require("assets.backgrounds.backgrounds-data")
sprite_height = 128
sprite_width = 64

remaining_rounds = 9
current_round = 1

gamePaused = true
isKO = false
isOverlay = true
isNewRound = false
isFight = false
isGameOver = false

debugConsoleActive = false
debugInput = ""
debugOutput = ""

consoleKeycodes = {}
for _, name in ipairs({"a","b","c","d","e","f","g","h","i","j","k","l","m",
                       "n","o","p","q","r","s","t","u","v","w","x","y","z",
                       "space","return","backspace","backquote","minus","0","1","2","3","4","5","6","7","8","9",
                    "(", ")", "%", "$", "."}) do
    consoleKeycodes[name] = getKeycodeByName(name)
end

winner = nil

math.randomseed(os.time())

groundTiles = {}

specialGroundTiles = {
    { x = -50,               y = 0,            width = 55,    height = HEIGHT,  special = true  },
    { x = WIDTH - 5,         y = 0,            width = 50,    height = HEIGHT,  special = true  }
}

knockbackMultiplierX = 22.4
knockbackMultiplierY = 7.73
minimalKnockbackMultiplierY = 40.3

function EvaluateDebugCode(code)
    local func, syntaxError = load(code, "debug", "t", _G)
    if not func then
        debugOutput = "Syntax error: " .. syntaxError
        return
    end

    local success, result = pcall(func)
    if not success then
        debugOutput = "Runtime error: " .. result
    else
        if result == nil then
            debugOutput = "Code executed successfully."
        else
            debugOutput = tostring(result)
        end
    end
end

function HandleDebugConsoleInput()
    if not debugConsoleActive then return end

    local shiftPressed = Input.isKeyDown("Left Shift")

    for keyName, keycode in pairs(consoleKeycodes) do
        if Input.isKeyPressedOnce(keyName) then
            if keyName == "backspace" then
                debugInput = debugInput:sub(1, -2)
            elseif keyName == "return" then
                EvaluateDebugCode(debugInput)
                debugInput = ""
            elseif #keyName == 1 then
                if shiftPressed then
                    debugInput = debugInput .. keyName:upper()
                else
                    debugInput = debugInput .. keyName
                end
            elseif keyName == "space" then
                debugInput = debugInput .. " "
            elseif keyName == "minus" then
                debugInput = debugInput .. "-"
            elseif keyName == "backquote" then
                debugInput = debugInput .. "`"
            end
        end
    end
end

function HandleDebugToggle()
    if Input.isKeyPressedOnce("`") then
        debugConsoleActive = not debugConsoleActive
        debugInput = ""
        debugOutput = ""
        gamePaused = debugConsoleActive  -- optional: pause game when console is open
    end
end

function SafeInitCharacter(character, default_x, default_y)
    character.x = default_x
    character.x_velocity = 1
    character.y = default_y
    character.y_velocity = 1
    character.current_sprite = character.asset_dir .. "/sprites/idle.png"
    character.default_sprite = character.current_sprite
    character.can_jump = false
    character.knockback_counter = 0
    character.combo_chain = 0
    character.can_apply_knockback = false
    character.facing = "right"
    character.isDead = false
    character.wins = 0
end

function Setup()
    local bgIndex = math.random(1, #backgrounds)
    setBgImage(backgrounds[bgIndex])
    SafeInitCharacter(player1Character, 250, 150)
    SafeInitCharacter(player2Character, WIDTH - 250 - (250 / 2), 150)

    groundTiles = {}

    local tileSize = 50
    local gridWidth = math.floor(WIDTH / tileSize)
    local gridHeight = math.floor(HEIGHT / tileSize)
    local clusterSizeMin, clusterSizeMax = 3, 5
    local clusterCount = 8

    for _ = 1, clusterCount do
        local clusterX = math.random(1, gridWidth - clusterSizeMax - 2)
        local clusterY = math.random(5, gridHeight - 3)
        local clusterSize = math.random(clusterSizeMin, clusterSizeMax)

        for i = 0, clusterSize - 1 do
            table.insert(groundTiles, {
                x = (clusterX + i) * tileSize,
                y = clusterY * tileSize,
                width = tileSize,
                height = tileSize,
                special = false
            })
        end
    end

    for _, tile in ipairs(specialGroundTiles) do
        table.insert(groundTiles, tile)
    end

    Timer.after(0.5, function ()
        isOverlay = false
        isNewRound = true
        Timer.after(2, function ()
            isNewRound = false
            isFight = true
            Timer.after(2, function ()
                isFight = false
                gamePaused = false
            end)
        end)
    end)
end

gravity = 1570

function DrawGroundTiles()
    for _, tile in ipairs(groundTiles) do
        if tile.special then
            return
        end
        queueTextureForRender("assets/images/tile.png", tile.x, tile.y)
    end
end

function IsOnGround(character)
    for _, tile in ipairs(groundTiles) do
        local characterFeetY = character.y + sprite_height

        local isWithinX = character.x + sprite_width > tile.x and character.x < tile.x + tile.width
        local isTouchingY = characterFeetY >= tile.y and characterFeetY <= tile.y + tile.height
        local isFalling = character.y_velocity >= 0

        if isWithinX and isTouchingY and isFalling then
            character.y = tile.y - sprite_height
            return true
        end
    end
    return false
end

function IsHittingCeiling(character)
    for _, tile in ipairs(groundTiles) do
        local characterHeadY = character.y
        local isWithinX = character.x + sprite_width > tile.x and character.x < tile.x + tile.width
        local isTouchingY = characterHeadY <= tile.y + tile.height and characterHeadY >= tile.y

        if isWithinX and isTouchingY then
            character.y = tile.y + tile.height
            character.y_velocity = 0
            return true
        end
    end
    return false
end

function AABBOverlap(a, b)
    return a.x < b.x + b.width and
           a.x + a.width > b.x and
           a.y < b.y + b.height and
           a.y + a.height > b.y
end

function IsTouchingWall(character, direction, exclude_special)
    local velocity = character.x_velocity

    -- Clamp how far ahead we look
    local max_check_distance = 1.0  -- only check 1 pixel ahead
    if velocity > max_check_distance then velocity = max_check_distance end
    if velocity < -max_check_distance then velocity = -max_check_distance end

    -- Prevent missing collisions on tiny glide
    if math.abs(velocity) < 0.01 then
        velocity = (direction == "right") and 0.01 or -0.01
    end

    local futureX = character.x + velocity

    local charBox = {
        x = futureX,
        y = character.y,
        width = sprite_width,
        height = sprite_height
    }

    for _, tile in ipairs(groundTiles) do
        if tile.special and exclude_special then goto continue end

        local tileBox = {
            x = tile.x,
            y = tile.y,
            width = tile.width,
            height = tile.height
        }

        if AABBOverlap(charBox, tileBox) then
            -- Resolve overlap
            if direction == "left" then
                character.x = tile.x + tile.width
            elseif direction == "right" then
                character.x = tile.x - sprite_width
            end
            character.x_velocity = 0
            return true
        end

        ::continue::
    end

    return false
end

function HandleP1Input()
    if player1Character.isDead then return end
    if Input.isKeyDown("W") and player1Character.can_jump then
        player1Character.y_velocity = player1Character.jump_strength * -1.0
        player1Character.can_jump = false
    end

    if Input.isKeyDown("D") then
        if not IsTouchingWall(player1Character, "right", false) then
            player1Character.x_velocity = player1Character.speed
            player1Character.facing = "right"
        else
            player1Character.x_velocity = 0
            player1Character.x = math.floor(player1Character.x)
        end
    end
    if Input.isKeyDown("A") then
        if not IsTouchingWall(player1Character, "left", false) then
            player1Character.x_velocity = -player1Character.speed
            player1Character.facing = "left"
        else
            player1Character.x_velocity = 0
            player1Character.x = math.floor(player1Character.x)
        end
    end

    if Input.isKeyPressedOnce("F") then
        PerformPunch(player1Character, player2Character)
    end
end

function HandleP2Input()
    if player2Character.isDead then return end
    if Input.isKeyDown("UP") and player2Character.can_jump then
        player2Character.y_velocity = player2Character.jump_strength * -1.0
        player2Character.can_jump = false
    end

    if Input.isKeyDown("RIGHT") then
        if not IsTouchingWall(player2Character, "right", false) then
            player2Character.x_velocity = player2Character.speed
            player2Character.facing = "right"
        else
            player2Character.x_velocity = 0
            player2Character.x = math.floor(player2Character.x)
        end
    end
    if Input.isKeyDown("LEFT") then
        if not IsTouchingWall(player2Character, "left", false) then
            player2Character.x_velocity = -player2Character.speed
            player2Character.facing = "left"
        else
            player2Character.x_velocity = 0
            player2Character.x = math.floor(player2Character.x)
        end
    end

    if Input.isKeyPressedOnce("L") then
        PerformPunch(player2Character, player1Character)
    end
end

function DrawUI()
    local fontFile = "assets/fonts/OpenSans-Bold.ttf"
    local fontSize = 24
    local text = player1Character.name

    local x = 20
    local y = 20

    queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)

    local text = player2Character.name

    local textWidth = getTextWidth(fontFile, fontSize, text)
    local x = WIDTH - textWidth - 20
    local y = 20

    queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)

    local fontFile = "assets/fonts/OpenSans-MediumItalic.ttf"
    local fontSize = 34
    local text = tonumber(player1Character.knockback_counter)

    local x = 40
    local y = 40

    queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)

    local fontFile = "assets/fonts/OpenSans-MediumItalic.ttf"
    local fontSize = 34
    local text = tostring(player2Character.knockback_counter)

    local textWidth = getTextWidth(fontFile, fontSize, text)
    local x = WIDTH - textWidth - 40

    queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)
end

function Respawn(character)
    SafeInitCharacter(character, 250, 150)
end

function ApplyKnockback(attacker, target)
    local direction = attacker.facing == "left" and -1 or 1
    local scaledY = -10 - (target.knockback_counter * knockbackMultiplierY)
    target.y_velocity = math.max(-950, scaledY)  -- clamp Y knockback

    target.x_velocity = target.knockback_counter * knockbackMultiplierX * direction
end

function ApplyMinimalKnockback(attacker, target)
    local knockbackCountMinimal = 5
    target.y_velocity = -10 - (knockbackCountMinimal * minimalKnockbackMultiplierY)

    local direction = 1
    if attacker.facing == "left" then
        direction = -1
    end

    target.x_velocity = knockbackCountMinimal * knockbackMultiplierX * direction
    -- target.x = target.x + direction * (target.knockback_counter * 0.7)
end

function GameOver()
    isGameOver = true
    Timer.after(10, function ()
        dofile("assets/scripts/mainMenu.lua")
        Setup()
    end)

    if player1Character.wins > player2Character.wins then
        winner = player1Character
    else
        winner = player2Character
    end
end

function KillPlayer(character)
    if gamePaused then return end
    if character.isDead then return end
    character.isDead = true

    playSound("assets/sounds/K-O.wav")
    gamePaused = true
    isKO = true

    Timer.after(2, function()
        remaining_rounds = remaining_rounds - 1
        current_round = current_round + 1
        character.knockback_counter = 0
        character.combo_chain = 0

        if character == player1Character then
            player2Character.wins = player2Character.wins + 1
        else
            player1Character.wins = player1Character.wins + 1
        end

        if remaining_rounds > 0 then
            Setup()
        else
            GameOver()
        end
        isKO = false
        if remaining_rounds > 0 then
            isOverlay = true
            Timer.after(2, function ()
                isOverlay = false
                isNewRound = true
                Timer.after(2, function ()
                    isNewRound = false
                    isFight = true
                    Timer.after(2, function ()
                        gamePaused = false
                        isFight = false
                    end)
                end)
            end)
        end
    end)
end

function RegisterHit(attacker, target, damage)
    target.knockback_counter = target.knockback_counter + damage
    target.combo_chain = 0
    attacker.combo_chain = attacker.combo_chain + 1

    if attacker.combo_chain >= 4 then
        ApplyKnockback(attacker, target)
        attacker.combo_chain = 0
    else
        ApplyMinimalKnockback(attacker, target)
    end

    if target.knockback_counter >= 1000 then
        KillPlayer(target)
    end
end

function PerformPunch(attacker, target)
    local punch_range = 27
    local punch_height = 80

    -- Determine direction of target relative to attacker
    local punch_direction = attacker.facing

    -- Set punch sprite
    attacker.current_sprite = attacker.asset_dir .. "/sprites/punch_" .. punch_direction .. ".png"

    -- Reset sprite after a short delay (e.g., 0.15s)
    Timer.clearAllTimers()
    Timer.after(0.15, function()
        attacker.current_sprite = attacker.default_sprite
    end)

    -- Define hitbox based on punch direction
    local hitbox = {
        x = attacker.x + (punch_direction == "right" and sprite_width or -punch_range),
        y = attacker.y + (sprite_height / 2) - (punch_height / 2),
        width = punch_range,
        height = punch_height
    }

    local targetHitbox = {
        x = target.x,
        y = target.y,
        width = sprite_width,
        height = sprite_height
    }

    local function isColliding(a, b)
        return a.x < b.x + b.width and
            a.x + a.width > b.x and
            a.y < b.y + b.height and
            a.y + a.height > b.y
    end

    if isColliding(hitbox, targetHitbox) then
        Timer.after(0.05, function ()
            RegisterHit(attacker, target, attacker.hit_strength)
        end)
    end
end

function ApplyPhysics(character)
    if character.isDead then return end

    local last_x = character.x
    local last_y = character.y

    character.y_velocity = character.y_velocity + gravity * deltaTime
    character.y = character.y + character.y_velocity * deltaTime
    character.x = character.x + character.x_velocity * deltaTime
    character.x_velocity = character.x_velocity * 0.88

    if character.y_velocity > 0 then
        -- Falling
        if IsOnGround(character) then
            character.y_velocity = 0
            character.can_jump = true
        end
    elseif character.y_velocity < 0 then
        -- Going upward
        IsHittingCeiling(character)
    end

    if IsTouchingWall(character, "left", true) then
        character.x = last_x
        character.x_velocity = 0
    end
    if IsTouchingWall(character, "right", true) then
        character.x = last_x
        character.x_velocity = 0
    end

    if not IsOnGround(character) then
        character.can_jump = false
    end

    if character.y > HEIGHT + 60 then
        KillPlayer(character)
    end

    if character.x < -60 then
        KillPlayer(character)
    end

    if character.x > WIDTH + 60 then
        KillPlayer(character)
    end
end

function DrawOverlay()
    queueRectForRender(0, 0, 1280, 720, 0, 0, 0, 150)
end

function DrawKOGraphic()
    DrawOverlay()

    local fontFile = "assets/fonts/OpenSans-Bold.ttf"
    local fontSize = 192
    local text = "K.O."
    local textWidth = getTextWidth(fontFile, fontSize, text)
    local x = (1280 - textWidth) // 2
    local y = 720 // 2 - fontSize // 2

    queueTextForRender(text, fontFile, x + 5, y + 5, fontSize, 255, 255, 0, 255)
    queueTextForRender(text, fontFile, x, y, fontSize, 255, 0, 0, 255)
end

function DrawNewRoundGraphic()
    DrawOverlay()

    local fontFile = "assets/fonts/OpenSans-Bold.ttf"
    local fontSize = 96
    local text = "Round " .. current_round
    if remaining_rounds == 1 then
        text = "Final Round"
    end
    local textWidth = getTextWidth(fontFile, fontSize, text)
    local x = (1280 - textWidth) // 2
    local y = 720 // 2 - fontSize // 2

    queueTextForRender(text, fontFile, x, y, fontSize, 255, 255, 255, 255)
end

function DrawFightGraphic()
    DrawOverlay()

    local fontFile = "assets/fonts/OpenSans-Bold.ttf"
    local fontSize = 96
    local text = "FIGHT!"
    local textWidth = getTextWidth(fontFile, fontSize, text)
    local x = (1280 - textWidth) // 2
    local y = 720 // 2 - fontSize // 2

    queueTextForRender(text, fontFile, x, y, fontSize, 255, 255, 0, 255)
end

function DrawGameOverGraphic()
    if not winner then return end

    setBgImage(backgrounds[1])

    local fontFile = "assets/fonts/OpenSans-Bold.ttf"
    local fontSize = 96
    local text = "WINNER"
    local textWidth = getTextWidth(fontFile, fontSize, text)
    local x = (1280 - textWidth) // 2
    local y = 20

    queueTextForRender(text, fontFile, x, y, fontSize, 255, 255, 0, 255)

    local portraitWidth = 128
    local portraitHeight = 256
    local portraitX = (WIDTH // 2) - (portraitWidth // 2)
    local portraitY = (HEIGHT // 2) - (portraitHeight // 2)

    queueTextureForRender(winner.asset_dir .. "/portrait.png", portraitX, portraitY)

    local nameFontSize = 24
    local nameWidth = getTextWidth(fontFile, nameFontSize, winner.name)
    local nameX = (1280 - nameWidth) // 2
    local nameY = portraitY + portraitHeight + 20

    queueTextForRender(winner.name, fontFile, nameX, nameY, nameFontSize, 255, 255, 255, 255)
end

function DrawDebugConsole()
    if not debugConsoleActive then return end

    local fontFile = "assets/fonts/OpenSans-Regular.ttf"
    local fontSize = 20

    -- Draw background rectangle for console input area
    queueRectForRender(10, HEIGHT - 60, WIDTH - 20, 50, 0, 0, 0, 180)

    -- Draw input text
    queueTextForRender("> " .. debugInput, fontFile, 20, HEIGHT - 50, fontSize, 255, 255, 255, 255)

    -- Draw output text above input
    queueTextForRender(debugOutput, fontFile, 20, HEIGHT - 80, fontSize, 255, 255, 0, 255)
end


function Update()
    local maxDelta = 0.05 -- max 50 ms per frame
    if deltaTime > maxDelta then
        deltaTime = maxDelta
    end

    Timer.update(deltaTime)

    HandleDebugToggle()

    if debugConsoleActive then
        HandleDebugConsoleInput()
        DrawDebugConsole()
    end

    if not gamePaused then
        ApplyPhysics(player1Character)
        ApplyPhysics(player2Character)

        HandleP1Input()
        HandleP2Input()
    end

    if not isGameOver then
        queueTextureForRender(player1Character.current_sprite, math.ceil(player1Character.x), math.ceil(player1Character.y))
        queueTextureForRender(player2Character.current_sprite, math.ceil(player2Character.x), math.ceil(player2Character.y))
        DrawGroundTiles()
        DrawUI()
    end

    if isKO then
        DrawKOGraphic()
    end
    if isOverlay then
        DrawOverlay()
    end
    if isNewRound then
        DrawNewRoundGraphic()
    end
    if isFight then
        DrawFightGraphic()
    end
    if isGameOver then
        DrawGameOverGraphic()
    end
end