diff --git a/lib/level/turn_order.lua b/lib/level/turn_order.lua index d4d09d9..33200e6 100644 --- a/lib/level/turn_order.lua +++ b/lib/level/turn_order.lua @@ -37,7 +37,9 @@ function turnOrder:next() local char = Tree.level.characters[self.current] char:try(Tree.behaviors.ai, function(ai) + Tree.level.selector:lock() ai:makeTurn()(function() + Tree.level.selector:unlock() self:next() end) end) diff --git a/lib/spellbook.lua b/lib/spellbook.lua index 6c8c51e..1767c4f 100644 --- a/lib/spellbook.lua +++ b/lib/spellbook.lua @@ -56,7 +56,37 @@ function walk:cast(caster, target) return 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 function walk:update(caster, dt) diff --git a/lib/task.lua b/lib/task.lua deleted file mode 100644 index e69de29..0000000 diff --git a/lib/utils/counter.lua b/lib/utils/counter.lua new file mode 100644 index 0000000..9e8273f --- /dev/null +++ b/lib/utils/counter.lua @@ -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