#include "GameBaseScene.h"
int GameBaseScene::tiledColsCount;
int GameBaseScene::tiledRowsCount;
bool** GameBaseScene::canPassGrid;
Vector<RicherPlayer*> GameBaseScene::players_vector;
Scene* GameBaseScene::createScene()
{
auto scene = Scene::create();
auto layer = GameBaseScene::create();
scene->addChild(layer);
return scene;
}
bool GameBaseScene::init()
{
if ( !Layer::init() )
{
return false;
}
addMap();
initTiledGrid();
setWayPassToGrid();
addRightBanner();
drawTable(2);
addPlayer();
addGoButton();
addNotificationObserver();
return true;
}
void GameBaseScene::addNotificationObserver()
{
NotificationCenter::getInstance()->addObserver(
this,
callfuncO_selector(GameBaseScene::receivedMsgForGo),
MSG_GO,
NULL);
}
void GameBaseScene::receivedMsgForGo(Object* data)
{
int retMsgType =((String*)data)->intValue();
Vector<Node*> vecMenuItem = getMenu()->getChildren();
Size winSize = Director::getInstance()->getWinSize();
if(retMsgType ==1)
{
for(auto it=vecMenuItem.begin();it!=vecMenuItem.end();it++)
{
Node* node = dynamic_cast<Node*>(*it);
MoveBy* moveBy = MoveBy::create(0.3,ccp(-(node->getContentSize().width)*2,0));
//RotateBy* rotateBy =RotateBy::create(1,360,10);
//Action* action =Spawn::create(moveBy,rotateBy,NULL);
node->runAction(moveBy);
}
}else
{
for(auto it=vecMenuItem.begin();it!=vecMenuItem.end();it++)
{
Node* node = dynamic_cast<Node*>(*it);
MoveBy* moveBy = MoveBy::create(0.3,ccp((node->getContentSize().width)*2,0));
//RotateBy* rotateBy =RotateBy::create(1,360,10);
//Action* action =Spawn::create(moveBy,rotateBy,NULL);
node->runAction(moveBy);
}
}
log("received go message is: %d",retMsgType);
}
void GameBaseScene::setWayPassToGrid()
{
TMXLayer* wayLayer = _map->layerNamed("way");
Size _mapSize = wayLayer->getLayerSize();
for (int j = 0; j < _mapSize.width; j++) {
for (int i = 0; i < _mapSize.height; i++) {
Sprite* _sp = wayLayer->tileAt(Point(j, i));
if (_sp)
{
float x = _sp->getPositionX();
float y = _sp->getPositionY();
int col = x/tiledWidth;
int row = y/tiledHeight;
canPassGrid[row][col] = true;
Vec2 p = _sp->getPosition();
wayLayerPass_vector.push_back(p);
log("canPassGrid row= %d ,col =%d ,canpass = %d" ,row,col,canPassGrid[row][col]);
}
}
}
log("setWayPassToGrid finished");
}
void GameBaseScene::addGoButton()
{
Menu* menu = Menu::create();
menu->setPosition(CCPointZero);
setMenu(menu);
MenuItemImage* goMenuItemButton = MenuItemImage::create("map/go_normal.png", "map/go_press.png", this, menu_selector(GameBaseScene::goButtonCallback));
goMenuItemButton->setPosition(ccp(tableStartPosition_x+2*tableWidth,tableStartPosition_y-tableHeight*6));
menu->addChild(goMenuItemButton);
addChild(menu);
}
void GameBaseScene::goButtonCallback(cocos2d::CCObject *pSender)
{
log("go button clicked");
int randNumber = rand()%6 + 1;
RouteNavigation::getInstance()->getPath(player1,randNumber,canPassGrid,tiledRowsCount,tiledColsCount);
std::vector<int> colVector = RouteNavigation::getInstance()->getPathCols_vector();
std::vector<int> rowVector = RouteNavigation::getInstance()->getPathRow_vector();
for(int i=0;i<rowVector.size();i++)
{
log(" rowVector row is %d --- colVector col is %d",rowVector[i],colVector[i]);
}
NotificationCenter::getInstance()->postNotification(MSG_GO,String::create("0"));
player1->startGo(rowVector,colVector);
log("go button clicked over");
}
void GameBaseScene::addRightBanner()
{
Sprite* rightBanner = Sprite::create(RIGHT_BANNER);
rightBanner->setAnchorPoint(ccp(0,0));
rightBanner->setPosition( ccp(tableStartPosition_x-10, 0));
addChild(rightBanner);
}
void GameBaseScene:: addPlayer()
{
addPlayerInfo();
struct timeval now;
gettimeofday(&now, NULL);
unsigned rand_seed = (unsigned)(now.tv_sec*1000 + now.tv_usec/1000); //都转化为毫秒
srand(rand_seed);
player1 = RicherPlayer::create(PLAYER_1_NAME,PLAYER_1_TAG,false);
int _rand1 = rand()%(wayLayerPass_vector.size());
log("rand %d" ,_rand1);
Vec2 vec2ForPlayer1 = wayLayerPass_vector.at(_rand1);
vec2ForPlayer1.y +=tiledHeight;
player1->setPosition(vec2ForPlayer1);
player1->setAnchorPoint(ccp(0,0.5));
int col = vec2ForPlayer1.x/tiledWidth;
int row = vec2ForPlayer1.y/tiledHeight;
log("player1 position row= %d ,col = %d" ,row,col);
log("player1 position x= %f ,y = %f" , vec2ForPlayer1.x, vec2ForPlayer1.y);
char money1[20];
memset(money1, 0, 20);
sprintf(money1, "$ %d",player1->getMoney());
getPlayer1_money_label()->setString(money1);
char strength1[20];
memset(strength1, 0, 20);
sprintf(strength1, "+ %d",player1->getStrength());
getPlayer1_strength_label()->setString(strength1);
addChild(player1);
players_vector.pushBack(player1);
player2 = RicherPlayer::create(PLAYER_2_NAME,PLAYER_2_TAG,true);
int _rand2 = rand()%(wayLayerPass_vector.size());
log("rand %d" ,_rand2);
Vec2 vec2ForPlayer2 = wayLayerPass_vector.at(_rand2);
vec2ForPlayer2.y +=tiledHeight;
player2->setPosition(vec2ForPlayer2);
player2->setAnchorPoint(ccp(0,0.5));
int col2 = vec2ForPlayer2.x/tiledWidth;
int row2 = vec2ForPlayer2.y/tiledHeight;
log("player1 position row= %d ,col = %d" ,row2,col2);
log("player1 position x= %f ,y = %f" , vec2ForPlayer2.x, vec2ForPlayer2.y);
char money2[20];
memset(money2, 0, 20);
sprintf(money2, "$ %d",player1->getMoney());
getPlayer2_money_label()->setString(money2);
char strength2[20];
memset(strength2, 0, 20);
sprintf(strength2, "+ %d",player1->getStrength());
getPlayer2_strength_label()->setString(strength2);
addChild(player2);
players_vector.pushBack(player2);
}
void GameBaseScene:: addPlayerInfo()
{
Sprite* player1_logo = Sprite::create(PLAYER_ME);
player1_logo->setPosition(tableStartPosition_x+tableWidth/2,tableStartPosition_y-tableHeight);
addChild(player1_logo);
player1_money_label = LabelTTF::create();
player1_money_label->setString("$");
player1_money_label->setAnchorPoint(ccp(0,0.5));
player1_money_label->setFontSize(FONT_SIZE);
player1_money_label->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2);
addChild(player1_money_label);
player1_strength_label = LabelTTF::create();
player1_strength_label->setString("+");
player1_strength_label->setAnchorPoint(ccp(0,0.5));
player1_strength_label->setFontSize(FONT_SIZE);
player1_strength_label->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2*3);
addChild(player1_strength_label);
Sprite* player2_logo = Sprite::create(PLAYER_ENEMY1);
player2_logo->setPosition(tableStartPosition_x+tableWidth/2,tableStartPosition_y-3*tableHeight);
addChild(player2_logo);
player2_money_label = LabelTTF::create();
player2_money_label->setString("$");
player2_money_label->setAnchorPoint(ccp(0,0.5));
player2_money_label->setFontSize(FONT_SIZE);
player2_money_label->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2*5);
addChild(player2_money_label);
player2_strength_label = LabelTTF::create();
player2_strength_label->setString("+");
player2_strength_label->setAnchorPoint(ccp(0,0.5));
player2_strength_label->setFontSize(FONT_SIZE);
player2_strength_label->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2*7);
addChild(player2_strength_label);
}
void GameBaseScene:: drawTable(int playerNumber)
{
auto s = Director::getInstance()->getWinSize();
auto draw = DrawNode::create();
this->addChild(draw);
for(int i=0;i<playerNumber;i++)
{
draw->drawSegment(Vec2(tableStartPosition_x,tableStartPosition_y-2*i*tableHeight),
Vec2(tableStartPosition_x+ 3*tableWidth,
没有合适的资源?快使用搜索试试~ 我知道了~
Cocos2d-x 3.2 大富翁游戏项目开发-第十部分实现人物轮流行走
共55个文件
png:30个
h:11个
cpp:10个
需积分: 15 75 下载量 143 浏览量
2014-12-25
23:50:16
上传
评论
收藏 2.1MB RAR 举报
温馨提示
Cocos2d-x 3.2 大富翁游戏项目开发-第十部分实现人物轮流行走
资源推荐
资源详情
资源评论
收起资源包目录
richer1225_第十部分.rar (55个子文件)
richer1224_第十部分
Resources
menuLogo.png 135KB
map
sea.png 559KB
player2.png 7KB
beach_item.png 195KB
player2_anim.plist 8KB
go_press.png 11KB
strength.png 1KB
player1_anim.png 44KB
go_normal.png 10KB
stage_background2.png 579KB
sea_item.png 235KB
right_banner12.png 832B
right_banner.png 53KB
moon_item.png 130KB
player1_anim.plist 8KB
money.png 1KB
player1.png 7KB
go.png 8KB
sea.tmx 2KB
player2_anim.png 40KB
popuplayer
button_bg1.png 546B
dialog_bg.png 5KB
button_bg2.png 554B
button_bg3.png 543B
press_menu.png 6KB
fonts
Marker Felt.ttf 25KB
mai.png 9KB
normal_menu.9.png 673B
ke.png 9KB
cheng.png 8KB
normal_menu.png 6KB
ji.png 9KB
rainbow.png 37KB
press_menu.9.png 523B
Classes
SplashScene.cpp 3KB
RouteNavigation.h 718B
MenuScene.h 1KB
GameBaseScene.h 2KB
AppDelegate.cpp 1KB
AppDelegate.h 947B
SeaScene.cpp 784B
MapChooseScene.h 1KB
PopupLayer
PopupLayer.h 1KB
PopupLayer.cpp 5KB
SplashScene.h 697B
GameBaseScene.cpp 9KB
RicherPlayer.h 2KB
RicherGameController.cpp 3KB
RicherPlayer.cpp 4KB
MenuScene.cpp 7KB
RicherGameController.h 1KB
MapChooseScene.cpp 4KB
RouteNavigation.cpp 4KB
SeaScene.h 361B
ConstUtil.h 2KB
共 55 条
- 1
资源评论
lideguo1979
- 粉丝: 148
- 资源: 77
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功