我正在尝试为追随玩家的自上而下的游戏创建一个简单的AI,但不会立即采取正确的方式。在他们决定采取另一个步骤之前,我的敌人必须逐步移动100像素。我把一些按照我想要的东西放在一起。它看着玩家的坐标并向它移动,但也有可能采取了不同的方向。
现在,我通过EnemyManager课程添加了多个敌人,他们很遗憾地完全跟随对方的动作。我知道这可能是因为Random函数是基于它执行的时间。我该如何解决这个问题?
我也很确定我使用的方法效率不高:
public void decideAImovement(Vector2 heroPosition)
{
Random random = new Random();
int forwardChance = 10;
int backChance = 10;
int leftChance = 10;
int rightChance = 10;
if (position.Y > heroPosition.Y)
{
backChance += 50;
}
else if (position.Y < heroPosition.Y)
{
forwardChance += 50;
}
if (position.X > heroPosition.X)
{
leftChance += 50;
}
else if (position.X < heroPosition.X)
{
rightChance += 50;
}
backChance = random.Next(backChance);
rightChance = random.Next(rightChance);
leftChance = random.Next(leftChance);
forwardChance = random.Next(forwardChance);
int vergelijkingUD,vergelijkingLR,vergelijking;
vergelijkingUD = Math.Max(forwardChance, backChance);
vergelijkingLR = Math.Max(leftChance, rightChance);
vergelijking = Math.Max(vergelijkingLR, vergelijkingUD);
if (vergelijking == vergelijkingLR) //Left Right
{
if (vergelijkingLR == leftChance)
{
state = EnemyState.walkLeft;
}
else
{
state = EnemyState.walkRight;
}
}
else //Up Down
{
if (vergelijkingUD == backChance)
{
state = EnemyState.walkBack;
}
else
{
state= EnemyState.walkForward;
}
}
}
public void moveEnemy(Vector2 heroPosition)
{
distanceWalked += 2;
if (distanceWalked == gridDimensions || state == EnemyState.stop)
{
distanceWalked = 0;
decideAImovement(heroPosition);
}
switch (state)
{
case EnemyState.walkRight:
position.X += 2.0f;
break;
case EnemyState.walkLeft:
position.X -= 2.0f;
break;
case EnemyState.walkBack:
position.Y -= 2.0f;
break;
case EnemyState.walkForward:
position.Y += 2.0f;
break;
case EnemyState.stop:
break;
}
}