add spellcaster behavior

This commit is contained in:
PeaAshMeter 2025-09-28 23:34:17 +03:00
parent 947787ff44
commit c4dfb5956d
6 changed files with 50 additions and 28 deletions

View File

@ -0,0 +1,25 @@
--- @class SpellcasterBehavior : Behavior
--- @field spellbook Spell[] собственный набор спеллов персонажа
--- @field cast Spell | nil ссылка на активный спелл из спеллбука
local behavior = {}
behavior.__index = behavior
behavior.id = "spellcaster"
---@param spellbook Spell[] | nil
---@return SpellcasterBehavior
function behavior.new(spellbook)
local spb = require "lib.spellbook" --- @todo временное добавление ходьбы всем персонажам
local t = {}
t.spellbook = spellbook or spb.of { spb.walk }
return setmetatable(t, behavior)
end
function behavior:update(dt)
if self.cast then self.cast:update(self.owner, dt) end
end
function behavior:draw()
if self.cast then self.cast:draw() end
end
return behavior

View File

@ -35,10 +35,10 @@ 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.render.new(spriteDir), Tree.behaviors.render.new(spriteDir),
Tree.behaviors.spellcaster.new()
} }
char:setState("idle") --- @todo сделать это отдельным модулем char:setState("idle") --- @todo сделать это отдельным модулем
local spb = require "lib.spellbook" --- @todo это тоже
char.spellbook = spb.of { spb.walk }
Tree.level.characters[char.id] = char Tree.level.characters[char.id] = char
return char return char
@ -120,20 +120,15 @@ end
function character:update(dt) function character:update(dt)
--- @todo ну ты понел --- @todo ну ты понел
-- for _, b in ipairs(self.behaviors) do for _, b in ipairs(self.behaviors) do
-- if b.update then b:update(dt) end if b.update then b:update(dt) end
-- end end
self:has(Tree.behaviors.map):update(dt)
if self.cast then self.cast:update(self, dt) end
self:has(Tree.behaviors.render):update(dt)
end end
function character:draw() function character:draw()
for _, b in ipairs(self.behaviors) do for _, b in ipairs(self.behaviors) do
if b.draw then b:draw() end if b.draw then b:draw() end
end end
if self.cast then self.cast:draw() end --- @todo 🤡
end end
return { spawn = spawn } return { spawn = spawn }

View File

@ -25,10 +25,11 @@ function selector:update(dt)
if not characterId and self.id then -- Когда кликаем по тайлу за персонажа в режиме каста, кастуем спелл if not characterId and self.id then -- Когда кликаем по тайлу за персонажа в режиме каста, кастуем спелл
local char = Tree.level.characters[self.id] local char = Tree.level.characters[self.id]
if char.cast then char:try(Tree.behaviors.spellcaster, function(b)
char.cast:cast(char, mousePosition) if not b.cast then return end
char.cast = nil b.cast:cast(char, mousePosition)
end b.cast = nil
end)
end end
self:select(characterId) self:select(characterId)

View File

@ -17,12 +17,11 @@ local walk = setmetatable({
}, spell) }, spell)
function walk:cast(caster, target) function walk:cast(caster, target)
caster.cast = nil
local path = self.path local path = self.path
path:pop_front() path:pop_front()
print("Following path: ") print("Following path: ")
for p in path:values() do print(p) end for p in path:values() do print(p) end
caster:followPath(path) caster:has(Tree.behaviors.map):followPath(path)
end end
function walk:update(caster, dt) function walk:update(caster, dt)

View File

@ -3,13 +3,14 @@
--- В love.update обновлять, в love.draw читать --- В love.update обновлять, в love.draw читать
Tree = { Tree = {
assets = (require "lib.utils.asset_bundle"):load() assets = (require "lib.utils.asset_bundle"):load()
} }
Tree.panning = require "lib/panning" Tree.panning = require "lib/panning"
Tree.controls = require "lib.controls" Tree.controls = require "lib.controls"
Tree.level = (require "lib.level.level").new("procedural", "flower_plains") -- для теста у нас только один уровень, который сразу же загружен Tree.level = (require "lib.level.level").new("procedural", "flower_plains") -- для теста у нас только один уровень, который сразу же загружен
Tree.behaviors = {} --- @todo написать нормальную загрузку поведений Tree.behaviors = {} --- @todo написать нормальную загрузку поведений
Tree.behaviors.map = require "lib.character.behaviors.map" Tree.behaviors.map = require "lib.character.behaviors.map"
Tree.behaviors.render = require "lib.character.behaviors.render" Tree.behaviors.render = require "lib.character.behaviors.render"
Tree.behaviors.spellcaster = require "lib.character.behaviors.spellcaster"

View File

@ -11,10 +11,11 @@ local SkillButton = ui.Rectangle {
} }
function SkillButton:update(dt) function SkillButton:update(dt)
ui.Rectangle.update(self, dt) ui.Rectangle.update(self, dt)
self.color = self.owner.cast and { 0, 1, 0 } or { 1, 0, 0 } self.owner:try(Tree.behaviors.spellcaster, function(spellcaster)
self:onTap(function() self.color = spellcaster.cast and { 0, 1, 0 } or { 1, 0, 0 }
print(self.owner.spellbook[self.spellId]) self:onTap(function()
self.owner.cast = self.owner.cast and nil or self.owner.spellbook[self.spellId] spellcaster.cast = spellcaster.cast and nil or spellcaster.spellbook[self.spellId]
end)
end) end)
end end