Compare commits

..

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

  1. 26
      Game/GameKeyPressed.lua
  2. 22
      Game/UpdateGame.lua
  3. 33
      Menu/DrawMenu.lua
  4. 31
      Pause/DrawPause.lua
  5. 14
      Pause/PauseKeyPressed.lua
  6. 5
      Win/DrawWin.lua
  7. 79
      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)
if key == "escape" then
if not MUTED then
musicBattle:setVolume(0)
musicPause:setVolume(0.6)
else
musicBattle:setVolume(0)
musicPause:setVolume(0)
end
_G.GAMESTATE = "PAUSE"
--print("STATE CHANEGD: PAUSED!")
print("STATE CHANEGD: PAUSED!")
_G.PAUSED = true
end
@ -17,25 +12,19 @@ function GameKeyPressed(key)
DebugFlag = not DebugFlag
end
--TODO: Better restart
if key == "r" and not _G.PAUSED then
RestartGame()
_G.GAMESTATE = "GAME"
love.load()
end
if key == "m" then
MUTED = not MUTED
-- Need to set the battle music to mute
if MUTED then
musicBattle:setVolume(0)
else
musicBattle:setVolume(0.5)
end
end
-- debug map changes!
--[[
if DebugFlag and key == "1" then
UserPlayer1.speed = 50000
print("player1 speed increased!")
_G.CUR_LEVEL = 1
RestartGame()
love.load()
end
if DebugFlag and key == "2" then
_G.CUR_LEVEL = 2
@ -47,4 +36,5 @@ function GameKeyPressed(key)
RestartGame()
love.load()
end
--]]
end

@ -21,7 +21,7 @@ local max = math.max -- optimisations
function UpdateGame(dt)
--Check if anyone has won
if checkLossState() == true then
--print("STATE CHNAGED: WIN!")
print("STATE CHNAGED: WIN!")
_G.GAMESTATE = "WIN"
end
--WindField
@ -57,17 +57,17 @@ function UpdateGame(dt)
-- Hit player2
if v.collider:enter("Player2") then
--print("Player1 hit Player2!")
print("Player1 hit Player2!")
table.remove(Bullets1, i)
v.collider:destroy()
if UserPlayer2.health > 0 then
UserPlayer2.health = UserPlayer2.health - 1
if UserPlayer1.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1
end
end
-- Hit player1
if v.collider:enter("Player1") then
--print("Player 1 hit themselves!")
print("Player 1 hit themselves!")
table.remove(Bullets1, i)
v.collider:destroy()
if UserPlayer1.health > 0 then
@ -100,19 +100,19 @@ function UpdateGame(dt)
v.collider:destroy()
end
-- Hit player1
-- Hit player2
if v.collider:enter("Player1") then
--print("Player2 hit Player1!")
print("Player2 hit Player1!")
table.remove(Bullets2, i)
v.collider:destroy()
if UserPlayer1.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1
if UserPlayer2.health > 0 then
UserPlayer2.health = UserPlayer2.health - 1
end
end
-- Hit player2
-- Hit player1
if v.collider:enter("Player2") then
--print("Player 2 hit themselves!")
print("Player 2 hit themselves!")
table.remove(Bullets2, i)
v.collider:destroy()
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 height = love.graphics.getHeight()
local width = love.graphics.getWidth()
@ -5,16 +28,16 @@ local function title()
love.graphics.setColor(0.5, 1, 1)
love.graphics.rectangle("fill", 0, 0, width, height)
love.graphics.setColor(0, 0, 0)
love.graphics.print("Tanks-A-Lot", 100, 100)
love.graphics.print("MENU", 100, 100)
end
function DrawMenu()
local bwidth, bheight = 300, 140
title()
GUI:newButton(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)
GUI:newButton(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, 200, bwidth, bheight, "Play", MENU_POS == 0 and true or false)
button(100, 350, bwidth, bheight, "???", MENU_POS == 1 and true or false)
button(100, 500, bwidth, bheight, "???", MENU_POS == 2 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
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()
local opacity = 0.3
local height = love.graphics.getHeight()
@ -13,9 +37,8 @@ function DrawPause()
love.graphics.print("PAUSED", 100, 100)
--love.graphics.print("" .. PAUSE_POS, 200,200)
GUI:newButton(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)
GUI:newButton(100, 500, bwidth, bheight, "Quit", PAUSE_POS == 2 and true or false)
button(100, 200, bwidth, bheight, "Return", PAUSE_POS == 0 and true or false)
button(100, 350, bwidth, bheight, "Menu", PAUSE_POS == 1 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
end

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

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

@ -1,9 +1,7 @@
Bullet = Object:extend()
local pi = math.pi
local cos = math.cos
local sin = math.sin
local atan = math.atan
local deg = math.deg
cos = math.cos
sin = math.sin --optimisation
pi = math.pi
function Bullet:new(x, y, p, speed, rotation)
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.collider = World:newCircleCollider(x, y, 10)
--self.collider = World:newRectangleCollider(x, y, 10, 10)
--self.collider:setType("")
self.collider:setFixedRotation(true)
self.collider:setPosition(self.x, self.y)
self.bounce = 0 -- how many times this bullet has bounced
if self.p == 1 then
self.collider:setCollisionClass("Bullet1")
elseif self.p == 2 then
@ -37,69 +31,34 @@ function Bullet:new(x, y, p, speed, rotation)
end
function Bullet:update(dt)
-- Calculate the velocity
local dx = math.cos(self.rotation) * self.speed * dt
local dy = math.sin(self.rotation) * self.speed * dt
-- Check for collisions
--If a bullet hits a wall, make it bounce off the wall and change its rotation
if self.p == 1 then
if self.collider:enter("Wall") then
self.bounce = self.bounce + 1
-- 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)
--calculate normal of the wall
--calculate the angle of reflection
local angle = (math.atan2(dx, dy))
--get normal of the wall without using getNormal()
-- 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("################################")
----TODO: fix this
-- Debug print to verify reflected velocity
--print("Reflected Velocity X:", dx, "Reflected Velocity Y:", dy)
self.rotation = deg(atan(dy, dx))
-- Update the rotation based on the new velocity direction
--self.rotation = deg(atan(dy, dx))
--print("Reflected Angle (radians):", self.rotation)
--print("Reflected Angle (degrees):", self.rotation)
--print("---------------------------------")
--set the new angle
self.rotation = angle
self.collider:setAngle(self.rotation)
end
-- Set the linear velocity of the collider
self.collider:setLinearVelocity(dx, dy)
-- Update position
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
if self.p == 2 then
local dx = cos(self.rotation) * self.speed * dt
local dy = sin(self.rotation) * self.speed * dt
self.collider:setLinearVelocity(dx, dy)
end
-- Update position
self.x = self.collider:getX()
self.y = self.collider:getY()
end
]]
function Bullet:draw()
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()
local cos = math.cos
local sin = math.sin --optimisation
cos = math.cos
sin = math.sin --optimisation
-- Constructor for the Player class
function Player:new(p, x, y, health, image, speed)

@ -51,10 +51,6 @@ end
function RestartGame()
setNewLevelFromRandom(getLevelCount())
--reset wins
_G.P1_WIN = false
_G.P2_WIN = false
-- Stop the music
--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