aboutsummaryrefslogtreecommitdiff
path: root/assets/scripts/local2P.lua
blob: 7fd2efa79449e3cfb4e14670227d466f1838d4d3 (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
---@diagnostic disable: undefined-global

backgrounds = require("assets.backgrounds.backgrounds-data")

groundTiles = {
    { x = 250, y = HEIGHT - 300, width = 200, height = 30 },
    { x = WIDTH - 250 - 200, y = HEIGHT - 300, width = 200, height = 30 },
}


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

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

gravity = 1.2 -- POSITIVE gravity

function DrawGroundTiles()
    for _, tile in ipairs(groundTiles) do
        queueRectForRender(tile.x, tile.y, tile.width, tile.height, 100, 100, 100, 255) -- gray boxes
    end
end

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

        local isWithinX = character.x + 64 > tile.x and character.x < tile.x + tile.width
        local isTouchingY = characterFeetY >= tile.y and characterFeetY <= tile.y + tile.height

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

function HandleP1Input()
    if Input.isKeyPressedOnce("W") and player1Character.can_jump then
       player1Character.y_velocity = player1Character.jump_strength * -1.0
       player1Character.can_jump = false
    end
    if Input.isKeyDown("D") then
        player1Character.x = player1Character.x + player1Character.speed
    end
    if Input.isKeyDown("A") then
        player1Character.x = player1Character.x - player1Character.speed
    end
    if Input.isKeyPressedOnce("F") then
        
    end
end

function HandleP2Input()
    if Input.isKeyPressedOnce("UP") and player2Character.can_jump then
        player2Character.y_velocity = player2Character.jump_strength * -1.0
        player2Character.can_jump = false
    end
    if Input.isKeyDown("RIGHT") then
        player2Character.x = player2Character.x + player2Character.speed
    end
    if Input.isKeyDown("LEFT") then
        player2Character.x = player2Character.x - player2Character.speed
    end
end

function Update()
    -- P1 physics
    player1Character.y_velocity = player1Character.y_velocity + gravity
    player1Character.y = player1Character.y + player1Character.y_velocity

    if IsOnGround(player1Character) then
        player1Character.y_velocity = 0
        player1Character.can_jump = true
    end

    -- P2 physics
    player2Character.y_velocity = player2Character.y_velocity + gravity
    player2Character.y = player2Character.y + player2Character.y_velocity

    if IsOnGround(player2Character) then
        player2Character.y_velocity = 0
        player2Character.can_jump = true
    end

    -- Render
    queueTextureForRender(
        player1Character.current_sprite,
        math.floor(player1Character.x),
        math.floor(player1Character.y)
    )

    queueTextureForRender(
        player2Character.current_sprite,
        math.floor(player2Character.x),
        math.floor(player2Character.y)
    )

    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)

    DrawGroundTiles()

    -- Input
    HandleP1Input()
    HandleP2Input()
end