/*
 * GameObject.cpp
 *
 *  Created on: Feb 16, 2011
 *      Author: cpresser
 */

#include "GameObject.h"

GameObject::GameObject(): m_sceneMgr(0),
        m_entity(0),
        m_node(0),
        m_name(),
        m_isInitialized(false),
        m_isSelected(false),
        m_direction(0, 0, 0),
        m_oldPosition(0, 0, 0)
{

}



void GameObject::init(Ogre::SceneManager *sceneMgr,
                Ogre::String name,
                Ogre::Vector3 position,
                Ogre::String mesh,
                Ogre::String material)
{
        //don't initialize more than once
        if(m_isInitialized)
                return;

        m_sceneMgr = sceneMgr;
        m_isInitialized = true;
        m_name = name;

        //set up the entity
        m_entity = m_sceneMgr->createEntity(name, mesh);

        //set up the scene node
        m_node = m_sceneMgr->getRootSceneNode()->createChildSceneNode(name + "Node");
        m_node->attachObject(m_entity);

        //set the position
        m_node->setPosition(position);
        m_oldPosition = position;

        //scale to HEIGHT
        Ogre::AxisAlignedBox box = m_entity->getBoundingBox();
        Ogre::Vector3 boxSize = box.getSize();

        //std::cerr << "ORIGINAL SIZE " << boxSize.y << std::endl;

        Ogre::Real scaleAmt = HEIGHT/boxSize.y;
        m_node->scale(scaleAmt, scaleAmt, scaleAmt);


        //std::cerr << "NEW SIZE " << scaleAmt*boxSize.y << std::endl;

        //set the material if any
        if(material != ""){
                m_entity->setMaterialName(material);
        }

}

GameObject::~GameObject() {

}

void GameObject::update(const Ogre::FrameEvent & evt)
{
        //don't do anything if the object has not been initialized
        if(!m_isInitialized){
                return;
        }

        //at least move this thing
        m_oldPosition = m_node->getPosition();
        Ogre::Vector3 location = m_oldPosition + (m_direction * evt.timeSinceLastFrame);

        m_node->setPosition(location);
}

void GameObject::setDirection(Ogre::Vector3 direction)
{
        m_direction = direction;
}



Ogre::Vector3 & GameObject::getDirection()
{
        return m_direction;
}



bool GameObject::isSelected()
{
        return m_isSelected;
}



bool GameObject::isInitialized()
{
        return m_isInitialized;
}



void GameObject::setSelected(bool select)
{
        m_isSelected = select;
        m_node->showBoundingBox(select);
}

Ogre::String GameObject::getName()
{
        if(!m_isInitialized)
                return "";

        return m_name;
}

Ogre::String GameObject::getEntityName()
{
        if(!m_isInitialized)
                return "";

        return m_entity->getName();
}

Ogre::String GameObject::getNodeName()
{
        if(!m_isInitialized)
                return "";

        return m_node->getName();
}

Ogre::AxisAlignedBox GameObject::getTransformedBoundingBox()
{
        if(!m_isInitialized)
                return Ogre::AxisAlignedBox();
        return m_node->_getWorldAABB();
}

void GameObject::stepBack(){
        if(m_isInitialized)
                m_node->setPosition(m_oldPosition);
}