diff --git a/lib/character/behaviors/behavior.lua b/lib/character/behaviors/behavior.lua index 2282389..37e6775 100644 --- a/lib/character/behaviors/behavior.lua +++ b/lib/character/behaviors/behavior.lua @@ -2,14 +2,12 @@ --- @class Behavior --- @field id string --- @field owner Character ---- @field dependencies Behavior[] --- @field new fun(...) : self --- @field update fun(self, dt): nil --- @field draw fun(self): nil local behavior = {} behavior.__index = behavior behavior.id = "behavior" -behavior.dependencies = {} function behavior.new() return setmetatable({}, behavior) end diff --git a/lib/character/behaviors/sprite.lua b/lib/character/behaviors/sprite.lua index 0e35f8b..2cb65bf 100644 --- a/lib/character/behaviors/sprite.lua +++ b/lib/character/behaviors/sprite.lua @@ -8,7 +8,6 @@ local anim8 = require "lib.utils.anim8" local sprite = {} sprite.__index = sprite sprite.id = "sprite" -sprite.dependencies = { Tree.behaviors.map } sprite.LEFT = -1 sprite.RIGHT = 1 --- Скорость между кадрами в анимации @@ -62,10 +61,10 @@ function sprite:play(state, loop) return print("[SpriteBehavior]: no animation for '" .. state .. "'") end self.animationTable[state] = anim8.newAnimation(self.animationGrid[state], self.ANIMATION_SPEED, - type(loop) == "function" and loop or - function() - if not loop then self:play("idle", true) end - end) + type(loop) == "function" and loop or + function() + if not loop then self:play("idle", true) end + end) self.state = state end diff --git a/lib/character/character.lua b/lib/character/character.lua index fd18f23..4be416f 100644 --- a/lib/character/character.lua +++ b/lib/character/character.lua @@ -76,14 +76,14 @@ end --- @return Character | nil function character:addBehavior(behaviors) for _, b in ipairs(behaviors) do - if b.dependencies then - for _, dep in ipairs(b.dependencies) do - if not self:has(dep) then - return print("[Character]: cannot add \"" .. b.id .. - "\" for a character (Id = " .. self.id .. "): needs \"" .. dep.id .. "\"!") - end - end - end + -- if b.dependencies then + -- for _, dep in ipairs(b.dependencies) do + -- if not self:has(dep) then + -- return print("[Character]: cannot add \"" .. b.id .. + -- "\" for a character (Id = " .. self.id .. "): needs \"" .. dep.id .. "\"!") + -- end + -- end + -- end b.owner = self table.insert(self.behaviors, b) self._behaviorsIdx[b.id] = #self.behaviors diff --git a/lib/tree.lua b/lib/tree.lua index e901fd6..3d47546 100644 --- a/lib/tree.lua +++ b/lib/tree.lua @@ -3,15 +3,15 @@ --- В love.update обновлять, в love.draw читать -Tree = { +Tree = { assets = (require "lib.utils.asset_bundle"):load() } -Tree.panning = require "lib/panning" -Tree.controls = require "lib.controls" -Tree.level = (require "lib.level.level").new("procedural", "flower_plains") -- для теста у нас только один уровень, который сразу же загружен +Tree.panning = require "lib/panning" +Tree.controls = require "lib.controls" +Tree.level = (require "lib.level.level").new("procedural", "flower_plains") -- для теста у нас только один уровень, который сразу же загружен -Tree.behaviors = {} --- @todo написать нормальную загрузку поведений -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" \ No newline at end of file +Tree.behaviors = (require "lib.utils.behavior_loader")("lib/character/behaviors") --- @todo написать нормальную загрузку поведений +-- 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" diff --git a/lib/utils/behavior_loader.lua b/lib/utils/behavior_loader.lua new file mode 100644 index 0000000..6ad2262 --- /dev/null +++ b/lib/utils/behavior_loader.lua @@ -0,0 +1,13 @@ +local function load(path) + local behaviors = {} + for _, b in ipairs(love.filesystem.getDirectoryItems(path)) do + b = string.gsub(b, ".lua", "") + local bpath = path .. "/" .. b + behaviors[b] = require(bpath) + print(behaviors[b], b) + end + behaviors.behavior = nil + return behaviors +end + +return load diff --git a/main.lua b/main.lua index c1cecbe..9c740fa 100644 --- a/main.lua +++ b/main.lua @@ -10,8 +10,8 @@ function love.conf(t) end function love.load() - character.spawn("Hero", Tree.assets.files.sprites.character) - character.spawn("Hero", Tree.assets.files.sprites.character, Vec3 { 3, 3 }) + character.spawn("Foodor", Tree.assets.files.sprites.character) + character.spawn("Baris", Tree.assets.files.sprites.character, Vec3 { 3, 3 }) love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true }) end