/*---------------------------------------------------------------------------
  Shinobi Sample
  COPYRIGHT (C) SEGA ENTERPRISES, LTD.
---------------------------------------------------------------------------*/

#include	<shinobi.h>
#include	"Nj.h"
#include	"njuTask.h"

#define	ZZZZ	-500.f
#define	NNNN	450

static	NJS_POINT3 *particle;
static	NJS_POINT3 *particle2;
static	NJS_POINT3 *particle3;
static	NJS_POINT3	particle0 = {
	0.f, 0.f, 0.f
};
static	Int		stop = 1;

/*---------------------------------------------------------------------------
		初期化
		initialize
---------------------------------------------------------------------------*/
Sint32	UserInit(void)
{
	NJS_POINT3		*p;
	NJS_POINT3		p0 = {0,0,1};
	register	int		i;
	
	
	njPushMatrixEx();
	
	particle = (NJS_POINT3*)syMalloc( NNNN * sizeof(NJS_POINT3) );
	p = particle;
	for( i=0; i<NNNN; i++ ) {
		njUnitMatrix(NULL);
		njRotateXYZ( NULL, njRandom()*0xffff,njRandom()*0xffff,njRandom()*0xffff);
		njCalcPoint( NULL, &p0, p );
		p++;
	}

	particle2 = (NJS_POINT3*)syMalloc( NNNN * sizeof(NJS_POINT3) );
	p = particle2;
	for( i=0; i<NNNN; i++ ) {
		njUnitMatrix(NULL);
		njRotateXYZ( NULL, njRandom()*0xffff,njRandom()*0xffff,njRandom()*0xffff);
		njScale( NULL, njRandom()+0.1,njRandom()+0.1,njRandom()+0.1 );
		njCalcPoint( NULL, &p0, p );
		p++;
	}

	particle3 = (NJS_POINT3*)syMalloc( NNNN * sizeof(NJS_POINT3) );
	p = particle3;
	for( i=0; i<NNNN; i++ ) {
		njUnitMatrix(NULL);
		njRotateXYZ( NULL, njRandom()*0x1000-0x800,njRandom()*0xffff,0);
		njScale( NULL, njRandom()+0.1,njRandom()+0.1,njRandom()+0.1 );
		njCalcPoint( NULL, &p0, p );
		p++;
	}
	
	njPopMatrixEx();
	
	njuInitTask( 1 );

	njSetBackColor( 0x000000, 0x000000, 0x000000 );

	return USER_CONTINUE;
}
/*---------------------------------------------------------------------------
		終了処理
		exit
---------------------------------------------------------------------------*/
Sint32	UserExit(void)
{
	syFree( particle3 );
	syFree( particle2 );
	syFree( particle  );

	return USER_CONTINUE;
}
//*-----------------------------------------------------------------
typedef struct {
	Float		x,y,y0,y1;
	Float		s,w;
	Sint32		ax,ay,az;
	Float		h;
	NJS_COLOR	col;
	NJS_POINT3	*p;
	Angle		r0,g0,b0;
	Angle		r,g,b;
	Float		inten;
} PARTICLE;

void func( NJS_TASK *task )
{
	PARTICLE	*p;
	
	p = task->buf;

	switch( task->status ) {
	case 0:
		p->x = njRandom()*560.f - 280.f;
		p->y0 = -240.f;
		p->y1 = njRandom()*9.f + 4.2f;
		p->s = njRandom()*5.0f + 1.0f;
		p->w = njRandom()*3.0f + 0.5f;
		p->h = njRandom()*1.0f + 0.5f;
		p->inten = 1.0f;
		p->r0 = njRandom() * 0x10000;
		p->g0 = njRandom() * 0x10000;
		p->b0 = njRandom() * 0x10000;
		p->r  = njRandom() * 0x400;
		p->g  = njRandom() * 0x400;
		p->b  = njRandom() * 0x400;
		p->col.argb.a = 0xff;
		p->col.argb.r = njSin(p->r0) * 100 + 150;
		p->col.argb.g = njSin(p->g0) * 100 + 150;
		p->col.argb.b = njSin(p->b0) * 100 + 150;
		p->ax = njRandom() * 0x10000;
		p->ay = njRandom() * 0x10000;
		p->az = njRandom() * 0x10000;
		if( njRandom() < 0.33f ) p->p = particle;
		else if( njRandom() < 0.66f ) p->p = particle2;
		else					p->p = particle3;
		task->status ++;
		break;
	case 1:
		njPushMatrixEx();
		njTranslate( NULL, p->x, p->y0, 0.f );
		njPtclPolygonStart( p->col.color );
		njPtclDrawPolygon( &particle0, 1, p->h*1.5f );
		njPtclSpriteEnd();
		njPopMatrixEx();
		
		if( stop ) {
			p->y0 += p->y1;
			p->y1 -= 0.2f;
		}
		if( p->y1 <= -1.f ) {
			task->status ++;
			p->y = p->y0;
		}
		break;
		
	default:
		if( stop ) {
			p->s += p->w;
			task->status++;
			if( task->status > 50 ) {
				p->h *= 0.95f;
//				p->inten *= 0.97f;
			}

			p->r0 += p->r;
			p->g0 += p->g;
			p->b0 += p->b;
			p->col.argb.r = (njSin(p->r0) * 100 + 150) * p->inten;
			p->col.argb.g = (njSin(p->g0) * 100 + 150) * p->inten;
			p->col.argb.b = (njSin(p->b0) * 100 + 150) * p->inten;
		}

		njPushMatrixEx();
		njTranslate( NULL, p->x, p->y, 0.f );
		njRotateXYZ( NULL, p->ax,p->ay,p->az);
		njScale( NULL, p->s,p->s,p->s );
		njPtclPolygonStart( p->col.color );
		njPtclDrawPolygon( p->p, NNNN, p->h );
		njPtclSpriteEnd();
		njPopMatrixEx();
		
		if( task->status > 85 ) njuDeleteTask( task );
	}

	
}


/*---------------------------------------------------------------------------
		メイン
		main
---------------------------------------------------------------------------*/
Sint32	UserMain(void)
{
	static	Sint32	rx=0,ry=0,rz=0;
	static	Sint32	time=10,count=0;


	njPushMatrixEx();

	if( Per->on & PDD_DGT_KL ) ry -= 200;
	if( Per->on & PDD_DGT_KR ) ry += 200;
	if( Per->on & PDD_DGT_KU ) rx -= 200;
	if( Per->on & PDD_DGT_KD ) rx += 200;
	if( Per->press & PDD_DGT_ST ) rx=0,ry=0;
	if( Per->press & PDD_DGT_TY ) time++;
	if( Per->press & PDD_DGT_TB ) time--;
	if( time < 1 ) time = 1;
	else if( time > 10 ) time = 10;
	
	stop = 1;
	if( Per->on & PDD_DGT_TX ) stop = 0;
	
	njTranslate( NULL, 0.f, 0.f, ZZZZ );
	njRotateXYZ( NULL, rx, ry, rz);

	if( stop ) {
		count++;
	}
	
	if( count >= time ) {
		if( !(Per->on & PDD_DGT_TA) ) {
			njuCreateTask( func, 0, 0, sizeof( PARTICLE ), NULL, NULL );
		}
		count=0;
	}

	njuExecTask();
	
	njPopMatrixEx();
	
	return USER_CONTINUE;
}
/*-------------------------------------------------------------------------*/
