简体   繁体   English

Pacman游戏组织(XNA) - 我应该在哪里/如何检查与鬼/食物的碰撞?

[英]Pacman Game Organization (XNA) - Where/how should I check for collisions with ghosts/food?

I have my Pacman game working, and i'm trying to go back through it and "organize" my code better. 我的Pacman游戏正在运行,我正在尝试回过头来更好地“整理”我的代码。 Before, I had this "TopObject" which referenced the object in game1.cs which basically referenced every object in my game (pacman, ghosts, map, etc). 之前,我有这个“TopObject”引用了game1.cs中的对象,它基本上引用了我游戏中的每个对象(pacman,ghosts,map等)。 I made it globally available from ANYWHERE so that I could access my Pacman object from my ghost objects, or my map/tile objects from my ghost objects, or even my ContentMananger from anywhere to load content. 我从ANYWHERE全局提供它,以便我可以从我的幽灵对象访问我的Pacman对象,或者从我的幽灵对象访问我的地图/平铺对象,甚至可以从任何地方访问我的ContentMananger来加载内容。

I know this is bad practice, so i'm trying to eliminate it. 我知道这是不好的做法,所以我试图消除它。 For instance, I created LoadContent() methods on all my objects which take a "ContentManager" type, which eliminated the need for me to call ContentManager using my TopObject. 例如,我在所有采用“ContentManager”类型的对象上创建了LoadContent()方法,这使我无需使用TopObject调用ContentManager。

Proceeding further, i'm having a really hard time doing stuff without this global reference to all my objects. 继续前进,我很难做到没有全局引用所有对象的东西。 For instance: - From within my Pacman Update().. 例如: - 从我的吃豆子更新()中...

  • I need to know if i'm going to run into a wall. 我需要知道我是否会碰到一堵墙。 I need references to my map/tiles. 我需要参考我的地图/瓷砖。

  • I need to know if i've collided with a ghost, so i need references to my ghost objects. 我需要知道我是否与鬼相撞,所以我需要引用我的幽灵物体。

  • I need to know if I collided with some food and then update the scoreboard, so i need have reference to my scoreboard object. 我需要知道我是否与某些食物相撞然后更新记分牌,所以我需要参考我的记分牌对象。

So essentially, it feels like i'm going to be making a giant mess passing so many object references around to all my objects, that it feels like doing a "global" topobject is much cleaner. 从本质上讲,感觉就像我将要在我的所有对象周围传递如此多的对象引用之类的巨大混乱,感觉就像做一个“全局”顶级对象更加清晰。

Does anyone know how to organize this better, so that i'm doing it cleanly? 有谁知道如何更好地组织这个,以便我干净利落地做到这一点? Should I even be checking for collisions inside my Pacman Update(), or should I be creating seperate "PacmanObject.CheckCollisionWith()" and calling that from my Game1 Update()? 我是否应该检查Pacman Update()中的冲突,还是应该创建单独的“PacmanObject.CheckCollisionWith()”并从我的Game1 Update()调用它? Should collision logic also be seperated out into a new class? 碰撞逻辑是否也应该分成新的类?

Thanks! 谢谢!

Having a central point that contain reference toward your objects isn't a bad thing per se. 拥有一个包含对象参考的中心点本身并不是一件坏事。 However, a good design will put the actions that are best related to each object in the correct class. 但是,一个好的设计会将与每个对象最相关的动作放在正确的类中。 For example, your player doesn't need to have reference towards the tiles and walls. 例如,您的播放器不需要参考瓷砖和墙壁。 While doing an Move method, the player could call a static method in the Map class that return true/false if the next move is valid. 在执行Move方法时,如果下一步移动有效,则播放器可以在Map类中调用返回true / false的静态方法。 This way, the player doesn't have any reference toward the map itself. 这样,玩家就没有对地图本身的任何引用。

public class Player
{
    public void Update()
    {
        //stuff to find nextTile position

        //Call static function toward a class that contains the map data.
        if (Map.TileIsWalkable(nextTile))
            Move();
    }
}

public class Map
{
    public static Map loadedMap; // Handling it as singleton, normally you only load one map at a time.

    public static bool TileIsWalkable(Vector2 position)
    {
        if (loadedMap == null) // Throw assert, shouldn't happen.

        return //whatever test needed in loadedMap structure
    }
}

You can also have a Ghost manager that keeps reference toward the ghost. 您还可以拥有一个Ghost管理器,可以引用鬼魂。 The player would only need to call a method in that manager that would loop over all the ghost to see if he collides. 玩家只需要在该管理器中调用一个方法,该方法将遍历所有鬼魂以查看他是否发生碰撞。 Even better, that method could take a position and not a reference. 更好的是,该方法可以采取立场而不是参考。 That way, it can be reused to see if ghosts collide between each other. 这样,可以重复使用它来查看鬼魂是否在彼此之间相互碰撞。 (random idea) (随机的想法)

There's plenty of way to do something. 有很多办法可以做点什么。 The hardest in any code design is to put the stuff at the place that make the more sense and makes it the easiest to update and modify later. 任何代码设计中最难的是将内容放在更有意义的地方,并使其最易于更新和修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM