sonic-radiance/sonic-radiance.love/game/events/init.lua

113 lines
2.7 KiB
Lua
Raw Permalink Normal View History

2021-03-20 21:08:54 +01:00
local EventManager = Object:extend()
local stepObjectList = require "game.events.event"
local eventUtils = require "game.events.utils"
2021-03-22 16:12:15 +01:00
local Conditions = require "game.events.conditions"
2021-03-20 21:08:54 +01:00
function EventManager:new(scene)
self.scene = scene
self.world = scene.world
self.isStarted = false
self.currentStepId = 0
self.currentStep = nil
self.stepList = {}
2021-03-22 14:57:21 +01:00
self.flags = {}
2021-03-20 21:08:54 +01:00
end
function EventManager:startEvent(owner, stepList)
self.stepList = stepList
2021-03-22 14:57:21 +01:00
self.flags = {}
2021-03-20 21:08:54 +01:00
self.owner = owner
self.isStarted = true
self.currentStepId = 0
self.currentStep = nil
self.scene:startEvent()
end
2021-03-22 14:57:21 +01:00
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
2021-03-22 16:12:15 +01:00
function EventManager:testCondition(condition)
if (utils.string.isEmpty(condition)) then
2021-03-22 14:57:21 +01:00
return true
end
2021-03-22 16:12:15 +01:00
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)
2021-03-22 14:57:21 +01:00
end
2021-03-22 16:12:15 +01:00
return conditionFulfilled
end
end
function EventManager:haveFlag(flagToTest)
if (flagToTest == nil or flagToTest == "") then
return true
2021-03-22 14:57:21 +01:00
end
2021-03-22 16:12:15 +01:00
return utils.table.contain(self.flags, flagToTest)
2021-03-22 14:57:21 +01:00
end
2021-03-20 21:08:54 +01:00
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)
2021-03-22 16:12:15 +01:00
if (not self:testCondition(stepData.condition)) then
2021-03-22 14:57:21 +01:00
self:switchStep()
else
if (stepObjectList[stepData.name] ~= nil) then
self.currentStep = stepObjectList[stepData.name](self, stepData.arguments)
end
2021-03-20 21:08:54 +01:00
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