Add ResidentSleeper behavior for async animation waiting

This commit is contained in:
PeaAshMeter 2025-10-22 03:50:01 +03:00
parent 5de9d9eb9c
commit 601766d5e8
4 changed files with 43 additions and 5 deletions

View File

@ -1,5 +1,6 @@
--- @meta _
Tree.behaviors.map = require "lib.character.behaviors.map"
Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster"
Tree.behaviors.sprite = require "lib.character.behaviors.sprite"
Tree.behaviors.stats = require "lib.character.behaviors.stats"
Tree.behaviors.map = require "lib.character.behaviors.map"
Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster"
Tree.behaviors.sprite = require "lib.character.behaviors.sprite"
Tree.behaviors.stats = require "lib.character.behaviors.stats"
Tree.behaviors.residentsleeper = require "lib.character.behaviors.residentsleeper"

View File

@ -0,0 +1,28 @@
--- Умеет асинхронно ждать какое-то время (для анимаций)
--- @class ResidentSleeperBehavior : Behavior
--- @field animationNode? AnimationNode
--- @field endsAt? number
local behavior = {}
behavior.__index = behavior
behavior.id = "residentsleeper"
function behavior.new() return setmetatable({}, behavior) end
function behavior:update(dt)
if not self.animationNode then return end
if love.timer.getTime() >= self.endsAt then
self.animationNode:finish()
self.animationNode = nil
self.endsAt = nil
end
end
--- @param ms number time to wait in milliseconds
--- @param node AnimationNode
function behavior:sleep(ms, node)
if self.animationNode then node:finish() end
self.animationNode = node
self.endsAt = love.timer.getTime() + ms / 1000
end
return behavior

View File

@ -27,6 +27,7 @@ local function spawn(name, spriteDir, position, size, level)
char._behaviorsIdx = {}
char:addBehavior {
Tree.behaviors.residentsleeper.new(),
Tree.behaviors.stats.new(),
Tree.behaviors.map.new(position, size),
Tree.behaviors.sprite.new(spriteDir),

View File

@ -46,8 +46,16 @@ function walk:cast(caster, target)
children = {
Animation {
function(node)
caster:has(Tree.behaviors.map):followPath(path, node)
caster:has(Tree.behaviors.sprite):loop('idle')
caster:has(Tree.behaviors.residentsleeper):sleep(1000, node)
end,
children = {
Animation {
function(node)
caster:has(Tree.behaviors.map):followPath(path, node)
end,
}
}
},
Animation {
function(node)