Beispiel #1
0
void GameLayer::addMapBg()
{
    float width = _visibleSize.width;
    DrawNode *mapBg = DrawNode::create();
    mapBg->drawSolidRect( Vec2::ZERO , Vec2(width, width), Color4F(Color3B(190,173,158)));
    mapBg->setAnchorPoint(Vec2::ZERO);
    this->addChild(mapBg);
    
    Vec2 mapOrigin = mapBg->getPosition();
    float gap = width/33;
    float tileWidth = (width-5*gap)/4;
    
    g_tileSize = tileWidth;
    g_mapBg = mapBg;
    g_gap = gap;
    
    for(int i = 0; i < MAP_SIZE; i++)
    {
        for(int j = 0; j < MAP_SIZE; j++)
        {
            DrawNode* tileBg = DrawNode::create();
            tileBg->drawSolidRect(Vec2::ZERO, Vec2(tileWidth, tileWidth) , Color4F( Color3B(208,193,179) ));
            tileBg->setPositionX((j+1)*gap+ j*tileWidth);
            tileBg->setPositionY((i+1)*gap+ i*tileWidth);
            _map->setTilePos(3-i, j, tileBg->getPosition() + Vec2(g_tileSize/2 , g_tileSize/2));
            mapBg->addChild(tileBg);
        }
    }
    
    _map->initStart2();
}
Beispiel #2
0
void GameLayer::addScore()
{
    // label2048
    Label *_2048 = Label::createWithTTF("2048", "fonts/ClearSans-Bold.ttf", _visibleSize.width/5.9);
    _2048->setColor(Color3B(121,110,100) );
    _2048->setAnchorPoint(Vec2( 0, 0.5 ));
    _2048->setPosition(Vec2( 0, _visibleSize.height-_2048->getContentSize().height/2) );
    this->addChild(_2048);
    
    // best score
    DrawNode *bestscoreback = DrawNode::create();
    bestscoreback->drawSolidRect( Vec2(-_visibleSize.width/6 ,-g_tileSize/3), Vec2(_visibleSize.width/6 ,g_tileSize/3), Color4F(Color3B(190,173,158)) );
    bestscoreback->setPosition( Vec2(_visibleSize.width - _visibleSize.width/6-10 , _2048->getPositionY()-10 ) );
    this->addChild(bestscoreback);
    Label *best = Label::createWithTTF("BEST", "fonts/ClearSans-Bold.ttf", _visibleSize.width/25);
    best->setColor(Color3B(240,228,216));
    best->setPositionY(g_tileSize/6.5 );
    bestscoreback->addChild(best);
    
    sprintf(buff, "%d", Score::getInstance()->getTopScore());
    bestscore = Label::createWithTTF(buff, "fonts/ClearSans-Bold.ttf", _visibleSize.width/15);
    bestscore->setPositionY( -g_tileSize/7.5);
    bestscoreback->addChild(bestscore);
    
    //score
    DrawNode *scoreback = DrawNode::create();
    scoreback->drawSolidRect( Vec2(-_visibleSize.width/6 ,-g_tileSize/3), Vec2(_visibleSize.width/6 ,g_tileSize/3), Color4F(Color3B(190,173,158)) );
    scoreback->setPosition( Vec2(_visibleSize.width - _visibleSize.width/6-10 , _2048->getPositionY() - g_tileSize/1 ) );
    this->addChild(scoreback);
    
    Label *sco = Label::createWithTTF("SCORE", "fonts/ClearSans-Bold.ttf", _visibleSize.width/25);
    sco->setColor(Color3B(240,228,216));
    sco->setPositionY(g_tileSize/6.5 );
    scoreback->addChild(sco);
    
    sprintf(buff, "%d", 0);
    score = Label::createWithTTF(buff, "fonts/ClearSans-Bold.ttf", _visibleSize.width/15);
    score->setPositionY( -g_tileSize/7.5);
    scoreback->addChild(score);
    
    // new game
    DrawNode *newGame = DrawNode::create();
    newGame->drawSolidRect( Vec2(-_visibleSize.width/6 ,-g_tileSize/4), Vec2(_visibleSize.width/6 ,g_tileSize/4), Color4F(Color3B(147,122,98)) );
    newGame->setPosition( Vec2(_visibleSize.width/5.5 , _2048->getPositionY() - g_tileSize/1 ) );
    this->addChild(newGame);
    
    Label *ngame = Label::createWithTTF("New Game", "fonts/ClearSans-Bold.ttf",  _visibleSize.width/17);
    ngame->setColor(Color3B(250,246,241));
    //newGame->addChild(ngame);
    
    // ai
    DrawNode *aiback = DrawNode::create();
    aiback->drawSolidRect( Vec2(-_visibleSize.width/15 ,-g_tileSize/4), Vec2(_visibleSize.width/15 ,g_tileSize/4), Color4F(Color3B(147,122,98)) );
    aiback->setPosition( Vec2(_visibleSize.width/2.3 , _2048->getPositionY() - g_tileSize/1 ) );
    this->addChild(aiback);
    
    Label *ai = Label::createWithTTF("AI", "fonts/ClearSans-Bold.ttf",  _visibleSize.width/17);
    ai->setColor(Color3B(250,246,241));
    //aiback->addChild(ai);
    
    MenuItemLabel *newgamelabel = MenuItemLabel::create(ngame, CC_CALLBACK_1(GameLayer::startNewGame, this));
    newgamelabel->setPosition(newGame->getPosition());
    
    ailable = MenuItemLabel::create(ai, CC_CALLBACK_1(GameLayer::startAi, this));
    ailable->setPosition(aiback->getPosition());
    
    Menu *menu = Menu::create(newgamelabel,ailable, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu);
}
///all screen move functions should use this
void HelloWorld::lookAt(Vec2 pos)
{
    Vec2 visible = Director::getInstance()->getVisibleSize();
    this->setPosition(-1*(pos - visible/2));
    protractor->setPosition(lookingAt());
    notifier->setPosition(lookingAt());

    //draw grid
    Vec2 botleft = screenspaceToWorldspace(Vec2::ZERO);
    Vec2 topright = screenspaceToWorldspace(visible);
    Vec2 drawSpaceBotLeft = botleft - GRID_LABEL_SPACING;
    Vec2 drawSpaceTopRight = topright + GRID_LABEL_SPACING;
	DrawNode* gridSprite = (DrawNode*)getChildByName("gridSprite");
    if (gridSprite)
    {
        gridSprite->clear();
        std::vector<std::string> toRemove;
        for (auto child : gridSprite->getChildren())
        {
            if (!onScreen(child->getPosition(), Vec2(GRID_LABEL_SPACING)))
                toRemove.push_back(child->getName());
        }
        for (auto name : toRemove)
        {
            gridSprite->removeChildByName(name);
        }
    }
    else
    {
        gridSprite = DrawNode::create();
        gridSprite->setName("gridSprite");
        addChild(gridSprite, 0);
    }
    //vertical lines
    for (int i= drawSpaceBotLeft.x; i<drawSpaceTopRight.x; i++)
    {
        if(i % (int)GRID_SPACING.x)
            continue;
        gridSprite->drawSegment(Vec2(i, drawSpaceBotLeft.y), Vec2(i, drawSpaceBotLeft.y+visible.y+2*GRID_LABEL_SPACING.y),
                                GRID_LINE_THICKNESS, Color4F(1,1,1,0.1));
        if(!(i % (int)GRID_LABEL_SPACING.x))
        {
            std::string txt = std::to_string(i);
            auto label = gridSprite->getChildByName("v" + txt);
            if (!label)
            {
                label = Label::createWithTTF(txt, "fonts/arial.ttf", GRID_LABEL_SIZE);
                label->setName("v" + txt);
                gridSprite->addChild(label);
            }
            label->setPosition(Vec2(i, topright.y - GRID_LABEL_SIZE * 2));
        }
    }
    //horizontal lines
    for (int i= drawSpaceBotLeft.y; i<drawSpaceTopRight.y; i++)
    {
        if(i % (int)GRID_SPACING.y)
            continue;
        gridSprite->drawSegment(Vec2(drawSpaceBotLeft.x, i), Vec2(drawSpaceBotLeft.x+visible.x + 2 * GRID_LABEL_SPACING.x, i),
                                GRID_LINE_THICKNESS, Color4F(1,1,1,0.1));   
        if(!(i % (int)GRID_LABEL_SPACING.y))
        {
            std::string txt = std::to_string(i);
            auto label = gridSprite->getChildByName("h" + txt);
            if (!label)
            {
                label = Label::createWithTTF(txt, "fonts/arial.ttf", GRID_LABEL_SIZE);
                label->setName("h" + txt);
                gridSprite->addChild(label);
            }
            label->setPosition(Vec2(botleft.x + GRID_LABEL_SIZE * 2, i));
        }
    }
    repaintCursor();
    Commorose* c = (Commorose*)commorose;
    c->setPosition(screenspaceToWorldspace(c->getPositionOnScreen()));
}