【发布时间】:2019-06-17 22:39:59
【问题描述】:
我有一些 NPC 在 2D 矩阵中环绕,我必须让他们使用环形地图跟随敌方派系 NPC。
我尝试了几种解决方案,但它们都给了我奇怪的行为,只对网格的某个 x 或 y 值起作用,然后它们不跟随它们返回一个图块。
这就是我现在要决定的方向:
public Position GetNextStepTowards(Position origin, Position target)
{
Position nextStep = new Position(0, 0);
float dx = MathF.Abs(target.X - origin.X);
float dy = MathF.Abs(target.Y - origin.Y);
if (dx > mapXSize / 2) nextStep.X = -1;
else if (dx < mapXSize / 2) nextStep.X = 1;
if (dy > mapYSize / 2) nextStep.Y = 1;
else if (dy < mapYSize / 2) nextStep.Y = -1;
return nextStep;
}
一个职位是:
public struct Position
{
public int X { get; set; }
public int Y { get; set; }
public Position(int x, int y)
{
this.X = x;
this.Y = y;
}
}
NPC 只能移动一个单元格(Moore's),因此移动向量只能是-1 和1 之间的值。
提前感谢您的帮助!
【问题讨论】:
-
@user3386109 解决了它,将尽快发布我提出的解决方案,如果更多人分享他们的做法会有所帮助。
标签: c# grid 2d artificial-intelligence console-application