【游戏核心算法编程内幕】---设计模式
来源:互联网 发布:js恶搞死机 编辑:程序博客网 时间:2024/06/01 14:41
单体模式
单体模式是个全局对象,整个应用程序中只有一个实例。首先声明一个类,只有一个公开方法,用于请求单体的实例。第一次请求时生成单体,后面再请求时只是返回该单体的指针。
class Singleton{public: static Singleton* Instance();protected: Singleton();private: static Singleton* _instance;};Singleton* Singleton::_instance = 0;Singleton* Singleton::Instance(){ if(_instance == 0) { _instance = new Singleton; } return _instance;}
战略模式
目的是分开类定义与一个或几个成员算法,使这些算法可以在运行时互换。需要2个类,第1个是战略类,提供战略算法,这是个抽象类,在子类中派生特定战略;第2个是情景类,定义采用战略的情景,其成员执行选择的战略并在需要时变换战略。
战略及2个派生类:
class strategy{public: virtual int recal_strategy(point, float) = 0;protected: strategy();};class fightstragegy::public strategy{public: strategy(); virtual int recal_strategy(point, float);};class idlestrategy::public strategy{public: strategy(); virtual int recal_strategy(point, float);};
士兵动态改变战略:
class soldier{public: soldier(strategy *); void recalc_AI(); void change_strategy(strategy *);private: point pos; float yaw; strategy* _thestrategy;};soldier::soldier(strategy *stra){ _thestrategy = stra;}void soldier::recalc_AI(){ _thestrategy->recal_strategy();}void soldier::change_strategy(strategy *stra){ _thestrategy = stra;}
用法:
soldier *soldier1 = new soldier(new idelstrategy); soldier->recalc_AI(); soldier1->change_strategy(new fightstreagy); soldier1->recalc_AI();
工厂模式
将对象生成/删除代码集中起来,从而提供了对象处理的通用可靠方法。
对于抽象工厂,最好的例子是游戏引擎中的中央对象生成器。
class Product{};class Texture : public Product{};class Mesh : public Product{};class Item : public Product{};typedef int ProductID;#define TEXTURE 0#define MESH 1#define ITEM 2class AbstractFactory{public: Product* Create(ProductID);};Product* AbstractFactory::Create(ProductID id){ switch(id) { case TEXTURE: return new Texture; break; case MESH: return new Mesh; break; case ITEM: return new Item; break; }}
调用:
AbstractFactory AF; Texture *t = (Texture*)AF.Create(TEXTURE);
不用抽象类
class Texture{};class Mesh{};class Item{};class Factory{public: Texture* CreateTexture(); Mesh* CreateMesh(); Item* CreateItem();};Texture* Factory::CreateTexture(){ return new Texture;}Mesh* Factory::CreateMesh(){ return new Mesh;}Item* Factory::CreateItem(){ return new Item;}
调用:
Factory F; Texture *t = F.CreateTexture();
复合模式
生成异构部分/整体层次,用标准接口访问基本对象和复合对象。
Level为整个关卡,LevelItem为关卡中的基本实体:药剂、用户可以抓住的物体等。
// 关卡中的基本实体class LevelItem{...};// 整个关卡class Level{public: virtual ~Level(); const char* Name(){return _name} virtual float LifePoints(); virtual int NumEnemies(); virtual void Add(LevelItem*); virtual void Remove(LevelItem*); virtual Iterator<LevelItem*>* CreateIterator();protected: LevelItem(const char*); private: const char* _name;};class Potion: public LevelItem{public: Potion(const char*); virtual ~Potion(); virtual float LifePoints();};class CompositItem: public LevelItem{public: virtual ~CompositionItem(); virtual float LifePoints(); virtual int NumEnemies(); virtual void Add(LevelItem*); virtual void Remove(LevelItem*); virtual Iterator<LevelItem*>* CreateIterator();protected: CompositItem(const char*);private: List<LevelItem*> _items;};float CompositItem::LifePoints(){ Iterator<LevelItem*>* i = CreateIterator(); float total = 0; for(i->First(); !i->IsDone(); i->Next()) { total += i->CurrentItem()->LifePoints(); } delete i; return total;}int CompositItem::NumEnemies(){ Iterator<LevelItem*>* i = CreateIterator(); int total = 0; for(i->First(); !i->IsDone(); i->Next()) { total += i->CurrentItem()->LifePoints(); } delete i; return total;}class Enemy: public CompositItem{public: Enemy(const char*); virtual ~Enemy(); virtual float LifePoints(); virtual int NumEnemies();};class SubLevel: public CompositeItem{public: SubLevel(const char*); virtual ~SubLevel(); virtual float LifePoints(); virtual int NumEnemies();};void LordOfTheRins(){ Level* MidleEarth = new Level("Midle Earth"); SubLevel* TheShire = new SubLevel("The Shire"); SubLevel* Moria = new SubLevel("Mines of Moria"); MidleEarth->Add(TheShire); MidleEarth->Add(Moria); Enemy *Balrog = new Enemy("Balrog"); Moria->Add(Balrog); Potion *Lembas = new Potion("Lembas"); TheShire->Add(Lembas); cout << "The number of monsters in Midle Earth is " << MidleEarth->NumEnemies() << endl; cout << "The life points for the monsters are " << MidleEarth->LifePoints() << endl;}
轻量级模式
把对象分成两个不同类。首先要生成实际的轻量级对象,这是核心对象,是所有实例共享的。轻量级对象通过FlyweightFactory类管理,其生成并在内存中存储轻量级对象。轻量级对象包含对象的所有固有元素,即独立于对象环境的所有信息,因此是共享的。其次,外部对象使用轻量级对象,将外部信息作为参数传入。这些具体对象包含状态信息,如战略游戏中步兵的位置和生命力。
// 包含所有无状态士兵数据结构和算法// 实际的轻量级对象,所有实例共享class InfectorySoldier: public AbstracFlyweight{ float speed; float turnspeed; (...)public: void Paint(ExtrinsicSoldierInfo *); void RecalAI(ExtrinsicSoldierInfo *);};// 简化的类// 外部对象使用轻量级对象class InfantrySoldierInstance{ ExtrinsicSoldierInfo info;public: void Paint(); void RecalAI();};void InfantrySoldierInstance::Paint(){ FlyweightFactory *F = new FlyweightFactory; InfectorySoldier *IS = F->GetFlyweight(INSTANCE_SOLDIER); IS->Paint(info);}void InfactrySoldierInstance::RecalAI(){ FlyweightFactory *F = new FlyweightFactory; InfectorySoldier *IS = F->GetFlyweight(INSTANCE_SOLDIER); IS->RecalAI(info);}// 将轻量级对象名作为参数传入,取得轻量级模式// 轻量级对象通过FlyweightFactory类管理class FlyweightFactory{ AbstractFlyweight *flyweight; int NumFlyweights;public: AbstractFlyweight* GetFlyweight();};AbstractFlyweight* FlyweightFactory::GetFlyweight(int key){ if(flyweight[key] exsts) return flyweight[key]; flyweight[key] = new AbstractFlyweight; return flyweight[key];}
0 0
- 【游戏核心算法编程内幕】---设计模式
- 游戏核心算法编程内幕学习(三):设计模式
- 游戏核心算法编程内幕学习(一):游戏发展历史
- 游戏核心算法编程内幕学习(二):游戏开发过程
- 游戏核心算法编程内幕学习(四):基本人工智能技术介绍
- Windows核心编程之线程内幕
- 游戏编程设计模式-命令模式Unity示例
- 微核心编程模式
- 游戏核心编程之一 概述
- 打地鼠游戏核心算法
- 消除类游戏核心算法
- 三消类游戏的核心算法
- 消除游戏的核心算法
- 打地鼠游戏核心算法
- struts2技术内幕读书笔记1--设计模式
- python核心编程第六章题目:python代码实现:设计一个"石头,剪子,布"游戏
- 【游戏设计模式】之四 《游戏编程模式》全书内容提炼总结
- 【游戏设计模式】之四 《游戏编程模式》全书内容提炼总结
- Java类修饰符
- 深入浅出JMS(一)--JMS基本概念
- Android-Tips
- 负载均衡时,在State_Server模式中的Session共享问题(不讨论负载均衡的问题)
- JAVA中数个字符串转换为JSON格式的字符串
- 【游戏核心算法编程内幕】---设计模式
- pageResponse.js一套视觉稿适配移动设备全家,让烦恼通通见鬼去
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- 【九度OJ】题目1180:对称矩阵 解题报告
- 不花大钱也能拍出专业质感作品!终极DIY大招
- Fiddler测试webAPI
- React Native 开发即时预览与分享工具
- 第一次开通博客
- 解决小米手机不能运行Android Studio程序的问题