在游戏开发领域,选择合适的工具和框架对于项目的成功至关重要。本文将介绍一个基于Ogre图形引擎、ODE物理引擎和CEGUI界面库的游戏开发框架。这个框架不仅包含了丰富的资源和源代码,而且其设计理念和实现方式对于游戏开发者来说具有很高的参考价值。
作为一名游戏爱好者,对《太空骑士》这部童年时期的卡通片有着深厚的感情。因此,决定利用之前所学的知识,将这部卡通片开发成一款角色扮演游戏(RPG)。在这个过程中,使用了多种工具和库来构建游戏,包括Ogre图形引擎、ODE物理引擎、CEGUI界面库等。这些工具的结合,使得游戏在视觉表现、物理模拟和用户界面方面都达到了较高的水准。
以下是在游戏开发中使用的主要工具和库:
游戏基于多个库构建,以下是游戏框架的UML图。游戏包含多个模块,如任务模块、角色模块、AI模块、UI模块、工具模块和媒体模块。
任务模块控制游戏的任务,包含BaseMission类、FirstMission类等。每个类代表游戏中的一个任务,基类还控制着大多数模块。
角色模块控制游戏中的玩家,如主角、太空骑士、怪物Lada、NPC等。这些实体由实体管理器管理,并在每一帧检查是否有碰撞。角色模块的部分也由AI模块控制。
AI模块使用状态机来模拟角色的AI。每个具有AI的角色都有自己的状态类,该类定义了其行为。这些行为可以轻松更改和扩展。
UI模块使用CEGUI来显示游戏的用户界面。它接收用户的输入,并将消息发布到其他模块。它使用OIS来接收键盘和鼠标消息。屏幕上的每个菜单都是由CEGUI渲染的脚本文件。
工具模块控制游戏中的工具、药品和其他对象,如路上的宝箱或开门。大多数工具都是根据XML文件中的状态构建的。
媒体模块是Fmod和DirectShow的简单适配器。它播放战斗的声音或背景音乐,以及开场视频。
以下是BaseMission类的代码实现:
// 这里是Base Mission类
class BaseMission : public OgreOde::CollisionListener, public OgreOde::StepListener, public OgreOde::TriangleMeshRayListener {
public:
    // ...
    OgreOde::World *mWorld;
    OgreOde::Space *mSpace;
    OgreOde::StepHandler *mStepper;
    OgreOde::TriangleMeshGeometry *terrainTriMeshGeom;
    OgreOde::InfinitePlaneGeometry *mGround;
    GameFrame *gameFrame;
    Ogre::Root *mRoot;
    Ogre::Camera *mCamera;
    Ogre::SceneManager *mSceneMgr;
    Ogre::RenderWindow *mWindow;
    CEGUI::System *mGUISystem;
    FrameListener *mFrameListener;
    BaseMissionListener *mMissionListener;
    GuiListener *mGuiListener;
    Overlay *talkOverlay, *gameOverlay, *blackOverlay, *loadingOverlay, *missionIntruOverlay;
    CEGUI::Window *gameSheet, *opSheet, *quitSheet, *gameOverSheet, *loadSheet, *introduceSheet, *useButton, *noUseButton, *currentPackage;
    std::vector animationStates;
    std::vector mModels;
    std::vector mGeoms;
    std::vector nodes;
    std::vector staticThings;
    std::vector monsters;
    Role *mainModel;
    BaseSound *mSound;
    SceneNode * _rocket_node_explosion;
    ParticleSystem * _rocketParticles_explosion;
public:
    BaseMission(Ogre::RenderWindow* mW, Ogre::Root* mR, Ogre::Camera* mC, Ogre::SceneManager* mS, CEGUI::System* mG, GameFrame *);
    virtual void Render();
    virtual void Clean();
    virtual void InitObjects();
    virtual void createFrameListener();
    virtual void NextMission();
    virtual void upDate();
    virtual void createScene();
    virtual void UpdateGUI(void);
    virtual void initDemoEventWiring(void);
    // ...
    Ogre::SceneManager* getSceneManager(void) const { return mSceneMgr; }
    Ogre::RenderWindow* getRenderWindow(void) const { return mWindow; }
    void resetParticleSystem(Ogre::ParticleSystem *ps, bool enable, const Ogre::Real delay);
    virtual void GameOver();
    void LoadGame(int i);
};
          
以下是游戏中AI的状态机部分代码:
template 
class StateMachine {
private:
    entity_type *m_pOwner;
    State *m_pCurrentState;
    State *m_pPreviousState;
    State *m_pGlobalState;
public:
    StateMachine(entity_type *owner) : m_pOwner(owner), m_pCurrentState(NULL), m_pPreviousState(NULL), m_pGlobalState(NULL) {}
    void SetCurrentState(State *s) { m_pCurrentState = s; }
    void SetPreviousState(State *s) { m_pPreviousState = s; }
    void SetGlobalState(State *s) { m_pGlobalState = s; }
    // ...
};
template 
class State {
public:
    virtual bool OnMessage(entity_type *, Telegram &msg) = 0;
    virtual void Enter(entity_type *) = 0;
    virtual void Exit(entity_type *) = 0;
    virtual void Execute(entity_type *) = 0;
    virtual ~State() {}
};
            
游戏的操作包括:
游戏包含七个任务,包括两个Boss任务和一个转移任务。以下是游戏的一些截图:
游戏的主菜单。