zoukankan      html  css  js  c++  java
  • CBiontCache

    /************************************************************************/
    /* 预先加载一些生物以备将来使用                                            */
    /* 专门用来缓存生物用的                                                    */
    /* 使用时通过生物ID或者名字来查找对象                                    */
    /************************************************************************/
    
    #ifndef __BIONTCACHE_H__
    #define __BIONTCACHE_H__
    #include "GameFrameHead.h"
    
    class CBiont;
    class GAMEFRAME_API CBiontCache
    {
    public:
        ~CBiontCache();
        static CBiontCache* getInstance();
        static void destroy();
        void init();
    
        void addBiont(int nType, CBiont* pBiont);
    
        vector<CBiont*>    getBionts(int nType);
    
        CBiont*    getBiontById(int nId);
    
        //获取一种生物  不够数拷贝
        //vector<CBiont*> getBiontKind(int nType, int nAmount);
    
        unsigned int getBiontKindSize();
    
        void releas();
    
        void setParentLayer(CCNode* pLayer, int zOrder = 1);
    
    //////////////////////////////////////////////////////////////////////////
        //没有添加到引用计数里(需要手动释放),调用这些函数释放 
        void removeAll();
        void removeVecBiont(int nType);
        void removeBiont(int nId);
    
    private:
        CBiontCache();
    
        static CBiontCache* g_pBiontCache;
    
        map<int, vector<CBiont*> > m_mapVecBion; //生物群字典(以类型为键)
        CCNode*        m_pParentLayer;        
    
    };
    
    #endif //__BIONTCACHE_H__
    #include "BiontCache.h"
    #include "Biont.h"
    
    CBiontCache* CBiontCache::g_pBiontCache = NULL;
    
    CBiontCache::CBiontCache()
    {
        m_mapVecBion.clear();
        m_pParentLayer = NULL;
    }
    
    CBiontCache::~CBiontCache()
    {
    
    }
    
    CBiontCache* CBiontCache::getInstance()
    {
        if (!g_pBiontCache)
        {
            g_pBiontCache = new CBiontCache();
            ASSERT(g_pBiontCache);
        }
        return g_pBiontCache;
    }
    
    void CBiontCache::destroy()
    {
        SAFE_DELETE(g_pBiontCache);
    }
    
    void CBiontCache::init()
    {
    
    }
    
    vector<CBiont*> CBiontCache::getBionts( int nType )
    {
        return m_mapVecBion.find(nType)->second;
    }
    
    void CBiontCache::releas()
    {
        this->removeAll();
    }
    
    void CBiontCache::addBiont( int nType, CBiont* pBiont )
    {
        ASSERT(pBiont);
    
        //判断存在已有的键
        for(map<int, vector<CBiont*> >::iterator it = m_mapVecBion.begin(); it != m_mapVecBion.end(); it++)
        {
            if (it->first == pBiont->getType())
            {
                ((*it).second).push_back(pBiont);
                return; //跳出 
            }
        }
    
        //没有就创建新键
        vector<CBiont*> vecBiont;
        vecBiont.push_back(pBiont);
        m_mapVecBion[nType] = vecBiont;
        
    }
    
    
    void CBiontCache::removeAll()
    {
        for (map<int, vector<CBiont*> >::iterator it = m_mapVecBion.begin(); it != m_mapVecBion.end(); it++)
        {
            for (vector<CBiont*>::iterator biontIt = (it->second).begin(); biontIt != (it->second).end(); biontIt++)
            {    
                (*biontIt)->removeAllChildrenWithCleanup(true);
                (*biontIt)->removeFromParentAndCleanup(true);
                SAFE_DELETE(*biontIt);
            }
            (it->second).clear();
        }
    }
    
    void CBiontCache::removeVecBiont( int nType ) 
    {
        for (map<int, vector<CBiont*> >::iterator it = m_mapVecBion.begin(); it != m_mapVecBion.end(); it++)
        {
            if (it->first == nType)
            {
                for (vector<CBiont*>::iterator vceBiontIt = (it->second).begin(); vceBiontIt != (it->second).end(); vceBiontIt++)
                {            
                    SAFE_DELETE(*vceBiontIt);
                }
                (it->second).clear();
                break;
            }
        }
    }
    
    
    void CBiontCache::removeBiont( int nId )
    {
        for (map<int, vector<CBiont*> >::iterator it = m_mapVecBion.begin(); it != m_mapVecBion.end(); it++)
        {
            for (vector<CBiont*>::iterator vecBiontIt =  (it->second).begin(); vecBiontIt != (it->second).end(); vecBiontIt++)
            {
                if ((*vecBiontIt)->getId() == nId)
                {
                    SAFE_DELETE(*vecBiontIt);
                    break;
                }
            }
        }
    
    }
    
    unsigned int CBiontCache::getBiontKindSize()
    {
        return m_mapVecBion.size();
    }
    
    void CBiontCache::setParentLayer( CCNode* pLayer, int zOrder /*= 1*/ )
    {
        ASSERT(pLayer);
    
        for (map<int, vector<CBiont*> >::iterator it = m_mapVecBion.begin(); it != m_mapVecBion.end(); it++)
        {
            for (vector<CBiont*>::iterator vecBiontIt =  (it->second).begin(); vecBiontIt != (it->second).end(); vecBiontIt++)
            {
                if (*vecBiontIt)
                {
                    if ((*vecBiontIt)->getParent())
                    {
                        m_pParentLayer->removeChild(*vecBiontIt, false);
                    }
                    pLayer->addChild(*vecBiontIt, zOrder);
                }
            }
        }
    
        m_pParentLayer = pLayer;
    }
    
    CBiont* CBiontCache::getBiontById( int nId )
    {
        for (map<int, vector<CBiont*> >::iterator it = m_mapVecBion.begin(); it != m_mapVecBion.end(); it++)
        {
            for (vector<CBiont*>::iterator vecBiontIt =  (it->second).begin(); vecBiontIt != (it->second).end(); vecBiontIt++)
            {
                if ((*vecBiontIt)->getId() == nId)
                {
                    return *vecBiontIt;
                }
            }
        }
    
        CCLog("error: CBiontCache::getBiontById");
        return NULL;
    
    }
  • 相关阅读:
    win8 vs2010 openni2 配置
    写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的十个词打印出来。
    电梯调度
    new 一个button 然后dispose,最后这个button是null吗???
    org.apache.hadoop.security.AccessControlException
    算法导论第二章、插入排序
    算法导论第六章、堆排序
    算法导论第八章、计数排序
    观后感
    第二次随笔
  • 原文地址:https://www.cnblogs.com/newlist/p/3163812.html
Copyright © 2011-2022 走看看