/************************************************************************/
/* 预先加载一些生物以备将来使用 */
/* 专门用来缓存生物用的 */
/* 使用时通过生物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. [Android Pro] Android 必知必会-使用 supportV4 的 RoundedBitmapDrawable 实现圆角

    RoundedBitmapDrawable 是 supportV4 下的一个类,有了它,显示圆角和圆形图片的情况下就不需要额外的第三方类库了,还能和各种图片加载库配合使用. 背景 今天无意间看到一段实 ...

  2. OpenCV特征点检测

    特征点检测 目标 在本教程中,我们将涉及: 使用 FeatureDetector 接口来发现感兴趣点.特别地: 使用 SurfFeatureDetector 以及它的函数 detect 来实现检测过程 ...

  3. javascript实现浏览器窗口传递参数

    a.html <html> <head> <title>主页面</title> <script language="javascript ...

  4. 利用MyEclipse开发一个webservice接口

    一直以来对于接口这个东西都很好奇,各种客户也一直在说那个什么什么数据我们提供给你们一个接口就好了,结果还是不是很明白.于是乎就有了下面的小故事,接下来我们就进入正文吧 ---大概可以分为这样的步骤 1 ...

  5. linux下vim命令具体解释

    很具体的介绍linux中vim的操作命令. 高级一些的编辑器,都会包括宏功能,vim当然不能缺少了.在vim中使用宏是很方便的: :qx     開始记录宏.并将结果存入寄存器x q     退出记录 ...

  6. 移动端项目中 @2x 图 和 @3x 图 的使用

    移动端开发过程中,因为手机的dpr(设备像素比不同),我们需要根据dpr来修改图标的大小: 1.通过mixin,动态修改图标的背景图片.通过@media (媒体查询),判断设备的dpr. @mixin ...

  7. JavaScript数组方法说明

    JavaScript的数组方法有: http://www.w3school.com.cn/jsref/jsref_obj_array.asp 其中:concat.join和slice方法都不会修改原数 ...

  8. sae python中Mysql中文乱码的解决

    一開始我用的是: db=MySQLdb.connect(db=sae.const.MYSQL_DB,user=sae.const.MYSQL_USER,passwd=sae.const.MYSQL_P ...

  9. js 判断进入可视区域

      js 判断进入可视区域 CreateTime--2018年4月14日08:17:41 Author:Marydon 1.使用场景 图片懒加载时候需要用到,其他情况暂时没有遇到,欢迎留言补充! 2. ...

  10. servlet awt随机图片验证码

    package rd.test; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java. ...