consistent naming; moving functions from level.lua to gameworld.lua
This commit is contained in:
@@ -10,8 +10,13 @@ function LevelLoadTiles()
|
||||
type = collision type
|
||||
]]
|
||||
|
||||
|
||||
-- Level data
|
||||
LevelData = dofile("Mothback/data/levels/"..currLevel..".lua")
|
||||
Tiles = dofile("Mothback/data/tileset/library.lua")
|
||||
|
||||
-- tiles data
|
||||
TileData = dofile("Mothback/data/tileset/library.lua")
|
||||
|
||||
LevelTiles = LevelData.tiles
|
||||
LevelData.Width = LevelGetWidth()
|
||||
LevelData.Height = LevelGetHeight()
|
||||
@@ -34,7 +39,7 @@ end
|
||||
function LevelIndexTiles()
|
||||
TileIndex = {}
|
||||
|
||||
-- number of tiles in tileset!
|
||||
-- index from tileset
|
||||
local width = LevelData.tileset:getPixelWidth()/tileProperties.width
|
||||
local height = LevelData.tileset:getPixelHeight()/tileProperties.height
|
||||
for i = 0, height do
|
||||
@@ -49,15 +54,15 @@ function LevelIndexTiles()
|
||||
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")
|
||||
properties.imgs = {}
|
||||
properties.current_image = 1
|
||||
properties.current_subimage = 1
|
||||
-- initialize tile data
|
||||
for _, Properties in pairs(TileData) do
|
||||
if Properties.animation ~= nil then
|
||||
Properties.tileset = love.graphics.newImage("assets/terrain/"..Properties.animation..".png")
|
||||
Properties.imgs = {}
|
||||
Properties.current_image = 1
|
||||
Properties.current_subimage = 1
|
||||
|
||||
local tileset = properties.tileset
|
||||
local tileset = Properties.tileset
|
||||
local width = tileset:getPixelWidth()/tileProperties.width
|
||||
local height = tileset:getPixelHeight()/tileProperties.height
|
||||
local image_count = 0
|
||||
@@ -74,14 +79,14 @@ function LevelIndexTiles()
|
||||
)
|
||||
image_count = image_count + 1
|
||||
|
||||
table.insert(properties.imgs,quad)
|
||||
table.insert(Properties.imgs,quad)
|
||||
end
|
||||
end
|
||||
properties.image_count = image_count
|
||||
Properties.image_count = image_count
|
||||
end
|
||||
end
|
||||
|
||||
-- instance level tiles according to the properties
|
||||
-- instance level tiles according to the Properties
|
||||
for i = 1, #LevelTiles do
|
||||
for j = 1, #LevelTiles[i] do
|
||||
local id = LevelTiles[i][j]
|
||||
@@ -89,18 +94,18 @@ function LevelIndexTiles()
|
||||
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)]
|
||||
for _, Properties in pairs(TileData) 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
|
||||
tile.display_overlay = Properties.overlay
|
||||
end
|
||||
|
||||
if type(properties.force) == "table" then
|
||||
tile.display = properties.force[math.random(#properties.force)]
|
||||
if type(Properties.force) == "table" then
|
||||
tile.display = Properties.force[math.random(#Properties.force)]
|
||||
else
|
||||
tile.display = properties.force
|
||||
tile.display = Properties.force
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -109,64 +114,26 @@ function LevelIndexTiles()
|
||||
end
|
||||
end
|
||||
|
||||
function LevelDisplayForeground()
|
||||
for i = 1, #LevelTiles do
|
||||
for j = 1, #LevelTiles[i] do
|
||||
if LevelTiles[i][j].id ~= 0 then
|
||||
|
||||
local depth = TileGetDepth(LevelTiles[i][j])
|
||||
DrawTile(
|
||||
LevelTiles[i][j],
|
||||
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
|
||||
tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) - Camera.pos.y,
|
||||
"foreground"
|
||||
)
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
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].id ~= 0 then
|
||||
|
||||
local depth = TileGetDepth(LevelTiles[i][j])
|
||||
DrawTile(
|
||||
LevelTiles[i][j],
|
||||
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
|
||||
tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) - Camera.pos.y,
|
||||
"background"
|
||||
)
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
love.graphics.setColor(1,1,1)
|
||||
end
|
||||
|
||||
function TileGetType(tile)
|
||||
for _, properties in ipairs(Tiles) do
|
||||
if properties.id == tile.id then
|
||||
return properties.type
|
||||
for _, Properties in pairs(TileData) do
|
||||
if Properties.id == tile.id then
|
||||
return Properties.type
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function TileGetDepth(tile)
|
||||
for _, properties in ipairs(Tiles) do
|
||||
if properties.id == tile.id then
|
||||
return properties.depth
|
||||
for _, Properties in pairs(TileData) do
|
||||
if Properties.id == tile.id then
|
||||
return Properties.depth
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function TileGetLight(tile)
|
||||
for _, properties in ipairs(Tiles) do
|
||||
if properties.id == tile.id then
|
||||
return properties.light
|
||||
for _, Properties in pairs(TileData) do
|
||||
if Properties.id == tile.id then
|
||||
return Properties.light
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -176,8 +143,8 @@ function GridDisplay()
|
||||
for j = 1, #LevelTiles[i] do
|
||||
love.graphics.rectangle(
|
||||
"line",
|
||||
tileProperties.scale * j * tileProperties.width + tileProperties.scale * (levelProperties.offset.x - tileProperties.width) - Camera.pos.x,
|
||||
tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) - Camera.pos.y,
|
||||
tileProperties.scale * (j * tileProperties.width + (levelProperties.offset.x - tileProperties.width)) - Camera.pos.x,
|
||||
tileProperties.scale * (i * tileProperties.height + (levelProperties.offset.y - tileProperties.height)) - Camera.pos.y,
|
||||
tileProperties.scale * tileProperties.width,
|
||||
tileProperties.scale * tileProperties.height
|
||||
)
|
||||
@@ -186,9 +153,9 @@ function GridDisplay()
|
||||
end
|
||||
|
||||
function TileCreateObjects()
|
||||
objects.collisions = {}
|
||||
objects.platforms = {}
|
||||
objects.ladders = {}
|
||||
LoadedObjects.Collisions = {}
|
||||
LoadedObjects.Platforms = {}
|
||||
LoadedObjects.Ladders = {}
|
||||
|
||||
for i = 1, #LevelTiles do
|
||||
for j = 1, #LevelTiles[i] do
|
||||
@@ -215,7 +182,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "half_bottom" then
|
||||
|
||||
@@ -225,7 +192,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "half_top" then
|
||||
|
||||
@@ -235,7 +202,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "half_right" then
|
||||
|
||||
@@ -245,7 +212,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "half_left" then
|
||||
|
||||
@@ -255,7 +222,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.height/2 * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "platform" then
|
||||
local plat = Collision:New(
|
||||
@@ -264,7 +231,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/4 * tileProperties.scale + tileProperties.scale * 2
|
||||
)
|
||||
table.insert(objects.platforms,plat)
|
||||
table.insert(LoadedObjects.Platforms,plat)
|
||||
|
||||
elseif type == "ramp2_bot_left_whole" then
|
||||
for k = 1, 8 do
|
||||
@@ -275,7 +242,7 @@ function TileCreateObjects()
|
||||
base_x + k * 2 * tileProperties.scale,
|
||||
base_y + k * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
-- fill lower half
|
||||
@@ -285,7 +252,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "ramp2_bot_left_half" then
|
||||
for k = 1, 8 do
|
||||
@@ -296,7 +263,7 @@ function TileCreateObjects()
|
||||
base_x + k * 2 * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale + k * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
|
||||
@@ -309,7 +276,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale - (k-1) * 2 * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale - tileProperties.scale + k * tileProperties.scale + tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
-- fill higher half
|
||||
@@ -319,7 +286,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "ramp2_top_left_half" then
|
||||
for k = 1, 8 do
|
||||
@@ -330,7 +297,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale - (k-1) * 2 * tileProperties.scale,
|
||||
base_y - tileProperties.scale + k * tileProperties.scale + tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
|
||||
@@ -343,7 +310,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y - tileProperties.scale + k * tileProperties.scale + tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
-- fill lower half
|
||||
@@ -353,7 +320,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "ramp2_bot_right_half" then
|
||||
for k = 1, 8 do
|
||||
@@ -364,7 +331,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale - tileProperties.scale + k * tileProperties.scale + tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
|
||||
@@ -377,7 +344,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale - k * tileProperties.scale + tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
|
||||
@@ -390,7 +357,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale + tileProperties.height/2 * tileProperties.scale - k * tileProperties.scale + tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
-- fill higher half
|
||||
@@ -400,7 +367,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/2 * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,col)
|
||||
table.insert(LoadedObjects.Collisions,col)
|
||||
|
||||
elseif type == "ramp1_bot_left" then
|
||||
|
||||
@@ -412,7 +379,7 @@ function TileCreateObjects()
|
||||
base_x + k * tileProperties.scale,
|
||||
base_y + k * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.collisions,slope)
|
||||
table.insert(LoadedObjects.Collisions,slope)
|
||||
|
||||
end
|
||||
|
||||
@@ -424,7 +391,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.ladders,ladder)
|
||||
table.insert(LoadedObjects.Ladders,ladder)
|
||||
|
||||
elseif type == "ladder_platform_right" then
|
||||
|
||||
@@ -434,7 +401,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.ladders,ladder)
|
||||
table.insert(LoadedObjects.Ladders,ladder)
|
||||
|
||||
local plat = Collision:New(
|
||||
base_x,
|
||||
@@ -442,7 +409,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/4 * tileProperties.scale + tileProperties.scale * 2
|
||||
)
|
||||
table.insert(objects.platforms,plat)
|
||||
table.insert(LoadedObjects.Platforms,plat)
|
||||
|
||||
elseif type == "ladder_left" then
|
||||
|
||||
@@ -453,7 +420,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.scale * 4,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.ladders,ladder)
|
||||
table.insert(LoadedObjects.Ladders,ladder)
|
||||
|
||||
elseif type == "ladder_platform_left" then
|
||||
|
||||
@@ -464,7 +431,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.scale * 4,
|
||||
base_y + tileProperties.height * tileProperties.scale
|
||||
)
|
||||
table.insert(objects.ladders,ladder)
|
||||
table.insert(LoadedObjects.Ladders,ladder)
|
||||
|
||||
local plat = Collision:New(
|
||||
base_x,
|
||||
@@ -472,7 +439,7 @@ function TileCreateObjects()
|
||||
base_x + tileProperties.width * tileProperties.scale,
|
||||
base_y + tileProperties.height/4 * tileProperties.scale + tileProperties.scale * 2
|
||||
)
|
||||
table.insert(objects.platforms,plat)
|
||||
table.insert(LoadedObjects.Platforms,plat)
|
||||
|
||||
end
|
||||
end
|
||||
@@ -481,42 +448,42 @@ function TileCreateObjects()
|
||||
end
|
||||
|
||||
function AnimateTiles()
|
||||
for _, properties in pairs(Tiles) do
|
||||
if properties.animation ~= nil then
|
||||
for _, Properties in pairs(TileData) do
|
||||
if Properties.animation ~= nil then
|
||||
-- calculate subimage
|
||||
properties.current_subimage = properties.current_subimage + current_dt
|
||||
Properties.current_subimage = Properties.current_subimage + current_dt
|
||||
-- cycle image
|
||||
if properties.current_subimage >= properties.delay then
|
||||
properties.current_subimage = properties.current_subimage - properties.delay
|
||||
properties.current_image = properties.current_image + 1
|
||||
if Properties.current_subimage >= Properties.delay then
|
||||
Properties.current_subimage = Properties.current_subimage - Properties.delay
|
||||
Properties.current_image = Properties.current_image + 1
|
||||
end
|
||||
|
||||
if properties.current_image > properties.image_count then
|
||||
properties.current_image = properties.current_image - properties.image_count
|
||||
if Properties.current_image > Properties.image_count then
|
||||
Properties.current_image = Properties.current_image - Properties.image_count
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function DrawTile(tile,x,y,depth)
|
||||
for _, properties in pairs(Tiles) do
|
||||
if tile.id == properties.id then
|
||||
for _, Properties in pairs(TileData) do
|
||||
if tile.id == Properties.id then
|
||||
|
||||
if properties.animation ~= nil then
|
||||
if properties.imgs[properties.current_image] ~= nil
|
||||
and properties.depth == depth
|
||||
if Properties.animation ~= nil then
|
||||
if Properties.imgs[Properties.current_image] ~= nil
|
||||
and Properties.depth == depth
|
||||
then love.graphics.draw(
|
||||
properties.tileset,
|
||||
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
|
||||
elseif Properties.depth == depth then
|
||||
if Properties.force ~= nil then
|
||||
if Properties.force ~= 0 then
|
||||
love.graphics.draw(
|
||||
LevelData.tileset,
|
||||
TileIndex[tile.display],
|
||||
@@ -530,7 +497,7 @@ function DrawTile(tile,x,y,depth)
|
||||
else
|
||||
love.graphics.draw(
|
||||
LevelData.tileset,
|
||||
TileIndex[properties.id],
|
||||
TileIndex[Properties.id],
|
||||
x,
|
||||
y,
|
||||
0,
|
||||
@@ -540,11 +507,11 @@ function DrawTile(tile,x,y,depth)
|
||||
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(Tiles) do
|
||||
if overlay_properties.id == properties.overlay then
|
||||
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],
|
||||
|
||||
Reference in New Issue
Block a user