/************************************************************************/
/* 预先加载一些生物以备将来使用 */
/* 专门用来缓存生物用的 */
/* 使用时通过生物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 = ); //////////////////////////////////////////////////////////////////////////
//没有添加到引用计数里(需要手动释放),调用这些函数释放
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; }

CBiontCache的更多相关文章

随机推荐

  1. easyui icon的使用相关

    easyui的默认图标有以下这些: .icon-blank{ background:url('icons/blank.gif') no-repeat; } .icon-add{ background: ...

  2. Android 使用SWIG生成Jni代码

    一,下载SWIG,并设置好环境变量 二以HelloJni为例子 导入ndk sample的HelloJni工程. 三 创建SWIG接口文件 在jni目录下新建文件Unix.i,输入SWIG代码: %m ...

  3. C#中this的作用

    一.C# this指针的几种用法 1.限定被相似的名称隐藏的成员   C# 代码   复制 public class ThisName { public string name = "张三& ...

  4. MSSQL 触发器 暂停 和 启动

    开启关闭触发器 禁用: ALTER TABLE member DISABLE TRIGGER trig1 GO 恢复: ALTER TABLE member ENABLE TRIGGER trig1 ...

  5. 如何修改Git commit的信息

    原文地址: http://xiguada.org/change-git-commit-message   Git cimmit信息push后,如何修改,amend可以修改最后一次commit信息,但对 ...

  6. Cognos TM1_10.1.1服务端配置

    场景:本文继Cognos TM1_10.1.1服务端安装 之后,简单的说一下本人对简单配置的拙见,确保服务端在安装过程一切正常,成功安装. 1:进入TM的Cognos Configuration 2: ...

  7. 用sc命令查询系统状态

    用sc命令查询系统状态 sc query 语法 sc query                - Enumerates status for active services & driver ...

  8. [AngularJS] Angular 1.3: ng-model-options updateOn, debounce

    <!DOCTYPE html> <html ng-app="app"> <head lang="en" > <meta ...

  9. ipkg包管理

    添加ipkg更新源 你可以通过修改/opt/etc/ipkg.conf去添加 ############################ echo src optware http://ipkg.nsl ...

  10. win7 配置IIS + php 环境

    一:开启win7 的IIS功能:并开启 CGI 二:下载php5 for windows 解压 三:打开IIS --->>处理程序映射- 参考:http://php.net/manual/ ...