/*
	Rogue Trader.
	Copyright (c) 2007, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   GameObject_Explosion
// Purpose:      Small class which handles the creation and animation of an
//               explosion effect.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "GameObject_Explosion.h"

//------------------------------------------------------------------------------
// Constant initialisation
//------------------------------------------------------------------------------

const float GameObject_Explosion::SOUND_DISTANCE = 64000;   // distance from which the explosion can be heard

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GameObject_Explosion::GameObject_Explosion()
Purpose:  Constructor for the explosion object.
Params:   as commented.
Returns:  n/a

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

GameObject_Explosion::GameObject_Explosion
(
    float       x,          // position of explosion: x
    float       y,          // position of explosion: y
    float       z,          // position of explosion: z
    float       radius,     // explosion radius
    float       time        // time the explosion will take
)
:
StaticGameObject(x,y,z,0)
{
    //--------------------------------------------------------------------------
    // Set the explosion time and time left for the explosion
    //--------------------------------------------------------------------------

    this->timeLeft      = time;
    this->explosionTime = time;

    //--------------------------------------------------------------------------
    // Create each explosion part:
    //--------------------------------------------------------------------------

    for ( unsigned i = 0 ; i < NUM_PARTS ; i++ )
    {
        //----------------------------------------------------------------------
        // Decide first on the explosion part's offset from the explosion center
        //----------------------------------------------------------------------

        explosionParts[i].pos_x = ( mlRandomFloat() - 0.5f ) * radius + x;
        explosionParts[i].pos_y = ( mlRandomFloat() - 0.5f ) * radius + y;
        explosionParts[i].pos_z = ( mlRandomFloat() - 0.5f ) * radius + z;

        //----------------------------------------------------------------------
        // Decide on explosion part size
        //----------------------------------------------------------------------

        explosionParts[i].size = mlRandomFloat() * radius;

        //----------------------------------------------------------------------
        // Decide on the explosion part's texture coordinates
        //----------------------------------------------------------------------

        switch ( mlRandomInt() & 3 )
        {
            //------------------------------------------------------------------

            case 0:

                explosionParts[i].u1 = 0;
                explosionParts[i].v1 = 0;
                explosionParts[i].u2 = 1;
                explosionParts[i].v2 = 0;
                explosionParts[i].u3 = 1;
                explosionParts[i].v3 = 1;
                explosionParts[i].u4 = 0;
                explosionParts[i].v4 = 1;

            break;

            //------------------------------------------------------------------

            case 1:

                explosionParts[i].u1 = 0;
                explosionParts[i].v1 = 1;
                explosionParts[i].u2 = 0;
                explosionParts[i].v2 = 0;
                explosionParts[i].u3 = 1;
                explosionParts[i].v3 = 0;
                explosionParts[i].u4 = 1;
                explosionParts[i].v4 = 1;

            break;

            //------------------------------------------------------------------

            case 2:

                explosionParts[i].u1 = 1;
                explosionParts[i].v1 = 1;
                explosionParts[i].u2 = 0;
                explosionParts[i].v2 = 1;
                explosionParts[i].u3 = 0;
                explosionParts[i].v3 = 0;
                explosionParts[i].u4 = 1;
                explosionParts[i].v4 = 0;

            break;

            //------------------------------------------------------------------

            case 3:

                explosionParts[i].u1 = 1;
                explosionParts[i].v1 = 0;
                explosionParts[i].u2 = 1;
                explosionParts[i].v2 = 1;
                explosionParts[i].u3 = 0;
                explosionParts[i].v3 = 1;
                explosionParts[i].u4 = 0;
                explosionParts[i].v4 = 0;

            break;

            //------------------------------------------------------------------

            default:

                explosionParts[i].u1 = 0;
                explosionParts[i].v1 = 0;
                explosionParts[i].u2 = 1;
                explosionParts[i].v2 = 0;
                explosionParts[i].u3 = 1;
                explosionParts[i].v3 = 1;
                explosionParts[i].u4 = 0;
                explosionParts[i].v4 = 1;

            break;

        }   // end switch: decide on explosion texture coordinates

    }   // end for every explosion part
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GameObject_Explosion::onFrame()
Purpose:  On frame event for the explosion: decrements its time to live
Params:   time of the last frame in seconds
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GameObject_Explosion::onFrame( float frameTime )
{
    // See if this is the first on frame event:

    if ( this->timeLeft == explosionTime )
    {
        // First frame: intrepret the scene this object belongs to as a game scene

        GameScene * scene = (GameScene*) this->getScene();

        // Create an explosion noise in the scene at the explosion position:

        scene->addSound
        (
            SoundManager::getSndExplosion(),    // sound to play
            this->getPosX(),                    // position of the sound
            this->getPosY(),                    // position of the sound
            this->getPosZ(),                    // position of the sound
            SOUND_DISTANCE,                     // distance sound can be heard from
            1,                                  // frequency of sound
            false                               // is sound looping
        );
    }

    // Decrement the time for the explosion to live:

    this->timeLeft -= frameTime;

    // If the time left is negative then make this object destructable and give zero health:

    if ( timeLeft <= 0 )
    {
        // The game object scene will remove objects when these variables are set as follows:

        this->indestructable    = false;
        this->health            = 0;
    }
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GameObject_Explosion::onDraw()
Purpose:  On draw event for the explosion. Draws the explosion.
Params:   Camera the scene is being viewed from.
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GameObject_Explosion::onDraw( Camera * camera )
{
    //--------------------------------------------------------------------------
    // If the explosion is already dead then don't bother:
    //--------------------------------------------------------------------------

    if ( timeLeft <= 0 ) return;

    //--------------------------------------------------------------------------
    // OpenGL setup
    //--------------------------------------------------------------------------

    // Enables / disables

    glEnable  ( GL_BLEND        );
    glEnable  ( GL_TEXTURE_2D   );
    glDisable ( GL_LIGHTING     );
    glDisable ( GL_CULL_FACE    );

    // Set blending mode: additive blending

    glBlendFunc( GL_ONE , GL_ONE );

    // No depth writes

    glDepthMask ( GL_FALSE );

    //--------------------------------------------------------------------------
    // Figure out what scale all explosion parts should be sized at.
    // When explosion is beginning size is zero. Half way through size is at
    // full size. At end the size contracts to zero again:
    //--------------------------------------------------------------------------

    //float explosionScale = timeLeft / explosionTime;

    float explosionScale = 1;

    //--------------------------------------------------------------------------
    // Figure out what texture frame of the explosion to draw from time left:
    //--------------------------------------------------------------------------

    // There are 18 frames in total

    int frame = int( ( timeLeft / explosionTime ) * 18.0f );

    // Now bind the texture for this particle frame:

    switch ( frame )
    {
        case 0:     GLTextureManager::getTexExplosion18()->bind();  break;
        case 1:     GLTextureManager::getTexExplosion17()->bind();  break;
        case 2:     GLTextureManager::getTexExplosion16()->bind();  break;
        case 3:     GLTextureManager::getTexExplosion15()->bind();  break;
        case 4:     GLTextureManager::getTexExplosion14()->bind();  break;
        case 5:     GLTextureManager::getTexExplosion13()->bind();  break;
        case 6:     GLTextureManager::getTexExplosion12()->bind();  break;
        case 7:     GLTextureManager::getTexExplosion11()->bind();  break;
        case 8:     GLTextureManager::getTexExplosion10()->bind();  break;
        case 9:     GLTextureManager::getTexExplosion9()->bind();   break;
        case 10:    GLTextureManager::getTexExplosion8()->bind();   break;
        case 11:    GLTextureManager::getTexExplosion7()->bind();   break;
        case 12:    GLTextureManager::getTexExplosion6()->bind();   break;
        case 13:    GLTextureManager::getTexExplosion5()->bind();   break;
        case 14:    GLTextureManager::getTexExplosion4()->bind();   break;
        case 15:    GLTextureManager::getTexExplosion3()->bind();   break;
        case 16:    GLTextureManager::getTexExplosion2()->bind();   break;
        case 17:    GLTextureManager::getTexExplosion1()->bind();   break;
        default:    GLTextureManager::getTexExplosion1()->bind();   break;
    }

    //--------------------------------------------------------------------------
    // Get the camera's orientation matrix and its xy axis vectors
    //--------------------------------------------------------------------------

    float matrix[9]; camera->getOrientationMatrix(matrix);

    float xaxis[3] = { matrix[0] , matrix[3] , matrix[6] };
    float yaxis[3] = { matrix[1] , matrix[4] , matrix[7] };

    //--------------------------------------------------------------------------
    // Now draw each part of the explosion:
    //--------------------------------------------------------------------------

    // Set draw color:

    glColor3f(1,1,1);

    // Begin drawing quads

    glBegin(GL_QUADS);

    // Draw each explosion part:

    for ( unsigned i = 0 ; i < NUM_PARTS ; i++ )
    {
        //----------------------------------------------------------------------
        // Figure out what size this part should be:
        //----------------------------------------------------------------------

        float size = explosionScale * explosionParts[i].size;

        //----------------------------------------------------------------------
        // Draw the explosion part quad, centered at its current position and
        // going along the axes of the view so that it always faces the camera
        //----------------------------------------------------------------------

        glTexCoord2f
        (
            explosionParts[i].u1,
            explosionParts[i].v1
        );

        glVertex3f
        (
            explosionParts[i].pos_x - size * xaxis[0] + size * yaxis[0],
            explosionParts[i].pos_y - size * xaxis[1] + size * yaxis[1],
            explosionParts[i].pos_z - size * xaxis[2] + size * yaxis[2]
        );

        glTexCoord2f
        (
            explosionParts[i].u2,
            explosionParts[i].v2
        );

        glVertex3f
        (
            explosionParts[i].pos_x + size * xaxis[0] + size * yaxis[0],
            explosionParts[i].pos_y + size * xaxis[1] + size * yaxis[1],
            explosionParts[i].pos_z + size * xaxis[2] + size * yaxis[2]
        );

        glTexCoord2f
        (
            explosionParts[i].u3,
            explosionParts[i].v3
        );

        glVertex3f
        (
            explosionParts[i].pos_x + size * xaxis[0] - size * yaxis[0],
            explosionParts[i].pos_y + size * xaxis[1] - size * yaxis[1],
            explosionParts[i].pos_z + size * xaxis[2] - size * yaxis[2]
        );

        glTexCoord2f
        (
            explosionParts[i].u4,
            explosionParts[i].v4
        );

        glVertex3f
        (
            explosionParts[i].pos_x - size * xaxis[0] - size * yaxis[0],
            explosionParts[i].pos_y - size * xaxis[1] - size * yaxis[1],
            explosionParts[i].pos_z - size * xaxis[2] - size * yaxis[2]
        );

    }   // end for every explosion part

    // Finish up drawing quads

    glEnd();
}
