/************************************************************************/
/* 预先加载一些生物以备将来使用 */
/* 专门用来缓存生物用的 */
/* 使用时通过生物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. react的传值类型PropTypes简单说明

    1.首先可以有那么多种类型 propTypes: { // 可以声明 prop 为指定的 JS 基本类型.默认 // 情况下,这些 prop 都是可传可不传的. optionalArray: Reac ...

  2. JS中实现字符串和数组的相互转化

    早上起来看了一道js的面试题,是这样描述的:利用var s1=prompt("请输入任意的字符串","")可以获取用户输入 的字符串,试编程将用户输入的字符串“ ...

  3. iOS:Xcode的beta下编译低版本项目时,出现的Link错误( "_fwrite$UNIX2003", referenced from:)

    开发的项目多了,对于一个i386的错误,处理起来应该是得心应手的,可是仔细看来,跟之前遇到i386的错误还不大一样,直接搜i386是搜不到该问题解决的方法,你要是搜“Undefined symbols ...

  4. 10723 Cyborg Genes (LCS + 记忆化搜索)

    Problem F Cyborg Genes Time Limit 1 Second September 11, 2132. This is the day that marks the beginn ...

  5. Python 爬虫(2)多线程

    前面说过由于GIL的存在,Python的多线程效率没有希望的那么高,python的多线程适合IO密集型的情况,而爬虫恰好就是一个IO密集的情况,因为爬虫中很大一部分时间,是在等待socket返回数据. ...

  6. 监听内容变化 TextWatcher @功能

    监听多个TextView的内容变化 使用示例 TextWatcherUtils.addTextChangedListener(isAllNotEmpty -> btnLogin.setEnabl ...

  7. 《java 语言程序设计》第3、4章编程练习

    3.1 public class test { public static void main(String[] args) { System.out.println("Enter a, b ...

  8. Ubuntu简单搭建git私有服务

    gitserver搭建过程 搭建gitserver过程记录 例如以下: 环境: serverUbuntu虚拟机(Boss),能通过网络訪问到(server地址:192.168.9.103). clie ...

  9. 函数y=sin(1/x)曲线

    该曲线在x趋近于零时振荡很剧烈,在远离零点时振荡越来越平缓. 图线: 代码: <!DOCTYPE html> <html lang="utf-8"> < ...

  10. Unity3d使用json与javaserver通信

    Unity3d使用json能够借助LitJson 下载LitJson,复制到Unity3d工作文件夹下 于是能够在代码中实现了 以下发送请求到server并解析 System.Collections. ...