state changes and a menu
This commit is contained in:
parent
99f0323635
commit
68b76eaaaf
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.DS_Store
|
||||
|
@ -1,6 +1,7 @@
|
||||
function GameKeyPressed(key)
|
||||
if key == "escape" then
|
||||
love.event.quit()
|
||||
_G.GAMESTATE = "MENU"
|
||||
print("STATE CHANEGD: MENU!")
|
||||
end
|
||||
|
||||
if key == "b" then
|
||||
|
@ -1,10 +1,43 @@
|
||||
function DrawMenu()
|
||||
love.graphics.setFont(GameFont)
|
||||
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()
|
||||
love.graphics.setColor(1,1,1)
|
||||
love.graphics.setFont(GameFont)
|
||||
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)
|
||||
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)
|
||||
end
|
@ -1,10 +1,40 @@
|
||||
function MenuKeyPressed(key)
|
||||
if key == "escape" then
|
||||
love.event.quit()
|
||||
if key == 'return' then
|
||||
-- 0 Start Game
|
||||
-- 1 ??
|
||||
-- 2 ???
|
||||
-- 3 Quit
|
||||
if MENU_POS == 0 then
|
||||
--Change state to GAME!
|
||||
_G.GAMESTATE = "GAME"
|
||||
print("STATE CHANEGD: GAME!")
|
||||
|
||||
elseif MENU_POS == 1 then
|
||||
print("STATE CHANEGD: DUNNO!")
|
||||
|
||||
elseif MENU_POS == 2 then
|
||||
print("STATE CHANEGD: DUNNO!")
|
||||
|
||||
elseif MENU_POS == 3 then
|
||||
love.event.quit()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if key == "space" then
|
||||
--Change state to GAME!
|
||||
GAMESTATE = "GAME"
|
||||
if love.keyboard.isDown("up") then
|
||||
if _G.MENU_POS == 0 then
|
||||
_G.MENU_POS = 0
|
||||
else
|
||||
_G.MENU_POS = _G.MENU_POS - 1
|
||||
end
|
||||
end
|
||||
|
||||
if love.keyboard.isDown("down") then
|
||||
if _G.MENU_POS >= _G.MENU_MAX then
|
||||
_G.MENU_POS = _G.MENU_MAX
|
||||
else
|
||||
_G.MENU_POS = _G.MENU_POS + 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
3
constants.lua
Normal file
3
constants.lua
Normal file
@ -0,0 +1,3 @@
|
||||
GAMESTATE = "MENU"
|
||||
MENU_POS = 0
|
||||
MENU_MAX = 3
|
103
main.lua
103
main.lua
@ -1,21 +1,64 @@
|
||||
GAMESTATE = "MENU"
|
||||
require 'constants'
|
||||
|
||||
function love.run()
|
||||
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
|
||||
|
||||
-- We don't want the first frame's dt to include time taken by love.load.
|
||||
if love.timer then love.timer.step() end
|
||||
|
||||
local dt = 0
|
||||
|
||||
-- Main loop time.
|
||||
return function()
|
||||
-- Process events.
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for name, a,b,c,d,e,f in love.event.poll() do
|
||||
if name == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
return a or 0
|
||||
end
|
||||
end
|
||||
love.handlers[name](a,b,c,d,e,f)
|
||||
end
|
||||
end
|
||||
|
||||
-- Update dt, as we'll be passing it to update
|
||||
if love.timer then dt = love.timer.step() end
|
||||
|
||||
-- Call update and draw
|
||||
if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
|
||||
|
||||
if love.graphics and love.graphics.isActive() then
|
||||
love.graphics.origin()
|
||||
love.graphics.clear(love.graphics.getBackgroundColor())
|
||||
|
||||
if love.draw then love.draw() end
|
||||
|
||||
love.graphics.present()
|
||||
end
|
||||
|
||||
if love.timer then love.timer.sleep(0.001) end
|
||||
end
|
||||
end
|
||||
|
||||
function love.load()
|
||||
Object = require("classic")
|
||||
require("player")
|
||||
require("bullet")
|
||||
require("menu")
|
||||
|
||||
WF = require("libs/windfield")
|
||||
STI = require("libs/sti")
|
||||
|
||||
require("UpdateGame")
|
||||
require("UpdateMenu")
|
||||
require("Game/UpdateGame")
|
||||
require("Menu/UpdateMenu")
|
||||
|
||||
require("DrawGame")
|
||||
require("DrawMenu")
|
||||
require("Game/DrawGame")
|
||||
require("Menu/DrawMenu")
|
||||
|
||||
require("Game/GameKeyPressed")
|
||||
require("Menu/MenuKeyPressed")
|
||||
|
||||
require("GameKeyPressed")
|
||||
require("MenuKeyPressed")
|
||||
require("mapsloader")
|
||||
|
||||
--WindField
|
||||
@ -31,31 +74,27 @@ function love.load()
|
||||
World:addCollisionClass("New") -- Used to make sure the bullet doesn't collide with the player that shot it
|
||||
|
||||
--STI Map
|
||||
--Making the map have collision
|
||||
GameMap = STI("maps/map.lua")
|
||||
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)
|
||||
wall:setType("static")
|
||||
table.insert(Walls, wall)
|
||||
Walls[#Walls]:setCollisionClass("Wall")
|
||||
end
|
||||
end
|
||||
loadMap(1)
|
||||
|
||||
--Fonts used in the game
|
||||
GameFont = love.graphics.newFont("assets/Daydream.ttf", 60)
|
||||
DebugFont = love.graphics.newFont("assets/Daydream.ttf", 12)
|
||||
love.graphics.setFont(GameFont)
|
||||
MenuFont = love.graphics.newFont("assets/Daydream.ttf", 45)
|
||||
|
||||
--Game consts
|
||||
HEALTH = 3
|
||||
DELAY = 0.5
|
||||
MAX = 6 --MAX number of bullets
|
||||
MAX = 6 --MAX number of bullets|
|
||||
|
||||
--Bullet lists
|
||||
Bullets1 = {}
|
||||
Bullets2 = {}
|
||||
|
||||
DebugFlag = false
|
||||
EnableKeyPress1 = true
|
||||
KeyPressTime1 = 0
|
||||
KeyDelay1 = DELAY
|
||||
|
||||
EnableKeyPress2 = true
|
||||
KeyPressTime2 = 0
|
||||
KeyDelay2 = DELAY
|
||||
@ -63,45 +102,37 @@ function love.load()
|
||||
local playerSpeed = 12000
|
||||
UserPlayer1 = Player(1, 1000, 100, HEALTH, "assets/player1.png", playerSpeed)
|
||||
UserPlayer2 = Player(2, 200, 300, HEALTH, "assets/player2.png", playerSpeed)
|
||||
Bullets1 = {}
|
||||
Bullets2 = {}
|
||||
end
|
||||
|
||||
function love.keypressed(key)
|
||||
|
||||
if GAMESTATE == "MENU" then
|
||||
if _G.GAMESTATE == "MENU" then
|
||||
MenuKeyPressed(key)
|
||||
end
|
||||
|
||||
if GAMESTATE == "GAME" then
|
||||
if _G.GAMESTATE == "GAME" then
|
||||
GameKeyPressed(key)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
if GAMESTATE == "MENU" then
|
||||
if _G.GAMESTATE == "MENU" then
|
||||
UpdateMenu(dt)
|
||||
end
|
||||
|
||||
if GAMESTATE == "GAME" then
|
||||
if _G.GAMESTATE == "GAME" then
|
||||
UpdateGame(dt)
|
||||
end
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
|
||||
--TODO: SWITCH/CASE this!
|
||||
if GAMESTATE == "MENU" then
|
||||
if _G.GAMESTATE == "MENU" then
|
||||
DrawMenu()
|
||||
end
|
||||
|
||||
if GAMESTATE == "GAME" then
|
||||
if _G.GAMESTATE == "GAME" then
|
||||
DrawGame()
|
||||
if DebugFlag then
|
||||
love.graphics.setFont(DebugFont)
|
||||
love.graphics.print("Debug Mode", 1200, 850)
|
||||
--love.graphics.print(love.report or "Please wait...")
|
||||
love.graphics.print("" .. GAMESTATE, 200, 200)
|
||||
end
|
||||
end
|
||||
|
||||
|
1023
maps/map.lua
1023
maps/map.lua
File diff suppressed because it is too large
Load Diff
@ -1,17 +1,9 @@
|
||||
function LoadMap(lvlnum)
|
||||
--TODO: FINISH THIS
|
||||
function loadMap(lvl)
|
||||
local mapfilelocation = "maps/"
|
||||
local extention = ".lua"
|
||||
local mapname = mapfilelocation .. "map" .. lvl .. extention
|
||||
|
||||
--unload the current map
|
||||
if GameMap then
|
||||
GameMap:removeLayer("Walls")
|
||||
end
|
||||
|
||||
--load the new map
|
||||
GameMap = mapfilelocation .. "map" .. lvlnum .. extention
|
||||
|
||||
--load the new map's walls
|
||||
GameMap = STI(mapname)
|
||||
Walls = {}
|
||||
if GameMap.layers["Walls"] then
|
||||
for _, obj in ipairs(GameMap.layers["Walls"].objects) do
|
||||
@ -21,4 +13,4 @@ function LoadMap(lvlnum)
|
||||
Walls[#Walls]:setCollisionClass("Wall")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
126
player.lua
126
player.lua
@ -47,80 +47,80 @@ function Player:shoot(bulletSpeed)
|
||||
return newBullet
|
||||
end
|
||||
|
||||
function handleKeys(dt)
|
||||
end
|
||||
|
||||
-- Update method for the Player class
|
||||
function Player:update(dt)
|
||||
self.vx = 0
|
||||
self.vy = 0
|
||||
local bulletSpeed = 20000
|
||||
if _G.GAMESTATE == "GAME" then
|
||||
self.vx = 0
|
||||
self.vy = 0
|
||||
local bulletSpeed = 20000
|
||||
|
||||
if self.p == 1 then
|
||||
-- Handle player 1 controls
|
||||
if love.keyboard.isDown("w") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt)
|
||||
self.vy = sin(self.rotation) * (self.speed * dt)
|
||||
elseif love.keyboard.isDown("s") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt) * -1
|
||||
self.vy = sin(self.rotation) * (self.speed * dt) * -1
|
||||
elseif love.keyboard.isDown("a") then
|
||||
self.rotation = self.rotation - (self.rotSpeed * dt)
|
||||
elseif love.keyboard.isDown("d") then
|
||||
self.rotation = self.rotation + (self.rotSpeed * dt)
|
||||
end
|
||||
if self.p == 1 then
|
||||
-- Handle player 1 controls
|
||||
if love.keyboard.isDown("w") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt)
|
||||
self.vy = sin(self.rotation) * (self.speed * dt)
|
||||
elseif love.keyboard.isDown("s") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt) * -1
|
||||
self.vy = sin(self.rotation) * (self.speed * dt) * -1
|
||||
elseif love.keyboard.isDown("a") then
|
||||
self.rotation = self.rotation - (self.rotSpeed * dt)
|
||||
elseif love.keyboard.isDown("d") then
|
||||
self.rotation = self.rotation + (self.rotSpeed * dt)
|
||||
end
|
||||
|
||||
self.collider:setLinearVelocity(self.vx, self.vy)
|
||||
self.collider:setLinearVelocity(self.vx, self.vy)
|
||||
|
||||
-- Check for collision with walls
|
||||
if self.collider:enter("Wall") then
|
||||
print("Player 1 collided with wall")
|
||||
end
|
||||
-- Check for collision with walls
|
||||
if self.collider:enter("Wall") then
|
||||
print("Player 1 collided with wall")
|
||||
end
|
||||
|
||||
if EnableKeyPress1 == true then
|
||||
if love.keyboard.isDown("space") then
|
||||
local newBullet = self:shoot(bulletSpeed)
|
||||
table.insert(Bullets1, newBullet)
|
||||
KeyPressTime1 = KeyDelay1
|
||||
EnableKeyPress1 = false
|
||||
if EnableKeyPress1 == true and GAMESTATE == "GAME" then
|
||||
if love.keyboard.isDown("space") then
|
||||
local newBullet = self:shoot(bulletSpeed)
|
||||
table.insert(Bullets1, newBullet)
|
||||
KeyPressTime1 = KeyDelay1
|
||||
EnableKeyPress1 = false
|
||||
end
|
||||
end
|
||||
self.x = self.collider:getX()
|
||||
self.y = self.collider:getY()
|
||||
|
||||
elseif self.p == 2 then
|
||||
-- Handle player 2 controls
|
||||
if love.keyboard.isDown("up") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt)
|
||||
self.vy = sin(self.rotation) * (self.speed * dt)
|
||||
elseif love.keyboard.isDown("down") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt) * -1
|
||||
self.vy = sin(self.rotation) * (self.speed * dt) * -1
|
||||
elseif love.keyboard.isDown("left") then
|
||||
self.rotation = self.rotation - (self.rotSpeed * dt)
|
||||
elseif love.keyboard.isDown("right") then
|
||||
self.rotation = self.rotation + (self.rotSpeed * dt)
|
||||
end
|
||||
self.collider:setLinearVelocity(self.vx, self.vy)
|
||||
|
||||
-- Check for collision with walls
|
||||
if self.collider:enter("Wall") then
|
||||
print("Player 2 collided with wall")
|
||||
end
|
||||
|
||||
if EnableKeyPress2 == true then
|
||||
if love.keyboard.isDown("return") then
|
||||
local newBullet = self:shoot(bulletSpeed)
|
||||
table.insert(Bullets2, newBullet)
|
||||
KeyPressTime2 = KeyDelay2
|
||||
EnableKeyPress2 = false
|
||||
end
|
||||
end
|
||||
end
|
||||
self.x = self.collider:getX()
|
||||
self.y = self.collider:getY()
|
||||
|
||||
-- Handlle map changes
|
||||
if love.keyboard.isDown("1") then
|
||||
LoadMap("1")
|
||||
print("Map 1 loaded")
|
||||
end
|
||||
elseif self.p == 2 then
|
||||
-- Handle player 2 controls
|
||||
if love.keyboard.isDown("up") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt)
|
||||
self.vy = sin(self.rotation) * (self.speed * dt)
|
||||
elseif love.keyboard.isDown("down") then
|
||||
self.vx = cos(self.rotation) * (self.speed * dt) * -1
|
||||
self.vy = sin(self.rotation) * (self.speed * dt) * -1
|
||||
elseif love.keyboard.isDown("left") then
|
||||
self.rotation = self.rotation - (self.rotSpeed * dt)
|
||||
elseif love.keyboard.isDown("right") then
|
||||
self.rotation = self.rotation + (self.rotSpeed * dt)
|
||||
end
|
||||
self.collider:setLinearVelocity(self.vx, self.vy)
|
||||
|
||||
-- Check for collision with walls
|
||||
if self.collider:enter("Wall") then
|
||||
print("Player 2 collided with wall")
|
||||
end
|
||||
|
||||
if EnableKeyPress2 == true then
|
||||
if love.keyboard.isDown("return") then
|
||||
local newBullet = self:shoot(bulletSpeed)
|
||||
table.insert(Bullets2, newBullet)
|
||||
KeyPressTime2 = KeyDelay2
|
||||
EnableKeyPress2 = false
|
||||
end
|
||||
end
|
||||
end
|
||||
self.x = self.collider:getX()
|
||||
self.y = self.collider:getY()
|
||||
end
|
||||
|
||||
function Player:draw()
|
||||
|
Loading…
x
Reference in New Issue
Block a user