/***************************************************************************
 *   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 "pwrup.h"

namespace gillo {

PwrUp::PwrUp(Context& c, PType t)
 : Entity(c), pType(t), pulse(0)
{
	sgVec3 center = { 0,0,0 };
	sgVec3 dim = { 0.35, 0, 0.35 };
	
	type = PWRUP;
	bid = dBodyCreate(c.getWid());
	gid = dCreateBox (c.getSid(), dim[0], 0.5*dim[2], dim[2]);
	Entity::Init();
	
	dGeomSetPosition (gid, (dReal) center[0], (dReal) center[1], (dReal) center[2]);
	
	ssgSimpleState* state = NULL;
	char* s = NULL;
	switch(pType) {
		case PwrUp::BALL_ADDER:
			state = c.getState(STA_PWRUP_BALL_ADD);
			s = "PwrUpBallAdd";
			break;
		case PwrUp::BALL_INFLATOR:
			state = c.getState(STA_PWRUP_BALL_INFLATE);
			s = "PwrUpBallInflate";
			break;
		case PwrUp::GRAVITY_ROTATOR:
			state = c.getState(STA_PWRUP_GRAVITY_ROTATOR);
			s = "PwrUpGravityRotator";
			break;
		case PwrUp::CHARGE_INVERTER:
			state = c.getState(STA_PWRUP_CHARGE_INVERTER);
			s = "PwrUpChargeInverter";
			break;
		default:
			printf("WARNING: (%d) powerup without a texture state!\n", pType);
			state = c.getState(STA_PWRUP_GRAVITY_ROTATOR);
			s = "PwrUp";
			break;
	}
		
	ssgEntity* e = c.searchInPool(s);
	if (e == NULL) {
		ssgCutout* co = new ssgCutout(true);
		ssgTransform* tr = new ssgTransform();
		ssgaCube * e_obj =  new ssgaCube     () ;
		e_obj -> setSize          ( dim ) ;
		e_obj -> setCenter        ( center ) ;
		e_obj -> setKidState      ( state ) ;
		e_obj -> regenerate       () ;
		tr->addKid(e_obj);
		co->setName(s);
		co->addKid(tr);
		e = (ssgEntity*) co;
		c.addToPool(e);
	}
	alphaTrans.addKid( e );
	
	dMassSetBoxTotal (&mass, ODE_CAR_MASS, dim[0], 0.5*dim[2], dim[2]);
	dBodySetMass(bid, &mass);
	dBodySetPosition(bid, center[0], center[1], center[2]);
}

PwrUp::~PwrUp()
{
	printf("Deleted PowerUp\n");
}

void PwrUp::update(float dt) {
// 	Entity::update(dt);
	sgMat4 rotm, trasm, totm, scalm;
	
	/* position */
	const dReal * bpos;
	
	bpos = dBodyGetPosition(bid); 
	sgMakeTransMat4 (trasm, (float) bpos[0], (float)  bpos[1], (float) bpos[2]) ; 
	alphaTrans.setTransform ( trasm ) ;
	
	pulse += 10*dt;
	float ci = 0.3*cos(pulse)+1.0;
	/* composition */
	Entity::transform((ssgTransform*) ((ssgCutout*) alphaTrans.getKid(0))->getKid(0), 0, ci);
}


};
