generated from sage/tiny-ecs-love-template
202 lines
5.7 KiB
Lua
202 lines
5.7 KiB
Lua
require("tiny-debug")
|
|
tiny = require("lib/tiny")
|
|
require("utils")
|
|
require("tiny-tools")
|
|
|
|
World = tiny.world()
|
|
|
|
require("generated/filter-types")
|
|
require("generated/assets")
|
|
require("generated/all-systems")
|
|
|
|
local width, height = love.graphics.getWidth(), love.graphics.getHeight()
|
|
|
|
local squareSize = 80
|
|
local tileSize = math.floor(squareSize * 1.2)
|
|
local marginSize = 10
|
|
|
|
CursorMask = 1
|
|
BallMask = 2
|
|
|
|
local function emptyTile(unhighlightSiblings, gridPosition)
|
|
local size = { x = squareSize, y = squareSize }
|
|
return {
|
|
unhighlightSiblings = unhighlightSiblings,
|
|
canBeCollidedBy = CursorMask,
|
|
highlightOnMouseOver = T.marker,
|
|
size = size,
|
|
gridPosition = gridPosition,
|
|
drawAsRectangle = { size = size },
|
|
}
|
|
end
|
|
|
|
local function replaceAt(grid, y, x, entity)
|
|
local current = grid[y][x]
|
|
grid[y][x] = entity
|
|
|
|
current.unhighlightSiblings[entity] = true
|
|
current.unhighlightSiblings[current] = nil
|
|
entity.unhighlightSiblings = current.unhighlightSiblings
|
|
|
|
-- entity.position = current.position
|
|
entity.gridPosition = current.gridPosition
|
|
entity.toLeft = current.toLeft
|
|
entity.toRight = current.toRight
|
|
entity.above = current.above
|
|
entity.below = current.below
|
|
|
|
current.toLeft.toRight = current.toRight
|
|
current.toRight.toLeft = current.toLeft
|
|
current.above.below = current.below
|
|
current.below.above = current.above
|
|
|
|
World:removeEntity(current)
|
|
World:addEntity(entity)
|
|
end
|
|
|
|
function PositionAtGridXy(x, y)
|
|
return {
|
|
x = 20 + ((x - 1) * tileSize),
|
|
y = 60 + ((y - 1) * tileSize),
|
|
}
|
|
end
|
|
|
|
Scenarios = {
|
|
---@return table xyGrid Where grid[y][x] is an Entity
|
|
emptyGrid = function()
|
|
local cursorSize = { x = 5, y = 5 }
|
|
World:addEntity({
|
|
size = cursorSize,
|
|
moveWithCursor = T.marker,
|
|
canCollideWith = bit.bor(CursorMask, BallMask),
|
|
position = { x = -999, y = -999 },
|
|
highlightsCollided = T.marker,
|
|
drawAsRectangle = {
|
|
size = cursorSize,
|
|
mode = "fill",
|
|
},
|
|
})
|
|
World:addEntity({
|
|
position = { x = 0, y = 0 },
|
|
drawAsSprite = Grass,
|
|
z = 0,
|
|
})
|
|
|
|
-- Temporary storages for connecting grid items
|
|
local previous
|
|
local unhighlightSiblings = {}
|
|
local yxGrid = {}
|
|
|
|
local xCount = 8
|
|
local yCount = 5
|
|
|
|
for y = 1, yCount do
|
|
yxGrid[y] = {}
|
|
for x = 1, xCount do
|
|
local current = World:addEntity(emptyTile(unhighlightSiblings, { x = x, y = y }))
|
|
yxGrid[y][x] = current
|
|
unhighlightSiblings[current] = true
|
|
current.toLeft = previous
|
|
if previous ~= nil then
|
|
if previous.toRight == nil then
|
|
previous.toRight = current
|
|
end
|
|
else
|
|
current.canReceiveButtons = T.marker
|
|
current.highlighted = T.marker
|
|
end
|
|
if yxGrid[y - 1] and yxGrid[y - 1][x] then
|
|
current.above = yxGrid[y - 1][x]
|
|
yxGrid[y - 1][x].below = current
|
|
end
|
|
if y == yCount then
|
|
-- Connect last row to first row
|
|
yxGrid[1][x].above = current
|
|
current.below = yxGrid[1][x]
|
|
end
|
|
if x == xCount then
|
|
-- Connect last entry to first Entry
|
|
current.toRight = yxGrid[y][1]
|
|
yxGrid[y][1].toLeft = current
|
|
end
|
|
previous = current
|
|
end
|
|
end
|
|
return yxGrid
|
|
end,
|
|
|
|
firstLevel = function()
|
|
local yxGrid = Scenarios.emptyGrid()
|
|
replaceAt(yxGrid, 4, 7, {
|
|
drawAsSprite = Flag,
|
|
z = 1,
|
|
effectsToApply = {
|
|
endOfRound = T.marker,
|
|
}
|
|
})
|
|
replaceAt(yxGrid, 2, 2, {
|
|
drawAsSprite = SmallSandTrap,
|
|
z = 1,
|
|
spriteAfterEffect = SmallSandTrapActivated,
|
|
effectsToApply = {
|
|
slowsBy = { x = 1, y = 1 },
|
|
movement = { x = 0, y = 0, },
|
|
},
|
|
})
|
|
replaceAt(yxGrid, 1, 1, {
|
|
pickedUpOnClick = T.marker,
|
|
drawAsSprite = GolferRight,
|
|
z = 1,
|
|
spriteAfterEffect = GolferRightActivated,
|
|
effectsToApply = {
|
|
movement = { x = 6, y = 0, },
|
|
},
|
|
})
|
|
replaceAt(yxGrid, 1, 7, {
|
|
pickedUpOnClick = T.marker,
|
|
drawAsSprite = GolferRight,
|
|
z = 1,
|
|
spriteAfterEffect = GolferRightActivated,
|
|
effectsToApply = {
|
|
movement = { x = 0, y = 3 },
|
|
},
|
|
})
|
|
World:addEntity({
|
|
ballEffects = {},
|
|
drawAsSprite = Ball,
|
|
gridPosition = { x = 1, y = 1 },
|
|
z = 2,
|
|
})
|
|
-- TODO: Make the start button start
|
|
World:addEntity({
|
|
drawAsSprite = StartButton,
|
|
z = 3,
|
|
position = {
|
|
x = width - 120,
|
|
y = height - 50,
|
|
},
|
|
})
|
|
end,
|
|
}
|
|
|
|
Scenarios.firstLevel()
|
|
|
|
function love.load()
|
|
love.graphics.setBackgroundColor(1, 1, 1)
|
|
love.graphics.setFont(EtBt7001Z0xa(32))
|
|
end
|
|
|
|
function love.draw()
|
|
local dt = love.timer.getDelta()
|
|
if love.keyboard.isDown("r") then
|
|
World:clearEntities()
|
|
Scenarios.firstLevel()
|
|
end
|
|
World:update(dt, function(_, system)
|
|
return not system.isDrawSystem
|
|
end)
|
|
World:update(dt, function(_, system)
|
|
return system.isDrawSystem
|
|
end)
|
|
end
|