Compare commits

..

11 Commits

Author SHA1 Message Date
Simon Kellet 7da12d3035 added mute 2 months ago
Simon Kellet ebf9369ba5 fixed game logix 2 months ago
Simon Kellet f6be8b2a16 removed btton func 2 months ago
Simon Kellet beb4c28a70 removed button func 2 months ago
Simon Kellet 2d22a50447 added mute 2 months ago
Simon Kellet d059017989 fix win 2 months ago
Simon Kellet 49a57a6e5d better bouncing finally 2 months ago
Simon Kellet f5770cd4e0 ops 2 months ago
Simon Kellet 49633072ac reset wind 2 months ago
Simon Kellet 7af90632fd added truncate method 2 months ago
Simon Kellet e47799650a added start of gui lib 2 months ago
  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,10 +1,15 @@
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
@ -12,19 +17,25 @@ 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
-- debug map changes!
--[[
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
if DebugFlag and key == "1" then
_G.CUR_LEVEL = 1
RestartGame()
love.load()
UserPlayer1.speed = 50000
print("player1 speed increased!")
end
if DebugFlag and key == "2" then
_G.CUR_LEVEL = 2
@ -36,5 +47,4 @@ 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 UserPlayer1.health > 0 then
UserPlayer1.health = UserPlayer1.health - 1
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!")
--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 player2
-- Hit player1
if v.collider:enter("Player1") then
print("Player2 hit Player1!")
--print("Player2 hit Player1!")
table.remove(Bullets2, 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
-- Hit player2
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,26 +1,3 @@
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()
@ -28,16 +5,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("MENU", 100, 100)
love.graphics.print("Tanks-A-Lot", 100, 100)
end
function DrawMenu()
local bwidth, bheight = 300, 140
title()
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)
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)
love.graphics.setColor(255, 255, 255) -- reset colours
end

@ -1,27 +1,3 @@
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()
@ -37,8 +13,9 @@ function DrawPause()
love.graphics.print("PAUSED", 100, 100)
--love.graphics.print("" .. PAUSE_POS, 200,200)
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)
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)
love.graphics.setColor(255, 255, 255) -- reset colours
end

@ -1,20 +1,30 @@
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,9 +4,10 @@ local function winner()
local opacity = 0.3
local s = ""
if not _G.P1_WIN then
if _G.P1_WIN then
s = "P1 Win! R - Restart"
elseif not _G.P2_WIN then
end
if _G.P2_WIN then
s = "P2 Win! R - Restart"
end

@ -1,7 +1,9 @@
Bullet = Object:extend()
cos = math.cos
sin = math.sin --optimisation
pi = math.pi
local pi = math.pi
local cos = math.cos
local sin = math.sin
local atan = math.atan
local deg = math.deg
function Bullet:new(x, y, p, speed, rotation)
self.image = love.graphics.newImage("assets/bullet.png")
@ -20,9 +22,13 @@ 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
@ -31,34 +37,69 @@ function Bullet:new(x, y, p, speed, rotation)
end
function Bullet:update(dt)
--If a bullet hits a wall, make it bounce off the wall and change its rotation
if self.p == 1 then
-- 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 self.collider:enter("Wall") then
--calculate normal of the wall
--calculate the angle of reflection
local angle = (math.atan2(dx, dy))
--get normal of the wall without using getNormal()
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)
----TODO: fix this
-- 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("################################")
--set the new angle
self.rotation = angle
self.collider:setAngle(self.rotation)
-- 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("---------------------------------")
end
dx = cos(self.rotation) * self.speed * dt
dy = sin(self.rotation) * self.speed * dt
-- 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
if self.p == 2 then
local dx = cos(self.rotation) * self.speed * dt
local dy = sin(self.rotation) * self.speed * dt
--[[
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
-- Update position
self.x = self.collider:getX()
self.y = self.collider:getY()
end
]]
function Bullet:draw()
for _, _ in ipairs(Bullets1) do

@ -0,0 +1,43 @@
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()
cos = math.cos
sin = math.sin --optimisation
local cos = math.cos
local sin = math.sin --optimisation
-- Constructor for the Player class
function Player:new(p, x, y, health, image, speed)

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

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