feat: add the event system

This commit is contained in:
Kazhnuz 2021-03-20 21:08:54 +01:00
parent 91fee0a856
commit 16d1f68805
9 changed files with 215 additions and 1 deletions

View file

@ -0,0 +1,16 @@
return {
["wait"] = {"duration"},
["simpleMessage"] = {"message"},
["dialogBox"] = {"message"},
--[name] = {args...},
}
-- The "origin" argument can have the different values
-- - "target" : the action target. if there is multiple target, it'll
-- be equal to the middle of the ennemy line
--
-- - "self" : the fighter's actor position
--
-- - "start" : the fighter's starting position
--
-- - "absolute" : the 0,0 coordinates

View file

@ -0,0 +1,4 @@
return {
["wait"] = require("game.events.event.wait"),
["simpleMessage"] = require("game.events.event.simpleMessage")
}

View file

@ -0,0 +1,26 @@
local StepParent = Object:extend()
function StepParent:new(eventSystem, arguments)
self.events = eventSystem
self.arguments = arguments
self.isStarted = false
end
function StepParent:updateStep(dt)
if (not self.isStarted) then
self:start()
self.isStarted = true
else
self:update(dt)
end
end
function StepParent:finish()
self.events:endStep()
end
function StepParent:draw()
end
return StepParent

View file

@ -0,0 +1,16 @@
local StepParent = require "game.events.event.parent"
local SimpleMessageStep = StepParent:extend()
function SimpleMessageStep:new(controller, args)
SimpleMessageStep.super.new(self, controller, args)
end
function SimpleMessageStep:start()
self.events.scene:showMessage(self.arguments.message)
end
function SimpleMessageStep:update(dt)
self:finish()
end
return SimpleMessageStep;

View file

@ -0,0 +1,19 @@
local StepParent = require "game.events.event.parent"
local WaitStep = StepParent:extend()
function WaitStep:new(controller, args)
WaitStep.super.new(self, controller, args)
end
function WaitStep:start()
self.timer = 0
end
function WaitStep:update(dt)
self.timer = self.timer + dt
if (self.timer > self.arguments.duration) then
self:finish()
end
end
return WaitStep;

View file

@ -0,0 +1,71 @@
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 = {}
end
function EventManager:startEvent(owner, stepList)
self.stepList = stepList
self.owner = owner
self.isStarted = true
self.currentStepId = 0
self.currentStep = nil
self.scene:startEvent()
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 (stepObjectList[stepData.name] ~= nil) then
self.currentStep = stepObjectList[stepData.name](self, stepData.arguments)
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

View file

@ -0,0 +1,45 @@
EventUtils = {}
-- steps utils
function EventUtils.getStepStructure(stepName)
local stepTypeList = require "game.events.arguments"
return stepTypeList[stepName]
end
function EventUtils.stepExists(stepName)
return (EventUtils.getStepStructure(stepName) ~= nil)
end
function EventUtils.validateStep(stepBaseDatas)
local structure = EventUtils.getStepStructure(stepBaseDatas[1])
if (structure == nil) then
return false
else
return ((#structure + 2) == #stepBaseDatas)
end
end
function EventUtils.getStepDatas(stepBaseDatas)
local stepData = {}
stepData.name = stepBaseDatas[1]
if (EventUtils.validateStep(stepBaseDatas)) then
stepData.condition = stepBaseDatas[2]
local structure = EventUtils.getStepStructure(stepData.name)
stepData.arguments = {}
for i, argumentName in ipairs(structure) do
local argumentContent = stepBaseDatas[i + 2]
stepData.arguments[argumentName] = argumentContent
end
return stepData
else
error("L'étape " .. stepData.name .. " à un nbr d'argument incorrect")
end
end
return EventUtils

View file

@ -2,6 +2,10 @@ local cwd = (...):gsub('%.gizmo$', '') .. "."
local Parent = require(cwd .. "parent")
local Gizmo = Parent:extend()
local defaultEffect = {
{"simpleMessage", "", "ERROR 000 NO EVENT"},
}
function Gizmo:new(world, x, y, w, h, overrides)
local w = w or 16;
local h = h or 16;
@ -45,7 +49,7 @@ function Gizmo:replaceProperties(properties)
end
function Gizmo:doAction()
self.scene:showMessage("I AM ERROR " .. self.creationID)
self.scene.events:startEvent(self, defaultEffect)
end
return Gizmo;

View file

@ -32,6 +32,8 @@ local screens = require "scenes.overworld.screens"
local gui = require "game.modules.gui"
local TweenManager = require "game.modules.tweenmanager"
local EventManager = require "game.events"
local PLAYER_MESSAGE = 240 - 32
function MovePlayer:new()
@ -60,6 +62,16 @@ function MovePlayer:new()
self.timeBorder = -10
self.message = "Test de message"
self.messageOpacity = 0
self.events = EventManager(self)
end
function MovePlayer:startEvent()
self.world.isActive = false
end
function MovePlayer:endEvent()
self.world.isActive = true
end
function MovePlayer:showMessage(message)
@ -79,6 +91,7 @@ end
function MovePlayer:update(dt)
local keys = self:getKeys(1)
self.tweens:update(dt)
self.events:update(dt)
if (self.world.isActive) then
self.charsetManager:update(dt)