added rooms
This commit is contained in:
parent
2b9f605a0a
commit
5643fc0140
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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}}
|
||||
|
Loading…
Reference in New Issue
Block a user