diff --git a/DrawGame.lua b/Game/DrawGame.lua similarity index 92% rename from DrawGame.lua rename to Game/DrawGame.lua index d4f3e39..45cb9fd 100644 --- a/DrawGame.lua +++ b/Game/DrawGame.lua @@ -1,10 +1,10 @@ -function drawFPS() +local function drawFPS() love.graphics.setColor(1, 1, 1) -- RGB values for white are (1, 1, 1) love.graphics.setFont(DebugFont) love.graphics.print("FPS: " .. love.timer.getFPS(), 1520, 10) end -function drawHealth() +local function drawHealth() love.graphics.setFont(GameFont) local height = love.graphics.getHeight() local width = love.graphics.getWidth() @@ -15,6 +15,7 @@ end function DrawGame() --STI GameMap:draw() + -- WindField if DebugFlag then World:draw() @@ -27,6 +28,7 @@ function DrawGame() for _, v in ipairs(Bullets2) do v:draw() end + UserPlayer1:draw() UserPlayer2:draw() diff --git a/KeyPressed.lua b/Game/GameKeyPressed.lua similarity index 83% rename from KeyPressed.lua rename to Game/GameKeyPressed.lua index ccfd937..41194db 100644 --- a/KeyPressed.lua +++ b/Game/GameKeyPressed.lua @@ -1,4 +1,4 @@ -function KeyPressed(key) +function GameKeyPressed(key) if key == "escape" then love.event.quit() end diff --git a/UpdateGame.lua b/Game/UpdateGame.lua similarity index 100% rename from UpdateGame.lua rename to Game/UpdateGame.lua diff --git a/Menu/DrawMenu.lua b/Menu/DrawMenu.lua new file mode 100644 index 0000000..b2db24f --- /dev/null +++ b/Menu/DrawMenu.lua @@ -0,0 +1,10 @@ +function DrawMenu() + love.graphics.setFont(GameFont) + local height = love.graphics.getHeight() + local width = love.graphics.getWidth() + love.graphics.setColor(1,1,1) + love.graphics.rectangle("fill", 0, 0, width, height) + love.graphics.setColor(0,0,0) + love.graphics.print("MENU", 100,100) + +end \ No newline at end of file diff --git a/Menu/MenuKeyPressed.lua b/Menu/MenuKeyPressed.lua new file mode 100644 index 0000000..194f8e7 --- /dev/null +++ b/Menu/MenuKeyPressed.lua @@ -0,0 +1,10 @@ +function MenuKeyPressed(key) + if key == "escape" then + love.event.quit() + end + + if key == "space" then + --Change state to GAME! + GAMESTATE = "GAME" + end +end diff --git a/Menu/UpdateMenu.lua b/Menu/UpdateMenu.lua new file mode 100644 index 0000000..aa993fb --- /dev/null +++ b/Menu/UpdateMenu.lua @@ -0,0 +1,2 @@ +function UpdateMenu(dt) +end \ No newline at end of file diff --git a/bullet.lua b/bullet.lua index 049ecff..81f9da7 100644 --- a/bullet.lua +++ b/bullet.lua @@ -1,6 +1,7 @@ Bullet = Object:extend() cos = math.cos sin = math.sin --optimisation +pi = math.pi function Bullet:new(x, y, p, speed, rotation) self.image = love.graphics.newImage("assets/bullet.png") @@ -19,43 +20,44 @@ function Bullet:new(x, y, p, speed, rotation) self.originY = self.height / 2 self.collider = World:newCircleCollider(x, y, 10) + self.collider:setFixedRotation(true) self.collider:setPosition(self.x, self.y) -end - -function Bullet:update(dt) - --New bullets are set to New Collision Class - --this is to make sure the bullet doesn't collide with the player that shot it - if self.p == 1 or self.p == 2 then - self.collider:setCollisionClass("New") - end if self.p == 1 then self.collider:setCollisionClass("Bullet1") elseif self.p == 2 then self.collider:setCollisionClass("Bullet2") end +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 - local dx = cos(self.rotation) * self.speed * dt - local dy = sin(self.rotation) * self.speed * dt - self.collider:setLinearVelocity(dx, dy) - self.x = self.collider:getX() - self.y = self.collider:getY() - - --If a bullet hits a wall, make it bounce off the wall and change its rotation if self.collider:enter("Wall") then - self.rotation = self.rotation + math.pi + --calculate normal of the wall + --calculate the angle of reflection + local angle = (math.atan2(dx, dy)) + --get normal of the wall without using getNormal() + + ----TODO: fix this + + --set the new angle + self.rotation = angle self.collider:setAngle(self.rotation) end + dx = cos(self.rotation) * self.speed * dt + dy = sin(self.rotation) * self.speed * dt + self.collider:setLinearVelocity(dx, dy) end if self.p == 2 then local dx = cos(self.rotation) * self.speed * dt local dy = sin(self.rotation) * self.speed * dt self.collider:setLinearVelocity(dx, dy) - self.x = self.collider:getX() - self.y = self.collider:getY() end + + self.x = self.collider:getX() + self.y = self.collider:getY() end function Bullet:draw() diff --git a/game.love b/game.love index 31fb1f8..910bff4 100644 Binary files a/game.love and b/game.love differ diff --git a/main.lua b/main.lua index 79c072d..5d30b08 100644 --- a/main.lua +++ b/main.lua @@ -1,13 +1,22 @@ +GAMESTATE = "MENU" + 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("DrawGame") - require("KeyPressed") + require("DrawMenu") + + require("GameKeyPressed") + require("MenuKeyPressed") + require("mapsloader") --WindField World = WF.newWorld(0, 0) --no gravity @@ -21,7 +30,8 @@ function love.load() World:addCollisionClass("Wall") World:addCollisionClass("New") -- Used to make sure the bullet doesn't collide with the player that shot it - --STI + --STI Map + --Making the map have collision GameMap = STI("maps/map.lua") Walls = {} if GameMap.layers["Walls"] then @@ -38,8 +48,10 @@ function love.load() DebugFont = love.graphics.newFont("assets/Daydream.ttf", 12) love.graphics.setFont(GameFont) + --Game consts HEALTH = 3 DELAY = 0.5 + MAX = 6 --MAX number of bullets DebugFlag = false EnableKeyPress1 = true KeyPressTime1 = 0 @@ -56,18 +68,41 @@ function love.load() end function love.keypressed(key) - KeyPressed(key) + + if GAMESTATE == "MENU" then + MenuKeyPressed(key) + end + + if GAMESTATE == "GAME" then + GameKeyPressed(key) + end + end function love.update(dt) - UpdateGame(dt) + if GAMESTATE == "MENU" then + UpdateMenu(dt) + end + + if GAMESTATE == "GAME" then + UpdateGame(dt) + end end function love.draw() - DrawGame() - if DebugFlag then - love.graphics.setFont(DebugFont) - love.graphics.print("Debug Mode", 1200, 850) - --love.graphics.print(love.report or "Please wait...") + + --TODO: SWITCH/CASE this! + if GAMESTATE == "MENU" then + DrawMenu() end + + if 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...") + end + end + end diff --git a/maps/map.tmx b/maps/map.tmx index 249f953..c566b29 100644 --- a/maps/map.tmx +++ b/maps/map.tmx @@ -1,5 +1,5 @@ - + @@ -54,5 +54,9 @@ + + + + diff --git a/maps/map1.lua b/maps/map1.lua new file mode 100644 index 0000000..b0b9a47 --- /dev/null +++ b/maps/map1.lua @@ -0,0 +1,1023 @@ +return { + version = "1.10", + luaversion = "5.1", + tiledversion = "1.10.2", + class = "", + orientation = "orthogonal", + renderorder = "right-down", + width = 25, + height = 15, + tilewidth = 64, + tileheight = 64, + nextlayerid = 5, + nextobjectid = 36, + properties = {}, + tilesets = { + { + name = "floor-tiles", + firstgid = 1, + class = "", + tilewidth = 64, + tileheight = 64, + spacing = 0, + margin = 0, + columns = 9, + image = "../assets/tileset.png", + imagewidth = 576, + imageheight = 384, + objectalignment = "unspecified", + tilerendersize = "tile", + fillmode = "stretch", + tileoffset = { + x = 0, + y = 0, + }, + grid = { + orientation = "orthogonal", + width = 64, + height = 64, + }, + properties = {}, + wangsets = {}, + tilecount = 54, + tiles = {}, + }, + }, + layers = { + { + type = "tilelayer", + x = 0, + y = 0, + width = 25, + height = 15, + id = 1, + name = "Floor + Outer Walls", + class = "", + visible = true, + opacity = 1, + offsetx = 0, + offsety = 0, + parallaxx = 1, + parallaxy = 1, + properties = {}, + encoding = "lua", + data = { + 4, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 5, + 6, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 22, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 23, + 24, + }, + }, + { + type = "tilelayer", + x = 0, + y = 0, + width = 25, + height = 15, + id = 2, + name = "Inner Walls", + class = "", + visible = true, + opacity = 1, + offsetx = 0, + offsety = 0, + parallaxx = 1, + parallaxy = 1, + properties = {}, + encoding = "lua", + data = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 7, + 9, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 7, + 8, + 8, + 8, + 9, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 16, + 18, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 25, + 26, + 26, + 26, + 27, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 25, + 27, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 7, + 8, + 9, + 0, + 0, + 7, + 8, + 8, + 8, + 8, + 8, + 9, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 7, + 8, + 17, + 17, + 18, + 0, + 0, + 16, + 17, + 17, + 17, + 17, + 17, + 18, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 16, + 17, + 17, + 26, + 26, + 8, + 8, + 17, + 26, + 26, + 26, + 26, + 26, + 27, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 16, + 17, + 18, + 0, + 0, + 16, + 17, + 18, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 16, + 17, + 18, + 0, + 0, + 25, + 26, + 27, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 7, + 8, + 8, + 8, + 9, + 0, + 0, + 0, + 0, + 16, + 17, + 27, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 7, + 8, + 9, + 0, + 0, + 0, + 16, + 17, + 17, + 17, + 18, + 0, + 0, + 0, + 0, + 25, + 27, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 16, + 17, + 18, + 0, + 0, + 0, + 16, + 17, + 17, + 17, + 18, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 25, + 26, + 27, + 0, + 0, + 0, + 25, + 26, + 26, + 26, + 27, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }, + }, + { + type = "objectgroup", + draworder = "topdown", + id = 4, + name = "Walls", + class = "", + visible = false, + opacity = 1, + offsetx = 0, + offsety = 0, + parallaxx = 1, + parallaxy = 1, + properties = {}, + objects = { + { + id = 10, + name = "", + type = "", + shape = "rectangle", + x = 512, + y = 128, + width = 320, + height = 128, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 11, + name = "", + type = "", + shape = "rectangle", + x = 1344, + y = 64, + width = 128, + height = 192, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 12, + name = "", + type = "", + shape = "rectangle", + x = 1152, + y = 640, + width = 320, + height = 256, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 13, + name = "", + type = "", + shape = "rectangle", + x = 768, + y = 704, + width = 192, + height = 192, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 19, + name = "", + type = "", + shape = "rectangle", + x = 576, + y = 384, + width = 448, + height = 192, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 20, + name = "", + type = "", + shape = "rectangle", + x = 448, + y = 512, + width = 192, + height = 192, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 21, + name = "", + type = "", + shape = "rectangle", + x = 256, + y = 384, + width = 192, + height = 192, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 22, + name = "", + type = "", + shape = "rectangle", + x = 128, + y = 448, + width = 192, + height = 320, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 23, + name = "", + type = "", + shape = "rectangle", + x = 128, + y = 768, + width = 128, + height = 64, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 30, + name = "", + type = "", + shape = "rectangle", + x = 0, + y = 0, + width = 16, + height = 960, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 31, + name = "", + type = "", + shape = "rectangle", + x = 1584, + y = 0, + width = 16, + height = 960, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 32, + name = "", + type = "", + shape = "rectangle", + x = 0, + y = 0, + width = 1600, + height = 16, + rotation = 0, + visible = true, + properties = {}, + }, + { + id = 33, + name = "", + type = "", + shape = "rectangle", + x = 0, + y = 944, + width = 1600, + height = 16, + rotation = 0, + visible = true, + properties = {}, + }, + }, + }, + }, +} diff --git a/mapsloader.lua b/mapsloader.lua new file mode 100644 index 0000000..dc25147 --- /dev/null +++ b/mapsloader.lua @@ -0,0 +1,24 @@ +function LoadMap(lvlnum) + --TODO: FINISH THIS + local mapfilelocation = "maps/" + local extention = ".lua" + + --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 + 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 +end diff --git a/player.lua b/player.lua index c6dfb44..7c1f310 100644 --- a/player.lua +++ b/player.lua @@ -3,13 +3,14 @@ cos = math.cos sin = math.sin --optimisation -- Constructor for the Player class -function Player:new(p, x, y, health, image, speed) +function Player:new(p, x, y, health, image, speed, max) self.p = p self.image = love.graphics.newImage(image) self.x = x self.y = y self.health = health self.speed = speed + self.max = max self.width = self.image:getWidth() self.height = self.image:getHeight() @@ -65,6 +66,7 @@ function Player:update(dt) elseif love.keyboard.isDown("d") then self.rotation = self.rotation + (self.rotSpeed * dt) end + self.collider:setLinearVelocity(self.vx, self.vy) -- Check for collision with walls @@ -82,6 +84,12 @@ function Player:update(dt) 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