From 9489cd0488688042f35102fcdb9878d7f96363fd Mon Sep 17 00:00:00 2001 From: neckrat Date: Fri, 23 Jan 2026 15:56:36 +0300 Subject: [PATCH] we can move under another (closest!!!!!!!!!) character cool!!!! --- lib/annotations.lua | 1 + lib/character/behaviors/ai.lua | 77 ++++++++++++++++++++++++++++++++++ lib/spellbook.lua | 6 ++- main.lua | 5 ++- 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 lib/character/behaviors/ai.lua diff --git a/lib/annotations.lua b/lib/annotations.lua index 6a0d688..cc32630 100644 --- a/lib/annotations.lua +++ b/lib/annotations.lua @@ -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" diff --git a/lib/character/behaviors/ai.lua b/lib/character/behaviors/ai.lua new file mode 100644 index 0000000..99ca588 --- /dev/null +++ b/lib/character/behaviors/ai.lua @@ -0,0 +1,77 @@ +local AnimationNode = require "lib.animation_node" + +--- @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 caster = Vec3 {} + self.owner:try(Tree.behaviors.positioned, function(b) + caster = b.position + end) + local target = Vec3 {} + 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 + target = b.position + charTarget = v + end + -- print(k, b.position) + end) + break + end + -- print(target) + charTarget:try(Tree.behaviors.positioned, function(b) + target = Vec3 { target.x, target.y + 1 } --- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти + end) + spellB.spellbook[1]:cast(self.owner, target) + -- print(minDist, target) + end + } + } + } + self.animationNode:run() + end) +end + +return behavior diff --git a/lib/spellbook.lua b/lib/spellbook.lua index a4ec146..2aceae1 100644 --- a/lib/spellbook.lua +++ b/lib/spellbook.lua @@ -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 diff --git a/main.lua b/main.lua index 3cd3ddd..460acc5 100644 --- a/main.lua +++ b/main.lua @@ -26,12 +26,13 @@ function love.load() character.spawn("Baris") :addBehavior { Tree.behaviors.residentsleeper.new(), - Tree.behaviors.stats.new(nil, nil, 1), + 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.spellcaster.new(), + Tree.behaviors.ai.new() }, }