---@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