Global是全局單類

Global是全局單類


wKiom1VItT7CJUNoAACHDMnta8g982.jpg


唯一實例Single

Single.h

#ifndef _SINGLETON_H
#define _SINGLETON_H
using namespace std;
template <class T>
class Singleton
{
    public:
        //獲取類的唯一實例
        static inline T* instance();
        //釋放類的唯一實例
        void release();
    protected:
        Singleton(void){}
        ~Singleton(void){}
        static T* _instance;
};
template <class T>
inline T* Singleton<T>::instance()
{
        if(NULL == _instance){
            _instance = new T;
        }
    return _instance;
}
template <class T>
void Singleton<T>::release()
{
    if (!_instance)
        return;
    delete _instance;
    _instance = 0;
}
//cpp文件中需要先聲明靜態變量
#define DECLARE_SINGLETON_MEMBER(_Ty)   \
    template <> _Ty* Singleton<_Ty>::_instance = NULL;
#endif//_SINGLETON_H


全局類Global

我把後面需要用的都放進去了,頭文件好說. 

Global.h

#ifndef _GLOBAL_H_
#define _GLOBAL_H_
#include "cocos2d.h"
USING_NS_CC; 
#include "Singleton.h"
#include "GameLayer.h"
#include "OperateLayer.h"
#include "StateLayer.h"
//需引入以下類,否則在這些類中訪問單例對象會報錯
class GameLayer;
class OperateLayer;
class StateLayer;
class Hero;
class Enemy;
//全局單例
class Global :public Singleton<Global>
{
    public:
        Global(void);
        ~Global(void);
        //GameScene *gameScene;
        GameLayer *gameLayer;           //遊戲層
        OperateLayer *operateLayer;     //操作層
        StateLayer * stateLayer;        //狀態層
        Hero *hero;                     //英雄
        __Array *enemies;               //敵人
        TMXTiledMap *tileMap;           //地圖
        Point tilePosFromLocation(Vec2 MovePoint, TMXTiledMap *map = NULL);//將point轉換成地圖GID的point
        bool  tileAllowMove(Vec2 MovePoint);
};
#define global Global::instance()
#endif


Global.cpp

#include "Global.h"
DECLARE_SINGLETON_MEMBER(Global);
Global::Global(void)
{
}
Global::~Global(void)
{
    CC_SAFE_DELETE(gameLayer);
    CC_SAFE_DELETE(operateLayer);
    CC_SAFE_DELETE(stateLayer);
    CC_SAFE_DELETE(hero);
    CC_SAFE_DELETE(enemies);
    //CC_SAFE_DELETE(tileMap);
    gameLayer = NULL;           
    operateLayer= NULL; 
    stateLayer= NULL;       
    hero= NULL;             
    enemies= NULL;      
    tileMap= NULL;      
}
Point Global::tilePosFromLocation(Point MovePoint, TMXTiledMap *map) 
{
    Point point = MovePoint - map->getPosition();
    Point pointGID = Vec2::ZERO;
    pointGID.x = (int) (point.x / map->getTileSize().width); 
    pointGID.y = (int) ((map->getMapSize().height * map->getTileSize().height - point.y) / map->getTileSize().height); 
    return pointGID; 
}
bool Global::tileAllowMove(Point MovePoint)
{
    TMXLayer *floor = global->tileMap->getLayer("Floor");
    Point tileGid = tilePosFromLocation(MovePoint,global->tileMap);
    auto allowpoint =floor->getTileGIDAt(tileGid);
    if(0 == allowpoint)
    {
        return false;
    }
    return true;
}

在需要用到的地方註冊他比如GameLayer中:

GameLayer.h

#include "Global.h"

根據Global.h中所定義的,在對應cpp中需要添加諸如 

- .cpp

GameLayer::GameLayer()
{
    global->gameLayer=this;
}
OperateLayer::OperateLayer()
{
    global->operateLayer=this;
}
StateLayer::StateLayer()
{
    global->stateLayer = this;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章