sonic-radiance/sonic-radiance.love/game/events/init.lua
2021-03-22 16:12:15 +01:00

113 lines
2.7 KiB
Lua

local EventManager = Object:extend()
local stepObjectList = require "game.events.event"
local eventUtils = require "game.events.utils"
local Conditions = require "game.events.conditions"
function EventManager:new(scene)
self.scene = scene
self.world = scene.world
self.isStarted = false
self.currentStepId = 0
self.currentStep = nil
self.stepList = {}
self.flags = {}
end
function EventManager:startEvent(owner, stepList)
self.stepList = stepList
self.flags = {}
self.owner = owner
self.isStarted = true
self.currentStepId = 0
self.currentStep = nil
self.scene:startEvent()
end
function EventManager:addFlag(flagname, flagcontent)
if (flagcontent == true) then
flagcontent = "V"
end
if (flagcontent == false) then
flagcontent = "F"
end
local flag = flagname .. ":" .. flagcontent
table.insert(self.flags, flag)
end
function EventManager:testCondition(condition)
if (utils.string.isEmpty(condition)) then
return true
end
local conditionArgs = utils.string.split(condition, ":")
if (Conditions[conditionArgs[1]] == nil) then
return self:haveFlag(condition)
else
local conditionFulfilled = Conditions[conditionArgs[1]](conditionArgs)
if (conditionArgs[#conditionArgs] == "not") then
conditionFulfilled = (not conditionFulfilled)
end
return conditionFulfilled
end
end
function EventManager:haveFlag(flagToTest)
if (flagToTest == nil or flagToTest == "") then
return true
end
return utils.table.contain(self.flags, flagToTest)
end
function EventManager:update(dt)
if (self.isStarted) then
if (self.currentStep ~= nil) then
self.currentStep:updateStep(dt)
else
self:switchStep()
end
end
end
function EventManager:switchStep()
if self:haveNextStep() then
self.currentStepId = self.currentStepId + 1
local stepData = eventUtils.getStepDatas(self.stepList[self.currentStepId])
core.debug:print("event", "Starting step " .. stepData.name)
if (not self:testCondition(stepData.condition)) then
self:switchStep()
else
if (stepObjectList[stepData.name] ~= nil) then
self.currentStep = stepObjectList[stepData.name](self, stepData.arguments)
end
end
else
self:endEvent()
end
end
function EventManager:endStep()
self.currentStep = nil
end
function EventManager:endEvent()
core.debug:print("event", "Ending event, giving back controle")
self.scene:endEvent()
self.isStarted = false
end
function EventManager:haveNextStep()
return ((self.currentStepId + 1) <= #self.stepList)
end
function EventManager:draw()
if (self.isStarted) then
if (self.currentStep ~= nil) then
self.currentStep:draw()
end
end
end
return EventManager