Compare commits

..

5 Commits

  1. 4
      Game/DrawGame.lua
  2. 2
      Game/GameKeyPressed.lua
  3. 11
      Game/UpdateGame.lua
  4. 9
      Menu/MenuKeyPressed.lua
  5. 1
      Pause/PauseKeyPressed.lua
  6. 33
      Win/DrawWin.lua
  7. 1
      Win/UpdateWin.lua
  8. 7
      Win/WinKeyPressed.lua
  9. 8
      constants.lua
  10. BIN
      game.love
  11. 49
      libs/restart.lua
  12. 57
      main.lua
  13. 6
      mapsloader.lua
  14. 8
      player.lua

@ -8,8 +8,8 @@ local function drawHealth()
love.graphics.setFont(GameFont) love.graphics.setFont(GameFont)
local height = love.graphics.getHeight() local height = love.graphics.getHeight()
local width = love.graphics.getWidth() local width = love.graphics.getWidth()
love.graphics.print("" .. UserPlayer1.health, 5, 5) love.graphics.print("P1:" .. UserPlayer1.health, 5, 5)
love.graphics.print("" .. UserPlayer2.health, width - 70, height - 95) love.graphics.print("P2:" .. UserPlayer2.health, width - 200, height - 95)
end end
function DrawGame() function DrawGame()

@ -14,6 +14,8 @@ function GameKeyPressed(key)
--TODO: Better restart --TODO: Better restart
if key == "r" and not _G.PAUSED then if key == "r" and not _G.PAUSED then
RestartGame()
_G.GAMESTATE = "GAME"
love.load() love.load()
end end
end end

@ -1,15 +1,14 @@
local function checkWinState() local function checkWinState()
-- Check P1's health -- Check P1's health
if UserPlayer1.health <= 0 then --P1 win if UserPlayer1.health <= 0 then --P1 win
_G.P1_WIN = true _G.P1_WIN = false
_G.P2_WIN = false _G.P2_WIN = true
UserPlayer1.health = 0
return true return true
end end
if UserPlayer2.health <= 0 then --P2 win if UserPlayer2.health <= 0 then --P2 win
_G.P1_WIN = false _G.P1_WIN = true
_G.P2_WIN = true _G.P2_WIN = false
UserPlayer2.health = 0
return true return true
end end
return false return false

@ -33,4 +33,13 @@ function MenuKeyPressed(key)
_G.MENU_POS = _G.MENU_POS + 1 _G.MENU_POS = _G.MENU_POS + 1
end end
end end
if key == "r" then
musicBattle:stop()
musicMenu:stop()
musicPause:stop()
musicStory:stop()
_G.GAMESTATE = "MENU"
love.load()
end
end end

@ -1,5 +1,6 @@
function PauseKeyPressed(key) function PauseKeyPressed(key)
if key == "return" then if key == "return" then
-- quickly return to the game
musicBattle:setVolume(0.5) musicBattle:setVolume(0.5)
musicPause:setVolume(0) musicPause:setVolume(0)
-- 0 Return to game -- 0 Return to game

@ -0,0 +1,33 @@
local function winner()
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local opacity = 0.3
local s = ""
if _G.P1_WIN then
s = "P1 Win! R - Restart"
elseif _G.P2_WIN then
s = "P2 Win! R - Restart"
end
if _G.P1_WIN or _G.P2_WIN then
-- Overlay
DrawGame() --Draw a single frame of the game
love.graphics.setColor(0.1, 0.1, 0.1, opacity) --overlay opaque img
love.graphics.rectangle("fill", 0, 0, width, height)
-- Who won text
local sw, sh = MenuFont:getWidth(s), GameFont:getHeight(s)
local centreX = width / 2
local centreY = height / 2
love.graphics.setFont(MenuFont)
love.graphics.setColor(1, 1, 1) -- white
love.graphics.print(s, centreX - sw / 2, centreY - sh / 2)
end
end
function DrawWin()
winner()
love.graphics.setColor(255, 255, 255) -- reset colours
end

@ -0,0 +1 @@
function UpdateWin(dt) end

@ -0,0 +1,7 @@
function WinKeyPressed(key)
if key == "r" then
RestartGame()
_G.GAMESTATE = "GAME"
love.load()
end
end

@ -15,6 +15,14 @@ PAUSED = false
PAUSE_POS = 0 PAUSE_POS = 0
PAUSE_MAX = 2 -- 0 resume, 1 menu, 2 quit PAUSE_MAX = 2 -- 0 resume, 1 menu, 2 quit
BULLETS_MAX = 10 -- MAX amount of bullets a player can shoot
--BULLETS_BOUCE_MAX = 7 -- MAX amount of bounces before exploding!
BULLETS_LIFETIME = 50
-- WIN flags for P1 and P2 -- WIN flags for P1 and P2
P1_WIN = false P1_WIN = false
P2_WIN = false P2_WIN = false
-- WIN counts!
P1_COUNT = 0
P2_COUNT = 0

Binary file not shown.

@ -0,0 +1,49 @@
-- Functions to handle restarting
-- all objects
-- Used in map switching and restarting!
function ClearWalls()
for w in pairs(Walls) do
Walls[w] = nil
end
Walls = {}
end
function StopAllMusic()
-- TODO: maybe find a way
-- to find all 'streaming'
-- sounds and then stop them all!
musicBattle:stop()
musicMenu:stop()
musicPause:stop()
--musicStory:stop() -- Unused
end
function ClearPlayerCollision()
UserPlayer1.collider:destroy()
UserPlayer2.collider:destroy()
end
function ClearBullets(bullets)
for i, v in ipairs(bullets) do
v.collider:destroy()
end
end
function RestartGame()
-- Stop the music
StopAllMusic()
-- Clear the players collision
ClearPlayerCollision()
-- Work through and delete all bullets
ClearBullets(Bullets1)
ClearBullets(Bullets2)
--Clear Walls
--ClearWalls()
-- Done!
end

@ -1,4 +1,14 @@
require("constants") require("constants")
WF = require("libs/windfield")
STI = require("libs/sti")
World = WF.newWorld(0, 0) --no gravity
World:setQueryDebugDrawing(true) -- Draws the area of a query for 10 frames
World:addCollisionClass("Player1")
World:addCollisionClass("Bullet1")
World:addCollisionClass("Player2")
World:addCollisionClass("Bullet2")
World:addCollisionClass("Wall")
function love.run() function love.run()
if love.load then if love.load then
@ -56,40 +66,25 @@ end
function love.load() function love.load()
Object = require("libs/classic") Object = require("libs/classic")
require("libs/restart")
require("player") require("player")
require("bullet") require("bullet")
require("mapsloader")
WF = require("libs/windfield")
STI = require("libs/sti")
require("Game/UpdateGame") require("Game/UpdateGame")
require("Menu/UpdateMenu") require("Menu/UpdateMenu")
require("Pause/UpdatePause") require("Pause/UpdatePause")
require("Win/UpdateWin")
require("Game/DrawGame") require("Game/DrawGame")
require("Menu/DrawMenu") require("Menu/DrawMenu")
require("Pause/DrawPause") require("Pause/DrawPause")
require("Win/DrawWin")
require("Game/GameKeyPressed") require("Game/GameKeyPressed")
require("Menu/MenuKeyPressed") require("Menu/MenuKeyPressed")
require("Pause/PauseKeyPressed") require("Pause/PauseKeyPressed")
require("Win/WinKeyPressed")
require("mapsloader")
--WindField
World = WF.newWorld(0, 0) --no gravity
World:setQueryDebugDrawing(true) -- Draws the area of a query for 10 frames
World:addCollisionClass("Player1")
World:addCollisionClass("Bullet1")
World:addCollisionClass("Player2")
World:addCollisionClass("Bullet2")
World:addCollisionClass("Wall")
World:addCollisionClass("New") -- Used to make sure the bullet doesn't collide with the player that shot it
--STI Map
loadMap(1)
--Fonts used in the game --Fonts used in the game
GameFont = love.graphics.newFont("assets/Daydream.ttf", 60) GameFont = love.graphics.newFont("assets/Daydream.ttf", 60)
@ -99,12 +94,13 @@ function love.load()
--Game consts --Game consts
HEALTH = 3 HEALTH = 3
DELAY = 0.5 DELAY = 0.5
MAX = 6 --MAX number of bullets|
--Bullet lists --Bullet lists
Bullets1 = {} Bullets1 = {}
Bullets2 = {} Bullets2 = {}
Walls = {}
DebugFlag = false DebugFlag = false
EnableKeyPress1 = true EnableKeyPress1 = true
KeyPressTime1 = 0 KeyPressTime1 = 0
@ -119,9 +115,9 @@ function love.load()
UserPlayer2 = Player(2, 200, 300, HEALTH, "assets/player2.png", playerSpeed) UserPlayer2 = Player(2, 200, 300, HEALTH, "assets/player2.png", playerSpeed)
-- Music streaming -- Music streaming
musicMenu = love.audio.newSource("music/menu.mp3", "stream") musicMenu = love.audio.newSource("music/menu.mp3", "stream") or nil
musicBattle = love.audio.newSource("music/battle.mp3", "stream") musicBattle = love.audio.newSource("music/battle.mp3", "stream") or nil
musicStory = love.audio.newSource("music/story.mp3", "stream") --musicStory = love.audio.newSource("music/story.mp3", "stream") or nil
musicPause = musicBattle:clone() musicPause = musicBattle:clone()
musicPause:setFilter({ musicPause:setFilter({
@ -129,6 +125,9 @@ function love.load()
volume = 0.7, volume = 0.7,
highgain = 0.4, highgain = 0.4,
}) })
--STI Map loading
LoadMap(1)
end end
function love.keypressed(key) function love.keypressed(key)
@ -141,6 +140,9 @@ function love.keypressed(key)
if _G.GAMESTATE == "GAME" then if _G.GAMESTATE == "GAME" then
GameKeyPressed(key) GameKeyPressed(key)
end end
if _G.GAMESTATE == "WIN" then
WinKeyPressed(key)
end
end end
function love.update(dt) function love.update(dt)
@ -173,9 +175,7 @@ function love.update(dt)
end end
end end
if _G.GAMESTATE == "WIN" then if _G.GAMESTATE == "WIN" then
print(P1_WIN) UpdateWin(dt)
print(P2_WIN)
love.event.quit()
end end
end end
@ -194,4 +194,7 @@ function love.draw()
love.graphics.print("" .. GAMESTATE, 200, 200) love.graphics.print("" .. GAMESTATE, 200, 200)
end end
end end
if _G.GAMESTATE == "WIN" then
DrawWin()
end
end end

@ -1,10 +1,12 @@
function loadMap(lvl) function LoadMap(lvl)
ClearWalls()
local mapfilelocation = "maps/" local mapfilelocation = "maps/"
local extention = ".lua" local extention = ".lua"
local mapname = mapfilelocation .. "map" .. lvl .. extention local mapname = mapfilelocation .. "map" .. lvl .. extention
GameMap = STI(mapname) GameMap = STI(mapname)
Walls = {} --Walls = {}
if GameMap.layers["Walls"] then if GameMap.layers["Walls"] then
for _, obj in ipairs(GameMap.layers["Walls"].objects) do for _, obj in ipairs(GameMap.layers["Walls"].objects) do
local wall = World:newRectangleCollider(obj.x, obj.y, obj.width, obj.height) local wall = World:newRectangleCollider(obj.x, obj.y, obj.width, obj.height)

@ -35,16 +35,24 @@ function Player:new(p, x, y, health, image, speed, max)
--Velocity --Velocity
self.vx = 0 self.vx = 0
self.vy = 0 self.vy = 0
-- Bullets shot
self.shot = 0
end end
-- Method to handle shooting -- Method to handle shooting
function Player:shoot(bulletSpeed) function Player:shoot(bulletSpeed)
if self.shot <= 10 then
self.shot = self.shot + 1
local offsetX = cos(self.rotation) * self.width * 1.5 local offsetX = cos(self.rotation) * self.width * 1.5
local offsetY = sin(self.rotation) * self.height * 1.5 local offsetY = sin(self.rotation) * self.height * 1.5
local bulletX = self.x + offsetX local bulletX = self.x + offsetX
local bulletY = self.y + offsetY local bulletY = self.y + offsetY
local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation) local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation)
return newBullet return newBullet
else
return nil
end
end end
function Player:handleKeys(up, down, left, right, dt) function Player:handleKeys(up, down, left, right, dt)

Loading…
Cancel
Save