您的位置:首页 > 移动开发 > Cocos引擎

【cocos2d-x IOS游戏开发-城市跑酷8】实现街区房子的摆放

2013-12-08 17:44 429 查看
尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17202061

<捕鱼达人>回顾

【cocos2d-x
IOS游戏开发-捕鱼达人1】内容介绍

上节回顾

【cocos2d-x
IOS游戏开发-城市跑酷7】设计烟囱与烟的效果

有了街区的一系列元素,你就可以开始摆放房子了。

这里,首先介绍STL的一种随机算法:random_shuffle(),用来对一个元素序列进行重新排序(随机的),包含在头文件 algorithm.h中。

函数原型如下:

template<class RandomAccessIterator>

void random_shuffle(

RandomAccessIterator _First, //指向序列首元素的迭代器

RandomAccessIterator _Last  //指向序列最后一个元素的下一个位置的迭代器

);

template<class RandomAccessIterator, class RandomNumberGenerator>

void random_shuffle(

RandomAccessIterator _First,

RandomAccessIterator _Last,

RandomNumberGenerator& _Rand //调用随机数产生器的函数

);
random_shuffle()是一个完全通用的算法,适用于内置数据类型和用户自定义类型。同时,由于STL算法不仅适用于容器,也适用于序列,因此,random_shuffle()算法可用于内置数组。

默认的random_shuffle中, 被操作序列的index 与 rand()
% N 两个位置的值交换,来达到乱序的目的。

来看具体是用法:

为了随机的生成街区,设置的一组模式;

//随机的模式、宽、高、及类型
int patterns[] = {1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,3,3,3};
int widths[] = {2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4};
int heights[] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,3,3,3,3,3,3,4};
int types[] = {1,2,3,4,1,3,2,4,3,2,1,4,2,3,1,4,2,3,1,2,3,2,3,4,1,2,4,3,1,3,1,4,2,4,2,1,2,3};

//街区模式
vector<int> _blockPattern (patterns, patterns + sizeof(patterns) / sizeof(int));
//街区宽度
vector<int> _blockWidths (widths, widths + sizeof(widths) / sizeof(int));
//街区高度
vector<int> _blockHeights (heights, heights + sizeof(heights) / sizeof(int));
//街区类型
vector<int> _blockTypes (types, types + sizeof(types) / sizeof(int));
就可以直接使用算法了:

random_shuffle(_blockPattern.begin(), _blockPattern.end());
random_shuffle(_blockWidths.begin(), _blockWidths.end());
random_shuffle(_blockHeights.begin(), _blockHeights.end());

来看街区的初始化:

blockWidth = _blockWidths[_currentWidthIndex];

_currentWidthIndex++;
if (_currentWidthIndex == _blockWidths.size()) {
random_shuffle(_blockWidths.begin(), _blockWidths.end());
_currentWidthIndex = 0;
}

if (_blockHeights[_currentHeightIndex] != 0) {

//change height of next block
blockHeight = _blockHeights[_currentHeightIndex];
//if difference too high, decrease it
if (blockHeight - _lastBlockHeight > 2 && _gapSize == 2) {
blockHeight = 1;
}

} else {
blockHeight = _lastBlockHeight;
}
_currentHeightIndex++;
if (_currentHeightIndex == _blockHeights.size()) {
_currentHeightIndex = 0;
random_shuffle(_blockHeights.begin(), _blockHeights.end());

}

block->setupBlock (blockWidth, blockHeight, type);
_lastBlockWidth = blockWidth;
_lastBlockHeight = blockHeight;

//select next block series pattern
_currentPatternCnt++;
if (_currentPatternCnt > _blockPattern[_currentPatternIndex]) {
_showGap = true;
//start new pattern
_currentPatternIndex++;
if (_currentPatternIndex == _blockPattern.size()) {
random_shuffle(_blockPattern.begin(), _blockPattern.end());
_currentPatternIndex = 0;
}
_currentPatternCnt = 1;
}
}

然后初始化地形图:

//设计一个街区块pool用数组存起来
_blockPool = CCArray::createWithCapacity(20);
_blockPool->retain();

//init object pools
Block * block;
for (int i = 0; i < 20; i++) {
//初始化街区块
block = Block::create();
this->addChild(block);
_blockPool->addObject(block);
}

_blocks = CCArray::createWithCapacity(20);
_blocks->retain();

_minTerrainWidth = _screenSize.width * 1.5f;

random_shuffle(_blockPattern.begin(), _blockPattern.end());
random_shuffle(_blockWidths.begin(), _blockWidths.end());
random_shuffle(_blockHeights.begin(), _blockHeights.end());

this->addBlocks(0);

添加街区实现:

void Terrain::addBlocks(int currentWidth) {

Block * block;
while (currentWidth < _minTerrainWidth)
{
block = (Block *) _blockPool->objectAtIndex(_blockPoolIndex);
_blockPoolIndex++;
if (_blockPoolIndex == _blockPool->count()) {
_blockPoolIndex = 0;
}
this->initBlock(block);
currentWidth +=  block->getWidth();
_blocks->addObject(block);

}

this->distributeBlocks();
}

//分配街区块
void Terrain::distributeBlocks() {
int count = _blocks->count();

Block * block;
Block * prev_block;
int i;

for (i = 0; i < count; i++) {
block = (Block *) _blocks->objectAtIndex(i);
if (i != 0) {
prev_block = (Block *) _blocks->objectAtIndex(i - 1);
block->setPositionX( prev_block->getPositionX() + prev_block->getWidth());
}
else
{
block->setPositionX ( 0 );
}
}
}

下节:设计陷阱
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐