consistent naming; moving functions from level.lua to gameworld.lua

This commit is contained in:
lustlion 2022-01-22 23:10:21 +01:00
parent f4b44dc7bc
commit 90ed1f6460
13 changed files with 381 additions and 202 deletions

View File

@ -3,11 +3,11 @@ return {
tileset = tileset.library, tileset = tileset.library,
tiles = { tiles = {
{13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13}, {13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13},
{ }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,25,26, 6,25,26, 7, 0, 5,25,26, 7}, { 0, 0, 0, 0, 0, 0, 5,25,26, 6,25,26, 7, 0, 5,25,26, 7, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,37,38, 6,37,38, 7, 0, 5,37,38, 7}, { 0, 0, 0, 0, 0, 0, 5,37,38, 6,37,38, 7, 0, 5,37,38, 7, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,37,38, 6,37,38, 7, 0, 5,37,38, 7}, { 0, 0, 0, 0, 0, 0, 5,37,38, 6,37,38, 7, 0, 5,37,38, 7, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 5,49,50, 6,49,50, 7, 0, 5,49,50, 7}, { 0, 0, 0, 0, 0, 0, 5,49,50, 6,49,50, 7, 0, 5,49,50, 7, 0, 0, 0, 0, 0, 0},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}, },
objects = {} objects = {}

View File

@ -28,7 +28,7 @@ end
end end
function DebugColisions() function DebugColisions()
objects.DrawCollisions() LoadedObjects.DrawCollisions()
end end
function DebugEntities() function DebugEntities()

View File

@ -13,6 +13,18 @@ function EditorStep()
selecting_tile = 51 selecting_tile = 51
end end
end end
if love.keyboard.isDown('a',"left") then
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
end
if love.keyboard.isDown("up", "w") then
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
end
end end
function EditorScroll(y) function EditorScroll(y)
@ -29,12 +41,87 @@ function EditorScroll(y)
end end
function EditorDraw() function EditorDraw()
GameworldDraw() GameworldDrawPrepare()
GameworldDrawBackground()
GridDisplay()
GameworldDrawForeground()
GameworldDrawEnd()
DrawSelectingPaletteTile()
if palette then if palette then
EditorDoPalette() EditorDoPalette()
end end
end end
function EditorDoEdit()
if love.mouse.isDown(1) then
local vertical = 1+math.floor((mouse.pos.y+Camera.pos.y)/(tileProperties.scale*tileProperties.height))
local horizontal = 1+math.floor((mouse.pos.x+Camera.pos.x)/(tileProperties.scale*tileProperties.width))
local h, v = GetCanvasSize()
local expand_h = 0
local expand_v = 0
if horizontal > h then
expand_h = horizontal-h
elseif horizontal <= 0 then
expand_h = horizontal-1
end
if vertical > v then
expand_v = math.sign(vertical-v)
elseif vertical <= 0 then
expand_v = math.sign(vertical-1)
end
if Level[vertical] ~= nil
and Level[vertical][horizontal] ~= nil
and love.keyboard.isDown("lshift") ~= true
and love.keyboard.isDown("lctrl") ~= true
then
Level[vertical][horizontal] = tile_carrying
elseif love.keyboard.isDown("lshift") and not expanded then
expanded = true
ExpandCanvas(math.sign(expand_h),math.sign(expand_v))
if expand_h < 0 then
Camera.pos.x = Camera.pos.x - expand_h*tileProperties.scale*tileProperties.width
end
if expand_v < 0 then
Camera.pos.y = Camera.pos.y - expand_v*tileProperties.scale*tileProperties.height
end
elseif love.keyboard.isDown("lctrl") and not expanded then
expanded = true
ReduceCanvas(math.sign(expand_h),math.sign(expand_v))
if expand_h < 0 then
Camera.pos.x = Camera.pos.x - expand_h*tileProperties.scale*tileProperties.width
end
if expand_v < 0 then
Camera.pos.y = Camera.pos.y - expand_v*tileProperties.scale*tileProperties.height
end
end
elseif love.mouse.isDown(1) ~= true then
expanded = false
end
end
function DrawSelectingPaletteTile()
if selecting_tile ~= nil then
local mouse_x = tileProperties.width * math.floor((love.mouse.getX()/game.scale) / tileProperties.width) - Camera.pos.x % tileProperties.width
local mouse_y = tileProperties.height * math.floor((love.mouse.getY()/game.scale) / tileProperties.height) - Camera.pos.y % tileProperties.height
love.graphics.draw(
LevelData.tileset,
TileIndex[selecting_tile],
mouse_x,
mouse_y
)
end
end
function EditorDoPalette() function EditorDoPalette()
local width = LevelData.tileset:getPixelWidth()/tileProperties.width local width = LevelData.tileset:getPixelWidth()/tileProperties.width
@ -71,17 +158,17 @@ function EditorDoPalette()
local mouse_x = love.mouse.getX() local mouse_x = love.mouse.getX()
local mouse_y = love.mouse.getY() local mouse_y = love.mouse.getY()
if mouse_x > tile_x if mouse_x > (tile_x) * game.scale
and mouse_x < tile_x + tileProperties.width + 1 and mouse_x < (tile_x + tileProperties.width) * game.scale
and mouse_y > tile_y and mouse_y > (tile_y) * game.scale
and mouse_y < tile_y + tileProperties.height + 1 and mouse_y < (tile_y + tileProperties.height) * game.scale
then then
selecting_tile = position_x + (position_y * width) selecting_tile = position_x + ((position_y-1) * width)
love.graphics.print(selecting_tile .. " | " .. tile_x .. ", " .. tile_y) love.graphics.print(selecting_tile .. " | " .. tile_x .. ", " .. tile_y)
else
--selecting_tile = nil
end end
else
selecting_tile = 0
end end
if selecting_tile ~= nil and i == selecting_tile then if selecting_tile ~= nil and i == selecting_tile then

View File

@ -41,7 +41,7 @@ function Arrow:DoPhysics()
while not isThereObjectAt( while not isThereObjectAt(
self.pos.x + math.sign(self.vel.x), self.pos.x + math.sign(self.vel.x),
self.pos.y, self.pos.y,
objects.collisions LoadedObjects.Collisions
) do ) do
self.pos.x = self.pos.x + math.sign(self.vel.x) self.pos.x = self.pos.x + math.sign(self.vel.x)
end end
@ -57,7 +57,7 @@ function Arrow:DoPhysics()
while not isThereObjectAt( while not isThereObjectAt(
self.pos.x, self.pos.x,
self.pos.y + math.sign(self.vel.y), self.pos.y + math.sign(self.vel.y),
objects.collisions LoadedObjects.Collisions
) do ) do
self.pos.y = self.pos.y + math.sign(self.vel.y) self.pos.y = self.pos.y + math.sign(self.vel.y)
end end

View File

@ -66,10 +66,5 @@ function Fairy:DoPhysics()
self.vel.x = self.vel.x + random_x self.vel.x = self.vel.x + random_x
self.vel.y = self.vel.y + random_y self.vel.y = self.vel.y + random_y
-- move -- move
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, objects.collisions) then self:CollisionMove()
self.pos.x = self.pos.x + self.vel.x
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then
self.pos.y = self.pos.y + self.vel.y
end
end end

View File

@ -73,10 +73,5 @@ function Particle:HandleAnimation()
end end
function Particle:DoPhysics() function Particle:DoPhysics()
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, objects.collisions) then self:Move()
self.pos.x = self.pos.x + self.vel.x
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then
self.pos.y = self.pos.y + self.vel.y
end
end end

View File

@ -144,13 +144,13 @@ function Player:DoPhysics()
self.vel.y = self.vel.y + gravity self.vel.y = self.vel.y + gravity
end end
if not self:isCollidingAt(self.pos.x + self.vel.x + self.move_x, self.pos.y, objects.collisions) then if not self:isCollidingAt(self.pos.x + self.vel.x + self.move_x, self.pos.y, LoadedObjects.Collisions) then
self.pos.x = self.pos.x + self.vel.x + self.move_x self.pos.x = self.pos.x + self.vel.x + self.move_x
else else
self.vel.x = 0 self.vel.x = 0
end end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
self.pos.y = self.pos.y + self.vel.y self.pos.y = self.pos.y + self.vel.y
else else
if self.vel.y > 0 then if self.vel.y > 0 then

View File

@ -31,6 +31,20 @@ end
function Entity:Smart() function Entity:Smart()
end end
function Entity:Move()
self.pos.x = self.pos.x + self.vel.x
self.pos.y = self.pos.y + self.vel.y
end
function Entity:CollisionMove()
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then
self.pos.x = self.pos.x + self.vel.x
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
self.pos.y = self.pos.y + self.vel.y
end
end
function Entity:Kill() function Entity:Kill()
if self.light ~= nil then if self.light ~= nil then
KillLight(self.light) KillLight(self.light)

View File

@ -17,8 +17,13 @@ end
function GameDraw() function GameDraw()
GameworldDraw() GameworldDrawPrepare()
GameworldLighting() GameworldDrawBackground()
GameworldDrawParticles()
GameworldDrawEntities()
GameworldDrawForeground()
GameworldDrawEnd()
GameworldDrawLighting()
-- hud -- hud
textScale = 0.5 textScale = 0.5

View File

@ -1,28 +1,72 @@
function GameworldDraw() function GameworldDrawPrepare()
-- resize proof
if game_resize then if game_resize then
Camera.height = game.height Camera.height = game.height
Camera.width = game.width Camera.width = game.width
end end
pcr, pcg, pcb, pca = love.graphics.getColor()
local pcr, pcg, pcb, pca = love.graphics.getColor()
love.graphics.scale(game.scale,game.scale) love.graphics.scale(game.scale,game.scale)
love.graphics.setColor(1,1,1,1) love.graphics.setColor(1,1,1,1)
LevelDisplayBackground() end
function GameworldDrawEnd()
love.graphics.setColor(pcr, pcg, pcb, pca)
pcr, pcg, pcb, pca = nil, nil, nil, nil
end
function GameworldDrawBackground()
-- obscure a bit
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
end
function GameworldDrawParticles()
love.graphics.setColor(0.7,0.7,0.7)
for _, particle in pairs(LoadedParticles) do for _, particle in pairs(LoadedParticles) do
particle:HandleAnimation() particle:HandleAnimation()
end end
end
function GameworldDrawEntities()
love.graphics.setColor(1,1,1)
for _, enty in pairs(LoadedEntities) do for _, enty in pairs(LoadedEntities) do
enty:HandleAnimation() enty:HandleAnimation()
end end
LevelDisplayForeground()
love.graphics.setColor(pcr, pcg, pcb, pca)
end end
function GameworldLighting() function GameworldDrawForeground()
love.graphics.setColor(1,1,1)
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 GameworldDrawLighting()
if game_resize then if game_resize then
Canvas.Darkness:release() Canvas.Darkness:release()
Canvas.Darkness = CreateDarkness() Canvas.Darkness = CreateDarkness()

View File

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

View File

@ -1,23 +1,23 @@
objects = { LoadedObjects = {
entities = {}, Entities = {},
collisions = {}, Collisions = {},
platforms = {}, Platforms = {},
ladders = {} Ladders = {}
} }
-- level functions -- level functions
function objects.DrawCollisions() function LoadedObjects.DrawCollisions()
for _, col in pairs(objects.collisions) do for _, collision in pairs(LoadedObjects.Collisions) do
col:Draw(1) collision:Draw(1)
end end
for _, plat in pairs(objects.platforms) do for _, platform in pairs(LoadedObjects.Platforms) do
if plat.disable == true then plat:Draw(2) end if platform.disable == true then platform:Draw(2) end
if plat.disable == false then plat:Draw(1) end if platform.disable == false then platform:Draw(1) end
end end
for _, ladder in pairs(objects.ladders) do for _, ladder in pairs(LoadedObjects.Ladders) do
ladder:Draw(2) ladder:Draw(2)
end end
end end
@ -25,48 +25,38 @@ end
-- returns true if theres a collision at that point. also marks collisioned tile as collision true -- returns true if theres a collision at that point. also marks collisioned tile as collision true
function isThereObjectAt(x,y,objectType) function isThereObjectAt(x,y,objectType)
local result = false local result = false
for _, col in pairs(objectType) do for _, collision in pairs(objectType) do
if x >= col.from.x if x >= collision.from.x
and x <= col.to.x and x <= collision.to.x
and y >= col.from.y and y >= collision.from.y
and y <= col.to.y and y <= collision.to.y
and col.disable ~= true then and collision.disable ~= true then
result = true result = true
col.collision = true collision.collision = true
end end
end end
return result return result
end end
function isThereAnyCollisionAt(x,y) function isThereAnyCollisionAt(x,y)
local result = false return isThereObjectAt(x,y,LoadedObjects.Collisions) or isThereObjectAt(x,y,LoadedObjects.Ladders) or isThereObjectAt(x,y,LoadedObjects.Platforms)
if not result then
result = isThereObjectAt(x,y,objects.collisions)
end
if not result then
result = isThereObjectAt(x,y,objects.ladders)
end
if not result then
result = isThereObjectAt(x,y,objects.platforms)
end
return result
end end
-- flags -- flags
function SetCollisionFlags(player) function SetCollisionFlags(player)
for _, col in pairs(objects.collisions) do for _, collision in pairs(LoadedObjects.Collisions) do
col.collision = false collision.collision = false
end end
for _, plat in pairs(objects.platforms) do for _, platform in pairs(LoadedObjects.Platforms) do
plat.collision = false platform.collision = false
if player.pos.y < plat.from.y then if player.pos.y < platform.from.y then
plat.disable = false platform.disable = false
else else
plat.disable = true platform.disable = true
end end
end end
for _, ladder in pairs(objects.ladders) do for _, ladder in pairs(LoadedObjects.Ladders) do
ladder.collision = false ladder.collision = false
end end
end end

82
data/scripts/particle.lua Normal file
View File

@ -0,0 +1,82 @@
Particle = Entity:New(x,y)
function Particle:New(x,y,particle_data)
local o = Entity:New(x,y)
o.pos = {x = x, y = y}
o.speed = particle_data.speed or 0
o.direction = particle_data.direction or o.direction
o.sprite_rotation = particle_data.sprite_rotation or o.sprite_rotation
o.sprite_offset = particle_data.sprite_offset or o.sprite_offset
o.sprite_scale = particle_data.sprite_scale or o.sprite_scale
o.sprite_tint = particle_data.sprite_tint or o.sprite_tint
o.sprite_alpha = particle_data.sprite_alpha or o.sprite_alpha
o.sprite_alpha_base = o.sprite_alpha
o.sprite_flip = particle_data.sprite_flip or o.sprite_flip
o.animation_active = particle_data.animation_active or false
o.time = 0.5
o.timer = 0
o.vel = {
x = o.speed * math.cos(o.direction),
y = o.speed * math.sin(o.direction)
}
if particle_data.light ~= nil then
o.lightRange = particle_data.light
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
end
-- animations
o.body = Animation:New(particle_data.animation)
o:centerOffset(o.body)
if not o.animation_active then
o.body.speed = 0
end
table.insert(LoadedParticles,o)
o.id = #LoadedParticles
setmetatable(o, self)
self.__index = self
return o
end
function Particle:Kill()
if self.light ~= nil then
KillLight(self.light)
end
if self.id ~= nil then
for _, e in pairs(LoadedParticles) do
if e.id > self.id then
e.id = e.id - 1
end
end
table.remove(LoadedParticles,self.id)
end
self = nil
end
function Particle:HandleAnimation()
self.body:Animate()
self.timer = self.timer + current_dt
self.sprite_alpha = self.sprite_alpha_base*(self.time-self.timer)/self.time
if self.light ~= nil then
self.light.range = self.lightRange * self.sprite_alpha/2
end
if self.sprite_alpha < 0 then self:Kill() end
self:Draw(self.body)
end
function Particle:DoPhysics()
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, objects.collisions) then
self.pos.x = self.pos.x + self.vel.x
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then
self.pos.y = self.pos.y + self.vel.y
end
end