state changes and a menu

main
Simon Kellet 4 months ago
parent 99f0323635
commit 68b76eaaaf
  1. 2
      .gitignore
  2. 3
      Game/GameKeyPressed.lua
  3. 39
      Menu/DrawMenu.lua
  4. 38
      Menu/MenuKeyPressed.lua
  5. 3
      constants.lua
  6. BIN
      game.love
  7. 103
      main.lua
  8. 1023
      maps/map.lua
  9. 14
      mapsloader.lua
  10. 12
      player.lua

2
.gitignore vendored

@ -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
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
if key == "space" then
--Change state to GAME!
GAMESTATE = "GAME"
end
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

@ -0,0 +1,3 @@
GAMESTATE = "MENU"
MENU_POS = 0
MENU_MAX = 3

Binary file not shown.

@ -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("Game/DrawGame")
require("Menu/DrawMenu")
require("DrawGame")
require("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

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

@ -47,8 +47,12 @@ function Player:shoot(bulletSpeed)
return newBullet
end
function handleKeys(dt)
end
-- Update method for the Player class
function Player:update(dt)
if _G.GAMESTATE == "GAME" then
self.vx = 0
self.vy = 0
local bulletSpeed = 20000
@ -74,7 +78,7 @@ function Player:update(dt)
print("Player 1 collided with wall")
end
if EnableKeyPress1 == true then
if EnableKeyPress1 == true and GAMESTATE == "GAME" then
if love.keyboard.isDown("space") then
local newBullet = self:shoot(bulletSpeed)
table.insert(Bullets1, newBullet)
@ -85,11 +89,6 @@ function Player:update(dt)
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
@ -121,6 +120,7 @@ function Player:update(dt)
end
self.x = self.collider:getX()
self.y = self.collider:getY()
end
end
function Player:draw()

Loading…
Cancel
Save