Esempio n. 1
0
void ColoredCubeApp::drawScene()
{
    int foo[1] = {0};
    switch (gsm->getGameState()) {
    case GameStateManager::START_GAME:
    {
        foo[0] = 1;
        break;
    }
    case GameStateManager::IN_GAME:
    {
        foo[0] = 0;
        break;
    }
    case GameStateManager::END_GAME:
    {
        foo[0] = 1;
        break;
    }
    default:
        foo[0] = 0;
        break;
    }

    mfxBlack_WhiteVar->SetRawValue(&foo[0], 0, sizeof(int));

    D3DApp::drawScene();

    // Restore default states, input layout and primitive topology
    // because mFont->DrawText changes them.  Note that we can
    // restore the default states by passing null.
    md3dDevice->OMSetDepthStencilState(0, 0);
    float blendFactors[] = {0.0f, 0.0f, 0.0f, 0.0f};
    md3dDevice->OMSetBlendState(0, blendFactors, 0xffffffff);
    md3dDevice->IASetInputLayout(mVertexLayout);
    md3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    //Get Camera viewMatrix
    mView = camera.getViewMatrix();
    mProj = camera.getProjectionMatrix();

    // set constants
    mWVP = mView*mProj;
    mfxWVPVar->SetMatrix((float*)&mWVP);

    D3D10_TECHNIQUE_DESC techDesc;
    mTech->GetDesc( &techDesc );
    for(UINT p = 0; p < techDesc.Passes; ++p)
    {
        mTech->GetPassByIndex( p )->Apply(0);

        //mBox.draw();
        //mAxes.draw();
        //mLine.draw();
        //mTriangle.draw();
        mQuad.draw();
        //particleBox.draw();
    }

    mWVP = gameObject1.getWorldMatrix()  *mView*mProj;
    mfxWVPVar->SetMatrix((float*)&mWVP);
    gameObject1.setMTech(mTech);
    gameObject1.draw();

    for (int i = 0; i < MAX_NUM_ENEMIES; i++)
    {
        mWVP = enemyObjects[i].getWorldMatrix()  *mView*mProj;
        mfxWVPVar->SetMatrix((float*)&mWVP);
        enemyObjects[i].setMTech(mTech);
        enemyObjects[i].draw();
    }


    for (int i = 0; i < MAX_NUM_BULLETS; i++)
    {
        mWVP = playerBullets[i].getWorldMatrix()  *mView*mProj;
        mfxWVPVar->SetMatrix((float*)&mWVP);
        playerBullets[i].setMTech(mTech);
        playerBullets[i].draw();
    }

    for (int i = 0; i < MAX_NUM_EXP_PARTICLES; i++)
    {
        mWVP = particles[i].getWorldMatrix()  *mView*mProj;
        mfxWVPVar->SetMatrix((float*)&mWVP);
        particles[i].setMTech(mTech);
        particles[i].draw();
    }

    for (int i = 0; i < MAX_NUM_STARS; i++)
    {
        mWVP = stars[i].getWorldMatrix()  *mView*mProj;
        mfxWVPVar->SetMatrix((float*)&mWVP);
        stars[i].setMTech(mTech);
        stars[i].draw();
    }

    if(gsm->getGameState() == GameStateManager::END_GAME && score > 0) {
        std::wostringstream gameOverString;
        gameOverString.precision(6);
        gameOverString << "YOU WIN!\n";
        gameOverString << "Score: " << score;
        gameOverString << "\nSpacebar to\nplay again.";
        finalScore = gameOverString.str();
        RECT R2 = {GAME_WIDTH/2 - 100, GAME_HEIGHT/2 - 100, 0, 0};
        endFont->DrawText(0, finalScore.c_str(), -1, &R2, DT_NOCLIP, GREEN);
    }
    else if(gsm->getGameState() == GameStateManager::END_GAME) {
        std::wostringstream gameOverString;
        gameOverString.precision(6);
        gameOverString << "YOU DIED!\n";
        gameOverString << "Score: " << score;
        gameOverString << "\nSpacebar to\nplay again.";
        finalScore = gameOverString.str();
        RECT R2 = {GAME_WIDTH/2 - 100, GAME_HEIGHT/2 - 100, 0, 0};
        endFont->DrawText(0, finalScore.c_str(), -1, &R2, DT_NOCLIP, GREEN);
    }
    if(gsm->getGameState() == GameStateManager::START_GAME) {
        std::wostringstream gameOverString;
        gameOverString.precision(6);
        gameOverString << "Controls:\n";
        gameOverString << "Move: A and D.\n";
        gameOverString << "Shoot: Enter \n";
        gameOverString << "Hit the spacebar to begin.";
        finalScore = gameOverString.str();
        RECT R2 = {50, GAME_HEIGHT/2 - 100, 0, 0};
        scoreFont->DrawText(0, finalScore.c_str(), -1, &R2, DT_NOCLIP, GREEN);
    }
    else {
        if (gsm->getGameState() == GameStateManager::IN_GAME) {
            std::wostringstream scoreString;
            scoreString.precision(6);
            scoreString << score;
            finalScore = scoreString.str();
            RECT R2 = {GAME_WIDTH/2 + 50, GAME_HEIGHT + 65, 0, 0};
            scoreFont->DrawText(0, finalScore.c_str(), -1, &R2, DT_NOCLIP, GREEN);
        }
    }


    std::wostringstream ts;
    ts.precision(6);
    ts << secondsRemaining;
    timeString = ts.str();
    RECT R3 = {GAME_WIDTH/2 + 50, 20, 0, 0};
    scoreFont->DrawText(0, timeString.c_str(), -1, &R3, DT_NOCLIP, GREEN);

    // We specify DT_NOCLIP, so we do not care about width/height of the rect.
    RECT R = {5, 5, 0, 0};
    //mFont->DrawText(0, mFrameStats.c_str(), -1, &R, DT_NOCLIP, BLACK);




    mSwapChain->Present(0, 0);
}
Esempio n. 2
0
void ColoredCubeApp::updateScene(float dt)
{
    for (int i = 0; i < MAX_NUM_STARS; i++)
    {
        stars[i].update(dt);
    }

    switch (gsm->getGameState()) {
    case GameStateManager::START_GAME: {
        D3DApp::updateScene(dt);
        gameObject1.update(dt);
        D3DXMATRIX w;

        D3DXMatrixTranslation(&w, 2, 2, 0);
        mfxWVPVar->SetMatrix(w);

        score = 0;

        // Build the view matrix.
        /*D3DXVECTOR3 pos(10.0f, 2.0f, 0.0f);
        D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
        D3DXMatrixLookAtLH(&mView, &pos, &target, &up);*/

        if(GetAsyncKeyState(VK_SPACE) & 0x8000 && gsm->getGameState() != GameStateManager::IN_GAME) {
            gsm->setGameState(GameStateManager::IN_GAME);
            audio->playCue(SELECT);
        }

        Vector3 oldEnemyPositions[MAX_NUM_ENEMIES];
        for (int i = 0; i < MAX_NUM_ENEMIES; i++)
        {
            oldEnemyPositions[i] = enemyObjects[i].getPosition();
        }

        Vector3 oldBulletPositions[MAX_NUM_BULLETS];
        for (int i = 0; i < MAX_NUM_BULLETS; i++)
        {
            oldBulletPositions[i] = playerBullets[i].getPosition();
        }

        //Camera Object
        camera.update(dt);
        break;
    }

    case GameStateManager::IN_GAME:
    {
        //Generate a block every three seconds
        if (enemyBuffer.elapsedTime() > 2) {
            enemyBuffer.resetClock();

            generateEnemy(enemyObjects, dt);
        }

        if (gameTimer.elapsedTime() >= 1) {
            gameTimer.resetClock();
            secondsRemaining--;
        }


        for (int i = 0; i < MAX_NUM_BULLETS; i++)
        {
            playerBullets[i].update(dt);
        }


        for (int i = 0; i < MAX_NUM_EXP_PARTICLES; i++)
        {
            particles[i].update(dt);
        }

        if (camera.isCameraRecoiling()) {
            camera.cameraRecoil(dt);
        }


        if(explosionRunning) explosionTimer += dt;
        if (explosionTimer > .55) {
            explosionTimer = 0;
            explosionRunning = false;
            for (int i = 0; i < MAX_NUM_EXP_PARTICLES; i++)
            {
                particles[i].setInActive();
            }
        }

        if(GetAsyncKeyState(VK_RETURN) & 0x8000) {
            if(shotRelease) {

                shootBullet(playerBullets, dt, gameObject1);

                //shotRelease = false;
            }
        }


        //if(!(GetAsyncKeyState(VK_RETURN) & 0x8000)) shotRelease = true;

        Vector3 oldEnemyPositions[MAX_NUM_ENEMIES];
        for (int i = 0; i < MAX_NUM_ENEMIES; i++)
        {
            oldEnemyPositions[i] = enemyObjects[i].getPosition();
        }

        Vector3 direction(0, 0, 0);
        Vector3 oldposition = gameObject1.getPosition();

        D3DApp::updateScene(dt);
        gameObject1.update(dt);
        for (int i = 0; i < MAX_NUM_ENEMIES; i++) {
            enemyObjects[i].update(dt);
        }

        if((GetAsyncKeyState('A') & 0x8000) && !(GetAsyncKeyState('D') & 0x8000))  direction.z = -1.0f;
        if((GetAsyncKeyState('D') & 0x8000) && !(GetAsyncKeyState('A') & 0x8000))  direction.z = +1.0f;

        D3DXVec3Normalize(&direction, &direction);

        for (int i = 0; i < MAX_NUM_ENEMIES; i++)
        {
            //if they collide and are active
            if(gameObject1.collided(&enemyObjects[i]) && enemyObjects[i].getActiveState())
            {
                audio->playCue(FAIL);
                enemyObjects[i].setInActive();
                //score++;
                camera.cameraShake(dt);
                score = 0;
                gsm->setGameState(GameStateManager::END_GAME);
            }


            if (enemyObjects[i].getPosition().x > 7) {
                enemyObjects[i].setInActive();
            }
        }

        for (int i = 0; i < MAX_NUM_BULLETS; i++)
        {
            for (int j = 0; j < MAX_NUM_ENEMIES; j++)
            {
                if(playerBullets[i].collided(&enemyObjects[j]) && enemyObjects[j].getActiveState())
                {
                    audio->playCue(BOOM);
                    explosionTimer = 0;
                    runExplosion(playerBullets[i].getPosition());
                    enemyObjects[j].setInActive();
                    playerBullets[i].setInActive();
                    score++;
                }
            }
        }


        gameObject1.setVelocity( direction * gameObject1.getSpeed());

        if (gameObject1.getPosition().z < -PLAYER_Z_RANGE) {
            gameObject1.setPosition(Vector3(oldposition.x, oldposition.y, -PLAYER_Z_RANGE));
            camera.setCameraMoveLeft(false);
            camera.setCameraMoveRight(true);
        }
        else if (gameObject1.getPosition().z > PLAYER_Z_RANGE) {
            gameObject1.setPosition(Vector3(oldposition.x, oldposition.y, PLAYER_Z_RANGE));
            camera.setCameraMoveRight(false);
            camera.setCameraMoveLeft(true);
        }
        else {
            camera.setCameraMoveRight(true);
            camera.setCameraMoveLeft(true);
        }

        //Destroys bullet if too far away
        for (int i = 0; i < MAX_NUM_BULLETS; i++)
        {
            if(playerBullets[i].getPosition().x < -10 && playerBullets[i].getActiveState())
                playerBullets[i].setInActive();
        }

        D3DXMATRIX w;

        D3DXMatrixTranslation(&w, 2, 2, 0);
        mfxWVPVar->SetMatrix(w);

        //Camera Object
        camera.update(dt);

        //Get Camera viewMatrix
        mView = camera.getViewMatrix();
        mProj = camera.getProjectionMatrix();
    }

    if (secondsRemaining <= 0)
    {
        gsm->setGameState(GameStateManager::END_GAME);
    }

    case GameStateManager::END_GAME:
    {
        //D3DApp::updateScene(dt);
        //gameObject1.update(dt);

        if (camera.isCameraShaking()) {
            camera.cameraShake(dt);
        }

        D3DXMATRIX w;

        D3DXMatrixTranslation(&w, 2, 2, 0);
        mfxWVPVar->SetMatrix(w);

        //// Build the view matrix.
        //D3DXVECTOR3 pos(10.0f, 2.0f, 0.0f);
        //D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
        //D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
        //D3DXMatrixLookAtLH(&mView, &pos, &target, &up);

        if(GetAsyncKeyState(VK_SPACE) & 0x8000 && gsm->getGameState() != GameStateManager::IN_GAME) {
            restartGame();
            gsm->setGameState(GameStateManager::IN_GAME);
            audio->playCue(SELECT);
        }

        //Camera Object
        camera.update(dt);

        break;
    }


    default: {
        throw(GameError(gameErrorNS::FATAL_ERROR, "Game State Error"));
        break;
    }
    }
}