107 lines
3.7 KiB
Lua
107 lines
3.7 KiB
Lua
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?
|
||
--- @field target Vec3?
|
||
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)
|
||
local charTarget = closestCharacter(self.owner)
|
||
charTarget:try(Tree.behaviors.positioned, function(b)
|
||
self.target = Vec3 { b.position.x, b.position.y + 1 } --- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти
|
||
end)
|
||
|
||
spellB.spellbook[1]:cast(self.owner, self.target):chain {
|
||
spellB.spellbook[1]:cast(self.owner, Vec3 { 10, 10 }):chain {
|
||
AnimationNode {
|
||
onEnd = function()
|
||
Tree.level.turnOrder:next()
|
||
end
|
||
}
|
||
}
|
||
}:run()
|
||
|
||
|
||
|
||
-- -- print('какещке')
|
||
-- self.animationNode = AnimationNode { -- кринж
|
||
-- function(node) end,
|
||
-- onEnd = function()
|
||
-- -- print('kakeshke')
|
||
-- end,
|
||
-- children = {
|
||
-- AnimationNode {
|
||
-- function(node) --тяжело
|
||
-- local charTarget = closestCharacter(self.owner)
|
||
-- charTarget:try(Tree.behaviors.positioned, function(b)
|
||
-- self.target = Vec3 { b.position.x, b.position.y + 1 } --- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти
|
||
-- end)
|
||
-- spellB.spellbook[1]:cast(self.owner, self.target)
|
||
-- end,
|
||
-- onEnd = function() --база
|
||
-- end,
|
||
-- children = {
|
||
-- AnimationNode {
|
||
-- function(node)
|
||
-- -- if not self.target then return end
|
||
-- print("пупупупупупупупупупуупупуууууу")
|
||
-- print(spellB.spellbook[3]:cast(self.owner, self.target))
|
||
-- end
|
||
-- }
|
||
-- }
|
||
-- }
|
||
-- }
|
||
-- }
|
||
-- self.animationNode:run()
|
||
end)
|
||
end
|
||
|
||
return behavior
|