/*
	Wheels Of Steel
	Copyright (c) 2006, 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
// Purpose:      Mostly abstract class that defines basic functionality and
//               attributes for all the objects in the game
//
//
//##############################################################################
//##############################################################################
//##############################################################################

#include "GameObject.h"


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     GameObject::GameObject
Purpose:  Base class constructor. Initialises the position and other attributes
          of the object to the initial values
Params:   Position, whether the object is moveable or not.
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

GameObject::GameObject
(
    float pos_x,
    float pos_y,
    float pos_z,
    bool  moveable
)
{
    // Initialise position

    this->pos_x = pos_x;
    this->pos_y = pos_y;
    this->pos_z = pos_z;

    // Initialise velocity

    this->vel_x = 0;
    this->vel_y = 0;
    this->vel_z = 0;

    // Initialise acceleration

    this->acl_x = 0;
    this->acl_y = 0;
    this->acl_z = 0;

    // Set whether the object is moveable or not

    this->moveable = moveable;

    // Initialise track section

    this->section = -1;

    // Initialise game object manager internal object array position for this object

    this->mgrArrayPos = -1;

}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     GameObject::drawPerspectiveImage
Purpose:  A common drawing function to all the game objects. draw the specified
          image to size of the objects dimensions, scaled by perspective
Params:   Window to draw to, camera position, mipmap image to draw
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void GameObject::drawPerspectiveImage( Window & window , Camera * camera , const Mipmap * mipmap )  const
{
    // If not a valid mipmap then abort

    if ( ! mipmap->isValidMipmap() ) return;

    // Get object coordinates relative to camera

    float x = this->pos_x - camera->getX();
    float y = this->pos_y - camera->getY();
    float z = this->pos_z - camera->getZ();

    // If z < Track::VIEW_PLANE_NEAR then abort draw, past the near plane

    if ( z < Track::VIEW_PLANE_NEAR ) return;

    // Project position with perspective and projected onto a screen sized plane

    x /=  z;
    y /= -z;

    x *= Game::RESOLUTION_X >> 1;
    y *= Game::RESOLUTION_Y >> 1;

    // Translate so that origin is at screen center

    x += Game::RESOLUTION_X >> 1;
    y += Game::RESOLUTION_Y >> 1;

    // Get dimensions of object scaled with perspective and projected onto a screen sized plane

    float size_x = this->size_x / z;
    float size_y = this->size_y / z;

    size_x *= Game::RESOLUTION_X >> 1;
    size_y *= Game::RESOLUTION_Y >> 1;

    // Subtract half the size so that image is centered at position of object

    x -= size_x * 0.5f;
    y -= size_y * 0.5f;

    // Round the position and size of the image

    if ( x - int(x) >= 0.5f ) x += 1;
    if ( y - int(y) >= 0.5f ) y += 1;

    if ( size_x - int(size_x) >= 0.5f ) size_x += 1;
    if ( size_y - int(size_y) >= 0.5f ) size_y += 1;

    // Draw the mipmap centered about the x,y coordinates

    mipmap->draw
    (
        &window,
        int(x),
        int(y),
        int(size_x),
        int(size_y)
    );
}
