TLDR: higher-order functions my beloved
Add counter utility and chain async animations in spell cast Introduce a Counter module to coordinate multiple asynchronous animation callbacks and update spellbook cast to run chained animations sequentially. Also lock selector during AI turns.
This commit is contained in:
parent
93a4961419
commit
86a599723e
@ -37,7 +37,9 @@ function turnOrder:next()
|
|||||||
|
|
||||||
local char = Tree.level.characters[self.current]
|
local char = Tree.level.characters[self.current]
|
||||||
char:try(Tree.behaviors.ai, function(ai)
|
char:try(Tree.behaviors.ai, function(ai)
|
||||||
|
Tree.level.selector:lock()
|
||||||
ai:makeTurn()(function()
|
ai:makeTurn()(function()
|
||||||
|
Tree.level.selector:unlock()
|
||||||
self:next()
|
self:next()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
@ -56,7 +56,37 @@ function walk:cast(caster, target)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
return caster:has(Tree.behaviors.tiled):followPath(path)
|
local testChar = Tree.level.characters[1];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return function(callback) -- <- вызовется после всех анимаций
|
||||||
|
local counter = require 'lib.utils.counter' (callback)
|
||||||
|
counter.push()
|
||||||
|
return caster:has(Tree.behaviors.tiled):followPath(path)(
|
||||||
|
function()
|
||||||
|
do
|
||||||
|
counter.push()
|
||||||
|
local initialPos2 = caster:has(Tree.behaviors.positioned).position:floor()
|
||||||
|
local path2 = require "lib.pathfinder" (initialPos2, Vec3 { 10, math.random(1, 10) })
|
||||||
|
path:pop_front()
|
||||||
|
caster:has(Tree.behaviors.tiled):followPath(path2)(counter.pop)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
counter.push()
|
||||||
|
local testInitialPos = testChar:has(Tree.behaviors.positioned).position:floor()
|
||||||
|
local testPath = require "lib.pathfinder" (testInitialPos, Vec3 { 10, math.random(20, 5) })
|
||||||
|
path:pop_front()
|
||||||
|
testChar:has(Tree.behaviors.tiled):followPath(testPath)(counter.pop)
|
||||||
|
end
|
||||||
|
|
||||||
|
counter.pop()
|
||||||
|
end
|
||||||
|
) -- <- callback вызовется после followPath
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function walk:update(caster, dt)
|
function walk:update(caster, dt)
|
||||||
|
|||||||
38
lib/utils/counter.lua
Normal file
38
lib/utils/counter.lua
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
--- @class Counter
|
||||||
|
--- @field private count integer
|
||||||
|
--- @field private onFinish fun(): nil
|
||||||
|
--- @field private isAlive boolean
|
||||||
|
--- @field push fun():nil добавить 1 к счетчику
|
||||||
|
--- @field pop fun():nil убавить 1 у счетчика
|
||||||
|
local counter = {}
|
||||||
|
counter.__index = counter
|
||||||
|
|
||||||
|
|
||||||
|
--- @private
|
||||||
|
function counter:_push()
|
||||||
|
self.count = self.count + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @private
|
||||||
|
function counter:_pop()
|
||||||
|
self.count = self.count - 1
|
||||||
|
if self.count == 0 and self.isAlive then
|
||||||
|
self.isAlive = false
|
||||||
|
self.onFinish()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param onFinish fun(): nil
|
||||||
|
local function new(onFinish)
|
||||||
|
local t = {
|
||||||
|
count = 0,
|
||||||
|
onFinish = onFinish,
|
||||||
|
isAlive = true,
|
||||||
|
}
|
||||||
|
t.push = function() counter._push(t) end
|
||||||
|
t.pop = function() counter._pop(t) end
|
||||||
|
|
||||||
|
return setmetatable(t, counter)
|
||||||
|
end
|
||||||
|
|
||||||
|
return new
|
||||||
Loading…
x
Reference in New Issue
Block a user