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,
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},
{ },
{ 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,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, 5,49,50, 6,49,50, 7, 0, 5,49,50, 7},
{ 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},
{ 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},
{ 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}
},
objects = {}

View File

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

View File

@ -13,6 +13,18 @@ function EditorStep()
selecting_tile = 51
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
function EditorScroll(y)
@ -29,12 +41,87 @@ function EditorScroll(y)
end
function EditorDraw()
GameworldDraw()
GameworldDrawPrepare()
GameworldDrawBackground()
GridDisplay()
GameworldDrawForeground()
GameworldDrawEnd()
DrawSelectingPaletteTile()
if palette then
EditorDoPalette()
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()
local width = LevelData.tileset:getPixelWidth()/tileProperties.width
@ -71,17 +158,17 @@ function EditorDoPalette()
local mouse_x = love.mouse.getX()
local mouse_y = love.mouse.getY()
if mouse_x > tile_x
and mouse_x < tile_x + tileProperties.width + 1
and mouse_y > tile_y
and mouse_y < tile_y + tileProperties.height + 1
if mouse_x > (tile_x) * game.scale
and mouse_x < (tile_x + tileProperties.width) * game.scale
and mouse_y > (tile_y) * game.scale
and mouse_y < (tile_y + tileProperties.height) * game.scale
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)
end
else
selecting_tile = 0
--selecting_tile = nil
end
end
if selecting_tile ~= nil and i == selecting_tile then

View File

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

View File

@ -66,10 +66,5 @@ function Fairy:DoPhysics()
self.vel.x = self.vel.x + random_x
self.vel.y = self.vel.y + random_y
-- move
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
self:CollisionMove()
end

View File

@ -73,10 +73,5 @@ function Particle:HandleAnimation()
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
self:Move()
end

View File

@ -144,13 +144,13 @@ function Player:DoPhysics()
self.vel.y = self.vel.y + gravity
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
else
self.vel.x = 0
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
else
if self.vel.y > 0 then

View File

@ -31,6 +31,20 @@ end
function Entity:Smart()
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()
if self.light ~= nil then
KillLight(self.light)

View File

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

View File

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

View File

@ -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],

View File

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