uh, a lot happened, i murdered physics code, redesigned tiles & levels, now i need to make another level editor. I'm sorry game. I'm sorry git.

This commit is contained in:
lustlion
2021-11-28 03:38:30 +01:00
parent 8607399d16
commit dd2debc0bd
30 changed files with 701 additions and 693 deletions

View File

@@ -1,20 +1,22 @@
function LevelLoadTiles()
LevelInfo = {}
Tiles = dofile("Mothback/data/tiles.lua")
--[[
on level format:
id = tile identifier
depth = order in the render
force = rendering other tile instead of the one in this position
overlay = render another tile id
overlay = render another tile id or, if multiple tiles {id, id, id,} or
overlay_depth = foreground/background overlay depth
type = collision type
]]
LevelTiles = json.decode(getInput("Mothback/data/levels/"..currLevel..".json"))
LevelInfo.Width = LevelGetWidth()
LevelInfo.Height = LevelGetHeight()
LevelData = dofile("Mothback/data/levels/"..currLevel..".lua")
Tiles = dofile("Mothback/data/tileset/library.lua")
LevelTiles = LevelData.tiles
LevelData.Width = LevelGetWidth()
LevelData.Height = LevelGetHeight()
LevelIndexTiles()
TileCreateObjects()
end
@@ -35,8 +37,8 @@ function LevelIndexTiles()
TileIndex = {}
-- number of tiles in tileset!
local width = tileProperties.tileset:getPixelWidth()/tileProperties.width
local height = tileProperties.tileset:getPixelHeight()/tileProperties.height
local width = LevelData.tileset:getPixelWidth()/tileProperties.width
local height = LevelData.tileset:getPixelHeight()/tileProperties.height
for i = 0, height do
for j = 0, width do
TileIndex[i*width+j+1] = love.graphics.newQuad(
@@ -44,12 +46,12 @@ function LevelIndexTiles()
i*tileProperties.height,
tileProperties.width,
tileProperties.height,
tileProperties.tileset:getDimensions()
LevelData.tileset:getDimensions()
)
end
end
-- init animated tile properties
for _, properties in pairs(Tiles) do
if properties.animation ~= nil then
properties.tileset = love.graphics.newImage("assets/terrain/"..properties.animation..".png")
@@ -75,18 +77,44 @@ function LevelIndexTiles()
image_count = image_count + 1
table.insert(properties.imgs,quad)
end
end
properties.image_count = image_count
end
end
-- instance level tiles according to the properties
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
local id = LevelTiles[i][j]
LevelTiles[i][j] = {}
local tile = LevelTiles[i][j]
tile.id = id
for _, properties in pairs(Tiles) do
if properties.id == tile.id then
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
tile.display = properties.force[math.random(#properties.force)]
else
tile.display = properties.force
end
end
end
end
end
end
function LevelDisplayForeground()
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
if LevelTiles[i][j] ~= 0 then
if LevelTiles[i][j].id ~= 0 then
local depth = TileGetDepth(LevelTiles[i][j])
DrawTile(
@@ -102,9 +130,10 @@ function LevelDisplayForeground()
end
function LevelDisplayBackground()
love.graphics.setColor(0.7,0.7,0.7)
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
if LevelTiles[i][j] ~= 0 then
if LevelTiles[i][j].id ~= 0 then
local depth = TileGetDepth(LevelTiles[i][j])
DrawTile(
@@ -117,28 +146,28 @@ function LevelDisplayBackground()
end
end
end
love.graphics.setColor(1,1,1)
end
function TileGetType(tile_id)
function TileGetType(tile)
for _, properties in ipairs(Tiles) do
if properties.id == tile_id then
if properties.id == tile.id then
return properties.type
end
end
end
function TileGetDepth(tile_id)
function TileGetDepth(tile)
for _, properties in ipairs(Tiles) do
if properties.id == tile_id then
if properties.id == tile.id then
return properties.depth
end
end
end
function TileGetLight(tile_id)
function TileGetLight(tile)
for _, properties in ipairs(Tiles) do
if properties.id == tile_id then
if properties.id == tile.id then
return properties.light
end
end
@@ -165,7 +194,7 @@ function TileCreateObjects()
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
if LevelTiles[i][j] ~= 0 then
if LevelTiles[i][j].id ~= 0 then
local type = TileGetType(LevelTiles[i][j])
local light = TileGetLight(LevelTiles[i][j])
@@ -180,7 +209,7 @@ function TileCreateObjects()
light
)
end
if type == "whole" then
local col = Collision:New(
base_x,
@@ -471,9 +500,10 @@ function AnimateTiles()
end
end
function DrawTile(tile_id,x,y,depth)
function DrawTile(tile,x,y,depth)
for _, properties in pairs(Tiles) do
if tile_id == properties.id then
if tile.id == properties.id then
if properties.animation ~= nil then
if properties.imgs[properties.current_image] ~= nil
and properties.depth == depth
@@ -488,18 +518,20 @@ function DrawTile(tile_id,x,y,depth)
) end
elseif properties.depth == depth then
if properties.force ~= nil then
if properties.force ~= 0 then
love.graphics.draw(
tileProperties.tileset,
TileIndex[properties.force],
LevelData.tileset,
TileIndex[tile.display],
x,
y,
0,
tileProperties.scale,
tileProperties.scale
)
end
else
love.graphics.draw(
tileProperties.tileset,
LevelData.tileset,
TileIndex[properties.id],
x,
y,
@@ -528,8 +560,8 @@ function DrawTile(tile_id,x,y,depth)
end
else
love.graphics.draw(
tileProperties.tileset,
TileIndex[properties.overlay],
LevelData.tileset,
TileIndex[tile.display_overlay],
x,
y,
0,
@@ -539,6 +571,11 @@ function DrawTile(tile_id,x,y,depth)
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
end