added rooms
This commit is contained in:
parent
2b9f605a0a
commit
5643fc0140
@ -4,6 +4,35 @@ Camera = {
|
|||||||
height = 0
|
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()
|
function Camera:ConfineToLevel()
|
||||||
self.pos.x = math.max(0,math.min(self.pos.x,LevelData.Width-self.width/game.scale))
|
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))
|
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)
|
function Camera:positionCenterAt(x,y)
|
||||||
self.pos.x = x-self.width/game.scale/2
|
self.pos.x = x-self.width/game.scale/2
|
||||||
self.pos.y = y-self.height/game.scale/2
|
self.pos.y = y-self.height/game.scale/2
|
||||||
self:ConfineToLevel()
|
--self:ConfineToLevel()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Camera:positionAt(x,y)
|
function Camera:positionAt(x,y)
|
||||||
|
@ -4,6 +4,8 @@ LoadedObjects.Collisions = {}
|
|||||||
LoadedObjects.Platforms = {}
|
LoadedObjects.Platforms = {}
|
||||||
LoadedObjects.Ladders = {}
|
LoadedObjects.Ladders = {}
|
||||||
LoadedObjects.Hazards = {}
|
LoadedObjects.Hazards = {}
|
||||||
|
LoadedObjects.Rooms = {}
|
||||||
|
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
Collision
|
Collision
|
||||||
@ -51,6 +53,8 @@ function Collision:New(ox,oy,tx,ty)
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function Collision:CenterAt(x, y)
|
function Collision:CenterAt(x, y)
|
||||||
self.from.x = x-self.width/2
|
self.from.x = x-self.width/2
|
||||||
self.from.y = y-self.height/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
|
self.to.y = self.from.x + self.height
|
||||||
end
|
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)
|
function Collision:Draw(color)
|
||||||
if self.isColliding == true then
|
if self.isColliding == true then
|
||||||
love.graphics.setColor(0,1,0,0.5)
|
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.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)
|
love.graphics.rectangle("line",self.from.x-Camera.pos.x, self.from.y-Camera.pos.y, self.width, self.height)
|
||||||
end
|
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
|
||||||
|
@ -93,3 +93,21 @@ end
|
|||||||
function logWrite(string)
|
function logWrite(string)
|
||||||
if logging then logFile:write(string.."\n") end
|
if logging then logFile:write(string.."\n") end
|
||||||
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
|
@ -1,6 +1,17 @@
|
|||||||
|
assert(editor == nil)
|
||||||
|
editor = { room_mode = false }
|
||||||
|
|
||||||
function EditorStep()
|
function EditorStep()
|
||||||
palette = palette or false
|
palette = palette or false
|
||||||
AnimateTiles()
|
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 Keybind:CheckPressed(Keybind.editor.palette) then
|
||||||
if palette then
|
if palette then
|
||||||
@ -13,17 +24,18 @@ function EditorStep()
|
|||||||
palette_scroll_y = 0
|
palette_scroll_y = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
-- TODO:
|
||||||
if love.keyboard.isDown('a',"left") then
|
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
|
end
|
||||||
if love.keyboard.isDown('d',"right") then
|
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
|
end
|
||||||
if love.keyboard.isDown("up", "w") then
|
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
|
end
|
||||||
if love.keyboard.isDown("down", "s") then
|
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
|
end
|
||||||
|
|
||||||
if palette then
|
if palette then
|
||||||
@ -83,6 +95,8 @@ function EditorDraw()
|
|||||||
GameworldDrawEnd()
|
GameworldDrawEnd()
|
||||||
EditorDoEdit()
|
EditorDoEdit()
|
||||||
|
|
||||||
|
DrawRooms()
|
||||||
|
|
||||||
DrawSelectingPaletteTile()
|
DrawSelectingPaletteTile()
|
||||||
if palette then
|
if palette then
|
||||||
EditorDoPalette()
|
EditorDoPalette()
|
||||||
@ -109,10 +123,39 @@ function EditorDoEdit()
|
|||||||
elseif vertical < 0 then
|
elseif vertical < 0 then
|
||||||
expand_v = vertical
|
expand_v = vertical
|
||||||
end
|
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("> " .. 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)
|
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
|
if LevelTiles[vertical] ~= nil
|
||||||
and LevelTiles[vertical][horizontal] ~= nil
|
and LevelTiles[vertical][horizontal] ~= nil
|
||||||
and love.keyboard.isDown("lshift") ~= true
|
and love.keyboard.isDown("lshift") ~= true
|
||||||
|
@ -162,6 +162,10 @@ end
|
|||||||
-- checks if the the reciever would collide with an object if it was positioned at the given point.
|
-- 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
|
-- also marks collisioned tile as collision true
|
||||||
function Entity:isCollidingAt(x,y,object)
|
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
|
for _, collision in pairs(object) do
|
||||||
if collision.disable then
|
if collision.disable then
|
||||||
-- Dont calculate if disabled
|
-- Dont calculate if disabled
|
||||||
@ -171,10 +175,10 @@ function Entity:isCollidingAt(x,y,object)
|
|||||||
and y + self.boxCollision.to.y > collision.from.y
|
and y + self.boxCollision.to.y > collision.from.y
|
||||||
then
|
then
|
||||||
collision.isColliding = true
|
collision.isColliding = true
|
||||||
return true
|
return collision
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return false
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:isCollidingWith(entity)
|
function Entity:isCollidingWith(entity)
|
||||||
|
@ -17,7 +17,8 @@ function GameStep()
|
|||||||
end
|
end
|
||||||
|
|
||||||
AnimateTiles()
|
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)
|
--camera:positionAt(main_Player.pos.x, main_Player.pos.y,game.width,game.height)
|
||||||
|
|
||||||
if Keybind:CheckPressed(Keybind.debug.debug) then
|
if Keybind:CheckPressed(Keybind.debug.debug) then
|
||||||
|
@ -101,7 +101,8 @@ function Keybind:Default()
|
|||||||
|
|
||||||
-- Editor
|
-- Editor
|
||||||
Keybind.editor.palette = { keys = {"tab"}}
|
Keybind.editor.palette = { keys = {"tab"}}
|
||||||
|
Keybind.editor.room_mode = { keys = {"r"}}
|
||||||
|
|
||||||
-- Generic
|
-- Generic
|
||||||
Keybind.generic.lclick = { keys = {1}}
|
Keybind.generic.lclick = { keys = {1}}
|
||||||
Keybind.generic.rclick = { keys = {2}}
|
Keybind.generic.rclick = { keys = {2}}
|
||||||
|
Loading…
Reference in New Issue
Block a user