try to attack & behavior annotation refactor

Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
This commit is contained in:
neckrat 2025-10-16 00:28:26 +03:00
parent 7ac0a53caa
commit 78f9347b86
4 changed files with 39 additions and 2 deletions

View File

@ -0,0 +1,5 @@
--- @meta _
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"

View File

@ -12,7 +12,7 @@ behavior.state = "idle"
function behavior.new(spellbook) function behavior.new(spellbook)
local spb = require "lib.spellbook" --- @todo временное добавление ходьбы (и читов) всем персонажам local spb = require "lib.spellbook" --- @todo временное добавление ходьбы (и читов) всем персонажам
local t = {} local t = {}
t.spellbook = spellbook or spb.of { spb.walk, spb.regenerateMana } t.spellbook = spellbook or spb.of { spb.walk, spb.regenerateMana, spb.attack }
return setmetatable(t, behavior) return setmetatable(t, behavior)
end end

View File

@ -84,6 +84,37 @@ function regenerateMana:cast(caster, target)
return true return true
end end
local attack = setmetatable({}, spell)
function attack:cast(caster, target)
-- caster:try(Tree.behaviors.map, function(map)
-- local dist = math.ceil((target - map.position):length())
-- if dist >= 2 then
-- return false
-- end
-- end)
--- @type Character
local targetCharacter = Tree.level.characterGrid:get(target)
targetCharacter:try(Tree.behaviors.stats, function(stats)
stats.hp = stats.hp - 4
end)
caster:try(Tree.behaviors.sprite, function(sprite)
sprite:play("attack", function()
sprite:play("idle")
caster:has(Tree.behaviors.spellcaster):endCast()
end)
end)
targetCharacter:try(Tree.behaviors.sprite, function(sprite)
sprite:play("hurt", function()
sprite:play("idle")
end)
end)
return true
end
---------------------------------------- ----------------------------------------
local spellbook = { local spellbook = {
walk = walk, walk = walk,

View File

@ -44,7 +44,8 @@ function layout:build()
ui.Row { ui.Row {
children = { children = {
setmetatable({ owner = Tree.level.characters[id], spellId = 1 }, { __index = SkillButton }), setmetatable({ owner = Tree.level.characters[id], spellId = 1 }, { __index = SkillButton }),
setmetatable({ owner = Tree.level.characters[id], spellId = 2 }, { __index = SkillButton }) setmetatable({ owner = Tree.level.characters[id], spellId = 2 }, { __index = SkillButton }),
setmetatable({ owner = Tree.level.characters[id], spellId = 3 }, { __index = SkillButton })
} }
} }
skillRows[id] = r skillRows[id] = r