使用C ++设计游戏引擎的框架(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/designing-a-framework-for-a-game-engine-using-c-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2673 个词 阅读量 0使用C ++设计游戏引擎的框架(译文)
原文地址:https://www.codeproject.com/Articles/27548/Designing-a-framework-for-a-game-engine-using-C
原文作者:nalla
译文由本站 robot-v1.0 翻译
前言
This article describes my work in creating a framework for a generic game engine in C++.
本文介绍了我在C ++中为通用游戏引擎创建框架的工作.
- 下载设计策略-122 KB(Download design strategy - 122 KB)
- 下载示例游戏引擎-129 KB(Download sample game engine - 129 KB)
介绍(Introduction)
我承担了编写有关设计游戏引擎的白皮书的任务.为此,我开始在C ++中实现一个框架,其中涉及设计模式的基本实现以及基于策略的设计等C ++概念.本文讨论了我的设计,并且还包括了一个已编译的代码片段.(I took a task of writing a white paper about designing a game engine. For this work, I started implementing a framework in C++ which involves the basic implementation of Design Patterns and C++ concepts like Policy based design. This article talks about my design, and it includes a compiled code snippet as well.)
背景(Background)
本文档描述了一个框架,该框架利用了著名的设计范例,如基于策略的设计,装饰器和策略模式,并使用C ++实现.(This describes a framework which makes use of famous design paradigms like Policy based design, Decorator and Strategy Patterns, and is implemented using C++.)
使用代码(Using the Code)
使用基于策略的设计进行设置(Use of Policy Based Design for Settings)
在开始深入研究足球游戏引擎设计的精妙之处之前,任何在启动过程中的游戏都必须允许用户选择难度级别.我假设默认情况下存在三种难度:低,中和高.由于必须在开始时选择级别,这给了我使用的机会(Before even diving into the nifty gritty of a Football gaming engine design, any game during start-up has to allow the user to choose the Difficulty level. I am assuming there are three levels of difficulty, by default: Low, Medium, and High. Since the level has to be chosen at the beginning, this gives me an opportunity to use)基于策略的设计(Policy based design)使用模板类(基于Andrei Alexandrescu的书,Modern C ++ Design).因此,这里的难度级别可以作为一项策略.另外,我将为播放模式添加另一个策略.播放模式可以是"自动"(在这种情况下,您将与机器对战)或"多种"(您可以与朋友一起玩).(using template classes (based on Andrei Alexandrescu’s book, Modern C++ Design). So, the difficulty level can be a policy here. Also, I am going to add another policy for the playing mode. The playing mode can be Auto (in this case, you will play against the machine) or Multi (you can play with your friend).) 基于策略的设计已被描述为策略模式的编译时变体(Policy based design has been described as a compile-time variant of the Strategy pattern)
template <typename T>
struct DifficultyLevel_High
{
static void setDifficultyLevel(T&) { cout << "Setting to High difficulty
level" <<endl; }
};
template <typename T>
struct DifficultyLevel_Med
{
static void setDifficultyLevel(T&)
{ cout << "Setting to Medium difficulty level" <<endl; }
};
template <typename T>
struct DifficultyLevel_Low
{
static void setDifficultyLevel(T&)
{cout << "Setting to Low difficulty level" <<endl; }
};
template <typename T>
struct Playmode_policy_auto
{
static void setPlaymodepolicy(T&)
{ cout << "Setting to auto Playmode" <<endl; }
};
template <typename T>
struct Playmode_policy_multi
{
static void setPlaymodepolicy(T&)
{ cout << "Setting to multi Playmode" <<endl; }
};
class FootballEngineType
{
public:
FootballEngineType()
{ cout << "Engine set as Football " << endl;
}
};
//---------------------Usage of Policy based design----------------//
template< typename T,
template <typename> class DifficultyLevel_policy,
template <typename> class Playmode_policy >
class GamingEngine
{
public:
void Run()
{
DifficultyLevel_policy<T>::setDifficultyLevel(engineType);
Playmode_policy<T> ::setPlaymodepolicy(engineType);
start();
}
private:
T engineType;
};
下一个重要的事情是,团队应该能够在运行时更改游戏的逻辑和策略.例如,用户可以选择防御而不是攻击.使用策略模式可以实现这一点.我们可以提供用于防御,攻击等的算法列表,以便用户能够在运行时选择团队策略.这可以通过使用(The next important thing is that a team should be able to change the logic and strategy of the game at runtime. For example, a user can opt to go for a defend instead of an attack. This can be made possible using the Strategy pattern. We can have a list of algorithms for defending, attacking etc., so that the user is able to choose the team strategy at runtime. This is possible by using the)策略模式(Strategy pattern).在这里,我定义了三种策略,这些策略可以由(. Here, I have defined three strategies, and the strategies can be set by the) GameLogic
类.(class.)
class Strategy
{
public:
Strategy() {}
virtual void Apply()=0;
virtual ~Strategy() {}
};
class DefendStrategy : public Strategy{
public:
DefendStrategy():Strategy() { cout << "Defend strategy set" << endl; }
void Apply() { cout << "Defend strategy applied" << endl; }
virtual ~DefendStrategy() {}
};
class AttackStrategy: public Strategy
{
public:
AttackStrategy():Strategy() { cout << "Attack strategy set" << endl; }
void Apply() { cout << "Attack strategy applied" << endl; }
virtual ~AttackStrategy() {}
};
class MediumStrategy: public Strategy
{
public:
MediumStrategy() :Strategy(){ cout << "Medium strategy set" << endl; }
void Apply() { cout << "Medium strategy applied" << endl; }
virtual ~MediumStrategy() {}
};
class GameLogic
{
public:
StratType StrategyType;
GameLogic()
{
m_Strategy = NULL;
}
void SetStrategy(StratType type)
{
if (m_Strategy) delete m_Strategy;
if (type == Med)
m_Strategy = new MediumStrategy();
else if (type == Defend)
m_Strategy = new DefendStrategy();
else if (type == Attack)
m_Strategy = new AttackStrategy();
}
void Exec() { m_Strategy->Apply(); }
~GameLogic() { if (m_Strategy) delete m_Strategy; }
private:
Strategy *m_Strategy;
};
然后是每个实体可以执行的不同角色.每个团队都有一个球员,教练,理疗师,经理,裁判和团队CEO等的列表.一个团队中的每个人都可以执行一个或多个角色,并且可以在运行时根据信誉和其他参数来分配角色.同样,每个人都可以扮演不同的职责,例如前锋,后卫,中场和守门员等.这也应该在运行时完成,而无需使用子类.(Then comes the different roles each entity can perform. Each team has a list of players, coach, physiotherapist, manager, referee, and team CEO etc. Each one in a team can perform one or more roles, and the role can be assigned at runtime based on the credibility and other parameters. Also, each person as a player can have different responsibilities like Forward, Defender, Midfielder, and Goalkeeper etc. This should also be done at runtime without using sub classing.) 角色和职责都在运行时使用Decorator模式分配.(Both the roles and responsibilities are assigned at runtime using the Decorator pattern.) 以下是用于在运行时获取游戏实体(一个人)的角色和职责的助手功能:(Following are the helper functions used to get the roles and responsibilities of a gaming entity (a person) at runtime:)
//------------templated helper functions for getting roles-------//
template <class T>
T* getresponsibility_entity(GameEntity *pEnt)
{
return dynamic_cast<T*>(pEnt->GetResponsibility(T::RESP_CLSID));
}
template <class T>
T* getroles_entitiy(GameEntity *pEnt)
{
return dynamic_cast<T*>(pEnt->GetRole(T::ROL_CLSID));
}
以下是创建游戏实体并在运行时分配角色和职责并使用上述助手功能检索这些对象的代码段(有关完整实现,请参阅随附的CPP文件):(Following is the code snippet which creates a gaming entity and assigns roles and responsibilities at run time and retrieves those objects using the above mentioned helper functions (for the full implementation, please refer to the attached CPP file):)
// Add a single player
GameEntity* play1 = new GameEntity("Beckham", "David");
//Adding role as Player
play1->AddRole(new Player(play1));
Player *playRole = getroles_entitiy<Player>(play1);
//Adding Responsibilities to play and manage
play1->AddResponsibilities(new ToPlay(play1));
play1->AddResponsibilities(new ToManage(play1));
同样,不同的团队可以在不同的操场上以不同的设置参加不同的联赛比赛.例如,足球游戏中的每个实体都可以组成一个团队,然后可以应用不同的演示设置.您可以将球员添加到特定的足球俱乐部(衣服颜色),并可以为联赛添加不同的球队(例如英超联赛等).在这里,每个实体的实际抽象(团队/联赛匹配)可以使用Bridge模式与实现(联赛/足球场/演示文稿设置等)分离.因此,实现和抽象可以独立地变化.另外,Builder模式也可以用于同一任务.(Also, different teams can play for different league matches in different playgrounds with different settings. For example, each entity in a Football game can be clubbed as a team, and then different presentation settings can be applied. You can add players to a specific football club (dress colour) and add different teams for a league match (like English premier league etc.). Here, the actual abstraction of each entity (Team/League match) can be decoupled from the implementation (League/Football ground/Presentation settings etc.) using the Bridge pattern. Hence the implementation and the abstraction can vary independently. Alternatively, the Builder pattern can also be used for the same task.) 整个设计在Visual Studio 2005(VC8.0)编译器中实现/编译和测试.实施的CPP文件(The entire design is implemented/compiled and tested in the Visual Studio 2005 (VC8.0) compiler. The implemented CPP files)**FootballEngine.cpp(FootballEngine.cpp)**和(and)**stdafx.h(stdafx.h)**被附上.(are attached.)
兴趣点(Points of Interest)
未来的附加组件(Future Add-Ons)
我当时正在考虑使用"主题观察者"模式来通知玩家(游戏实体)有关球的策略和位置以及其他对手的坐标的信息.我将在以后的阶段中添加它.(I was thinking of using the Subject-Observer pattern to notify players (Gaming entity) about the strategy and the position of the ball and the co-ordinates of other opponents. I will add it in a later stage.)
历史(History)
不适用(N/A)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC8.0 C++ VC7.1 Win2003 Vista WinXP Visual-Studio STL VS2005 Design 新闻 翻译