diff --git a/lib/character/behaviors/stats.lua b/lib/character/behaviors/stats.lua new file mode 100644 index 0000000..a6161bd --- /dev/null +++ b/lib/character/behaviors/stats.lua @@ -0,0 +1,17 @@ +--- @class StatsBehavior : Behavior +--- @field hp integer +--- @field mana integer +local behavior = {} +behavior.__index = behavior +behavior.id = "stats" + +--- @param hp? integer +--- @param mana? integer +function behavior.new(hp, mana) + return setmetatable({ + hp = hp or 20, + mana = mana or 10 + }, behavior) +end + +return behavior \ No newline at end of file diff --git a/lib/character/character.lua b/lib/character/character.lua index f2c1a2d..1001426 100644 --- a/lib/character/character.lua +++ b/lib/character/character.lua @@ -6,7 +6,6 @@ local characterId = 1 --- @class Character --- @field id Id ---- @field stats Stats --- @field behaviors Behavior[] --- @field _behaviorsIdx {string: integer} local character = {} @@ -25,11 +24,11 @@ local function spawn(name, template, spriteDir, position, size, level) char = setmetatable(char, character) char.id = characterId characterId = characterId + 1 - char.stats = require('lib.character.stats').new() char.behaviors = {} char._behaviorsIdx = {} char:addBehavior { + Tree.behaviors.stats.new(), Tree.behaviors.map.new(position, size), Tree.behaviors.sprite.new(spriteDir), Tree.behaviors.spellcaster.new() diff --git a/lib/character/class.lua b/lib/character/class.lua deleted file mode 100644 index 0f3a6f5..0000000 --- a/lib/character/class.lua +++ /dev/null @@ -1,17 +0,0 @@ ---- @alias ClassTemplate "warrior"|"mage"|"archer" - ---- @class Class ---- @field skills table ---- @field stats Stats -local class = {} - ---- @param template ClassTemplate ---- @param level? integer -local function new(template, level) - return setmetatable({ - stats = (require "lib/character/stats").fromTemplate(template), - skills = {} - }, class) -end - -return { new = new } diff --git a/lib/character/info.lua b/lib/character/info.lua deleted file mode 100644 index 6b8ba68..0000000 --- a/lib/character/info.lua +++ /dev/null @@ -1,16 +0,0 @@ ---- @class Info ---- @field name string ---- @field class Class -local info = {} - ---- @param name string ---- @param classTemplate ClassTemplate ---- @param level? integer -local function new(name, classTemplate, level) - return setmetatable({ - name = name, - class = (require 'lib/character/class').new(classTemplate, level) - }, info) -end - -return { new = new } diff --git a/lib/character/stats.lua b/lib/character/stats.lua deleted file mode 100644 index 4f65ae7..0000000 --- a/lib/character/stats.lua +++ /dev/null @@ -1,46 +0,0 @@ ---- @class Stats ---- @field hp integer ---- @field mana integer -local stats = {} -stats.__index = stats - ---- @param level? integer -local function new(level) - return { - hp = 20, - mana = 10 - } -end - ---- creates stats from character template (like warrior etc etc) ---- ---- TODO: написать скалирование по уровню ---- @param template ClassTemplate ---- @param level? integer -local function fromTemplate(template, level) - local tempStats = {} - if template == "warrior" then - tempStats = { - hp = 30, - initiative = 10, - damage = 5, - defence = 10, - } - elseif template == "mage" then - tempStats = { - hp = 15, - initiative = 8, - damage = 8, - defence = 0, - } - elseif template == "archer" then - tempStats = { - hp = 20, - initiative = 12, - damage = 5, - defence = 5, - } - end -end - -return { new = new, fromTemplate = fromTemplate } diff --git a/lib/spellbook.lua b/lib/spellbook.lua index 3fb2199..67d5605 100644 --- a/lib/spellbook.lua +++ b/lib/spellbook.lua @@ -26,11 +26,10 @@ local walk = setmetatable({ }, spell) function walk:cast(caster, target) - if caster.stats.mana < 2 then - print("not enough mana!") - return false - end - + if not caster:try(Tree.behaviors.stats, function (stats) + return stats.mana >= 2 + end) then return false end + local path = self.path if path:is_empty() then return false end path:pop_front() @@ -40,8 +39,10 @@ function walk:cast(caster, target) end) -- TODO: списать деньги за каст (антиутопия какая-то) -- TODO: привязка тинькоффа - caster.stats.mana = caster.stats.mana - 2 - print(caster.stats.mana) + caster:try(Tree.behaviors.stats, function (stats) + stats.mana = stats.mana - 2 + print(stats.mana) + end) return true end @@ -65,7 +66,9 @@ end local regenerateMana = setmetatable({}, spell) function regenerateMana:cast(caster, target) - caster.stats.mana = 10 + caster:try(Tree.behaviors.stats, function (stats) + stats.mana = 10 + end) print(caster.id, "has regenerated mana") caster:try(Tree.behaviors.sprite, function (sprite) -- бойлерплейт (временный) -- В данный момент заклинание не позволяет отслеживать состояние последствий своего применения, так что надо повесить хоть какую-то анимашку просто для того, чтобы отложить завершение каста куда-то в будущее diff --git a/lib/tree.lua b/lib/tree.lua index 07a06b4..e901fd6 100644 --- a/lib/tree.lua +++ b/lib/tree.lua @@ -14,3 +14,4 @@ Tree.behaviors = {} 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