Compare commits
	
		
			5 Commits
		
	
	
		
			6049054a26
			...
			9e968b9390
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					9e968b9390 | ||
| 
						 | 
					cdc0f35f22 | ||
| 
						 | 
					e83ee4ec64 | ||
| 
						 | 
					7b68420027 | ||
| 
						 | 
					5f915f617e | 
@ -8,8 +8,8 @@ local function drawHealth()
 | 
			
		||||
	love.graphics.setFont(GameFont)
 | 
			
		||||
	local height = love.graphics.getHeight()
 | 
			
		||||
	local width = love.graphics.getWidth()
 | 
			
		||||
	love.graphics.print("" .. UserPlayer1.health, 5, 5)
 | 
			
		||||
	love.graphics.print("" .. UserPlayer2.health, width - 70, height - 95)
 | 
			
		||||
	love.graphics.print("P1:" .. UserPlayer1.health, 5, 5)
 | 
			
		||||
	love.graphics.print("P2:" .. UserPlayer2.health, width - 200, height - 95)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function DrawGame()
 | 
			
		||||
 | 
			
		||||
@ -14,6 +14,8 @@ function GameKeyPressed(key)
 | 
			
		||||
 | 
			
		||||
	--TODO: Better restart
 | 
			
		||||
	if key == "r" and not _G.PAUSED then
 | 
			
		||||
		RestartGame()
 | 
			
		||||
		_G.GAMESTATE = "GAME"
 | 
			
		||||
		love.load()
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,15 +1,14 @@
 | 
			
		||||
local function checkWinState()
 | 
			
		||||
	-- Check P1's health
 | 
			
		||||
	if UserPlayer1.health <= 0 then --P1 win
 | 
			
		||||
		_G.P1_WIN = true
 | 
			
		||||
		_G.P2_WIN = false
 | 
			
		||||
		UserPlayer1.health = 0
 | 
			
		||||
		return true
 | 
			
		||||
	end
 | 
			
		||||
	if UserPlayer2.health <= 0 then --P2 win
 | 
			
		||||
		_G.P1_WIN = false
 | 
			
		||||
		_G.P2_WIN = true
 | 
			
		||||
		UserPlayer2.health = 0
 | 
			
		||||
		return true
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	if UserPlayer2.health <= 0 then --P2 win
 | 
			
		||||
		_G.P1_WIN = true
 | 
			
		||||
		_G.P2_WIN = false
 | 
			
		||||
		return true
 | 
			
		||||
	end
 | 
			
		||||
	return false
 | 
			
		||||
 | 
			
		||||
@ -33,4 +33,13 @@ function MenuKeyPressed(key)
 | 
			
		||||
			_G.MENU_POS = _G.MENU_POS + 1
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	if key == "r" then
 | 
			
		||||
		musicBattle:stop()
 | 
			
		||||
		musicMenu:stop()
 | 
			
		||||
		musicPause:stop()
 | 
			
		||||
		musicStory:stop()
 | 
			
		||||
		_G.GAMESTATE = "MENU"
 | 
			
		||||
		love.load()
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
function PauseKeyPressed(key)
 | 
			
		||||
	if key == "return" then
 | 
			
		||||
		-- quickly return to the game
 | 
			
		||||
		musicBattle:setVolume(0.5)
 | 
			
		||||
		musicPause:setVolume(0)
 | 
			
		||||
		-- 0 Return to game
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										33
									
								
								Win/DrawWin.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								Win/DrawWin.lua
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
							
								
								
									
										1
									
								
								Win/UpdateWin.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Win/UpdateWin.lua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
function UpdateWin(dt) end
 | 
			
		||||
							
								
								
									
										7
									
								
								Win/WinKeyPressed.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								Win/WinKeyPressed.lua
									
									
									
									
									
										Normal file
									
								
							@ -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_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
 | 
			
		||||
P1_WIN = false
 | 
			
		||||
P2_WIN = false
 | 
			
		||||
 | 
			
		||||
-- WIN counts!
 | 
			
		||||
P1_COUNT = 0
 | 
			
		||||
P2_COUNT = 0
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										49
									
								
								libs/restart.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								libs/restart.lua
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
							
								
								
									
										57
									
								
								main.lua
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								main.lua
									
									
									
									
									
								
							@ -1,4 +1,14 @@
 | 
			
		||||
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()
 | 
			
		||||
	if love.load then
 | 
			
		||||
@ -56,40 +66,25 @@ end
 | 
			
		||||
 | 
			
		||||
function love.load()
 | 
			
		||||
	Object = require("libs/classic")
 | 
			
		||||
	require("libs/restart")
 | 
			
		||||
	require("player")
 | 
			
		||||
	require("bullet")
 | 
			
		||||
 | 
			
		||||
	WF = require("libs/windfield")
 | 
			
		||||
	STI = require("libs/sti")
 | 
			
		||||
	require("mapsloader")
 | 
			
		||||
 | 
			
		||||
	require("Game/UpdateGame")
 | 
			
		||||
	require("Menu/UpdateMenu")
 | 
			
		||||
	require("Pause/UpdatePause")
 | 
			
		||||
	require("Win/UpdateWin")
 | 
			
		||||
 | 
			
		||||
	require("Game/DrawGame")
 | 
			
		||||
	require("Menu/DrawMenu")
 | 
			
		||||
	require("Pause/DrawPause")
 | 
			
		||||
	require("Win/DrawWin")
 | 
			
		||||
 | 
			
		||||
	require("Game/GameKeyPressed")
 | 
			
		||||
	require("Menu/MenuKeyPressed")
 | 
			
		||||
	require("Pause/PauseKeyPressed")
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
	require("Win/WinKeyPressed")
 | 
			
		||||
 | 
			
		||||
	--Fonts used in the game
 | 
			
		||||
	GameFont = love.graphics.newFont("assets/Daydream.ttf", 60)
 | 
			
		||||
@ -99,12 +94,13 @@ function love.load()
 | 
			
		||||
	--Game consts
 | 
			
		||||
	HEALTH = 3
 | 
			
		||||
	DELAY = 0.5
 | 
			
		||||
	MAX = 6 --MAX number of bullets|
 | 
			
		||||
 | 
			
		||||
	--Bullet lists
 | 
			
		||||
	Bullets1 = {}
 | 
			
		||||
	Bullets2 = {}
 | 
			
		||||
 | 
			
		||||
	Walls = {}
 | 
			
		||||
 | 
			
		||||
	DebugFlag = false
 | 
			
		||||
	EnableKeyPress1 = true
 | 
			
		||||
	KeyPressTime1 = 0
 | 
			
		||||
@ -119,9 +115,9 @@ function love.load()
 | 
			
		||||
	UserPlayer2 = Player(2, 200, 300, HEALTH, "assets/player2.png", playerSpeed)
 | 
			
		||||
 | 
			
		||||
	-- Music streaming
 | 
			
		||||
	musicMenu = love.audio.newSource("music/menu.mp3", "stream")
 | 
			
		||||
	musicBattle = love.audio.newSource("music/battle.mp3", "stream")
 | 
			
		||||
	musicStory = love.audio.newSource("music/story.mp3", "stream")
 | 
			
		||||
	musicMenu = love.audio.newSource("music/menu.mp3", "stream") or nil
 | 
			
		||||
	musicBattle = love.audio.newSource("music/battle.mp3", "stream") or nil
 | 
			
		||||
	--musicStory = love.audio.newSource("music/story.mp3", "stream") or nil
 | 
			
		||||
 | 
			
		||||
	musicPause = musicBattle:clone()
 | 
			
		||||
	musicPause:setFilter({
 | 
			
		||||
@ -129,6 +125,9 @@ function love.load()
 | 
			
		||||
		volume = 0.7,
 | 
			
		||||
		highgain = 0.4,
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	--STI Map loading
 | 
			
		||||
	LoadMap(1)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function love.keypressed(key)
 | 
			
		||||
@ -141,6 +140,9 @@ function love.keypressed(key)
 | 
			
		||||
	if _G.GAMESTATE == "GAME" then
 | 
			
		||||
		GameKeyPressed(key)
 | 
			
		||||
	end
 | 
			
		||||
	if _G.GAMESTATE == "WIN" then
 | 
			
		||||
		WinKeyPressed(key)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function love.update(dt)
 | 
			
		||||
@ -173,9 +175,7 @@ function love.update(dt)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	if _G.GAMESTATE == "WIN" then
 | 
			
		||||
		print(P1_WIN)
 | 
			
		||||
		print(P2_WIN)
 | 
			
		||||
		love.event.quit()
 | 
			
		||||
		UpdateWin(dt)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -194,4 +194,7 @@ function love.draw()
 | 
			
		||||
			love.graphics.print("" .. GAMESTATE, 200, 200)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	if _G.GAMESTATE == "WIN" then
 | 
			
		||||
		DrawWin()
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,12 @@
 | 
			
		||||
function loadMap(lvl)
 | 
			
		||||
function LoadMap(lvl)
 | 
			
		||||
	ClearWalls()
 | 
			
		||||
 | 
			
		||||
	local mapfilelocation = "maps/"
 | 
			
		||||
	local extention = ".lua"
 | 
			
		||||
	local mapname = mapfilelocation .. "map" .. lvl .. extention
 | 
			
		||||
 | 
			
		||||
	GameMap = STI(mapname)
 | 
			
		||||
	Walls = {}
 | 
			
		||||
	--Walls = {}
 | 
			
		||||
	if GameMap.layers["Walls"] then
 | 
			
		||||
		for _, obj in ipairs(GameMap.layers["Walls"].objects) do
 | 
			
		||||
			local wall = World:newRectangleCollider(obj.x, obj.y, obj.width, obj.height)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								player.lua
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								player.lua
									
									
									
									
									
								
							@ -35,16 +35,24 @@ function Player:new(p, x, y, health, image, speed, max)
 | 
			
		||||
	--Velocity
 | 
			
		||||
	self.vx = 0
 | 
			
		||||
	self.vy = 0
 | 
			
		||||
 | 
			
		||||
	-- Bullets shot
 | 
			
		||||
	self.shot = 0
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- Method to handle shooting
 | 
			
		||||
function Player:shoot(bulletSpeed)
 | 
			
		||||
	local offsetX = cos(self.rotation) * self.width * 1.5
 | 
			
		||||
	local offsetY = sin(self.rotation) * self.height * 1.5
 | 
			
		||||
	local bulletX = self.x + offsetX
 | 
			
		||||
	local bulletY = self.y + offsetY
 | 
			
		||||
	local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation)
 | 
			
		||||
	return newBullet
 | 
			
		||||
	if self.shot <= 10 then
 | 
			
		||||
		self.shot = self.shot + 1
 | 
			
		||||
		local offsetX = cos(self.rotation) * self.width * 1.5
 | 
			
		||||
		local offsetY = sin(self.rotation) * self.height * 1.5
 | 
			
		||||
		local bulletX = self.x + offsetX
 | 
			
		||||
		local bulletY = self.y + offsetY
 | 
			
		||||
		local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation)
 | 
			
		||||
		return newBullet
 | 
			
		||||
	else
 | 
			
		||||
		return nil
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function Player:handleKeys(up, down, left, right, dt)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user