chore: divide choregraphy into mixins

This commit is contained in:
Kazhnuz 2021-08-08 09:49:43 +02:00
parent 9321eb411f
commit 01b450fef7
7 changed files with 254 additions and 184 deletions

View file

@ -1,186 +1,31 @@
local ChoregraphySystem = Object:extend() local ChoregraphySystem = Object:extend()
local stepObjectList = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.step" local QteMixin = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.mixins.qtes"
local qteObjectList = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.qte" local StepsMixin = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.mixins.steps"
local TagsMixin = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.mixins.tags"
local CountersMixin = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.mixins.counters"
local WrappersMixin = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.mixins.wrappers"
local Predicate = require "birb.classes.predicate" ChoregraphySystem:implement(QteMixin)
local Conditions = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.conditions" ChoregraphySystem:implement(StepsMixin)
ChoregraphySystem:implement(TagsMixin)
local ACTION_STARTED = 1 ChoregraphySystem:implement(CountersMixin)
local ACTION_FINISHED = 2 ChoregraphySystem:implement(WrappersMixin)
function ChoregraphySystem:new(action, choregraphy) function ChoregraphySystem:new(action, choregraphy)
self.action = action self:initWrappers(action)
self.fighter = action.fighter
self.actor = self.fighter.actor self:initSteps(choregraphy)
self:initQte()
self:initTagActions()
self:initCounters()
self.actor:resetFrameSignal() self.actor:resetFrameSignal()
self.assets = self.fighter.actor.assets
self.world = self.actor.world
self.target = action.target
self.haveSentDamage = false
self.currentStepId = 0
self.currentStep = nil
self.stepList = choregraphy
self.qte = {}
self.qte.current = nil
self.qte.wasSuccess = false
self.qte.isActive = false
self.qte.list = {}
self.finishedTagActions = {}
self.counters = {}
end end
function ChoregraphySystem:update(dt) function ChoregraphySystem:update(dt)
if (self.qte.current ~= nil) then self:updateQte(dt)
self.qte.current:updateQte(dt, self.qte.isActive) self:updateSteps(dt)
end
if (self.currentStep ~= nil) then
self.currentStep:updateStep(dt)
else
self:switchStep()
end
end
function ChoregraphySystem:findTaggedAction(tag)
for stepId, step in ipairs(self.stepList) do
if (step[1] == "taggedAction") and (step[2] == tag) then
return stepId
end
end
return 0
end
function ChoregraphySystem:skipToStepByTag(tag)
self:skipToStep(self:findTaggedAction(tag))
end
function ChoregraphySystem:skipToStep(id)
if (self.stepList[id] ~= nil) then
self.currentStepId = id - 1
self:switchStep()
end
end
function ChoregraphySystem:switchStep()
if self:haveNextStep() then
self.currentStepId = self.currentStepId + 1
local stepData, tagName = self:parseStep(self.stepList[self.currentStepId])
if (stepObjectList[stepData.name] ~= nil and self:checkCondition(stepData.condition)) then
self.currentStep = stepObjectList[stepData.name](self, stepData.arguments)
if (not utils.string.isEmpty(tagName)) then
self.currentStep:addTag(tagName)
self:startTagAction(tagName)
end
end
else
self:endChoregraphy()
end
end
function ChoregraphySystem:parseStep(step)
local tagName = ""
if (step[1] == "taggedAction") then
tagName = step[2]
step = step[3]
end
local stepData = core.datas:parse("choregraphystep", step)
core.debug:print("cbs/choregraphy", "Starting step " .. stepData.name)
return stepData, tagName
end
function ChoregraphySystem:checkCondition(condition)
local predicate = Predicate.createPredicate(condition, Conditions, self)
return predicate:solve()
end
function ChoregraphySystem:addQTE(action, origin, qteBaseData, blockProcess, tag)
local qteData = core.datas:parse("qtesteps", qteBaseData)
if (qteObjectList[qteData.name] ~= nil) then
core.debug:print("cbs/choregraphy", "Starting qte " .. qteData.name)
self.qte.current = qteObjectList[qteData.name](self, qteData.arguments, 0, 0)
self.qte.current:blockAction(action, blockProcess)
self.qte.current:setOrigin(origin)
self.qte.current:setTag(tag)
self.qte.isActive = true
end
end
function ChoregraphySystem:isQteSuccess(qteID)
qteID = qteID or #self.qte.list
return self.qte.list[qteID]
end
function ChoregraphySystem:endQte(success)
self.qte.isActive = false
self.qte.wasSuccess = success
table.insert(self.qte.list, success)
end
function ChoregraphySystem:removeQte()
self.qte.current = nil
end
function ChoregraphySystem:testTagAction(tag, statut)
local tagStatut = self.finishedTagActions[tag] or 0
return (statut <= tagStatut)
end
function ChoregraphySystem:startTagAction(tag)
self.finishedTagActions[tag] = ACTION_STARTED
end
function ChoregraphySystem:finishTagAction(tag)
core.debug:print("choregraphy/step", "Tag action " .. tag .. " finished.")
self.finishedTagActions[tag] = ACTION_FINISHED
end
function ChoregraphySystem:getCounter(counterName)
return (self.counters[counterName] or 0)
end
function ChoregraphySystem:setCounter(counterName, number, relative)
if (relative == true) then
number = number + self:getCounter(counterName)
end
self.counters[counterName] = number
end
function ChoregraphySystem:haveFrameSignal(signal)
return self.actor:haveFrameSignal(signal)
end
function ChoregraphySystem:getActor(name)
if (name == "actor") then
return self.actor
elseif (name == "target") then
return self.target.actor
else
return self.fighter.world:getActorByName(name)
end
end
function ChoregraphySystem:sendDamage(power, type, element, isSpecial)
if (self.target ~= nil) then
if (self.fighter.isAlive) then
self.haveSentDamage = self.fighter:sendDamage(self.target, power, type, element, isSpecial)
else
self.haveSentDamage = false
end
end
end
function ChoregraphySystem:useItemEffect()
self.action:useItemEffect()
end
function ChoregraphySystem:endStep()
self.currentStep = nil
end end
function ChoregraphySystem:endChoregraphy() function ChoregraphySystem:endChoregraphy()
@ -189,14 +34,8 @@ function ChoregraphySystem:endChoregraphy()
self.fighter.turnSystem:applyDeath() self.fighter.turnSystem:applyDeath()
end end
function ChoregraphySystem:haveNextStep()
return ((self.currentStepId + 1) <= #self.stepList)
end
function ChoregraphySystem:draw() function ChoregraphySystem:draw()
if (self.qte.current ~= nil) then self:drawQte()
self.qte.current:draw()
end
end end
return ChoregraphySystem return ChoregraphySystem

View file

@ -0,0 +1,18 @@
local CountersMixin = Object:extend()
function CountersMixin:initCounters()
self.counters = {}
end
function CountersMixin:getCounter(counterName)
return (self.counters[counterName] or 0)
end
function CountersMixin:setCounter(counterName, number, relative)
if (relative == true) then
number = number + self:getCounter(counterName)
end
self.counters[counterName] = number
end
return CountersMixin

View file

@ -0,0 +1,53 @@
local QteMixin = Object:extend()
local qteObjectList = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.qte"
function QteMixin:initQte()
self.qte = {}
self.qte.current = nil
self.qte.wasSuccess = false
self.qte.isActive = false
self.qte.list = {}
end
function QteMixin:updateQte(dt)
if (self.qte.current ~= nil) then
self.qte.current:updateQte(dt, self.qte.isActive)
end
end
function QteMixin:isQteSuccess(qteID)
qteID = qteID or #self.qte.list
return self.qte.list[qteID]
end
function QteMixin:addQTE(action, origin, qteBaseData, blockProcess, tag)
local qteData = core.datas:parse("qtesteps", qteBaseData)
if (qteObjectList[qteData.name] ~= nil) then
core.debug:print("cbs/choregraphy", "Starting qte " .. qteData.name)
self.qte.current = qteObjectList[qteData.name](self, qteData.arguments, 0, 0)
self.qte.current:blockAction(action, blockProcess)
self.qte.current:setOrigin(origin)
self.qte.current:setTag(tag)
self.qte.isActive = true
end
end
function QteMixin:endQte(success)
self.qte.isActive = false
self.qte.wasSuccess = success
table.insert(self.qte.list, success)
end
function QteMixin:removeQte()
self.qte.current = nil
end
function QteMixin:drawQte()
if (self.qte.current ~= nil) then
self.qte.current:draw()
end
end
return QteMixin

View file

@ -0,0 +1,74 @@
local StepsMixins = Object:extend()
local Predicate = require "birb.classes.predicate"
local Conditions = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.conditions"
local stepObjectList = require "scenes.battlesystem.controllers.fighters.systems.choregraphy.step"
function StepsMixins:initSteps(choregraphy)
self.currentStepId = 0
self.currentStep = nil
self.stepList = choregraphy
end
function StepsMixins:haveNextStep()
return ((self.currentStepId + 1) <= #self.stepList)
end
-- UPDATE
function StepsMixins:updateSteps(dt)
if (self.currentStep ~= nil) then
self.currentStep:updateStep(dt)
else
self:switchStep()
end
end
function StepsMixins:checkCondition(condition)
local predicate = Predicate.createPredicate(condition, Conditions, self)
return predicate:solve()
end
function StepsMixins:parseStep(step)
local tagName = ""
if (step[1] == "taggedAction") then
tagName = step[2]
step = step[3]
end
local stepData = core.datas:parse("choregraphystep", step)
core.debug:print("cbs/choregraphy", "Starting step " .. stepData.name)
return stepData, tagName
end
function StepsMixins:switchStep()
if self:haveNextStep() then
self.currentStepId = self.currentStepId + 1
local stepData, tagName = self:parseStep(self.stepList[self.currentStepId])
if (stepObjectList[stepData.name] ~= nil and self:checkCondition(stepData.condition)) then
self.currentStep = stepObjectList[stepData.name](self, stepData.arguments)
self.currentStep:addTag(tagName)
end
else
self:endChoregraphy()
end
end
-- SKIP OR END STEPS
function StepsMixins:skipToStepByTag(tag)
self:skipToStep(self:findTaggedAction(tag))
end
function StepsMixins:skipToStep(id)
if (self.stepList[id] ~= nil) then
self.currentStepId = id - 1
self:switchStep()
end
end
function StepsMixins:endStep()
self.currentStep = nil
end
return StepsMixins

View file

@ -0,0 +1,33 @@
local TagsMixin = Object:extend()
local ACTION_STARTED = 1
local ACTION_FINISHED = 2
function TagsMixin:initTagActions()
self.finishedTagActions = {}
end
function TagsMixin:findTaggedAction(tag)
for stepId, step in ipairs(self.stepList) do
if (step[1] == "taggedAction") and (step[2] == tag) then
return stepId
end
end
return 0
end
function TagsMixin:testTagAction(tag, statut)
local tagStatut = self.finishedTagActions[tag] or 0
return (statut <= tagStatut)
end
function TagsMixin:startTagAction(tag)
self.finishedTagActions[tag] = ACTION_STARTED
end
function TagsMixin:finishTagAction(tag)
core.debug:print("choregraphy/step", "Tag action " .. tag .. " finished.")
self.finishedTagActions[tag] = ACTION_FINISHED
end
return TagsMixin

View file

@ -0,0 +1,50 @@
local InfosMixin = Object:extend()
function InfosMixin:initWrappers(action)
self.action = action
self.fighter = action.fighter
self.actor = self.fighter.actor
self.assets = self.fighter.actor.assets
self.world = self.actor.world
self:initTargets(action.target)
end
function InfosMixin:initTargets(target)
self.target = target
self.haveSentDamage = false
end
function InfosMixin:getActor(name)
if (name == "actor") then
return self.actor
elseif (name == "target") then
return self:getTargetActor()
else
return self.fighter.world:getActorByName(name)
end
end
function InfosMixin:getTargetActor()
return self.target.actor
end
function InfosMixin:sendDamage(power, type, element, isSpecial)
if (self.target ~= nil) then
if (self.fighter.isAlive) then
self.haveSentDamage = self.fighter:sendDamage(self.target, power, type, element, isSpecial)
else
self.haveSentDamage = false
end
end
end
function InfosMixin:haveFrameSignal(signal)
return self.actor:haveFrameSignal(signal)
end
function InfosMixin:useItemEffect()
self.action:useItemEffect()
end
return InfosMixin

View file

@ -9,9 +9,12 @@ function StepParent:new(choregraphySystem, arguments, canBeAsync)
end end
function StepParent:addTag(tag) function StepParent:addTag(tag)
if (self.canBeAsync) then if (not utils.string.isEmpty(tag)) then
core.debug:print("choregraphy/step", "Tag " .. tag .. " added to step.") if (self.canBeAsync) then
self.tag = tag core.debug:print("choregraphy/step", "Tag " .. tag .. " added to step.")
self.tag = tag
end
self.choregraphy:startTagAction(tag)
end end
end end