Add ResidentSleeper behavior for async animation waiting
This commit is contained in:
parent
5de9d9eb9c
commit
601766d5e8
@ -3,3 +3,4 @@ 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"
|
||||
|
||||
28
lib/character/behaviors/residentsleeper.lua
Normal file
28
lib/character/behaviors/residentsleeper.lua
Normal 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
|
||||
@ -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),
|
||||
|
||||
@ -43,11 +43,19 @@ function walk:cast(caster, target)
|
||||
Animation {
|
||||
function(node) caster:has(Tree.behaviors.sprite):animate("hurt", node) end,
|
||||
onEnd = function() caster:has(Tree.behaviors.spellcaster):endCast() end,
|
||||
children = {
|
||||
Animation {
|
||||
function(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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user