sonic-radiance/sonic-radiance.love/scenes/overworld/map.lua

190 lines
5.4 KiB
Lua
Raw Normal View History

2021-05-05 08:30:32 +02:00
local Parent = require "birb.modules.world.maps.parent"
2021-03-23 21:59:33 +01:00
local TiledMultiMap = Parent:extend()
2021-05-05 08:30:32 +02:00
local StiWrapper = require "birb.modules.world.maps.tiled.stiwrapper"
2021-03-23 21:59:33 +01:00
2021-05-05 08:30:32 +02:00
local TiledMixins = require "birb.modules.world.maps.tiled.mixins"
2021-03-23 21:59:33 +01:00
TiledMultiMap:implement(TiledMixins)
local mapFolder = "datas/gamedata/maps/sti/"
local areaFolder = "datas.gamedata.maps.area."
2021-04-02 23:26:22 +02:00
function TiledMultiMap:new(world, area, playerx, playery)
2021-03-23 21:59:33 +01:00
self.wrappers = {}
2021-04-02 23:26:22 +02:00
self.data = require(areaFolder .. area)
2021-03-23 21:59:33 +01:00
self.w, self.h = 0, 0
2021-04-02 23:26:22 +02:00
self.playerx, self.playery = playerx * 16, playery * 16
2021-03-23 21:59:33 +01:00
self:loadMaps()
TiledMultiMap.super.new(self, world)
self.supportTileCollision = true
2021-03-23 21:59:33 +01:00
self:setBackgroundColorFromTable(self.data.color)
self.mapname = self.data.areaName
end
function TiledMultiMap:getMapName()
return self.data.areaName
end
function TiledMultiMap:getDimensions()
return self.w, self.h
end
function TiledMultiMap:loadObjects()
for i, wrapper in ipairs(self.wrappers) do
wrapper:loadObjects()
end
self:loadWalls()
2021-04-02 23:26:22 +02:00
self:loadPlayer()
end
function TiledMultiMap:loadWalls()
if (self.data.exitTo == nil) then
self.world.obj.collisions["wall"](self.world, -16, -16, self.w, 16)
self.world.obj.collisions["wall"](self.world, -16, -16, 16, self.h)
self.world.obj.collisions["wall"](self.world, self.w, -16, 16, self.h)
self.world.obj.collisions["wall"](self.world, -16, self.h, self.w, 16)
end
end
2021-04-02 23:26:22 +02:00
function TiledMultiMap:loadPlayer()
self.world:addPlayer(self.playerx, self.playery, 0, 1)
2021-03-23 21:59:33 +01:00
end
-- MAP HANDLING FUNCTIONS
-- Handle maps
function TiledMultiMap:loadMaps()
for i, mapData in ipairs(self.data.maps) do
self:addMap(i, mapData)
end
end
function TiledMultiMap:addMap(id, mapData)
2021-04-11 15:18:47 +02:00
core.debug:print("Loading map " .. mapData.name)
2021-03-23 21:59:33 +01:00
local mapFile = mapFolder .. mapData.folder .. "/" .. mapData.map .. ".lua"
2021-04-02 23:26:22 +02:00
local wrapper = StiWrapper(self, mapFile, mapData.x, mapData.y, false)
2021-03-23 21:59:33 +01:00
wrapper.id = id
wrapper.music = mapData.music
wrapper.name = mapData.name
table.insert(self.wrappers, wrapper)
self:pushDimensions(wrapper)
end
function TiledMultiMap:pushDimensions(wrapper)
local x, y, w, h = wrapper:getRect()
self.w = math.max(self.w, x + w)
self.h = math.max(self.h, y + h)
end
function TiledMultiMap:getMapAtPoint(x, y)
for i, wrapper in ipairs(self.wrappers) do
local recx, recy, recw, rech = wrapper:getRect()
local relx, rely = x - recx, y - recy
if (relx <= recw and relx >= 0) then
if (rely <= rech and rely >= 0) then
return wrapper
end
end
end
end
function TiledMultiMap:getMapsInRect(x, y, w, h)
local listWrapper = {}
for i, wrapper in ipairs(self.wrappers) do
local recx, recy, recw, rech = wrapper:getRect()
local relx, rely = x - recx, y - recy
if (relx <= recw and relx + w >= 0) then
if (rely <= rech and rely + h >= 0) then
table.insert(listWrapper, wrapper)
end
end
end
return listWrapper
end
-- TILE FUNCTIONS
-- Get tiles
function TiledMultiMap:getTileTypeAtPoint(x, y)
local wrapper = self:getMapAtPoint(x, y)
if (wrapper ~= nil) then
return wrapper:getTileTypeAtPoint(x, y)
end
end
2021-03-23 21:59:33 +01:00
function TiledMultiMap:haveTileTypeInRect(x, y, w, h, type)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
local haveTile = wrapper:haveTileTypeInRect(x, y, w, h, type)
if (haveTile) then
return true
end
return false
end
end
-- UPDATE FUNCTION
-- Update or modify the map
function TiledMultiMap:resize(w, h)
for i, wrapper in ipairs(self.wrappers) do
wrapper:resize(w, h)
end
end
function TiledMultiMap:update(dt)
local x, y, w, h = self.world.cameras:getViewCoordinate(1)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
wrapper:update(dt)
end
end
-- DRAW FUNCTIONS
-- Handle drawing the wrapper
function TiledMultiMap:drawUpperLayers()
local player = self.world.players[1].actor
local pos1AUpperLayer = self:haveUpperLayerAtCoord(player.x, player.y + 8)
local pos2AUpperLayer = self:haveUpperLayerAtCoord(player.x + 16, player.y + 8)
2021-04-21 21:17:34 +02:00
local y = math.floor((player.y + 8)/16)*16
if (pos1AUpperLayer or pos2AUpperLayer) then
y = math.floor((player.y - player.z)/16)*16
end
local camx, camy = self.world.cameras:getViewCoordinate(1)
local viewy = math.floor(y - camy - 16)
love.graphics.setScissor(0, viewy, 424, 240)
self:drawUpperWrappers()
love.graphics.setScissor()
end
function TiledMultiMap:haveUpperLayerAtCoord(x, y)
local wrapper = self:getMapAtPoint(x, y)
2021-04-10 11:23:04 +02:00
if (wrapper == nil) then
return false
end
return wrapper:haveUpperLayerAtCoord(x, y)
end
function TiledMultiMap:drawUpperWrappers()
2021-03-23 21:59:33 +01:00
local x, y, w, h = self.world.cameras:getViewCoordinate(1)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
wrapper:drawUpperLayers()
end
end
function TiledMultiMap:drawLowerLayers()
local x, y, w, h = self.world.cameras:getViewCoordinate(1)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
wrapper:drawLowerLayers()
end
self:drawUpperWrappers()
2021-03-23 21:59:33 +01:00
end
return TiledMultiMap