d8c9a652ae
Fix #5
100 lines
2.2 KiB
Lua
100 lines
2.2 KiB
Lua
local EventManager = Object:extend()
|
|
|
|
local stepObjectList = require "game.events.event"
|
|
local eventUtils = require "game.events.utils"
|
|
|
|
|
|
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:haveFlag(flagToTest)
|
|
if (flagToTest == nil or flagToTest == "") then
|
|
return true
|
|
end
|
|
for i, flag in ipairs(self.flags) do
|
|
if (flag == flagToTest) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
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:haveFlag(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
|