diff options
| author | Arslaan Pathan <[email protected]> | 2025-06-15 11:10:31 +1200 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2025-06-15 11:10:31 +1200 |
| commit | 5348dbdef1c55c076c8603bad98d20c73085d12b (patch) | |
| tree | 443667e47fd88446e235e26e175cdbe0463c2598 /assets/scripts/local2P.lua | |
| parent | 2e463dff8ccb0e2431a255b8623fab9cb455ce61 (diff) | |
| download | showdownofthesticks-5348dbdef1c55c076c8603bad98d20c73085d12b.tar.xz showdownofthesticks-5348dbdef1c55c076c8603bad98d20c73085d12b.zip | |
Add proper ground/platforms using Rects - may be swapped for textures later
Diffstat (limited to 'assets/scripts/local2P.lua')
| -rw-r--r-- | assets/scripts/local2P.lua | 104 |
1 files changed, 89 insertions, 15 deletions
diff --git a/assets/scripts/local2P.lua b/assets/scripts/local2P.lua index c8e0aa5..7fd2efa 100644 --- a/assets/scripts/local2P.lua +++ b/assets/scripts/local2P.lua @@ -1,42 +1,80 @@ ---@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() - SafeInitCharacter(player1Character, 250, 250) - SafeInitCharacter(player2Character, WIDTH - 250 - (250 / 2), 250) + 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 -floor_y = HEIGHT - 250 + +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.y == floor_y then - player1Character.y_velocity = player1Character.jump_strength * -1.0; + 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; + player1Character.x = player1Character.x + player1Character.speed end if Input.isKeyDown("A") then - player1Character.x = player1Character.x - player1Character.speed; + player1Character.x = player1Character.x - player1Character.speed + end + if Input.isKeyPressedOnce("F") then + end end function HandleP2Input() - if Input.isKeyPressedOnce("UP") and player2Character.y == floor_y then - player2Character.y_velocity = player2Character.jump_strength * -1.0; + 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; + player2Character.x = player2Character.x + player2Character.speed end if Input.isKeyDown("LEFT") then - player2Character.x = player2Character.x - player2Character.speed; + player2Character.x = player2Character.x - player2Character.speed end end @@ -45,18 +83,18 @@ function Update() player1Character.y_velocity = player1Character.y_velocity + gravity player1Character.y = player1Character.y + player1Character.y_velocity - if player1Character.y > floor_y then - player1Character.y = floor_y + 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 player2Character.y > floor_y then - player2Character.y = floor_y + if IsOnGround(player2Character) then player2Character.y_velocity = 0 + player2Character.can_jump = true end -- Render @@ -72,6 +110,42 @@ function Update() 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() |
