#include "PopStarLayer.h"
#include "GameLevel.h"
#include "GameScene.h"
#include "Star.h"
#include "PopStarDataMgr.h"
#include "PopStarState.h"
#include "StartLayer.h"
#include "PopStar.h"
USING_NS_CC;
namespace
{
const int STAR_WIDTH = 48;
const int STAR_HEIGHT = 48;
}
PopStar::PopStar(PopStarLayer* layer)
{
gameLayer = layer;
//ZeroMemory(stars, sizeof(Star*) * ROW_NUM * COL_NUM);
memset(stars, 0, sizeof(Star*) * ROW_NUM * COL_NUM);
elapsedTime = 0;
currentState = NULL;
init();
}
PopStar::~PopStar()
{
for (int row = 0; row < ROW_NUM; ++row)
{
for (int col = 0; col < COL_NUM; ++col)
{
Star* star = stars[row][col];
if (stars[row][col])
{
gameLayer->removeChild(stars[row][col]);
stars[row][col] = NULL;
}
}
}
if (currentState)
{
delete currentState;
currentState = NULL;
}
gameLayer = NULL;
}
PopStar* PopStar::create(PopStarLayer* layer)
{
PopStar* pRet = new PopStar(layer);
if (pRet)
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
bool PopStar::init()
{
if (!CCNode::init())
{
return false;
}
if (!gameLayer)
{
return false;
}
for (int row = 0; row < ROW_NUM; ++row)
{
for (int col = 0; col < COL_NUM; ++col)
{
int index = (rand() % 5) + 1;
Star* star = Star::create(index);
if (star)
{
star->setScale(1.5);
star->setPos(ccp(STAR_WIDTH*col + STAR_WIDTH/2, STAR_HEIGHT*row + STAR_HEIGHT/2 + row*20+col*5));
star->setDestPos(ccp(STAR_WIDTH*col + STAR_WIDTH/2, STAR_HEIGHT*row + STAR_HEIGHT/2));
gameLayer->addChild(star);
stars[row][col] = star;
}
}
}
int level = getPopStarDataMgr().getLevel();
gameLayer->onGuiEvent(EVENT_UPDATE_LEVEL, level);
int score = getPopStarDataMgr().getScore();
gameLayer->onGuiEvent(EVENT_UPDATE_SCORE, score);
int historyScore = getPopStarDataMgr().getHistoryScore();
gameLayer->onGuiEvent(EVENT_UPDATE_TOTAL_HISTORY_SCORE, historyScore);
int historyLevelScore = getPopStarDataMgr().getHistoryLevelScoreByLevel(level);
gameLayer->onGuiEvent(EVENT_UPDATE_LEVEL_HISTORY_SCORE, historyLevelScore);
int targetScore = getPopStarDataMgr().getTargetScoreByLevel(level);
gameLayer->onGuiEvent(EVENT_UPDATE_TARGET_SCORE, targetScore);
currentState = new GameInitState(this);
return true;
}
void PopStar::onClick(const cocos2d::CCPoint& pos)
{
int row = pos.y / STAR_HEIGHT;
int col = pos.x / STAR_WIDTH;
if (row >= ROW_NUM || col >= COL_NUM)
{
return;
}
Star* star = stars[row][col];
if (!star)
{
return;
}
if (star->isHighLight())
{
// 高亮则消除
this->onReduce();
gameLayer->onGuiEvent(EVENT_UPDATE_REDUCE_SCORE, 0);
}
else
{
// 选中则高亮
SelectStar selectStar;
selectStar.row = row;
selectStar.col = col;
selectStar.star = star;
this->onSelect(selectStar);
}
}
void PopStar::setScore(int score)
{
if (!gameLayer)
{
return;
}
getPopStarDataMgr().setScore(score);
gameLayer->onGuiEvent(EVENT_UPDATE_SCORE, score);
}
void PopStar::setLevel(int level)
{
if (!gameLayer)
{
return;
}
getPopStarDataMgr().setLevel(level);
gameLayer->onGuiEvent(EVENT_UPDATE_LEVEL, level);
}
void PopStar::setHistoryScore(int score)
{
if (!gameLayer)
{
return;
}
getPopStarDataMgr().setHistoryScore(score);
gameLayer->onGuiEvent(EVENT_UPDATE_TOTAL_HISTORY_SCORE, score);
}
void PopStar::setHistoryLevelScore(int score)
{
if (!gameLayer)
{
return;
}
int level = getPopStarDataMgr().getLevel();
getPopStarDataMgr().setHistoryLevelScore(level, score);
gameLayer->onGuiEvent(EVENT_UPDATE_LEVEL_HISTORY_SCORE, score);
}
void PopStar::setTargetScore(int score)
{
if (!gameLayer)
{
return;
}
gameLayer->onGuiEvent(EVENT_UPDATE_TARGET_SCORE, score);
}
void PopStar::onUpdate(float delta)
{
for (int row = 0; row < ROW_NUM; ++row)
{
for (int col = 0; col < COL_NUM; ++col)
{
Star* star = stars[row][col];
if (star)
{
star->onUpdate(delta);
}
}
}
if (currentState)
{
currentState->execute(delta);
}
}
bool PopStar::isLevelEnd()
{
for (int row = 0; row < ROW_NUM; ++row)
{
for (int col = 0; col < COL_NUM; ++col)
{
Star* star = stars[row][col];
if (star == NULL)
{
continue;
}
int checkRow = row + 1;
if (checkRow < ROW_NUM)
{
Star* checkStar = stars[checkRow][col];
if ( checkStar && (star->getIndex() == checkStar->getIndex()) )
{
return false;
}
}
int checkCol = col + 1;
if (checkCol < COL_NUM)
{
Star* checkStar = stars[row][checkCol];
if ( checkStar && (star->getIndex() == checkStar->getIndex()) )
{
return false;
}
}
}
}
return true;
}
bool PopStar::isGameOver()
{
int gameLevel = getPopStarDataMgr().getLevel();
int needScore = getPopStarDataMgr().getTargetScoreByLevel(gameLevel);
int curScore = getPopStarDataMgr().getScore();
return (curScore < needScore);
}
void PopStar::changeState(PopStarState* newState)
{
if (!currentState || !newState)
{
return;
}
// 调用现有状态的退出方法
currentState->exit();
// 改变状态到新状态
currentState = newState;
// 调用新状态的进入方法
currentState->enter();
}
bool PopStar::isInitFinish()
{
Star* star = stars[ROW_NUM-1][COL_NUM-1];
if (star)
{
CCPoint pos = star->getPosition();
CCPoint destPos = star->getDestPos();
if ( pos.equals(destPos) )
{
return true;
}
}
return false;
}
void PopStar::onReduce()
{
if (!gameLayer)
{
return;
}
int num = selectStars.size();
int score = getPopStarDataMgr().getScore() + getPopStarDataMgr().getScoreByReduceNum(num);
setScore(score);
StarListIter iter;
for (iter = selectStars.begin(); iter != selectStars.end(); ++iter)
{
gameLayer->removeChild(iter->star);
stars[iter->row][iter->col] = NULL;
}
selectStars.clear();
// 竖直调整
for (int row = 0; row < ROW_NUM; ++row)
{
for (int col = 0; col < COL_NUM; ++col)
{
int tempRow = row;
Star* star = stars[row][col];
if (star)
{
continue;
}
else
{
while (!star && (tempRow < ROW_NUM))
{
star = stars[tempRow][col];
if (star)
{
break;
}
++tempRow;
}
if (star)
{
stars[row][col] = stars[tempRow][col];
stars[tempRow][col] = NULL;
}
}
}
}
while (isNeedHoriAdjust())
{
int endCol = getCheckEndCol();
for (int col = 0; col <= endCol; ++col)
{
if (!stars[0][col])
{
for (int i = col; i < endCol; ++i)
{
for (int row = 0; row < ROW_NUM; ++row)
{
stars[row][i] = stars[row][i+1];
}
}
for (int row = 0; row < ROW_NUM; ++row)
{
stars[row][endCol] = NULL;
}
break;
}
}
}
// 刷新位置
for (int row = 0; row < ROW_NUM; ++row)
{
for (int col = 0; col < COL_NUM; ++col)
{
Star* star = stars[row][col];
if (star)
{
star->setDestPos(ccp(STAR_WIDTH*col + STAR_WIDTH/2, STAR_HEIGHT*row + STAR_HEIGHT/2));
}
}
}
if (isLevelEnd())
{
int num = getLeftStarNum();
int score = getPopStarDataMgr().getScore() + getPopStarDataMgr().getScoreByLeftNum(num);
setScore(score);
int historyScore = getPopStarDataMgr().getHistoryScore();
if (score > historyScore)
{
setHistoryScore(score);
}
int level = getPopStarDataMgr().getLevel();
int historyLevelScore = getPopStarDataMgr().getHistoryLevelScoreByLevel(level);
if (score > historyLevelScore)
{
setHistoryLevelScore(score);
}
if (isGameOver()) // 游戏结束
{
changeState( new GameOverState(this) );
}
else // 下一等级
{
changeState( new GamePassLevelState(this) );
}
}
}
void PopStar::onSelect(const SelectStar& selectStar)
{
clearSelectStars();
std::map<int, Star*> checkList;
checkList.insert(std::make_pair(selectStar.row*REVERSE_NUM+selectStar.col, selectStar.star));
int index = selectStar.star->getIndex();
while (!checkList.empty())
{
std::map<int, Star*>::iterator iter;
for (iter = checkList.begin(); iter != checkList.end(); )
{
int row = iter->first/REVERSE_NUM;
int col = iter->first%REVERSE_NUM;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
cocos2dx 消灭星星源码-popStar cocos精品小游戏源码 , 完整项目源码 是整个项目的压缩包 , 新建空项目直接导入到项目里面就可以运行了,无需其他操作。 适合二次开发和学习使用,都是经典游戏,需要其他游戏源码包可以看我主页。 直接可以运行 , 如果运行不了 请切换编译器版本 , 经过测试是没问题的 如果打不开请切换编译器版本。 有别的资源或代码需求可以通过我的主页联系 谢谢! cocos游戏代码 ,cocos经典游戏源码 , cocos休闲游戏源码, cocos容易上手的游戏源码,cocos小游戏源码,cocos新手练习源码,cocos个人开发源码,cocos优秀项目。
资源推荐
资源详情
资源评论
收起资源包目录
cocos2dx 消灭星星源码-popStar (105个子文件)
all-wcprops 2KB
all-wcprops 720B
all-wcprops 630B
all-wcprops 331B
all-wcprops 312B
all-wcprops 89B
PopStar.cpp 11KB
Star.cpp 5KB
PopStarDataMgr.cpp 4KB
PopStarLayer.cpp 4KB
PopStarState.cpp 2KB
GameLevel.cpp 2KB
StartLayer.cpp 1KB
AppDelegate.cpp 1KB
GameScene.cpp 594B
main.cpp 591B
Thumbs.db 15KB
Thumbs.db 15KB
Thumbs.db 13KB
entries 3KB
entries 1KB
entries 1KB
entries 645B
entries 526B
entries 202B
popStar.vcxproj.filters 2KB
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
PopStar.h 1KB
PopStarState.h 1KB
PopStarLayer.h 1KB
AppDelegate.h 955B
PopStarDataMgr.h 882B
Star.h 603B
GameLevel.h 571B
StartLayer.h 468B
main.h 267B
GameScene.h 209B
HelloWorld.png 136KB
popstar_start.png 107KB
popstar_bg.png 8KB
CloseNormal.png 6KB
CloseSelected.png 5KB
00010.png 5KB
green.png 5KB
yellow.png 5KB
purple.png 5KB
red.png 5KB
blue.png 5KB
popStar.sln 4KB
arial.ttf.svn-base 760KB
HelloWorld.png.svn-base 136KB
Marker Felt.ttf.svn-base 25KB
Thumbs.db.svn-base 15KB
PopStar.cpp.svn-base 11KB
Thumbs.db.svn-base 8KB
popstar_bg.png.svn-base 8KB
Thumbs.db.svn-base 7KB
CloseNormal.png.svn-base 6KB
CloseSelected.png.svn-base 5KB
00010.png.svn-base 5KB
green.png.svn-base 5KB
yellow.png.svn-base 5KB
purple.png.svn-base 5KB
red.png.svn-base 5KB
blue.png.svn-base 5KB
Star.cpp.svn-base 5KB
PopStarDataMgr.cpp.svn-base 4KB
PopStarLayer.cpp.svn-base 3KB
PopStarState.cpp.svn-base 2KB
GameLevel.cpp.svn-base 2KB
StartLayer.cpp.svn-base 2KB
PopStar.h.svn-base 1KB
AppDelegate.cpp.svn-base 1KB
PopStarState.h.svn-base 1KB
PopStarLayer.h.svn-base 1KB
AppDelegate.h.svn-base 955B
PopStarDataMgr.h.svn-base 882B
Star.h.svn-base 603B
GameScene.cpp.svn-base 594B
GameLevel.h.svn-base 571B
StartLayer.h.svn-base 468B
GameScene.h.svn-base 209B
00010.png.svn-base 53B
Thumbs.db.svn-base 53B
arial.ttf.svn-base 53B
Marker Felt.ttf.svn-base 53B
purple.png.svn-base 53B
yellow.png.svn-base 53B
green.png.svn-base 53B
red.png.svn-base 53B
blue.png.svn-base 53B
Thumbs.db.svn-base 53B
popstar_bg.png.svn-base 53B
CloseNormal.png.svn-base 53B
Thumbs.db.svn-base 53B
共 105 条
- 1
- 2
资源评论
若风华浊世间
- 粉丝: 72
- 资源: 164
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功