#include "Terrain2.h"
#include <algorithm>
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));
Terrain2::~Terrain2 () {
CC_SAFE_RELEASE(_blockPool);
CC_SAFE_RELEASE(_blocks);
}
Terrain2::Terrain2()
:_screenSize(CCDirector::getInstance()->getWinSize())
,_startTerrain(false)
,_blockPoolIndex(0)
,_currentPatternCnt(1)
,_currentPatternIndex(0)
,_currentTypeIndex(0)
,_currentWidthIndex(0)
,_currentHeightIndex(0)
,_showGap(false)
{
}
Terrain2 * Terrain2::create() {
Terrain2 * terrain = new Terrain2();
//if (terrain && terrain->initWithFile("blank.png")) {
if (terrain && terrain->initWithSpriteFrameName("blank.png")) {
terrain->setAnchorPoint(Vect(0,0));
terrain->initTerrain();
terrain->autorelease();
return terrain;
}
CC_SAFE_DELETE(terrain);
return NULL;
}
void Terrain2::initTerrain () {
_increaseGapInterval = 5000;
_increaseGapTimer = 0;
_gapSize = 2;
_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 Terrain2::addBlocks(int currentWidth) {
Block * block;
while (currentWidth < _minTerrainWidth)
{
block = (Block *) _blockPool->getObjectAtIndex(_blockPoolIndex);
_blockPoolIndex++;
if (_blockPoolIndex == _blockPool->count()) {
_blockPoolIndex = 0;
}
this->initBlock(block);
currentWidth += block->getWidth();
_blocks->addObject(block);
printf("blockArray.count=%d\n",(int)_blocks->count());
}
this->distributeBlocks();
}
void Terrain2::initBlock(Block * block) {
int blockWidth;
int blockHeight;
int type = _blockTypes[_currentTypeIndex];
_currentTypeIndex++;
if (_currentTypeIndex == _blockTypes.size()) {
_currentTypeIndex = 0;
}
// _showGap=false;
//check if min distance reached;
if (_startTerrain) {
if (_showGap) {//生成空隙,用于主角掉下去
int gap = rand() % _gapSize; //设置多少空隙
if (gap < 2) gap = 2;
// gap=5;
block->setupBlock (gap, 0, kBlockGap);
_showGap = false;
}
else {//生成新的屋顶,有高度和宽度
//得到block的宽度
blockWidth = _blockWidths[_currentWidthIndex];
_currentWidthIndex++;
if (_currentWidthIndex == _blockWidths.size()) {
random_shuffle(_blockWidths.begin(), _blockWidths.end());
_currentWidthIndex = 0;
}
//得到block的高度
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 { //等于0,高度和上一个block一样
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;
_currentPatternIndex++;
if (_currentPatternIndex == _blockPattern.size()) {
random_shuffle(_blockPattern.begin(), _blockPattern.end());
_currentPatternIndex = 0;
}
_currentPatternCnt = 1;
}
}
} else { //初始的那5个走这个,高度都为2,相当于热身
_lastBlockHeight = 2;
_lastBlockWidth = rand() % 2 + 2;
block->setupBlock (_lastBlockWidth, _lastBlockHeight, type);
}
}
void Terrain2::distributeBlocks() {
int count = (int)_blocks->count();
Block * block;
Block * prev_block;
int i;
for (i = 0; i < count; i++) {
block = (Block *) _blocks->getObjectAtIndex(i);
if (i != 0) {
prev_block = (Block *) _blocks->getObjectAtIndex(i - 1);
block->setPositionX( prev_block->getPositionX() + prev_block->getWidth());
}
else
{
block->setPositionX ( 0 );
}
}
}
void Terrain2::move (float xMove) {
if (xMove < 0) {
return;
}
if (_startTerrain) { //开始生成间隙了
if (xMove > 0 && _gapSize < 5) //我的理解是越往后越难
{
_increaseGapTimer += xMove;
}
if (_increaseGapTimer > _increaseGapInterval) {
_increaseGapTimer = 0;
_gapSize += 1;
}
}
this->setPositionX(this->getPositionX() - xMove);
Block * block;
block = (Block *) _blocks->getObjectAtIndex(0);
if (this->getPositionX() + block->getWidth() < 0) { //最开始的一个block退到头了,第二个block出现在屏幕最左侧了马上
_blocks->removeObjectAtIndex(0);
//移除之后,为什么要重新添加呢,因为上面移除只是移除了blocks数组里面的block,但是这个block还是存在的,没有被释放,他还被添加到了
//Terrain中,所以如果你只是移除他,但是不重新添加,那么之后distributeBlocks中对block数组的排列就不会包括这个删除的block,也就是说
//这个block依然是原来的坐标,添加到Terrain中的,重新排列坐标对他没有影响,从而产生奇怪现象,你可以试试,把他屏蔽掉
_blocks->addObject(block);
printf("以前的坐标是%f\n",this->getPositionX() );
this->setPositionX(this->getPositionX() + block->getWidth());
printf("现在的坐标是%f\n",this->getPositionX());
float width_cnt = this->getWidth() - block->getWidth() - ((Block *) _blocks->getObjectAtIndex(0))->getWidth();
this->initBlock(block);//重新初始化,从而生成屋顶新的高度和宽度
//因为我们设置的最小block集合宽度为_minTerrainWidth,所以需要添加这一步,从而达到这个最小宽度
//当达到了,�
没有合适的资源?快使用搜索试试~ 我知道了~
RunSource_cppAndlua.zip
共46个文件
png:11个
cpp:7个
h:7个
需积分: 5 0 下载量 72 浏览量
2022-10-19
20:55:06
上传
评论
收藏 802KB ZIP 举报
温馨提示
RunSource_cppAndlua.zip
资源详情
资源评论
资源推荐
收起资源包目录
RunSource_cppAndlua.zip (46个子文件)
跑酷最好的代码-lua
res
font.fnt 1KB
iTunesArtwork 61KB
sprite_sheet.png 215KB
bg.png 5KB
.DS_Store 6KB
font.png 19KB
sprite_sheet.plist 14KB
Info.plist 1KB
blank.png 923B
.DS_Store 6KB
src
Terrain2.lua 10KB
main.lua 2KB
GameSprite.lua 778B
config.lua 661B
.DS_Store 6KB
Player.lua 6KB
GameLayer.lua 6KB
Block.lua 4KB
跑酷最好的代码c++3.0
Resources
font.fnt 1KB
iTunesArtwork 61KB
CloseSelected.png 5KB
CloseNormal.png 6KB
sprite_sheet.png 215KB
bg.png 5KB
HelloWorld.png 142KB
.DS_Store 6KB
font.png 19KB
sprite_sheet.plist 14KB
Info.plist 1KB
blank.png 923B
.DS_Store 6KB
Classes
GameLayer.cpp 9KB
Block.cpp 10KB
VisibleRect.cpp 3KB
GameSprite.h 991B
AppDelegate.cpp 2KB
Player.h 2KB
AppDelegate.h 1017B
Terrain2.h 1KB
.DS_Store 6KB
VisibleRect.h 580B
Terrain2.cpp 13KB
Block.h 2KB
GameLayer.h 1KB
Player.cpp 6KB
GameSprite.cpp 192B
共 46 条
- 1
weixin_73697659
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0