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 = ); //////////////////////////////////////////////////////////////////////////
//没有添加到引用计数里(需要手动释放),调用这些函数释放
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的更多相关文章
随机推荐
- html端编码规范
理想的方式是让HTML只用于定义内容呈现的结构,让CSS控制内容呈现的样式,而所有功能的实现定义在JavaScript中
- 一些LR的经验,讲的还不错
https://blog.csdn.net/Dinosoft/article/details/50492309 记录一下.
- JavaScript 你不知道的事 -- 关于函数
接上篇Javascript 你不知道的事,直接条列了: 每个函数创建时默认带有一个prototype属性,其中包含一个constructor属性,和一个指向Object对象的隐藏属性__proto__ ...
- jQuery代码性能优化
代码优化是一个很重要的开发态度,一点点的优化对于程序来讲可能是微乎其微的,但是把所有的一点都加起来就能够达到水滴石穿的效果,所以要在平时的开发过程中养成优化代码的好习惯. 1. 检测元素是否存在 避免 ...
- 神奇的container_of
container_of是linux内核中常用的一个宏,这个宏的功能是,根据某个结构体字段的指针,找到对应的结构体指针. 话不多说,先上源码: /** * container_of - cast a ...
- [Grunt] Minifying your output with grunt-uglify
For production we want to use minified javascript to reduce the payload that is sent from the server ...
- windows bat文件运行中文乱码
windows bat文件运行中文乱码 CreationTime--2018年7月17日08点51分 Author:Marydon 1.情景展示 运行bat文件,里面的中文提示显示乱码 2.问题剖 ...
- sql各种连接详解
迁移时间:2017年6月1日16:33:58 CreateTime--2016年9月14日11:19:00Author:Marydon sql各种连接详解 参考链接: http://www.jb5 ...
- FastDFS简易概括
FastDFS是一个文件系统,可以部署在Linux上. 该文件系统具备高可用和负载均衡特性,还可以动态扩充容量. 此文件系统有两个服务组成:跟踪服务和存储服务,也就是说你必须部署了这两种服务,这个文件 ...
- Android异步下载
概述 实现App常见下载公共 支持通知栏显示 支持 暂停.取消功能,使用Service.AsyncTask实现异步下载.特点简单.实用.方便源码扩展修改 详细 代码下载:http://www.demo ...