circleVectors function and some ai progress
This commit is contained in:
parent
b6737e8f0b
commit
bad4b494cd
@ -1,5 +1,10 @@
|
|||||||
local easing = require "lib.utils.easing"
|
local easing = require "lib.utils.easing"
|
||||||
|
local pf = require "lib.pathfinder"
|
||||||
|
local utils = require "lib.utils.utils"
|
||||||
|
|
||||||
|
EPSILON = 0.01
|
||||||
|
|
||||||
|
--- @return Character
|
||||||
local function closestCharacter(char)
|
local function closestCharacter(char)
|
||||||
local caster = Vec3 {}
|
local caster = Vec3 {}
|
||||||
char:try(Tree.behaviors.positioned, function(b)
|
char:try(Tree.behaviors.positioned, function(b)
|
||||||
@ -20,16 +25,54 @@ local function closestCharacter(char)
|
|||||||
return charTarget
|
return charTarget
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Возвращает все точки в радиусе в виде векторов
|
||||||
|
--- @param radius integer
|
||||||
|
local function circleVectors(radius)
|
||||||
|
local cam = Tree.level.camera
|
||||||
|
local vecs = {}
|
||||||
|
for t = 0, 2 * math.pi, EPSILON do
|
||||||
|
local x = math.sin(t) * radius
|
||||||
|
local y = math.cos(t) * radius
|
||||||
|
vecs[cam:toWorldPosition(Vec3 { x, y })] = true
|
||||||
|
end
|
||||||
|
return utils.keys(vecs)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param owner Character
|
||||||
|
--- @param space integer здесь мы должны сами определять, сколько должны не доходить до персонажа (1 <= n)
|
||||||
|
--- @return Vec3|nil
|
||||||
|
local function pathToClosestCharacter(owner, space)
|
||||||
|
local charTarget = closestCharacter(owner)
|
||||||
|
local targetPosition, ownerPosition = charTarget:has(Tree.behaviors.positioned), owner:has(Tree.behaviors.positioned)
|
||||||
|
if not targetPosition or not ownerPosition then return end
|
||||||
|
local target = Vec3 {}
|
||||||
|
print(ownerPosition.position, targetPosition.position)
|
||||||
|
local path = pf(ownerPosition.position, targetPosition.position)
|
||||||
|
for c in path:values() do
|
||||||
|
print(c)
|
||||||
|
end
|
||||||
|
print(path)
|
||||||
|
space = math.min(space, path:size())
|
||||||
|
print(space, path:size())
|
||||||
|
for _ = 0, space - 1 do
|
||||||
|
path:pop_back()
|
||||||
|
end
|
||||||
|
if path:size() ~= 0 then
|
||||||
|
target = path:pop_back()
|
||||||
|
else
|
||||||
|
target = ownerPosition.position
|
||||||
|
end
|
||||||
|
print(target, targetPosition.position)
|
||||||
|
--- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти
|
||||||
|
return target
|
||||||
|
end
|
||||||
|
|
||||||
---@type {[Class]: fun(self: AIBehavior): Task<nil>} возможно где-то здесь на объявлении типа сломается типизация
|
---@type {[Class]: fun(self: AIBehavior): Task<nil>} возможно где-то здесь на объявлении типа сломается типизация
|
||||||
local aiNature = {
|
local aiNature = {
|
||||||
["dev_warrior"] = function(self)
|
["dev_warrior"] = function(self)
|
||||||
return function(callback) -- почему так, описано в Task
|
return function(callback) -- почему так, описано в Task
|
||||||
self.owner:try(Tree.behaviors.spellcaster, function(spellB)
|
self.owner:try(Tree.behaviors.spellcaster, function(spellB)
|
||||||
local charTarget = closestCharacter(self.owner)
|
self.target = pathToClosestCharacter(self.owner, 2)
|
||||||
charTarget:try(Tree.behaviors.positioned, function(b)
|
|
||||||
self.target = Vec3 { b.position.x, b.position.y + 1 } --- @todo тут захардкожено + 1, но мы должны как-то хитро определять с какой стороны обойти
|
|
||||||
end)
|
|
||||||
|
|
||||||
local task1 = spellB.spellbook[1]:cast(self.owner, self.target)
|
local task1 = spellB.spellbook[1]:cast(self.owner, self.target)
|
||||||
if task1 then
|
if task1 then
|
||||||
task1(
|
task1(
|
||||||
@ -46,6 +89,7 @@ local aiNature = {
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
|
print('рот этого казино')
|
||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
6
main.lua
6
main.lua
@ -19,7 +19,7 @@ function love.load()
|
|||||||
:addBehavior {
|
:addBehavior {
|
||||||
Tree.behaviors.residentsleeper.new(),
|
Tree.behaviors.residentsleeper.new(),
|
||||||
Tree.behaviors.stats.new(nil, nil, 1),
|
Tree.behaviors.stats.new(nil, nil, 1),
|
||||||
Tree.behaviors.positioned.new(Vec3 { 3, 3 }),
|
Tree.behaviors.positioned.new(Vec3 { 1, 1 }),
|
||||||
Tree.behaviors.tiled.new(),
|
Tree.behaviors.tiled.new(),
|
||||||
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
|
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
|
||||||
Tree.behaviors.shadowcaster.new(),
|
Tree.behaviors.shadowcaster.new(),
|
||||||
@ -29,7 +29,7 @@ function love.load()
|
|||||||
:addBehavior {
|
:addBehavior {
|
||||||
Tree.behaviors.residentsleeper.new(),
|
Tree.behaviors.residentsleeper.new(),
|
||||||
Tree.behaviors.stats.new(nil, nil, 1),
|
Tree.behaviors.stats.new(nil, nil, 1),
|
||||||
Tree.behaviors.positioned.new(Vec3 { 4, 3 }),
|
Tree.behaviors.positioned.new(Vec3 { 3, 1 }),
|
||||||
Tree.behaviors.tiled.new(),
|
Tree.behaviors.tiled.new(),
|
||||||
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
|
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
|
||||||
Tree.behaviors.shadowcaster.new(),
|
Tree.behaviors.shadowcaster.new(),
|
||||||
@ -39,7 +39,7 @@ function love.load()
|
|||||||
:addBehavior {
|
:addBehavior {
|
||||||
Tree.behaviors.residentsleeper.new(),
|
Tree.behaviors.residentsleeper.new(),
|
||||||
Tree.behaviors.stats.new(nil, nil, 3),
|
Tree.behaviors.stats.new(nil, nil, 3),
|
||||||
Tree.behaviors.positioned.new(Vec3 { 5, 3 }),
|
Tree.behaviors.positioned.new(Vec3 { 7, 1 }),
|
||||||
Tree.behaviors.tiled.new(),
|
Tree.behaviors.tiled.new(),
|
||||||
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
|
Tree.behaviors.sprite.new(Tree.assets.files.sprites.character),
|
||||||
Tree.behaviors.shadowcaster.new(),
|
Tree.behaviors.shadowcaster.new(),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user