micro refactoring
This commit is contained in:
parent
9489cd0488
commit
d48f1af1af
@ -1,5 +1,25 @@
|
|||||||
local AnimationNode = require "lib.animation_node"
|
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
|
--- @class AIBehavior : Behavior
|
||||||
--- @field animationNode AnimationNode?
|
--- @field animationNode AnimationNode?
|
||||||
local behavior = {}
|
local behavior = {}
|
||||||
@ -41,31 +61,12 @@ function behavior:makeMove()
|
|||||||
children = {
|
children = {
|
||||||
AnimationNode {
|
AnimationNode {
|
||||||
function(node)
|
function(node)
|
||||||
local caster = Vec3 {}
|
local charTarget = closestCharacter(self.owner)
|
||||||
self.owner:try(Tree.behaviors.positioned, function(b)
|
local target
|
||||||
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)
|
charTarget:try(Tree.behaviors.positioned, function(b)
|
||||||
target = Vec3 { target.x, target.y + 1 } --- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти
|
target = Vec3 { b.position.x, b.position.y + 1 } --- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти
|
||||||
end)
|
end)
|
||||||
spellB.spellbook[1]:cast(self.owner, target)
|
spellB.spellbook[1]:cast(self.owner, target)
|
||||||
-- print(minDist, target)
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
main.lua
20
main.lua
@ -23,6 +23,26 @@ function love.load()
|
|||||||
Tree.behaviors.shadowcaster.new(),
|
Tree.behaviors.shadowcaster.new(),
|
||||||
Tree.behaviors.spellcaster.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 { 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")
|
character.spawn("Baris")
|
||||||
:addBehavior {
|
:addBehavior {
|
||||||
Tree.behaviors.residentsleeper.new(),
|
Tree.behaviors.residentsleeper.new(),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user