/***************************************************************************
 *   Copyright (C) 2004-2005 by                                            *
 *     Paolo Sacconier   <axa1981@tin.it>                                  *
 *     Francesco Tamagni <minchiahead@hacari.org>                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "goal.h"

namespace gillo {

Goal::Goal(Context& c)
 : Entity(c)
{
	type = GOAL;
	dSpaceID sid = dSimpleSpaceCreate(c.getSid());
	gid = (dGeomID) sid;
	Entity::Init();
	
	/* ssg part */
	ssgSimpleState * state = c.getState(STA_GOAL);
	ssgSimpleState * alphastate = c.getState(STA_GOAL_CENTER);
			
	sgVec3 dim[] = {
		{ ODE_GOAL_DIMX, ODE_GOAL_DIMY, ODE_GOAL_DIMZ },
		{ ODE_GOAL_DIMX, ODE_GOAL_DIMY, ODE_GOAL_DIMZ },
		{ ODE_GOAL_DISTANCE - ODE_GOAL_DIMX, ODE_GOAL_DIMY, ODE_GOAL_DIMY },
		{ ODE_GOAL_DISTANCE - ODE_GOAL_DIMX, ODE_GOAL_DIMY, ODE_GOAL_DIMY },
		{ ODE_GOAL_DISTANCE, 0.1*ODE_GOAL_DIMY, 0.9*(ODE_GOAL_DIMZ - ODE_GOAL_DIMY)},
		{ 2*areaRadius, 2*areaRadius, ODE_GOAL_DIMZ}
	};
	sgVec3 pos[] = {
		{  ODE_GOAL_DISTANCE/2, 0, 0 },
		{ -ODE_GOAL_DISTANCE/2, 0, 0 },
		{ 0, 0, -0.9*(ODE_GOAL_DIMZ/2 - ODE_GOAL_DIMY/2)},
		{ 0, 0,  0.9*(ODE_GOAL_DIMZ/2 - ODE_GOAL_DIMY/2)},
		{ 0, 0, 0 }
	}; 
	
	ssgaCube* e_obj;
	for (int i = 0; i < 5; i++) {
		e_obj =  new ssgaCube     () ;
		e_obj -> setSize          ( dim[i] ) ;
		e_obj -> setCenter        ( pos[i] ) ;
		e_obj -> setKidState      ( state ) ;
		e_obj -> regenerate       () ;
		trans.addKid( e_obj );
		gids[i] = dCreateBox (sid, dim[i][0], dim[i][1], dim[i][2]);
		dGeomSetData(gids[i], this);
		dGeomSetPosition (gids[i], (dReal) pos[i][0], (dReal) pos[i][1], (dReal) pos[i][2]);
	}
	
	alphaTrans.addKid( e_obj );
	trans.removeKid( e_obj );
	
	e_obj -> setKidState      ( alphastate ) ;
	e_obj -> regenerate       () ;
	
	// area geom
	gids[5] = dCreateCCylinder (sid, areaRadius, ODE_GOAL_DIMZ);
	dGeomSetData(gids[5], this);
	ssgaCylinder* area_obj = new ssgaCylinder();
	ssgTransform* area_tr  = new ssgTransform();
	area_obj -> setSize          ( dim[5] ) ;
	area_obj -> setCenter        ( pos[4] ) ;
	area_obj -> setKidState      ( c.getState(STA_GOAL_AREA) ) ;
	area_obj -> makeCapped	     ( 0 );
	area_obj -> regenerate       () ;
	area_tr -> addKid( area_obj );
	alphaTrans.addKid( area_tr  );
	
}


void Goal::update(float dt) {
	sgMat4 rotm;
	const sgVec3 axis = { 0, 0, 1 };
	static float angle = 0;

	angle += 500*dt;
	sgMakeRotMat4(rotm, angle, axis);

	((ssgTransform*) alphaTrans.getKid(1))->setTransform(rotm);
}


Goal::~Goal()
{
}

void Goal::getPos(sgVec3 out)
{
	sgZeroVec3(out);
}

};
