【游戏核心算法编程内幕】---设计模式

单体模式

单体模式是个全局对象,整个应用程序中只有一个实例。首先声明一个类,只有一个公开方法,用于请求单体的实例。第一次请求时生成单体,后面再请求时只是返回该单体的指针。

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;
}

【游戏核心算法编程内幕】---设计模式_第1张图片

战略模式

目的是分开类定义与一个或几个成员算法,使这些算法可以在运行时互换。需要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 2

class 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();

【游戏核心算法编程内幕】---设计模式_第2张图片

复合模式

生成异构部分/整体层次,用标准接口访问基本对象和复合对象。

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* 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* CreateIterator();

protected:
    CompositItem(const char*);

private:
    List _items;
};

float CompositItem::LifePoints()
{
    Iterator* i = CreateIterator();
    float total = 0;
    for(i->First(); !i->IsDone(); i->Next())
    {
        total += i->CurrentItem()->LifePoints();
    }
    delete i;
    return total;
}

int CompositItem::NumEnemies()
{
    Iterator* 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;
}

【游戏核心算法编程内幕】---设计模式_第3张图片

轻量级模式

把对象分成两个不同类。首先要生成实际的轻量级对象,这是核心对象,是所有实例共享的。轻量级对象通过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];
}

【游戏核心算法编程内幕】---设计模式_第4张图片

你可能感兴趣的:(游戏)