XNA AI:管理屏幕上的敌人

发布于 2024-10-12 07:25:04 字数 450 浏览 3 评论 0原文

我有两个班级,人类和怪物。

两者都有一个名为 MoveBehavior 的属性

,人类具有 HumanMoveBehavior,怪物具有 MonsterMoveBehavior

我希望 HumanMoveBehavior 远离怪物,而 MonsterMoveBehavior 则向人类移动。

我遇到的问题是我应该将代码移动到哪里?

在人类/怪物类别中?

使用这种方法,我有一个 Move() 方法,它获取游戏中所有实体的列表,使用名为 GetListOfOpponents(List allsprites) 的方法确定它是怪物还是人类,然后运行 ​​GetNearestOpponent(List对手);

但这看起来真的很混乱。

我应该有一个 SpriteController 来决定精灵移动的位置吗?我不确定需要将此代码放在哪里:(

谢谢!

I have two classes, Human and Monster.

both have a Property called MoveBehavior

Human has HumanMoveBehavior, and Monster has MonsterMoveBehavior

I want the HumanMoveBehavior to move AWAY from Monsters, and MonsterMoveBehavior to move TOWARD Humans.

The problem I'm having is where should I put my code to move?

In the Human/Monster class?

Using this approach, I had a Move() Method, which takes a List of all entities in game, decides whether it's a Monster or Human using a method called GetListOfOpponents(List allsprites) and then runs GetNearestOpponent(List opponents);

But this looks really messy.

Should I have a SpriteController that decides where the Sprites move? I'm unsure where I need to put this code :(

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缘字诀 2024-10-19 07:25:04

您可以想到一个 AIManager ,它只是说:

foreach(GameObject go in m_myObjects) // m_myObjects is a list of all objects that require updating
{
    go.Update(); // standard GameObject function
}

之后,每个类都应该处理自己的代码片段。所以更新在类本身中起作用。

所以人类说:

// just a class which is a gameObject and also has moving behaviour
// do the same with monster
public class Human : GameObject, IMoveBehaviour
{
    public override Update()
    {
        GoMove();
    }

    public void GoMove()
    {
        // human specific logic here
    }
}

// This interface describes that some movement 
// will happen with the implementing class
public interface IMoveBehaviour
{
    void GoMove();
}

通过使用接口,您可以将特定语言作为类的一部分,并且您不需要还创建一些可以为您处理该问题的类。当然有可能。但在现实生活中,人类/怪物是正在移动的,而不是他携带的某种物体。

更新

对评论的回答。因为有一个 AIManager,甚至一个完整的 GameObjectManager 就可以很好地维护所有 GameObjects,所以你可以询问 AIManager code> 为你不能去的地方。

由于寻路大部分时间是通过使用某些导航网格或指定网格来完成的,因此 GameObjectManager 可以返回包含所有可导航点的特定网格。您当然不应该定义每个怪物的所有位置。因为大多数时候,怪物并不确切知道每个人在哪里(在现实生活中)。因此,知道不该去的地方确实很好,但知道每个人都在哪里,也会给你的人工智能带来太多优势。

因此,考虑返回一个网格,其中包含可以去的点和不可以去的点,而不是将这些东西保留在怪物/人类体内。始终通过思考现实生活中的事物来检查您应该在哪里留下什么。

You could think of a AIManager that just says:

foreach(GameObject go in m_myObjects) // m_myObjects is a list of all objects that require updating
{
    go.Update(); // standard GameObject function
}

After that, each class should take care of its own piece of code. So updating works in the class itself.

So Human says:

// just a class which is a gameObject and also has moving behaviour
// do the same with monster
public class Human : GameObject, IMoveBehaviour
{
    public override Update()
    {
        GoMove();
    }

    public void GoMove()
    {
        // human specific logic here
    }
}

// This interface describes that some movement 
// will happen with the implementing class
public interface IMoveBehaviour
{
    void GoMove();
}

With using an interface, you can make the specific language part of the class and you don't have need to ALSO create some class that will handle that for you. Of course it is possible. But in real life, the human/monster is the one that is moving, not some object he is carrying.

UPDATE

Answer to the comment. Because there is an AIManager, or even a complete GameObjectManager would be nice to maintain all GameObjects, you could ask the AIManager for the placed where you could not go.

Because pathfinding is most of the time done by use of some navigation mesh or a specified grid, the GameObjectManager can return the specific Grid with all navigable points on it. You should for certain not define all positions in every monster. Because most of the time, the monster does not exactly know where everyone is (in real life). So knowing where not to go is indeed good, but knowing where everyone is, will give your AI too much advantage as well.

So think of returning a grid with the points where to go and where not to, instead of maintaining such things inside the monster/human. Always check where you should leave what, by thinking about what would be the thing in real life.

汐鸠 2024-10-19 07:25:04

我认为 Valve 在《半条命 2》中为实体处理这个问题的方式是更好的方式之一。它没有为每个 AI 提供自己单独的 Move 方法并调用这些方法,而是简单地调用 Think() 方法并让实体决定需要做什么。

我会按照 Marnix 的说法实现一个 AIManager,它循环遍历游戏世界中的每个活动 AI,调用每个的 Think() 方法。我不建议将您的 Human 类与“IMoveBehavior”进行交互,因为将其抽象为“WorldEntity”抽象类会更好。

你可能有看不见的实体来控制自动保存、触发器、照明等,但有些实体会在世界上占有一席之地。这些人将有一个向量来识别他们的位置。让 AI 的 Think() 方法调用它自己的 move() 方法,但保持其私有。唯一需要考虑移动的是人工智能本身。

如果你想鼓励人工智能超越“思考”方法,我会建议某种命令,例如面向目标的行动计划(GOAP)系统。 Jeff Orkin 写了关于这个奇妙概念的文章,它被用在《FEAR》和《Fallout 3》等游戏中。这对于您的应用程序来说可能有点大材小用,但我认为它很有趣。

http://web.media.mit.edu/~jorkin/goap.html

The way Valve handled this for entities in Half Life 2, is one of the better ways, I think. Instead of giving each AI its own separate Move methods and calling those, it simply called the Think() method and let the entity decide what it needed to do.

I'd go with what Marnix says and implement an AIManager that loops through each active AI in the game world, calling the Think() method of each. I would not recommended interfacing your Human class with an "IMoveBehavior" simply because it would be better to abstract that into a "WorldEntity" abstract class.

You might have invisible entities that control things like autosaves, triggers, lighting, etc, but some will have a position in the world. These are the ones who will have a vector identifying their position. Have the AI's Think() method call its own move() method, but keep it private. The only one who needs to think about moving is the AI itself.

If you want to encourage the AI to move outside of the Think) method, I would suggest some kind of imperative, such as a Goal-Oriented Action Planning (GOAP) system. Jeff Orkin wrote about this fantastic concept, and it was used in games such as F.E.A.R. and Fallout 3. It might be a bit overkill for your application, but I thought it was interesting.

http://web.media.mit.edu/~jorkin/goap.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文