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

137 lines
3.0 KiB

2 months ago
local function checkLossState()
2 months ago
-- Check P1's health
2 months ago
if UserPlayer1.health <= 0 then --P1 Lost, P2 Win
_G.P2_COUNT = _G.P2_COUNT + 1
2 months ago
_G.P1_WIN = false
_G.P2_WIN = true
2 months ago
return true
end
2 months ago
2 months ago
if UserPlayer2.health <= 0 then --P2 Lost, P1 Win
_G.P1_COUNT = _G.P1_COUNT + 1
2 months ago
_G.P1_WIN = true
_G.P2_WIN = false
2 months ago
return true
end
return false
end
local max = math.max -- optimisations
5 months ago
function UpdateGame(dt)
2 months ago
--Check if anyone has won
2 months ago
if checkLossState() == true then
2 months ago
--print("STATE CHNAGED: WIN!")
2 months ago
_G.GAMESTATE = "WIN"
end
4 months ago
--WindField
World:update(dt)
5 months ago
KeyPressTime1 = max(0, KeyPressTime1 - dt)
if KeyPressTime1 <= 0 then
EnableKeyPress1 = true
end
KeyPressTime2 = max(0, KeyPressTime2 - dt)
if KeyPressTime2 <= 0 then
EnableKeyPress2 = true
end
2 months ago
5 months ago
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
5 months ago
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
5 months ago
if v.collider:enter("Player2") then
2 months ago
--print("Player1 hit Player2!")
4 months ago
table.remove(Bullets1, i)
v.collider:destroy()
2 months ago
if UserPlayer2.health > 0 then
UserPlayer2.health = UserPlayer2.health - 1
4 months ago
end
end
-- Hit player1
4 months ago
if v.collider:enter("Player1") then
2 months ago
--print("Player 1 hit themselves!")
5 months ago
table.remove(Bullets1, i)
v.collider:destroy()
4 months ago
if UserPlayer1.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1
end
5 months ago
end
--Hit another bullet
if v.collider:enter("Bullet1") then
table.remove(Bullets1, i)
v.collider:destroy()
end
5 months ago
end
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
5 months ago
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
2 months ago
-- Hit player1
5 months ago
if v.collider:enter("Player1") then
2 months ago
--print("Player2 hit Player1!")
5 months ago
table.remove(Bullets2, i)
v.collider:destroy()
2 months ago
if UserPlayer1.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1
4 months ago
end
end
2 months ago
-- Hit player2
4 months ago
if v.collider:enter("Player2") then
2 months ago
--print("Player 2 hit themselves!")
4 months ago
table.remove(Bullets2, i)
v.collider:destroy()
if UserPlayer2.health > 0 then
UserPlayer2.health = UserPlayer2.health - 1
end
5 months ago
end
--Hit another bullet
if v.collider:enter("Bullet2") then
table.remove(Bullets2, i)
v.collider:destroy()
end
5 months ago
end
2 months ago
UserPlayer1:handleKeys("w", "s", "a", "d", dt)
UserPlayer2:handleKeys("up", "down", "left", "right", dt)
UserPlayer1:updateCol()
UserPlayer2:updateCol()
5 months ago
UserPlayer1:update(dt)
UserPlayer2:update(dt)
end