Compare commits

..

No commits in common. '7da12d303545d940cfdb5a4b78bab599292f201b' and '8440be2ab1162fba986d7ff98350b5ff2f07fe1c' have entirely different histories.

  1. 30
      Game/GameKeyPressed.lua
  2. 22
      Game/UpdateGame.lua
  3. 33
      Menu/DrawMenu.lua
  4. 31
      Pause/DrawPause.lua
  5. 22
      Pause/PauseKeyPressed.lua
  6. 5
      Win/DrawWin.lua
  7. 87
      bullet.lua
  8. 43
      gui.lua
  9. 4
      player.lua
  10. 4
      restart.lua
  11. 6
      truncate.lua

@ -1,15 +1,10 @@
function GameKeyPressed(key) function GameKeyPressed(key)
if key == "escape" then if key == "escape" then
if not MUTED then musicBattle:setVolume(0)
musicBattle:setVolume(0) musicPause:setVolume(0.6)
musicPause:setVolume(0.6)
else
musicBattle:setVolume(0)
musicPause:setVolume(0)
end
_G.GAMESTATE = "PAUSE" _G.GAMESTATE = "PAUSE"
--print("STATE CHANEGD: PAUSED!") print("STATE CHANEGD: PAUSED!")
_G.PAUSED = true _G.PAUSED = true
end end
@ -17,25 +12,19 @@ function GameKeyPressed(key)
DebugFlag = not DebugFlag DebugFlag = not DebugFlag
end end
--TODO: Better restart
if key == "r" and not _G.PAUSED then if key == "r" and not _G.PAUSED then
RestartGame() RestartGame()
_G.GAMESTATE = "GAME" _G.GAMESTATE = "GAME"
love.load() love.load()
end end
if key == "m" then -- debug map changes!
MUTED = not MUTED --[[
-- Need to set the battle music to mute
if MUTED then
musicBattle:setVolume(0)
else
musicBattle:setVolume(0.5)
end
end
if DebugFlag and key == "1" then if DebugFlag and key == "1" then
UserPlayer1.speed = 50000 _G.CUR_LEVEL = 1
print("player1 speed increased!") RestartGame()
love.load()
end end
if DebugFlag and key == "2" then if DebugFlag and key == "2" then
_G.CUR_LEVEL = 2 _G.CUR_LEVEL = 2
@ -47,4 +36,5 @@ function GameKeyPressed(key)
RestartGame() RestartGame()
love.load() love.load()
end end
--]]
end end

@ -21,7 +21,7 @@ local max = math.max -- optimisations
function UpdateGame(dt) function UpdateGame(dt)
--Check if anyone has won --Check if anyone has won
if checkLossState() == true then if checkLossState() == true then
--print("STATE CHNAGED: WIN!") print("STATE CHNAGED: WIN!")
_G.GAMESTATE = "WIN" _G.GAMESTATE = "WIN"
end end
--WindField --WindField
@ -57,17 +57,17 @@ function UpdateGame(dt)
-- Hit player2 -- Hit player2
if v.collider:enter("Player2") then if v.collider:enter("Player2") then
--print("Player1 hit Player2!") print("Player1 hit Player2!")
table.remove(Bullets1, i) table.remove(Bullets1, i)
v.collider:destroy() v.collider:destroy()
if UserPlayer2.health > 0 then if UserPlayer1.health > 0 then
UserPlayer2.health = UserPlayer2.health - 1 UserPlayer1.health = UserPlayer1.health - 1
end end
end end
-- Hit player1 -- Hit player1
if v.collider:enter("Player1") then if v.collider:enter("Player1") then
--print("Player 1 hit themselves!") print("Player 1 hit themselves!")
table.remove(Bullets1, i) table.remove(Bullets1, i)
v.collider:destroy() v.collider:destroy()
if UserPlayer1.health > 0 then if UserPlayer1.health > 0 then
@ -100,19 +100,19 @@ function UpdateGame(dt)
v.collider:destroy() v.collider:destroy()
end end
-- Hit player1 -- Hit player2
if v.collider:enter("Player1") then if v.collider:enter("Player1") then
--print("Player2 hit Player1!") print("Player2 hit Player1!")
table.remove(Bullets2, i) table.remove(Bullets2, i)
v.collider:destroy() v.collider:destroy()
if UserPlayer1.health > 0 then if UserPlayer2.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1 UserPlayer2.health = UserPlayer2.health - 1
end end
end end
-- Hit player2 -- Hit player1
if v.collider:enter("Player2") then if v.collider:enter("Player2") then
--print("Player 2 hit themselves!") print("Player 2 hit themselves!")
table.remove(Bullets2, i) table.remove(Bullets2, i)
v.collider:destroy() v.collider:destroy()
if UserPlayer2.health > 0 then if UserPlayer2.health > 0 then

@ -1,3 +1,26 @@
local function button(x, y, w, h, text, selected)
--x,y is the top left corner of the button
local rounding = 30 -- used for rounding the buttons
if not selected then
love.graphics.setColor(love.math.colorFromBytes(41, 134, 204))
elseif selected then
love.graphics.setColor(love.math.colorFromBytes(244, 67, 54))
end
-- Draw rectangle
love.graphics.rectangle("line", x, y, w, h, rounding, rounding)
-- Get width and height of text
local tw = MenuFont:getWidth(text)
local th = MenuFont:getHeight(text)
-- Calculate position to center the text
local textX = x + (w - tw) / 2
local textY = y + (h - th) / 2
-- Place text inside the rectangle
love.graphics.setFont(MenuFont)
love.graphics.print(text, textX, textY)
end
local function title() local function title()
local height = love.graphics.getHeight() local height = love.graphics.getHeight()
local width = love.graphics.getWidth() local width = love.graphics.getWidth()
@ -5,16 +28,16 @@ local function title()
love.graphics.setColor(0.5, 1, 1) love.graphics.setColor(0.5, 1, 1)
love.graphics.rectangle("fill", 0, 0, width, height) love.graphics.rectangle("fill", 0, 0, width, height)
love.graphics.setColor(0, 0, 0) love.graphics.setColor(0, 0, 0)
love.graphics.print("Tanks-A-Lot", 100, 100) love.graphics.print("MENU", 100, 100)
end end
function DrawMenu() function DrawMenu()
local bwidth, bheight = 300, 140 local bwidth, bheight = 300, 140
title() title()
GUI:newButton(100, 200, bwidth, bheight, "Play", MENU_POS == 0 and true or false) button(100, 200, bwidth, bheight, "Play", MENU_POS == 0 and true or false)
GUI:newButton(100, 350, bwidth, bheight, "???", MENU_POS == 1 and true or false) button(100, 350, bwidth, bheight, "???", MENU_POS == 1 and true or false)
GUI:newButton(100, 500, bwidth, bheight, "???", MENU_POS == 2 and true or false) button(100, 500, bwidth, bheight, "???", MENU_POS == 2 and true or false)
GUI:newButton(100, 650, bwidth, bheight, "Quit", MENU_POS == 3 and true or false) button(100, 650, bwidth, bheight, "Quit", MENU_POS == 3 and true or false)
love.graphics.setColor(255, 255, 255) -- reset colours love.graphics.setColor(255, 255, 255) -- reset colours
end end

@ -1,3 +1,27 @@
local function button(x, y, w, h, text, selected)
--x,y is the top left corner of the button
local rounding = 30 -- used for rounding the buttons
if not selected then
love.graphics.setColor(love.math.colorFromBytes(41, 134, 204))
elseif selected then
love.graphics.setColor(love.math.colorFromBytes(244, 67, 54))
end
-- Draw rectangle
love.graphics.rectangle("fill", x, y, w, h, rounding, rounding)
-- Get width and height of text
local tw = MenuFont:getWidth(text)
local th = MenuFont:getHeight(text)
-- Calculate position to center the text
local textX = x + (w - tw) / 2
local textY = y + (h - th) / 2
-- Place text inside the rectangle
love.graphics.setFont(MenuFont)
love.graphics.setColor(1, 1, 1) -- reset colours
love.graphics.print(text, textX, textY)
end
function DrawPause() function DrawPause()
local opacity = 0.3 local opacity = 0.3
local height = love.graphics.getHeight() local height = love.graphics.getHeight()
@ -13,9 +37,8 @@ function DrawPause()
love.graphics.print("PAUSED", 100, 100) love.graphics.print("PAUSED", 100, 100)
--love.graphics.print("" .. PAUSE_POS, 200,200) --love.graphics.print("" .. PAUSE_POS, 200,200)
GUI:newButton(100, 200, bwidth, bheight, "Return", PAUSE_POS == 0 and true or false) button(100, 200, bwidth, bheight, "Return", PAUSE_POS == 0 and true or false)
GUI:newButton(100, 350, bwidth, bheight, "Menu", PAUSE_POS == 1 and true or false) button(100, 350, bwidth, bheight, "Menu", PAUSE_POS == 1 and true or false)
GUI:newButton(100, 500, bwidth, bheight, "Quit", PAUSE_POS == 2 and true or false) button(100, 500, bwidth, bheight, "Quit", PAUSE_POS == 2 and true or false)
love.graphics.setColor(255, 255, 255) -- reset colours love.graphics.setColor(255, 255, 255) -- reset colours
end end

@ -1,30 +1,20 @@
function PauseKeyPressed(key) function PauseKeyPressed(key)
if key == "return" then if key == "return" then
-- quickly return to the game -- quickly return to the game
if not MUTED then musicBattle:setVolume(0.5)
musicBattle:setVolume(0.5) musicPause:setVolume(0)
musicPause:setVolume(0)
else
musicBattle:setVolume(0)
musicPause:setVolume(0)
end
-- 0 Return to game -- 0 Return to game
-- 1 Quit -- 1 Quit
if PAUSE_POS == 0 then if PAUSE_POS == 0 then
-- unpause the game -- unpause the game
_G.GAMESTATE = "GAME" _G.GAMESTATE = "GAME"
--print("STATE CHANEGD: GAME!") print("STATE CHANEGD: GAME!")
_G.PAUSED = false _G.PAUSED = false
if not MUTED then musicBattle:setVolume(0.5)
musicBattle:setVolume(0.5) musicPause:setVolume(0)
musicPause:setVolume(0)
else
musicBattle:setVolume(0)
musicPause:setVolume(0)
end
elseif PAUSE_POS == 1 then elseif PAUSE_POS == 1 then
_G.GAMESTATE = "MENU" _G.GAMESTATE = "MENU"
--print("STATE CHANEGD: MENU!") print("STATE CHANEGD: MENU!")
_G.PAUSED = false _G.PAUSED = false
musicPause:stop() musicPause:stop()
musicBattle:stop() musicBattle:stop()

@ -4,10 +4,9 @@ local function winner()
local opacity = 0.3 local opacity = 0.3
local s = "" local s = ""
if _G.P1_WIN then if not _G.P1_WIN then
s = "P1 Win! R - Restart" s = "P1 Win! R - Restart"
end elseif not _G.P2_WIN then
if _G.P2_WIN then
s = "P2 Win! R - Restart" s = "P2 Win! R - Restart"
end end

@ -1,9 +1,7 @@
Bullet = Object:extend() Bullet = Object:extend()
local pi = math.pi cos = math.cos
local cos = math.cos sin = math.sin --optimisation
local sin = math.sin pi = math.pi
local atan = math.atan
local deg = math.deg
function Bullet:new(x, y, p, speed, rotation) function Bullet:new(x, y, p, speed, rotation)
self.image = love.graphics.newImage("assets/bullet.png") self.image = love.graphics.newImage("assets/bullet.png")
@ -22,13 +20,9 @@ function Bullet:new(x, y, p, speed, rotation)
self.originY = self.height / 2 self.originY = self.height / 2
self.collider = World:newCircleCollider(x, y, 10) self.collider = World:newCircleCollider(x, y, 10)
--self.collider = World:newRectangleCollider(x, y, 10, 10)
--self.collider:setType("")
self.collider:setFixedRotation(true) self.collider:setFixedRotation(true)
self.collider:setPosition(self.x, self.y) self.collider:setPosition(self.x, self.y)
self.bounce = 0 -- how many times this bullet has bounced
if self.p == 1 then if self.p == 1 then
self.collider:setCollisionClass("Bullet1") self.collider:setCollisionClass("Bullet1")
elseif self.p == 2 then elseif self.p == 2 then
@ -37,69 +31,34 @@ function Bullet:new(x, y, p, speed, rotation)
end end
function Bullet:update(dt) function Bullet:update(dt)
-- Calculate the velocity --If a bullet hits a wall, make it bounce off the wall and change its rotation
local dx = math.cos(self.rotation) * self.speed * dt if self.p == 1 then
local dy = math.sin(self.rotation) * self.speed * dt if self.collider:enter("Wall") then
--calculate normal of the wall
-- Check for collisions --calculate the angle of reflection
if self.collider:enter("Wall") then local angle = (math.atan2(dx, dy))
self.bounce = self.bounce + 1 --get normal of the wall without using getNormal()
-- Get the collision normal
local collision_data = self.collider:getEnterCollisionData("Wall")
local contact = collision_data.contact
local normal_x, normal_y = contact:getNormal()
-- Debug print to verify normal values
--print("---------------------------------")
--print("Collision detected!")
--print("Normal X:", normal_x, "Normal Y:", normal_y)
--print("Initial Velocity X:", dx, "Initial Velocity Y:", dy)
--print("Initial Angle: ", self.rotation)
-- Reflect the velocity using the normal
local dot_product = dx * normal_x + dy * normal_y
dx = dx - 2 * dot_product * normal_x
dy = dy - 2 * dot_product * normal_y
--print("################################")
-- Debug print to verify reflected velocity ----TODO: fix this
--print("Reflected Velocity X:", dx, "Reflected Velocity Y:", dy)
self.rotation = deg(atan(dy, dx))
-- Update the rotation based on the new velocity direction --set the new angle
--self.rotation = deg(atan(dy, dx)) self.rotation = angle
--print("Reflected Angle (radians):", self.rotation) self.collider:setAngle(self.rotation)
--print("Reflected Angle (degrees):", self.rotation) end
--print("---------------------------------") dx = cos(self.rotation) * self.speed * dt
dy = sin(self.rotation) * self.speed * dt
self.collider:setLinearVelocity(dx, dy)
end end
-- Set the linear velocity of the collider if self.p == 2 then
self.collider:setLinearVelocity(dx, dy) local dx = cos(self.rotation) * self.speed * dt
local dy = sin(self.rotation) * self.speed * dt
-- Update position self.collider:setLinearVelocity(dx, dy)
self.x = self.collider:getX()
self.y = self.collider:getY()
end
--[[
function Bullet:update(dt)
local normal_x, normal_y = 0, 0
local dx, dy = 0, 0
if self.p == 1 or self.p == 2 then
if self.collider:enter("Wall") then
self.collider:setLinearVelocity(dx, dy)
else
dx = cos(self.rotation) * self.speed * dt
dy = sin(self.rotation) * self.speed * dt
self.collider:setLinearVelocity(dx, dy)
end
end end
-- Update position
self.x = self.collider:getX() self.x = self.collider:getX()
self.y = self.collider:getY() self.y = self.collider:getY()
end end
]]
function Bullet:draw() function Bullet:draw()
for _, _ in ipairs(Bullets1) do for _, _ in ipairs(Bullets1) do

@ -1,43 +0,0 @@
GUI = Object:extend()
function GUI:new() end
function GUI:newButton(x, y, w, h, text, selected)
love.graphics.setColor(255, 255, 255) -- reset colours
--x,y is the top left corner of the button
local rounding = 30 -- used for rounding the buttons
local xoff = x + 10
local yoff = y - 10
local textX, textY = 0, 0
-- Get width and height of text
local tw = MenuFont:getWidth(text)
local th = MenuFont:getHeight(text)
love.graphics.setFont(MenuFont)
if not selected then
love.graphics.setColor(love.math.colorFromBytes(41, 134, 204))
love.graphics.rectangle("fill", x, y, w, h, rounding, rounding)
-- Calculate position to center the text
textX = x + (w - tw) / 2
textY = y + (h - th) / 2
-- Place text inside the rectangle
love.graphics.setColor(0, 0, 0)
love.graphics.print(text, textX, textY)
elseif selected then
-- If selected, raise the button and draw a shadow
love.graphics.setColor(love.math.colorFromBytes(0, 0, 0))
love.graphics.rectangle("fill", x, y, w, h, rounding, rounding) -- shadow
love.graphics.setColor(love.math.colorFromBytes(244, 67, 54))
love.graphics.rectangle("fill", xoff, yoff, w, h, rounding, rounding) -- button offset
-- Calculate position to center the text
textX = xoff + (w - tw) / 2
textY = yoff + (h - th) / 2
-- Place text inside the rectangle
love.graphics.setColor(1, 1, 1)
love.graphics.print(text, textX, textY)
end
end

@ -1,6 +1,6 @@
Player = Object:extend() Player = Object:extend()
local cos = math.cos cos = math.cos
local sin = math.sin --optimisation sin = math.sin --optimisation
-- Constructor for the Player class -- Constructor for the Player class
function Player:new(p, x, y, health, image, speed) function Player:new(p, x, y, health, image, speed)

@ -51,10 +51,6 @@ end
function RestartGame() function RestartGame()
setNewLevelFromRandom(getLevelCount()) setNewLevelFromRandom(getLevelCount())
--reset wins
_G.P1_WIN = false
_G.P2_WIN = false
-- Stop the music -- Stop the music
--StopAllMusic() --StopAllMusic()

@ -1,6 +0,0 @@
function truncate(number, decdigits)
number = number * (10 ^ decdigits)
number = math.floor(number)
number = number / (10 ^ decdigits)
return number
end
Loading…
Cancel
Save