Compare commits

..

2 Commits

Author SHA1 Message Date
d48f1af1af micro refactoring 2026-01-23 15:57:10 +03:00
9489cd0488 we can move under another (closest!!!!!!!!!) character
cool!!!!
2026-01-23 15:56:36 +03:00
4 changed files with 107 additions and 3 deletions

View File

@ -8,3 +8,4 @@ Tree.behaviors.light = require "character.behaviors.light"
Tree.behaviors.positioned = require "character.behaviors.positioned"
Tree.behaviors.tiled = require "character.behaviors.tiled"
Tree.behaviors.cursor = require "character.behaviors.cursor"
Tree.behaviors.ai = require "lib.character.behaviors.ai"

View File

@ -0,0 +1,78 @@
local AnimationNode = require "lib.animation_node"
local function closestCharacter(char)
local caster = Vec3 {}
char:try(Tree.behaviors.positioned, function(b)
caster = b.position
end)
local charTarget
local minDist = 88005553535 -- spooky magic number
for k, v in pairs(Tree.level.characters) do
v:try(Tree.behaviors.positioned, function(b)
local dist = ((caster.x - b.position.x) ^ 2 + (caster.y - b.position.y) ^ 2) ^ 0.5
if dist < minDist and dist ~= 0 then
minDist = dist
charTarget = v
end
-- print(k, b.position)
end)
end
return charTarget
end
--- @class AIBehavior : Behavior
--- @field animationNode AnimationNode?
local behavior = {}
behavior.__index = behavior
behavior.id = "ai"
function behavior.new()
return setmetatable({}, behavior)
end
function behavior:update(dt)
self.owner:try(Tree.behaviors.spellcaster, function(b)
if b.state == "casting" then
b.cast:update(self.owner, dt)
end
end)
if self.animationNode and self.animationNode.state == "running" then
self.animationNode:update(dt)
-- print(self.animationNode.t)
end
end
function behavior:draw()
self.owner:try(Tree.behaviors.spellcaster, function(b)
if b.state == "casting" then
b.cast:draw()
end
end)
end
function behavior:makeMove()
self.owner:try(Tree.behaviors.spellcaster, function(spellB)
-- print('какещке')
self.animationNode = AnimationNode {
function(node) end,
onEnd = function()
-- print('kakeshke')
end,
children = {
AnimationNode {
function(node)
local charTarget = closestCharacter(self.owner)
local target
charTarget:try(Tree.behaviors.positioned, function(b)
target = Vec3 { b.position.x, b.position.y + 1 } --- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти
end)
spellB.spellbook[1]:cast(self.owner, target)
end
}
}
}
self.animationNode:run()
end)
end
return behavior

View File

@ -106,6 +106,9 @@ function regenerateMana:cast(caster, target)
sprite:animate("hurt", node)
Tree.audio:crossfade(audioPath.music.level1.battle,
audioPath.music.level1.choral, 5000)
caster:try(Tree.behaviors.ai, function(b)
b:makeMove()
end)
end,
onEnd = function() caster:has(Tree.behaviors.spellcaster):endCast() end
}:run()
@ -176,7 +179,7 @@ function attack:cast(caster, target)
volume = 1,
lowgain = 0.1
}
Tree.audio:play(audioPath.sounds.hurt, settings)
Tree.audio:play(audioPath.sounds.hurt)
end
}
}
@ -199,6 +202,7 @@ local spellbook = {
function spellbook.of(list)
local spb = {}
for i, sp in ipairs(list) do
print(i)
spb[i] = setmetatable({}, { __index = sp })
end
return spb

View File

@ -23,16 +23,37 @@ function love.load()
Tree.behaviors.shadowcaster.new(),
Tree.behaviors.spellcaster.new()
},
character.spawn("Baris")
character.spawn("Foodor")
:addBehavior {
Tree.behaviors.residentsleeper.new(),
Tree.behaviors.stats.new(nil, nil, 1),
Tree.behaviors.positioned.new(Vec3 { 5, 5 }),
Tree.behaviors.positioned.new(Vec3 { 4, 3 }),
Tree.behaviors.tiled.new(),
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
Tree.behaviors.shadowcaster.new(),
Tree.behaviors.spellcaster.new()
},
character.spawn("Foodor")
:addBehavior {
Tree.behaviors.residentsleeper.new(),
Tree.behaviors.stats.new(nil, nil, 1),
Tree.behaviors.positioned.new(Vec3 { 5, 3 }),
Tree.behaviors.tiled.new(),
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
Tree.behaviors.shadowcaster.new(),
Tree.behaviors.spellcaster.new()
},
character.spawn("Baris")
:addBehavior {
Tree.behaviors.residentsleeper.new(),
Tree.behaviors.stats.new(nil, nil, 2),
Tree.behaviors.positioned.new(Vec3 { 5, 5 }),
Tree.behaviors.tiled.new(),
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
Tree.behaviors.shadowcaster.new(),
Tree.behaviors.spellcaster.new(),
Tree.behaviors.ai.new()
},
}
for id, _ in pairs(chars) do