From b8106bc1aacffc1b31851af7b55050924ee45480 Mon Sep 17 00:00:00 2001 From: Simon Kellet Date: Wed, 24 Jul 2024 15:57:59 +0100 Subject: [PATCH] deletes bullets after bouncing so many times --- Game/UpdateGame.lua | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Game/UpdateGame.lua b/Game/UpdateGame.lua index 1dc9441..2c30460 100644 --- a/Game/UpdateGame.lua +++ b/Game/UpdateGame.lua @@ -39,6 +39,14 @@ function UpdateGame(dt) 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() @@ -46,6 +54,8 @@ function UpdateGame(dt) table.remove(Bullets1, i) v.collider:destroy() end + + -- Hit player2 if v.collider:enter("Player2") then print("Player1 hit Player2!") table.remove(Bullets1, i) @@ -55,6 +65,7 @@ function UpdateGame(dt) end end + -- Hit player1 if v.collider:enter("Player1") then print("Player 1 hit themselves!") table.remove(Bullets1, i) @@ -63,10 +74,24 @@ function UpdateGame(dt) 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 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() @@ -75,6 +100,7 @@ function UpdateGame(dt) v.collider:destroy() end + -- Hit player2 if v.collider:enter("Player1") then print("Player2 hit Player1!") table.remove(Bullets2, i) @@ -83,16 +109,24 @@ function UpdateGame(dt) UserPlayer2.health = UserPlayer2.health - 1 end end + + -- Hit player1 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 + UserPlayer1:handleKeys("w", "s", "a", "d", dt) UserPlayer2:handleKeys("up", "down", "left", "right", dt) UserPlayer1:updateCol()