From 16d1f688051e2301759d6b222917cc7385f73da5 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 20 Mar 2021 21:08:54 +0100 Subject: [PATCH] feat: add the event system --- sonic-radiance.love/game/events/arguments.lua | 16 +++++ .../game/events/event/init.lua | 4 ++ .../game/events/event/parent.lua | 26 +++++++ .../game/events/event/simpleMessage.lua | 16 +++++ .../game/events/event/wait.lua | 19 +++++ sonic-radiance.love/game/events/init.lua | 71 +++++++++++++++++++ sonic-radiance.love/game/events/utils.lua | 45 ++++++++++++ .../scenes/overworld/actors/gizmo.lua | 6 +- sonic-radiance.love/scenes/overworld/init.lua | 13 ++++ 9 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 sonic-radiance.love/game/events/arguments.lua create mode 100644 sonic-radiance.love/game/events/event/init.lua create mode 100644 sonic-radiance.love/game/events/event/parent.lua create mode 100644 sonic-radiance.love/game/events/event/simpleMessage.lua create mode 100644 sonic-radiance.love/game/events/event/wait.lua create mode 100644 sonic-radiance.love/game/events/init.lua create mode 100644 sonic-radiance.love/game/events/utils.lua diff --git a/sonic-radiance.love/game/events/arguments.lua b/sonic-radiance.love/game/events/arguments.lua new file mode 100644 index 0000000..ab4ded1 --- /dev/null +++ b/sonic-radiance.love/game/events/arguments.lua @@ -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 diff --git a/sonic-radiance.love/game/events/event/init.lua b/sonic-radiance.love/game/events/event/init.lua new file mode 100644 index 0000000..44884aa --- /dev/null +++ b/sonic-radiance.love/game/events/event/init.lua @@ -0,0 +1,4 @@ +return { + ["wait"] = require("game.events.event.wait"), + ["simpleMessage"] = require("game.events.event.simpleMessage") +} \ No newline at end of file diff --git a/sonic-radiance.love/game/events/event/parent.lua b/sonic-radiance.love/game/events/event/parent.lua new file mode 100644 index 0000000..725e2f5 --- /dev/null +++ b/sonic-radiance.love/game/events/event/parent.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/game/events/event/simpleMessage.lua b/sonic-radiance.love/game/events/event/simpleMessage.lua new file mode 100644 index 0000000..9118cc8 --- /dev/null +++ b/sonic-radiance.love/game/events/event/simpleMessage.lua @@ -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; diff --git a/sonic-radiance.love/game/events/event/wait.lua b/sonic-radiance.love/game/events/event/wait.lua new file mode 100644 index 0000000..d81162e --- /dev/null +++ b/sonic-radiance.love/game/events/event/wait.lua @@ -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; diff --git a/sonic-radiance.love/game/events/init.lua b/sonic-radiance.love/game/events/init.lua new file mode 100644 index 0000000..027ffc7 --- /dev/null +++ b/sonic-radiance.love/game/events/init.lua @@ -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 diff --git a/sonic-radiance.love/game/events/utils.lua b/sonic-radiance.love/game/events/utils.lua new file mode 100644 index 0000000..5a27eec --- /dev/null +++ b/sonic-radiance.love/game/events/utils.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/scenes/overworld/actors/gizmo.lua b/sonic-radiance.love/scenes/overworld/actors/gizmo.lua index de39abf..2d13bda 100644 --- a/sonic-radiance.love/scenes/overworld/actors/gizmo.lua +++ b/sonic-radiance.love/scenes/overworld/actors/gizmo.lua @@ -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; \ No newline at end of file diff --git a/sonic-radiance.love/scenes/overworld/init.lua b/sonic-radiance.love/scenes/overworld/init.lua index f13099f..2cc8374 100644 --- a/sonic-radiance.love/scenes/overworld/init.lua +++ b/sonic-radiance.love/scenes/overworld/init.lua @@ -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)