/************************************************************************/
/* 预先加载一些生物以备将来使用 */
/* 专门用来缓存生物用的 */
/* 使用时通过生物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. 神经网络可以拟合任意函数的视觉证明A visual proof that neural nets can compute any function

    One of the most striking facts about neural networks is that they can compute any function at all. T ...

  2. Python学习(三)流程控制

    Python流程控制 本章介绍 python 的基础流程控制.包括 if 条件语句.for 循环 和 while 循环语句: continue 及 break 的用法等. 基本用法与 C 和 Java ...

  3. 1644 免费馅饼 题解(c++)

    1644 免费馅饼(巴蜀oj上的编号) 题面:          SERKOI最新推出了一种叫做"免费馅饼"的游戏.         游戏在一个舞台上进行.舞台的宽度为W格,天幕的 ...

  4. 如何模拟登陆添加了CSRF保护的网站

    上次写了篇文章,内容是如何利用WebClient模拟登陆CSRF控制的网站,回复中有人还是不理解,现在另开一篇,打算说说用Python如何来登陆. 开写之前,先说下为什么webrequest不行,常规 ...

  5. RS报表自动适应屏幕分辨率大小

    问题:同一个报表,由于用户电脑显示器大小,分辨率大小不同显示的不一样,看起来不完整或者很不协调 原因:设计报表大小属性的时候使用了固定值属性,比如限制为宽:1200px 那么在电脑屏幕小分辨率很小的情 ...

  6. 第一个Xamarin的 Android 应用程序!

    你好,安卓 Xamarin的工作室 Xamarin的应用程序图标和启动屏幕 脱机使用PDF格式: 介绍与Xamarin的Android开发 示例代码: 开始使用应用程序的探险家 显示说明: Visua ...

  7. 关于CPU的运行队列与系统负载

    在linux操作系统中,我们一般查看系统的cpu负载情况常用的命令可以是uptime,top,还有vmstat等这些个都是可以有的.每个工具所提供的信息各不相同, 我这里要讨论的仅说cpu部分.使用u ...

  8. PostgreSQL流复制参数max_wal_senders详解

    转自:http://my.oschina.net/Kenyon/blog/152234PostgreSQL 9.2.4 主机:192.25.10.76 从机:192.25.10.71 做postgre ...

  9. 读TIJ -7 多形性

    <Think in java·第 7 章  多形性> [面向对象的程序设计语言三种最主要的特征:数据抽象.继承和多态] 在这个层面是没有什么"思想"好谈的!当你依照人们 ...

  10. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...