#include "GameBaseScene.h"
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);
addPlayerAnimation();
addPlayer();
addGoButton();
return true;
}
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()
{
//Sprite* goButton = Sprite::create(GO_BUTTON);
//goButton->setAnchorPoint(ccp(0,0));
//goButton->setPosition(ccp(tableStartPosition_x+2*tableWidth,tableStartPosition_y-tableHeight*5));
Menu* menu = Menu::create();
menu->setPosition(CCPointZero);
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");
RouteNavigation::getInstance()->getPath(player1,6,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]);
}
colVector.clear();
rowVector.clear();
std::vector<int>(colVector).swap(colVector);
std::vector<int>(rowVector).swap(rowVector);
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()
{
Sprite* player_me = Sprite::create(PLAYER_ME);
player_me->setPosition(tableStartPosition_x+tableWidth/2,tableStartPosition_y-tableHeight);
addChild(player_me);
LabelTTF *player_me_money = LabelTTF::create();
player_me_money->setString("$");
player_me_money->setAnchorPoint(ccp(0,0.5));
player_me_money->setFontSize(25);
player_me_money->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2);
addChild(player_me_money);
LabelTTF *player_me_strength = LabelTTF::create();
player_me_strength->setString("+");
player_me_strength->setAnchorPoint(ccp(0,0.5));
player_me_strength->setFontSize(28);
player_me_strength->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2*3);
addChild(player_me_strength);
Sprite* player_enemy1 = Sprite::create(PLAYER_ENEMY1);
player_enemy1->setPosition(tableStartPosition_x+tableWidth/2,tableStartPosition_y-3*tableHeight);
addChild(player_enemy1);
LabelTTF *player_enemy1_money = LabelTTF::create();
player_enemy1_money->setString("$");
player_enemy1_money->setAnchorPoint(ccp(0,0.5));
player_enemy1_money->setFontSize(25);
player_enemy1_money->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2*5);
addChild(player_enemy1_money);
LabelTTF *player_enemy1_strength = LabelTTF::create();
player_enemy1_strength->setString("+");
player_enemy1_strength->setAnchorPoint(ccp(0,0.5));
player_enemy1_strength->setFontSize(28);
player_enemy1_strength->setPosition(tableStartPosition_x+tableWidth,tableStartPosition_y-tableHeight/2*7);
addChild(player_enemy1_strength);
struct timeval now;
gettimeofday(&now, NULL);
unsigned rand_seed = (unsigned)(now.tv_sec*1000 + now.tv_usec/1000); //都转化为毫秒
srand(rand_seed);
SpriteFrame* spf1 = player1_spriteFrameCache->getSpriteFrameByName("player1_anim_01.png");
player1 = RicherPlayer::create("player1",spf1,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);
addChild(player1);
SpriteFrame* spf2 =player2_spriteFrameCache->getSpriteFrameByName("player2_anim_01.png");
player2 = RicherPlayer::create("player2",spf2,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);
addChild(player2);
//player1->runAction(RepeatForever::create(player1_animate_left));
}
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,tableStartPosition_y-2*i*tableHeight), 1,
Color4F(0, 1, 0, 1));
draw->drawSegment(Vec2(tableStartPosition_x,tableStartPosition_y - 2*(i+1)*tableHeight),
Vec2(tableStartPosition_x+ 3*tableWidth,tableStartPosition_y - 2*(i+1)*tableHeight), 1,
Color4F(0, 1, 0, 1));
draw->drawSegment(Vec2(tableStartPosition_x+ tableWidth,tableStartPosition_y - tableHeight-2*i*tableHeight),
Vec2(tableStartPosition_x+ 3*tableWidth,tableStartPosition_y - tableHeight-2*i*tableHeight), 1,
Color4F(0, 1, 0, 1));
draw->drawSegment(Vec2(tableStartPosition_x+ tableWidth,tableStartPosition_y -2*i*tableHeight),
Vec2(tableStartPosition_x+ tableWidth,tableStartPosition_y -2* tableHeight-2*i*tableHeight), 1,
Color4F(0, 1, 0, 1));
}
}
void GameBaseScene::addPlayerAnimation()
{
player1_spriteFrameCache = SpriteFrameCache::getInstance();
player1_spriteFrameCache->addSpriteFramesWithFile("map/player1_anim.plist","map/player1_anim.png");
player2_spriteFrameCache = SpriteFrameCache::getInstance();
player2_spriteFrameCache->addSpriteFramesWithFile("map/player2_anim.plist","map/player2_anim.png");
Vector<SpriteFrame*> player1_anim_left_vector ;
Vector<SpriteFrame*> player1_anim_right_vector;
Vector<SpriteFrame*> player1_anim_down_vector;
Vector<SpriteFrame*> player1_anim_up_vector;
Vector<SpriteFrame*> player2_anim_left_vector;
Vector<SpriteFrame*> player2_anim_right_vector;
Vector<SpriteFrame*> player2_anim_down_vector;
Vector<SpriteFrame*> player2_anim_up_vector;
char name[20];
memset(name, 0, 20);
for (int i=1; i<=4; i++)
{
sprintf(name, "player1_anim_%02d.png",i);
player1_anim_left_vector.pushBack(player1_spriteFrameCache->getSpriteFrameByName(name));
sprintf(name, "player2_anim_%02d.p
没有合适的资源?快使用搜索试试~ 我知道了~
Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3
共49个文件
png:26个
h:10个
cpp:9个
需积分: 9 56 下载量 23 浏览量
2014-12-23
19:09:29
上传
评论
收藏 2.08MB RAR 举报
温馨提示
Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3
资源推荐
资源详情
资源评论
收起资源包目录
richer1221_path.rar (49个子文件)
richer1221_path
Resources
menuLogo.png 135KB
map
sea.png 559KB
player2.png 7KB
beach_item.png 195KB
player2_anim.plist 8KB
go_press.png 11KB
player1_anim.png 44KB
go_normal.png 10KB
stage_background2.png 579KB
sea_item.png 235KB
right_banner.png 53KB
moon_item.png 130KB
player1_anim.plist 8KB
player1.png 7KB
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 691B
MenuScene.h 1KB
GameBaseScene.h 1KB
AppDelegate.cpp 1KB
AppDelegate.h 947B
SeaScene.cpp 784B
MapChooseScene.h 1KB
PopupLayer
PopupLayer.h 1KB
PopupLayer.cpp 5KB
SplashScene.h 697B
GameBaseScene.cpp 10KB
RicherPlayer.h 628B
RicherPlayer.cpp 615B
MenuScene.cpp 7KB
MapChooseScene.cpp 4KB
RouteNavigation.cpp 4KB
SeaScene.h 361B
ConstUtil.h 1KB
共 49 条
- 1
资源评论
lideguo1979
- 粉丝: 147
- 资源: 77
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功