From 601766d5e8cbbb100d4f26ea60348d8f6a09b3a2 Mon Sep 17 00:00:00 2001 From: PeaAshMeter Date: Wed, 22 Oct 2025 03:50:01 +0300 Subject: [PATCH] Add ResidentSleeper behavior for async animation waiting --- dev_utils/annotations.lua | 9 ++++--- lib/character/behaviors/residentsleeper.lua | 28 +++++++++++++++++++++ lib/character/character.lua | 1 + lib/spellbook.lua | 10 +++++++- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 lib/character/behaviors/residentsleeper.lua diff --git a/dev_utils/annotations.lua b/dev_utils/annotations.lua index e8b0fdd..9753b5d 100644 --- a/dev_utils/annotations.lua +++ b/dev_utils/annotations.lua @@ -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" diff --git a/lib/character/behaviors/residentsleeper.lua b/lib/character/behaviors/residentsleeper.lua new file mode 100644 index 0000000..6c5af5f --- /dev/null +++ b/lib/character/behaviors/residentsleeper.lua @@ -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 diff --git a/lib/character/character.lua b/lib/character/character.lua index 4be416f..4e7aec1 100644 --- a/lib/character/character.lua +++ b/lib/character/character.lua @@ -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), diff --git a/lib/spellbook.lua b/lib/spellbook.lua index 5989514..334c712 100644 --- a/lib/spellbook.lua +++ b/lib/spellbook.lua @@ -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)