Add ResidentSleeper behavior for async animation waiting
This commit is contained in:
parent
5de9d9eb9c
commit
601766d5e8
@ -1,5 +1,6 @@
|
|||||||
--- @meta _
|
--- @meta _
|
||||||
Tree.behaviors.map = require "lib.character.behaviors.map"
|
Tree.behaviors.map = require "lib.character.behaviors.map"
|
||||||
Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster"
|
Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster"
|
||||||
Tree.behaviors.sprite = require "lib.character.behaviors.sprite"
|
Tree.behaviors.sprite = require "lib.character.behaviors.sprite"
|
||||||
Tree.behaviors.stats = require "lib.character.behaviors.stats"
|
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._behaviorsIdx = {}
|
||||||
|
|
||||||
char:addBehavior {
|
char:addBehavior {
|
||||||
|
Tree.behaviors.residentsleeper.new(),
|
||||||
Tree.behaviors.stats.new(),
|
Tree.behaviors.stats.new(),
|
||||||
Tree.behaviors.map.new(position, size),
|
Tree.behaviors.map.new(position, size),
|
||||||
Tree.behaviors.sprite.new(spriteDir),
|
Tree.behaviors.sprite.new(spriteDir),
|
||||||
|
|||||||
@ -46,8 +46,16 @@ function walk:cast(caster, target)
|
|||||||
children = {
|
children = {
|
||||||
Animation {
|
Animation {
|
||||||
function(node)
|
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,
|
end,
|
||||||
|
children = {
|
||||||
|
Animation {
|
||||||
|
function(node)
|
||||||
|
caster:has(Tree.behaviors.map):followPath(path, node)
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Animation {
|
Animation {
|
||||||
function(node)
|
function(node)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user