feature/task #29

Merged
PeaAshMeter merged 10 commits from feature/task into main 2026-02-02 02:36:31 +03:00
4 changed files with 48 additions and 29 deletions
Showing only changes of commit 1986228670 - Show all commits

View File

@ -70,7 +70,7 @@ function sprite:draw()
end
--- @return Task<nil>
function sprite:animate(state, node)
function sprite:animate(state)
return function(callback)
if not self.animationGrid[state] then
print("[SpriteBehavior]: no animation for '" .. state .. "'")

View File

@ -7,7 +7,7 @@
--- --TODO: каждый каст должен возвращать объект, который позволит отследить момент завершения анимации спелла
--- Да, это Future/Promise/await/async
local Counter = require 'lib.utils.counter'
local task = require 'lib.utils.task'
--- @class Spell Здесь будет много бойлерплейта, поэтому тоже понадобится спеллмейкерский фреймворк, который просто вернет готовый Spell
--- @field tag string
@ -55,11 +55,7 @@ function walk:cast(caster, target)
return
end
return function(callback)
return caster:has(Tree.behaviors.tiled):followPath(path)(
callback
) -- <- callback вызовется после followPath
end
return caster:has(Tree.behaviors.tiled):followPath(path)
end
function walk:update(caster, dt)
@ -103,14 +99,10 @@ function regenerateMana:cast(caster, target)
Tree.behaviors.positioned.new(caster:has(Tree.behaviors.positioned).position + Vec3 { 0.5, 0.5 }),
}
return function(callback)
light:has(Tree.behaviors.light):animateColor(Vec3 {})(
function()
light:die()
end
)
sprite:animate("hurt")(callback)
end
return task.wait {
light:has(Tree.behaviors.light):animateColor(Vec3 {}),
sprite:animate("hurt")
}
end
local attack = setmetatable({}, spell)
@ -143,20 +135,16 @@ function attack:cast(caster, target)
caster:try(Tree.behaviors.positioned, function(b) b:lookAt(target) end)
return function(callback)
local c = Counter(callback)
c.push()
sprite:animate("attack")(c.pop)
c.push()
targetCharacter:has(Tree.behaviors.residentsleeper):sleep(200)(
function()
targetSprite:animate("hurt")(c.pop)
return
task.wait {
sprite:animate("attack"),
task.wait {
task.chain(targetCharacter:has(Tree.behaviors.residentsleeper):sleep(200),
function() return targetSprite:animate("hurt") end
),
Tree.audio:play(Tree.assets.files.audio.sounds.hurt)
end
)
end
}
}
end
----------------------------------------

View File

@ -61,6 +61,23 @@ local function wait(tasks)
end
end
--- Последовательно объединяет два `Task` в один.
--- @generic T
--- @generic R
--- @param task Task<T> `Task`, который выполнится первым
--- @param onCompleted fun(value: T): Task<R> Конструктор второго `Task`. Принимает результат выполнения первого `Task`
--- @return Task<R>
local function chain(task, onCompleted)
return function(callback)
task(function(value)
local task2 = onCompleted(value)
task2(callback)
end)
end
end
return {
wait = wait,
chain = chain
}

View File

@ -41,7 +41,21 @@ function test:run(complete)
print("task.wait completed in " .. dt .. " sec", "t1 = " .. t1 - t0, "t2 = " .. t2 - t0)
complete()
t0 = love.timer.getTime()
task.chain(task1(), function(value)
t1 = value
assert(t1 - t0 >= 1)
return task2()
end)(
function(value)
t2 = value
assert(t2 - t0 >= 2)
print("task.chain completed in " .. t2 - t0 .. " sec")
complete()
end
)
end)
end