You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
love2d-tank/Game/UpdateGame.lua

167 lines
3.7 KiB

local function checkLossState()
-- Check P1's health
if UserPlayer1.health <= 0 then --P1 Lost, P2 Win
_G.P2_COUNT = _G.P2_COUNT + 1
_G.P1_WIN = false
_G.P2_WIN = true
return true
end
if UserPlayer2.health <= 0 then --P2 Lost, P1 Win
_G.P1_COUNT = _G.P1_COUNT + 1
_G.P1_WIN = true
_G.P2_WIN = false
return true
end
return false
end
local max = math.max -- optimisations
function UpdateGame(dt)
--Check if anyone has won
if checkLossState() == true then
--print("STATE CHNAGED: WIN!")
_G.GAMESTATE = "WIN"
end
--WindField
World:update(dt)
KeyPressTime1 = max(0, KeyPressTime1 - dt)
if KeyPressTime1 <= 0 then
EnableKeyPress1 = true
end
KeyPressTime2 = max(0, KeyPressTime2 - dt)
if KeyPressTime2 <= 0 then
EnableKeyPress2 = true
end
-- Update Bullets1
for i, v in ipairs(Bullets1) do
v:update(dt)
-- Check bounce count
if v.bounce >= _G.BULLETS_BOUNCE_MAX then
table.remove(Bullets1, i)
v.collider:destroy()
end
-- Handle screen edges
if v.y < 0 then --top of screen
table.remove(Bullets1, i)
v.collider:destroy()
elseif v.y > love.graphics.getHeight() then --bottom of screen
table.remove(Bullets1, i)
v.collider:destroy()
end
-- Hit player2
if v.collider:enter("Player2") then
--print("Player1 hit Player2!")
table.remove(Bullets1, i)
v.collider:destroy()
if UserPlayer2.health > 0 then
UserPlayer2.health = UserPlayer2.health - 1
end
end
-- Hit player1
if v.collider:enter("Player1") then
--print("Player 1 hit themselves!")
table.remove(Bullets1, i)
v.collider:destroy()
if UserPlayer1.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1
end
end
--Hit another bullet
if v.collider:enter("Bullet1") then
table.remove(Bullets1, i)
v.collider:destroy()
end
end
-- Update Bullets2
for i, v in ipairs(Bullets2) do
v:update(dt)
-- Check bounce count
if v.bounce >= _G.BULLETS_BOUNCE_MAX then
table.remove(Bullets2, i)
v.collider:destroy()
end
-- Handle screen edges
if v.y < 0 then --top of screen
table.remove(Bullets2, i)
v.collider:destroy()
elseif v.y > love.graphics.getHeight() then --bottom of screen
table.remove(Bullets2, i)
v.collider:destroy()
end
-- Hit player1
if v.collider:enter("Player1") then
--print("Player2 hit Player1!")
table.remove(Bullets2, i)
v.collider:destroy()
if UserPlayer1.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1
end
end
-- Hit player2
if v.collider:enter("Player2") then
--print("Player 2 hit themselves!")
table.remove(Bullets2, i)
v.collider:destroy()
if UserPlayer2.health > 0 then
UserPlayer2.health = UserPlayer2.health - 1
end
end
--Hit another bullet
if v.collider:enter("Bullet2") then
table.remove(Bullets2, i)
v.collider:destroy()
end
end
-- PowerUp updates
for i, v in ipairs(PowerUps) do
v:update(dt)
if v.collider:enter("Player1") then
UserPlayer1:getPowerUp(v)
UserPlayer1:powerup()
table.remove(PowerUps, i)
v.collider:destroy()
end
if v.collider:enter("Player2") then
UserPlayer2:getPowerUp(v)
UserPlayer2:powerup()
table.remove(PowerUps, i)
v.collider:destroy()
end
end
--PowerUp timer to place
PUTimeToPlace = PUTimeToPlace - dt
--print(PUTimeToPlace)
if PUTimeToPlace <= 0 then
local w, h = love.graphics.getDimensions()
local ranx = love.math.random(30, w)
local rany = love.math.random(100, h)
local newPU = PowerUp("speed", ranx, rany, "assets/powerup.jpeg", 10)
table.insert(PowerUps, newPU)
PUTimeToPlace = _G.PU_TIMER --reset
end
UserPlayer1:handleKeys("w", "s", "a", "d", dt)
UserPlayer2:handleKeys("up", "down", "left", "right", dt)
UserPlayer1:updateCol()
UserPlayer2:updateCol()
UserPlayer1:update(dt)
UserPlayer2:update(dt)
end