optimized tiledata

This commit is contained in:
lustlion 2022-01-29 10:46:49 +01:00
parent a1bf842cef
commit 3e8a907aa2
2 changed files with 162 additions and 168 deletions

View File

@ -112,48 +112,42 @@ function TileDataInitialize()
end end
function InstanceTile(i,j,id) function InstanceTile(i,j,id)
LevelTiles[i][j] = {} LevelTiles[i][j] = {}
local tile = LevelTiles[i][j] local tile = LevelTiles[i][j]
tile.id = id
for _, Properties in pairs(TileData) do tile.id = id
if Properties.id == tile.id then local Properties = TileData[tile.id]
if type(Properties.overlay) == "table" then
tile.display_overlay = Properties.overlay[math.random(#Properties.overlay)]
else
tile.display_overlay = Properties.overlay
end
if type(Properties.force) == "table" then if Properties ~= nil then
tile.display = Properties.force[math.random(#Properties.force)] if type(Properties.overlay) == "table" then
else tile.display_overlay = Properties.overlay[math.random(#Properties.overlay)]
tile.display = Properties.force else
end tile.display_overlay = Properties.overlay
end
end end
if type(Properties.force) == "table" then
tile.display = Properties.force[math.random(#Properties.force)]
else
tile.display = Properties.force
end
end
end end
function TileGetType(tile) function TileGetType(tile)
for _, Properties in pairs(TileData) do if TileData[tile.id] ~= nil then
if Properties.id == tile.id then return TileData[tile.id].type
return Properties.type
end
end end
end end
function TileGetDepth(tile) function TileGetDepth(tile)
for _, Properties in pairs(TileData) do if TileData[tile.id] ~= nil then
if Properties.id == tile.id then return TileData[tile.id].depth
return Properties.depth
end
end end
end end
function TileGetLight(tile) function TileGetLight(tile)
for _, Properties in pairs(TileData) do if TileData[tile.id] ~= nil then
if Properties.id == tile.id then return TileData[tile.id].light
return Properties.light
end
end end
end end
@ -468,55 +462,84 @@ end
function AnimateTiles() function AnimateTiles()
for _, Properties in pairs(TileData) do for _, Properties in pairs(TileData) do
if Properties.animation ~= nil then if Properties ~= nil then
-- calculate subimage if Properties.animation ~= nil then
Properties.current_subimage = Properties.current_subimage + current_dt -- calculate subimage
-- cycle image Properties.current_subimage = Properties.current_subimage + current_dt
if Properties.current_subimage >= Properties.delay then -- cycle image
Properties.current_subimage = Properties.current_subimage - Properties.delay if Properties.current_subimage >= Properties.delay then
Properties.current_image = Properties.current_image + 1 Properties.current_subimage = Properties.current_subimage - Properties.delay
end Properties.current_image = Properties.current_image + 1
end
if Properties.current_image > Properties.image_count then if Properties.current_image > Properties.image_count then
Properties.current_image = Properties.current_image - Properties.image_count Properties.current_image = Properties.current_image - Properties.image_count
end
end end
end end
end end
end end
function DrawTile(tile,x,y,depth) function DrawTile(tile,x,y,depth)
for _, Properties in pairs(TileData) do local Properties = TileData[tile.id]
if tile.id == Properties.id then
if Properties.animation ~= nil then if Properties ~= nil then
if Properties.imgs[Properties.current_image] ~= nil if Properties.animation ~= nil then
and Properties.depth == depth if Properties.imgs[Properties.current_image] ~= nil
then love.graphics.draw( and Properties.depth == depth
Properties.tileset, then love.graphics.draw(
Properties.imgs[Properties.current_image], Properties.tileset,
Properties.imgs[Properties.current_image],
x,
y,
0,
tileProperties.scale,
tileProperties.scale
)
end
elseif Properties.depth == depth then
if Properties.force ~= nil then
if Properties.force ~= 0 then
love.graphics.draw(
LevelData.tileset,
TileIndex[tile.display],
x, x,
y, y,
0, 0,
tileProperties.scale, tileProperties.scale,
tileProperties.scale tileProperties.scale
) end )
elseif Properties.depth == depth then end
if Properties.force ~= nil then else
if Properties.force ~= 0 then love.graphics.draw(
LevelData.tileset,
TileIndex[tile.id],
x,
y,
0,
tileProperties.scale,
tileProperties.scale
)
end
end
if Properties.overlay ~= nil then
if Properties.overlay_depth == depth or Properties.overlay_depth == nil and Properties.depth == depth then
if Properties.overlay_animated then
local overlay_properties = TileData[Properties.overlay]
love.graphics.draw( love.graphics.draw(
LevelData.tileset, overlay_properties.tileset,
TileIndex[tile.display], overlay_properties.imgs[overlay_properties.current_image],
x, x,
y, y,
0, 0,
tileProperties.scale, tileProperties.scale,
tileProperties.scale tileProperties.scale
) )
end
else else
love.graphics.draw( love.graphics.draw(
LevelData.tileset, LevelData.tileset,
TileIndex[Properties.id], TileIndex[tile.display_overlay],
x, x,
y, y,
0, 0,
@ -525,41 +548,11 @@ function DrawTile(tile,x,y,depth)
) )
end end
end end
if Properties.overlay ~= nil then
if Properties.overlay_depth == depth or Properties.overlay_depth == nil and Properties.depth == depth then
if Properties.overlay_animated then
for _, overlay_properties in pairs(TileData) do
if overlay_properties.id == Properties.overlay then
love.graphics.draw(
overlay_properties.tileset,
overlay_properties.imgs[overlay_properties.current_image],
x,
y,
0,
tileProperties.scale,
tileProperties.scale
)
end
end
else
love.graphics.draw(
LevelData.tileset,
TileIndex[tile.display_overlay],
x,
y,
0,
tileProperties.scale,
tileProperties.scale
)
end
end
end
--[[
love.graphics.setColor(0,0,1)
love.graphics.print(tostring(tile.display),x+16,y)
love.graphics.setColor(1,1,1)
]]
end end
--[[
love.graphics.setColor(0,0,1)
love.graphics.print(tostring(tile.display),x+16,y)
love.graphics.setColor(1,1,1)
]]
end end
end end

View File

@ -1,80 +1,81 @@
return { local properties = {}
{
id = 1, properties[1] = {
type = "whole", type = "whole",
depth = "foreground" depth = "foreground"
},
{
id = 2,
type = "emtpy",
depth = "foreground"
},
{
id = 5,
type = "emtpy",
depth = "background"
},
{
id = 6,
type = "emtpy",
depth = "background"
},
{
id = 7,
type = "emtpy",
depth = "background"
},
{
id = 13,
type = "whole",
depth = "foreground"
},
{
id = 25,
overlay = {17,19,21,23},
type = "emtpy",
depth = "background"
},
{
id = 26,
overlay = {18,20,22,24},
type = "emtpy",
depth = "background"
},
{
id = 37,
overlay = {29,31,33,35,42,44,46,48},
type = "emtpy",
depth = "background"
},
{
id = 38,
overlay = {30,32,34,36,41,43,45,47},
type = "emtpy",
depth = "background"
},
{
id = 49,
overlay = {53,55,57,59},
type = "emtpy",
depth = "background"
},
{
id = 50,
overlay = {54,56,58,60},
type = "emtpy",
depth = "background"
},
{
id = 61,
overlay = {1},
type = "emtpy",
depth = "background"
},
{
id = 62,
overlay = {1},
type = "emtpy",
depth = "background"
}
} }
properties[2] = {
type = "emtpy",
depth = "foreground"
}
properties[5] = {
type = "emtpy",
depth = "background"
}
properties[6] = {
type = "emtpy",
depth = "background"
}
properties[7] = {
type = "emtpy",
depth = "background"
}
properties[13] = {
type = "whole",
depth = "foreground"
}
properties[25] = {
overlay = {17,19,21,23},
type = "emtpy",
depth = "background"
}
properties[26] = {
overlay = {18,20,22,24},
type = "emtpy",
depth = "background"
}
properties[37] = {
overlay = {29,31,33,35,41,43,45,47},
type = "emtpy",
depth = "background"
}
properties[38] = {
overlay = {30,32,34,36,42,44,46,48},
type = "emtpy",
depth = "background"
}
properties[49] = {
overlay = {53,55,57,59},
type = "emtpy",
depth = "background"
}
properties[50] = {
overlay = {54,56,58,60},
type = "emtpy",
depth = "background"
}
properties[61] = {
overlay = {1},
type = "emtpy",
depth = "background"
}
properties[62] = {
overlay = {1},
type = "emtpy",
depth = "background"
}
return properties