Example #1
0
// on "init" you need to initialize your instance
bool GameScene::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

    // Variables de tamanyo
    CCSize vs = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    // Layer con los botones al hacer pause
    _layerPause = CCLayerColor::create(ccc4(255,255,255,0));
    _layerPause->setContentSize(CCSizeMake(350, 100));
    _layerPause->ignoreAnchorPointForPosition(false);
    _layerPause->setAnchorPoint(ccp(0.5, 0.5));
    _layerPause->setPosition(ccp(vs.width/2, vs.height/2));
    _layerPause->retain();

    CCMenuItem* button_resume = CCMenuItemImage::create("btn_resume.png", "btn_resume_h.png", this, menu_selector(GameScene::menuResumeCallback));
    button_resume->setPosition(ccp(50, 50));
    CCMenuItem* button_restart = CCMenuItemImage::create("btn_restart.png", "btn_restart_h.png", this, menu_selector(GameScene::menuRestartCallback));
    button_restart->setPosition(ccp(175, 50));
    CCMenuItem* button_exit = CCMenuItemImage::create("btn_exit.png", "btn_exit_h.png", this, menu_selector(GameScene::menuExitCallback));
    button_exit->setPosition(ccp(300, 50));

    CCMenu* menu_pause = CCMenu::create(button_resume, button_restart, button_exit, NULL);
    menu_pause->setPosition(ccp(0,0));

    _layerPause->addChild(menu_pause);

    // Layer que se muestra al terminar la carrera
    _layerEnd = CCLayerColor::create(ccc4(0,0,0,0));
    _layerEnd->setContentSize(CCSizeMake(225, 225));
    _layerEnd->ignoreAnchorPointForPosition(false);
    _layerEnd->setAnchorPoint(ccp(0.5, 0.5));
    _layerEnd->setPosition(ccp(vs.width/2, vs.height/2));
    _layerEnd->retain();

    CCMenuItem* button_end_restart = CCMenuItemImage::create("btn_restart.png", "btn_restart_h.png", this, menu_selector(GameScene::menuRestartCallback));
    button_end_restart->setPosition(ccp(50, 50));
    CCMenuItem* button_end_exit = CCMenuItemImage::create("btn_exit.png", "btn_exit_h.png", this, menu_selector(GameScene::menuExitCallback));
    button_end_exit->setPosition(ccp(175, 50));

    CCMenu* menu_end = CCMenu::create(button_end_restart, button_end_exit, NULL);
    menu_end->setPosition(ccp(0,0));

    CCLayerColor* layerTime = CCLayerColor::create(ccc4(255,255,255,255));
    layerTime->setContentSize(CCSizeMake(225, 100));
    layerTime->ignoreAnchorPointForPosition(false);
    layerTime->setAnchorPoint(ccp(0.5, 0.5));
    layerTime->setPosition(ccp(112.5, 175));
    layerTime->retain();

    _labelTimeEnd = CCLabelTTF::create("Time:\n0:00:000", "fonts/FrancoisOne.ttf", 30, CCSizeMake(225, 100), kCCTextAlignmentCenter);
    _labelTimeEnd->setColor(ccc3(0,0,0));
    _labelTimeEnd->setPosition(ccp(112.5, 50));
    layerTime->addChild(_labelTimeEnd);

    _layerEnd->addChild(menu_end);
    _layerEnd->addChild(layerTime);


    // Boton de pause (menu)
    CCMenuItem* button_pause = CCMenuItemImage::create("btn_pause.png", "btn_pause_h.png", this, menu_selector(GameScene::menuPauseCallback));
    button_pause->setPosition(ccp(75, vs.height - 75));

    CCMenu* menu = CCMenu::create(button_pause, NULL);
    menu->setPosition(ccp(0,0));

    // Velocidad
    CCString* stringVel = CCString::createWithFormat("%d km/h", (int)_vel);
    _labelVel = CCLabelTTF::create(stringVel->getCString(), "fonts/FrancoisOne.ttf", 30, CCSizeMake(140, 50), kCCTextAlignmentCenter);
    _labelVel->setColor(ccc3(255,255,255));
    _labelVel->setPosition(ccp(vs.width - 72, 50));

    // Distancia
    CCString* stringDist = CCString::createWithFormat("%d m", (int)_dist);
    _labelDist = CCLabelTTF::create(stringDist->getCString(), "fonts/FrancoisOne.ttf", 30, CCSizeMake(140, 50), kCCTextAlignmentCenter);
    _labelDist->setColor(ccc3(255,255,255));
    _labelDist->setPosition(ccp(72, 50));

    // Tiempo
    int hs = _time/3600000;
    int mins = (_time%3600000)/60000;
    int ss = ((_time%3600000)%60000)/1000;
    int ms = ((_time%3600000)%60000)%1000;
    CCString* stringTime = CCString::createWithFormat("%d:%02d:%03d", mins, ss, ms);
    _labelTime = CCLabelTTF::create(stringTime->getCString(), "fonts/FrancoisOne.ttf", 30, CCSizeMake(140, 50), kCCTextAlignmentCenter);
    _labelTime->setColor(ccc3(255,255,255));
    _labelTime->setPosition(ccp(vs.width - 72, vs.height - 50));

    // Click to start
    _labelStart = CCLabelTTF::create("Click to Start", "fonts/FrancoisOne.ttf", 50, CCSizeMake(200, 200), kCCTextAlignmentCenter);
    _labelStart->setColor(ccc3(255,255,255));
    _labelStart->setPosition(ccp(vs.width/2, vs.height/2));
    _labelStart->retain();

    // Hierba
    ccTexParams tp = { GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
    CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("texture_grass2.jpg");
    texture->setTexParameters(&tp);
    _grass = CCSprite::createWithTexture(texture, CCRectMake(0, 0, vs.width, vs.height + 256));
    _grass->setPosition( ccp( vs.width/2, vs.height/2 + 128 ) );

    // Carretera
    texture = CCTextureCache::sharedTextureCache()->addImage("texture_road2.jpg");
    texture->setTexParameters(&tp);
    _road = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 512, vs.height + 512));
    _road->setPosition( ccp( vs.width/2, vs.height/2 + 256 ) );

    // Pianos
    texture = CCTextureCache::sharedTextureCache()->addImage("piano.png");
    texture->setTexParameters(&tp);
    _piano1 = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 64, vs.height + 64));
    _piano1->setPosition( ccp(vs.width * 0.15, vs.height / 2 + 32) );
    _piano2 = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 64, vs.height + 64));
    _piano2->setPosition( ccp(vs.width * 0.85, vs.height / 2 + 32) );

    // Coche
    _car = CCSprite::create("car_above_blue.png");
    _car->setPosition(ccp(vs.width/2, 50));

    // Creamos los indicadores de curva
    _curvasI = new CCArray;
    _curvasD = new CCArray;

    for (int i = 1; i <= 5; i++) {
        CCString* str_curvaI = CCString::createWithFormat("curva_izq_%d.png", i);
        CCString* str_curvaD = CCString::createWithFormat("curva_der_%d.png", i);
        CCSprite* curvaI = CCSprite::create(str_curvaI->getCString());
        curvaI->setOpacity(0);
        curvaI->setPosition(ccp(vs.width/2, vs.height/4*3));
        CCSprite* curvaD = CCSprite::create(str_curvaD->getCString());
        curvaD->setOpacity(0);
        curvaD->setPosition(ccp(vs.width/2, vs.height/4*3));
        _curvasI->addObject(curvaI);
        _curvasD->addObject(curvaD);
        this->addChild(curvaI, 5);
        this->addChild(curvaD, 5);
    }

    // Anyadimos los elementos a la vista
    this->addChild(_grass, 1);
    this->addChild(_road, 2);
    this->addChild(_car, 3);
    this->addChild(menu, 5);
    this->addChild(_labelVel, 5);
    this->addChild(_labelDist, 5);
    this->addChild(_labelTime, 5);
    this->addChild(_labelStart, 5);


    // Inicializamos el vector de curvas segun el nivel
    if (_level == 1) {
        _curvas.push_back(Curva(250, 200, 2, false));
        _curvas.push_back(Curva(550, 200, 2, false));
        _curvas.push_back(Curva(1200, 100, 4, false));
        _curvas.push_back(Curva(1650, 200, 2, false));
        _curvas.push_back(Curva(1950, 200, 2, false));
        _curvas.push_back(Curva(2500, 200, 2, false));
        _curvas.push_back(Curva(2800, 200, 2, false));
        _mCircuito = 3000;

    } else if (_level == 2) {
        _curvas.push_back(Curva(100, 150, 2, false));
        _curvas.push_back(Curva(350, 150, 2, false));
        _curvas.push_back(Curva(700, 250, 1, false));
        _curvas.push_back(Curva(1150, 150, 3, false));
        _curvas.push_back(Curva(1500, 100, 4, false));
        _curvas.push_back(Curva(1800, 100, 4, false));
        _curvas.push_back(Curva(2000, 200, 2, true));
        _curvas.push_back(Curva(2300, 50, 5, true));
        _curvas.push_back(Curva(2750, 150, 2, true));
        _curvas.push_back(Curva(3000, 50, 4, true));
        _curvas.push_back(Curva(3100, 100, 2, false));
        _curvas.push_back(Curva(3300, 100, 2, false));
        _curvas.push_back(Curva(3500, 100, 4, false));
        _curvas.push_back(Curva(3800, 250, 2, false));
        _curvas.push_back(Curva(4250, 250, 1, false));
        _curvas.push_back(Curva(4700, 150, 3, false));
        _mCircuito = 5350;

    } else { // _level == 3
        _curvas.push_back(Curva(100, 50, 2, true));
        _curvas.push_back(Curva(250, 50, 2, false));
        _curvas.push_back(Curva(400, 100, 4, false));
        _curvas.push_back(Curva(600, 100, 2, false));
        _curvas.push_back(Curva(800, 50, 4, true));
        _curvas.push_back(Curva(950, 50, 4, true));
        _curvas.push_back(Curva(1100, 100, 2, false));
        _curvas.push_back(Curva(1300, 50, 4, true));
        _curvas.push_back(Curva(1450, 50, 4, true));
        _curvas.push_back(Curva(1600, 100, 4, false));
        _curvas.push_back(Curva(1900, 100, 2, false));
        _curvas.push_back(Curva(2100, 50, 5, true));
        _curvas.push_back(Curva(2450, 100, 2, true));
        _curvas.push_back(Curva(2650, 50, 5, false));
        _curvas.push_back(Curva(2900, 100, 4, false));
        _curvas.push_back(Curva(3200, 150, 2, true));
        _curvas.push_back(Curva(3450, 100, 4, true));
        _curvas.push_back(Curva(3650, 50, 4, true));
        _curvas.push_back(Curva(3800, 100, 4, false));
        _curvas.push_back(Curva(4000, 50, 4, true));
        _curvas.push_back(Curva(4150, 150, 2, true));
        _curvas.push_back(Curva(4400, 100, 2, false));
        _curvas.push_back(Curva(4600, 100, 2, false));
        _curvas.push_back(Curva(4800, 100, 5, true));
        _curvas.push_back(Curva(5000, 50, 2, true));
        _curvas.push_back(Curva(5150, 100, 5, false));
        _curvas.push_back(Curva(5450, 200, 5, true));
        _mCircuito = 5850;
    }

    // Inicializamos el vector de coches en contra
    _oppCars = new CCArray;

    // Activamos el acelerometro y el touch
    this->setTouchEnabled(true);

    // Calculamos el tamanyo de cada tramo para informar al Arduino
    int nleds = CCUserDefault::sharedUserDefault()->getIntegerForKey("nleds");
    _longTramo = (int) (_mCircuito/nleds);

    // Enviamos al Arduino que hemos entrado
    int iColor = CCUserDefault::sharedUserDefault()->getIntegerForKey("color");
    CCDictionary* prms = CCDictionary::create();
    prms->setObject(CCString::createWithFormat("%d", iColor), "color");
    SendMessageWithParams(string("JugadorEntra"), prms);

    return true;
}