From 5643fc0140a8cd43b3450d8a75c9023a5e0ba4df Mon Sep 17 00:00:00 2001 From: binarycat Date: Fri, 4 Mar 2022 14:41:45 -0500 Subject: [PATCH] added rooms --- code/camera.lua | 31 ++++++++++++++++++++++++++- code/collision.lua | 18 ++++++++++++++++ code/debug.lua | 18 ++++++++++++++++ code/editor.lua | 53 +++++++++++++++++++++++++++++++++++++++++----- code/entity.lua | 8 +++++-- code/game.lua | 3 ++- code/keybind.lua | 3 ++- main.lua | 2 ++ 8 files changed, 126 insertions(+), 10 deletions(-) diff --git a/code/camera.lua b/code/camera.lua index 750e11f..899035b 100644 --- a/code/camera.lua +++ b/code/camera.lua @@ -4,6 +4,35 @@ Camera = { height = 0 } +function Camera:followPlayer(player) + local pos = player.pos + local room = player:getCollidingAt(pos.x,pos.y,LoadedObjects.Rooms) + + self:positionCenterAt(pos.x, pos.y) + + self:confineTo(room) +end + +function Camera:confineTo(box) + if box == nil then + --frameDebug("not in a room") + return + end + --frameDebug("in a room") + + local w = self.width/game.scale + local h = self.height/game.scale + + -- bottom edge + self.pos.y = math.min(self.pos.y+h, box.to.y)-h + -- right edge + self.pos.x = math.min(self.pos.x+w, box.to.x)-w + -- top edge + self.pos.y = math.max(self.pos.y, box.from.y) + -- left edge + self.pos.x = math.max(self.pos.x, box.from.x) +end + function Camera:ConfineToLevel() self.pos.x = math.max(0,math.min(self.pos.x,LevelData.Width-self.width/game.scale)) self.pos.y = math.max(0,math.min(self.pos.y,LevelData.Height-self.height/game.scale)) @@ -12,7 +41,7 @@ end function Camera:positionCenterAt(x,y) self.pos.x = x-self.width/game.scale/2 self.pos.y = y-self.height/game.scale/2 - self:ConfineToLevel() + --self:ConfineToLevel() end function Camera:positionAt(x,y) diff --git a/code/collision.lua b/code/collision.lua index 688bf50..7e22aa7 100644 --- a/code/collision.lua +++ b/code/collision.lua @@ -4,6 +4,8 @@ LoadedObjects.Collisions = {} LoadedObjects.Platforms = {} LoadedObjects.Ladders = {} LoadedObjects.Hazards = {} +LoadedObjects.Rooms = {} + --[[ Collision @@ -51,6 +53,8 @@ function Collision:New(ox,oy,tx,ty) return o end + + function Collision:CenterAt(x, y) self.from.x = x-self.width/2 self.from.y = y-self.height/2 @@ -65,6 +69,13 @@ function Collision:PlaceAt(x, y) self.to.y = self.from.x + self.height end +function Collision:ContainsPoint(x, y) + return x => self.from.x and + y >= self.from.y and + x <= self.to.x and + y <= self.to.y +end + function Collision:Draw(color) if self.isColliding == true then love.graphics.setColor(0,1,0,0.5) @@ -77,3 +88,10 @@ function Collision:Draw(color) love.graphics.setColor(0,1,90,0.5) love.graphics.rectangle("line",self.from.x-Camera.pos.x, self.from.y-Camera.pos.y, self.width, self.height) end + +function DrawRooms() + for _, room in pairs(LoadedObjects.Rooms) do + love.graphics.setColor(0,0,100,1) + love.graphics.rectangle("line",room.from.x-Camera.pos.x, room.from.y-Camera.pos.y, room.width, room.height) + end +end diff --git a/code/debug.lua b/code/debug.lua index 4c5ed2a..8807690 100644 --- a/code/debug.lua +++ b/code/debug.lua @@ -93,3 +93,21 @@ end function logWrite(string) if logging then logFile:write(string.."\n") end end + +local frameDebug_lines = {} + +-- used for debug output that will be printed every frame +function frameDebug(str) + table.insert(frameDebug_lines, str) +end + +-- called at the end of each frame, draw everything passed to frameDebug this frame +function frameDebugFlush() + local y = 0 + love.graphics.setColor(100, 100, 100, 0.8) + for _, str in ipairs(frameDebug_lines) do + love.graphics.print(str, 2, y) + y = y + 10 + end + frameDebug_lines = {} +end \ No newline at end of file diff --git a/code/editor.lua b/code/editor.lua index 64422de..e31e5d7 100644 --- a/code/editor.lua +++ b/code/editor.lua @@ -1,6 +1,17 @@ +assert(editor == nil) +editor = { room_mode = false } + function EditorStep() palette = palette or false AnimateTiles() + if Keybind:CheckPressed(Keybind.editor.room_mode) then + if love.keyboard.isDown("lshift") then + editor.room_mode = "delete" + else + editor.room_mode = not editor.room_mode + end + editor.room_points = {} + end if Keybind:CheckPressed(Keybind.editor.palette) then if palette then @@ -13,17 +24,18 @@ function EditorStep() palette_scroll_y = 0 end end + -- TODO: if love.keyboard.isDown('a',"left") then - Camera.pos.x = Camera.pos.x - 3*game.scale + Camera.pos.x = Camera.pos.x - 3/game.scale end if love.keyboard.isDown('d',"right") then - Camera.pos.x = Camera.pos.x + 3*game.scale + Camera.pos.x = Camera.pos.x + 3/game.scale end if love.keyboard.isDown("up", "w") then - Camera.pos.y = Camera.pos.y - 3*game.scale + Camera.pos.y = Camera.pos.y - 3/game.scale end if love.keyboard.isDown("down", "s") then - Camera.pos.y = Camera.pos.y + 3*game.scale + Camera.pos.y = Camera.pos.y + 3/game.scale end if palette then @@ -83,6 +95,8 @@ function EditorDraw() GameworldDrawEnd() EditorDoEdit() + DrawRooms() + DrawSelectingPaletteTile() if palette then EditorDoPalette() @@ -109,10 +123,39 @@ function EditorDoEdit() elseif vertical < 0 then expand_v = vertical end + love.graphics.setColor(100, 100, 100, 0.8) love.graphics.print("> " .. horizontal .. ", " .. vertical .. "; " .. math.floor(mouse_x / game.scale + Camera.pos.x) .. ", " .. math.floor(mouse_y / game.scale + Camera.pos.y)) love.graphics.print("> " .. LevelWidth .. "(" .. expand_h .. "), " .. LevelHeight .. "(".. expand_v .. ")", 0, 10) - if not palette then + if editor.room_mode then + local rx = horizontal * tileProperties.width + local ry = vertical * tileProperties.height + local r = editor.room_points + if Keybind:CheckPressed(Keybind.generic.rclick) then + editor.room_points = {} + elseif Keybind:CheckPressed(Keybind.generic.lclick) then + if editor.room_mode == "delete" then + for i, room in ipairs(LoadedObjects.Rooms) do + if room:ContainsPoint(rx, ry) then + table.remove(LoadedObjects.Rooms, i) + end + end + else + table.insert(r, { x = rx, y = ry }) + end + end + if #editor.room_points == 2 then + table.insert(LoadedObjects.Rooms, Collision:New(r[1].x,r[1].y,r[2].x,r[2].y)) + editor.room_points = {} + end + if editor.room_mode == "delete" then + love.graphics.print("Select room to delete", 0, 20) + elseif #editor.room_points == 0 then + love.graphics.print("Select top left of new room", 0, 20) + else + love.graphics.print("Select bottom right of new room", 0, 20) + end + elseif not palette then if LevelTiles[vertical] ~= nil and LevelTiles[vertical][horizontal] ~= nil and love.keyboard.isDown("lshift") ~= true diff --git a/code/entity.lua b/code/entity.lua index 339ad2e..7c3fc9f 100644 --- a/code/entity.lua +++ b/code/entity.lua @@ -162,6 +162,10 @@ end -- checks if the the reciever would collide with an object if it was positioned at the given point. -- also marks collisioned tile as collision true function Entity:isCollidingAt(x,y,object) + return self:getCollidingAt(x,y,object) ~= nil +end + +function Entity:getCollidingAt(x,y,object) for _, collision in pairs(object) do if collision.disable then -- Dont calculate if disabled @@ -171,10 +175,10 @@ function Entity:isCollidingAt(x,y,object) and y + self.boxCollision.to.y > collision.from.y then collision.isColliding = true - return true + return collision end end - return false + return nil end function Entity:isCollidingWith(entity) diff --git a/code/game.lua b/code/game.lua index 2b71147..b923309 100644 --- a/code/game.lua +++ b/code/game.lua @@ -17,7 +17,8 @@ function GameStep() end AnimateTiles() - Camera:positionCenterAt(main_Player.pos.x, main_Player.pos.y) + Camera:followPlayer(main_Player) + --Camera:positionCenterAt(main_Player.pos.x, main_Player.pos.y) --camera:positionAt(main_Player.pos.x, main_Player.pos.y,game.width,game.height) if Keybind:CheckPressed(Keybind.debug.debug) then diff --git a/code/keybind.lua b/code/keybind.lua index 73ce73f..5b2938b 100644 --- a/code/keybind.lua +++ b/code/keybind.lua @@ -101,7 +101,8 @@ function Keybind:Default() -- Editor Keybind.editor.palette = { keys = {"tab"}} - + Keybind.editor.room_mode = { keys = {"r"}} + -- Generic Keybind.generic.lclick = { keys = {1}} Keybind.generic.rclick = { keys = {2}} diff --git a/main.lua b/main.lua index 89d5344..ee54272 100644 --- a/main.lua +++ b/main.lua @@ -133,4 +133,6 @@ function love.draw() love.graphics.print(game.scale,10,40) if DemoRecording or DemoPlayback then Demo:Draw() end + + frameDebugFlush() end