polf/systems/rounds.lua

108 lines
3.5 KiB
Lua

local gridElements = filteredSystem("gridElements", { gridPosition = T.XyPair, effectsToApply = T.AnyComponent })
filteredSystem("timers", { timerSec = T.number, callback = T.SelfFunction }, function(e, dt, system)
e.timerSec = e.timerSec - dt
if e.timerSec < 0 then
e:callback()
system.world:removeEntity(e)
end
end)
local function sign(n)
return n > 0 and 1 or n < 0 and -1 or 0
end
local roundSystem = filteredSystem("rounds", { roundState = T.str }, function(e)
print("roundState: " .. e.roundState)
end)
function roundSystem:preProcess()
if #self.entities ~= 0 then
print("#states: " .. #self.entities)
end
end
local activeBallEffects = filteredSystem(
"activeBallEffects",
{ ballEffects = T.AnyComponent, gridPosition = T.XyPair },
function(e, dt, system)
local gridPosition, effects = e.gridPosition, e.ballEffects
-- Search for new effects from the current tile
for _, gridElement in pairs(gridElements.entities) do
if gridPosition.x == gridElement.gridPosition.x and gridPosition.y == gridElement.gridPosition.y then
-- More direct-mutation-y than I'd like,
-- but offers a simple way to overwrite existing effects.
-- We're "setting InRelations" :D
for key, value in pairs(gridElement.effectsToApply) do
effects[key] = value
end
if gridElement.spriteAfterEffect then
gridElement.drawAsSprite = gridElement.spriteAfterEffect
gridElement.spriteAfterEffect = nil
end
gridElement.effectsToApply = nil
system.world:addEntity(gridElement)
end
end
-- Apply any effects currently connected to this ball
if effects.movement ~= nil then
gridPosition.x = gridPosition.x + sign(effects.movement.x)
gridPosition.y = gridPosition.y + sign(effects.movement.y)
effects.movement.x = effects.movement.x - sign(effects.movement.x)
effects.movement.y = effects.movement.y - sign(effects.movement.y)
if effects.movement.x == 0 and effects.movement.y == 0 then
effects.movement = nil
end
end
if effects.endOfRound ~= nil then
effects.endOfRound = nil
system.world:addEntity({ roundState = "end" })
system.world:removeEntity(e)
print("End of round")
return
end
for _, _ in pairs(effects) do
-- Return if there are any effects left
print("Setting roundState to animating...")
--system.world:addEntity({ roundState = "animating" })
--system.world:removeEntity(e)
return
end
print("End of round")
system.world:addEntity({ roundState = "end" })
end
)
local stepTimeSec = 0.1
local stepTimer = stepTimeSec
function activeBallEffects:preProcess(dt)
stepTimer = stepTimer - dt
if stepTimer <= 0 then
stepTimer = stepTimeSec
else
return tiny.SKIP_PROCESS
end
-- for _, round in pairs(roundSystem.entities) do
-- if round.roundState == "animating" then
-- return tiny.SKIP_PROCESS
-- end
-- end
for _, round in pairs(roundSystem.entities) do
if round.roundState == "active" then
return
end
end
return tiny.SKIP_PROCESS
end