diff --git a/lib/character/behaviors/map.lua b/lib/character/behaviors/map.lua index c1573ca..535087e 100644 --- a/lib/character/behaviors/map.lua +++ b/lib/character/behaviors/map.lua @@ -27,8 +27,8 @@ end function mapBehavior:followPath(path) if path:is_empty() then return end self.position = self.displayedPosition - self.owner:try(Tree.behaviors.animated, function(animated) - animated:play("run", true) + self.owner:try(Tree.behaviors.sprite, function(sprite) + sprite:play("run", true) end) self.path = path; ---@type Vec3 @@ -41,12 +41,12 @@ end function mapBehavior:runTo(target) self.t0 = love.timer.getTime() self.runTarget = target - self.owner:try(Tree.behaviors.animated, - function(animated) + self.owner:try(Tree.behaviors.sprite, + function(sprite) 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 - animated.side = Tree.behaviors.animated.RIGHT + sprite.side = Tree.behaviors.sprite.RIGHT end end ) @@ -63,8 +63,8 @@ function mapBehavior:update(dt) self:runTo(self.path:peek_front()) self.path:pop_front() else -- мы добежали до финальной цели - self.owner:try(Tree.behaviors.animated, function(animated) - animated:play("idle", true) + self.owner:try(Tree.behaviors.sprite, function(sprite) + sprite:play("idle", true) end) self.runTarget = nil end diff --git a/lib/character/behaviors/animated.lua b/lib/character/behaviors/sprite.lua similarity index 76% rename from lib/character/behaviors/animated.lua rename to lib/character/behaviors/sprite.lua index d3784ea..89eecb7 100644 --- a/lib/character/behaviors/animated.lua +++ b/lib/character/behaviors/sprite.lua @@ -1,21 +1,21 @@ local anim8 = require "lib.utils.anim8" ---- @class AnimatedBehavior : Behavior +--- @class SpriteBehavior : Behavior --- @field animationTable table --- @field animationGrid table --- @field state "idle"|"run"|"hurt"|"attack" --- @field side 1|-1 -local animated = {} -animated.__index = animated -animated.id = "animated" -animated.dependencies = { Tree.behaviors.map } -animated.LEFT = -1 -animated.RIGHT = 1 +local sprite = {} +sprite.__index = sprite +sprite.id = "sprite" +sprite.dependencies = { Tree.behaviors.map } +sprite.LEFT = -1 +sprite.RIGHT = 1 --- Скорость между кадрами в анимации -animated.ANIMATION_SPEED = 0.1 +sprite.ANIMATION_SPEED = 0.1 -function animated.new(spriteDir) - local anim = setmetatable({}, animated) +function sprite.new(spriteDir) + local anim = setmetatable({}, sprite) anim.animationTable = {} anim.animationGrid = {} @@ -27,19 +27,19 @@ function animated.new(spriteDir) end anim.state = "idle" - anim.side = animated.RIGHT + anim.side = sprite.RIGHT anim:play("idle") return anim end -function animated:update(dt) +function sprite:update(dt) local anim = self.animationTable[self.state] or self.animationTable["idle"] or nil if not anim then return end anim:update(dt) 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 self.owner:try(Tree.behaviors.map, @@ -57,9 +57,9 @@ end --- @param state string --- @param loop boolean | nil -function animated:play(state, loop) +function sprite:play(state, loop) if not self.animationGrid[state] then - return print("[AnimatedBehavior]: no animation for '" .. state .. "'") + return print("[SpriteBehavior]: no animation for '" .. state .. "'") end self.animationTable[state] = anim8.newAnimation(self.animationGrid[state], self.ANIMATION_SPEED, function() if not loop then self:play("idle", true) end @@ -67,4 +67,4 @@ function animated:play(state, loop) self.state = state end -return animated +return sprite diff --git a/lib/character/character.lua b/lib/character/character.lua index 9968731..fd14504 100644 --- a/lib/character/character.lua +++ b/lib/character/character.lua @@ -30,7 +30,7 @@ local function spawn(name, template, spriteDir, position, size, level) char:addBehavior { Tree.behaviors.map.new(position, size), - Tree.behaviors.animated.new(spriteDir), + Tree.behaviors.sprite.new(spriteDir), Tree.behaviors.spellcaster.new() } diff --git a/lib/tree.lua b/lib/tree.lua index 5c634cd..07a06b4 100644 --- a/lib/tree.lua +++ b/lib/tree.lua @@ -13,4 +13,4 @@ Tree.level = (require "lib.level.level").new("procedural", "flow Tree.behaviors = {} --- @todo написать нормальную загрузку поведений Tree.behaviors.map = require "lib.character.behaviors.map" Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster" -Tree.behaviors.animated = require "lib.character.behaviors.animated" +Tree.behaviors.sprite = require "lib.character.behaviors.sprite" diff --git a/main.lua b/main.lua index 47e2dad..89f65f5 100644 --- a/main.lua +++ b/main.lua @@ -11,6 +11,7 @@ end function love.load() 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 }) end