rename AnimatedBehavior -> SpriteBehavior

This commit is contained in:
PeaAshMeter 2025-10-06 01:30:17 +03:00
parent 99d523a761
commit 95b94bb701
5 changed files with 27 additions and 26 deletions

View File

@ -27,8 +27,8 @@ end
function mapBehavior:followPath(path) function mapBehavior:followPath(path)
if path:is_empty() then return end if path:is_empty() then return end
self.position = self.displayedPosition self.position = self.displayedPosition
self.owner:try(Tree.behaviors.animated, function(animated) self.owner:try(Tree.behaviors.sprite, function(sprite)
animated:play("run", true) sprite:play("run", true)
end) end)
self.path = path; self.path = path;
---@type Vec3 ---@type Vec3
@ -41,12 +41,12 @@ end
function mapBehavior:runTo(target) function mapBehavior:runTo(target)
self.t0 = love.timer.getTime() self.t0 = love.timer.getTime()
self.runTarget = target self.runTarget = target
self.owner:try(Tree.behaviors.animated, self.owner:try(Tree.behaviors.sprite,
function(animated) function(sprite)
if target.x < self.position.x then if target.x < self.position.x then
animated.side = Tree.behaviors.animated.LEFT sprite.side = Tree.behaviors.sprite.LEFT
elseif target.x > self.position.x then elseif target.x > self.position.x then
animated.side = Tree.behaviors.animated.RIGHT sprite.side = Tree.behaviors.sprite.RIGHT
end end
end end
) )
@ -63,8 +63,8 @@ function mapBehavior:update(dt)
self:runTo(self.path:peek_front()) self:runTo(self.path:peek_front())
self.path:pop_front() self.path:pop_front()
else -- мы добежали до финальной цели else -- мы добежали до финальной цели
self.owner:try(Tree.behaviors.animated, function(animated) self.owner:try(Tree.behaviors.sprite, function(sprite)
animated:play("idle", true) sprite:play("idle", true)
end) end)
self.runTarget = nil self.runTarget = nil
end end

View File

@ -1,21 +1,21 @@
local anim8 = require "lib.utils.anim8" local anim8 = require "lib.utils.anim8"
--- @class AnimatedBehavior : Behavior --- @class SpriteBehavior : Behavior
--- @field animationTable table<string, table> --- @field animationTable table<string, table>
--- @field animationGrid table --- @field animationGrid table
--- @field state "idle"|"run"|"hurt"|"attack" --- @field state "idle"|"run"|"hurt"|"attack"
--- @field side 1|-1 --- @field side 1|-1
local animated = {} local sprite = {}
animated.__index = animated sprite.__index = sprite
animated.id = "animated" sprite.id = "sprite"
animated.dependencies = { Tree.behaviors.map } sprite.dependencies = { Tree.behaviors.map }
animated.LEFT = -1 sprite.LEFT = -1
animated.RIGHT = 1 sprite.RIGHT = 1
--- Скорость между кадрами в анимации --- Скорость между кадрами в анимации
animated.ANIMATION_SPEED = 0.1 sprite.ANIMATION_SPEED = 0.1
function animated.new(spriteDir) function sprite.new(spriteDir)
local anim = setmetatable({}, animated) local anim = setmetatable({}, sprite)
anim.animationTable = {} anim.animationTable = {}
anim.animationGrid = {} anim.animationGrid = {}
@ -27,19 +27,19 @@ function animated.new(spriteDir)
end end
anim.state = "idle" anim.state = "idle"
anim.side = animated.RIGHT anim.side = sprite.RIGHT
anim:play("idle") anim:play("idle")
return anim return anim
end end
function animated:update(dt) function sprite:update(dt)
local anim = self.animationTable[self.state] or self.animationTable["idle"] or nil local anim = self.animationTable[self.state] or self.animationTable["idle"] or nil
if not anim then return end if not anim then return end
anim:update(dt) anim:update(dt)
end end
function animated:draw() function sprite:draw()
if not self.animationTable[self.state] or not Tree.assets.files.sprites.character[self.state] then return end if not self.animationTable[self.state] or not Tree.assets.files.sprites.character[self.state] then return end
self.owner:try(Tree.behaviors.map, self.owner:try(Tree.behaviors.map,
@ -57,9 +57,9 @@ end
--- @param state string --- @param state string
--- @param loop boolean | nil --- @param loop boolean | nil
function animated:play(state, loop) function sprite:play(state, loop)
if not self.animationGrid[state] then if not self.animationGrid[state] then
return print("[AnimatedBehavior]: no animation for '" .. state .. "'") return print("[SpriteBehavior]: no animation for '" .. state .. "'")
end end
self.animationTable[state] = anim8.newAnimation(self.animationGrid[state], self.ANIMATION_SPEED, function() self.animationTable[state] = anim8.newAnimation(self.animationGrid[state], self.ANIMATION_SPEED, function()
if not loop then self:play("idle", true) end if not loop then self:play("idle", true) end
@ -67,4 +67,4 @@ function animated:play(state, loop)
self.state = state self.state = state
end end
return animated return sprite

View File

@ -30,7 +30,7 @@ local function spawn(name, template, spriteDir, position, size, level)
char:addBehavior { char:addBehavior {
Tree.behaviors.map.new(position, size), Tree.behaviors.map.new(position, size),
Tree.behaviors.animated.new(spriteDir), Tree.behaviors.sprite.new(spriteDir),
Tree.behaviors.spellcaster.new() Tree.behaviors.spellcaster.new()
} }

View File

@ -13,4 +13,4 @@ Tree.level = (require "lib.level.level").new("procedural", "flow
Tree.behaviors = {} --- @todo написать нормальную загрузку поведений Tree.behaviors = {} --- @todo написать нормальную загрузку поведений
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.animated = require "lib.character.behaviors.animated" Tree.behaviors.sprite = require "lib.character.behaviors.sprite"

View File

@ -11,6 +11,7 @@ end
function love.load() function love.load()
character.spawn("Hero", "warrior", Tree.assets.files.sprites.character) character.spawn("Hero", "warrior", Tree.assets.files.sprites.character)
character.spawn("Hero", "warrior", Tree.assets.files.sprites.character, Vec3 { 3, 3 })
love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true }) love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true })
end end