#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,3,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]);
}
player1->startGo(rowVector,colVector);
/*
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->setAnimate_left(Animate::create( Animation::createWithSpriteFrames(player1_anim_left_vector,0.1f)));
player1->setAnimate_right(Animate::create(Animation::createWithSpriteFrames(player1_anim_right_vector,0.1f)));
player1->setAnimate_down(Animate::create(Animation::createWithSpriteFrames(player1_anim_down_vector,0.1f)));
player1->setAnimate_up(Animate::create(Animation::createWithSpriteFrames(player1_anim_up_vector,0.1f)));
player2->setAnimate_left(Animate::create(Animation::createWithSpriteFrames(player2_anim_left_vector,0.1f)));
player2->setAnimate_right(Animate::create(Animation::createWithSpriteFrames(player2_anim_right_vector,0.1f)));
player2->setAnimate_down(Animate::create(Animation::createWithSpriteFrames(player2_anim_down_vector,0.1f)));
player2->setAnimate_up(Animate::create(Animation::createWithSpriteFrames(player2_anim_up_vector,0.1f)));
//player1->runAction(RepeatForever::create(player1_animate_left));
*/
player1->setAnim_left_vector(player1_anim_left_vector);
player1->setAnim_right_vector(player1_anim_right_vector);
player1->setAnim_down_vector(player1_anim_down_vector);
player1->setAnim_up_vector(player1_anim_up_vector);
}
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