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

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

#define	OBJECT1	 object_babyn2_Bip01_Bip01
#define	OBJECT2	 object_babyn2_Box01_Box01

#define TEXLIST1 texlist_babyn2_Bip01
#define TEXLIST2 texlist_babyn2_Box01

#define	MOTION1	 motion_backten_Bip01
#define	MOTION2	 motion_bback_Bip01
#define	MOTION3	 motion_facehit_Bip01
#define	MOTION4	 motion_babyn2_Bip01
#define	MOTION5	 motion_robot_Bip01

extern NJS_CNK_OBJECT	OBJECT1[];
extern NJS_CNK_OBJECT	OBJECT2[];
extern NJS_MOTION		MOTION1[];
extern NJS_MOTION		MOTION2[];
extern NJS_MOTION		MOTION3[];
extern NJS_MOTION		MOTION4[];
extern NJS_MOTION		MOTION5[];
extern NJS_TEXLIST		TEXLIST1[];
extern NJS_TEXLIST		TEXLIST2[];


static	Float	zzz = -300.f;
static	Float	xxx = 0.f;
static	Float	yyy = -70.f;

static	Sint32	xx = 0;
static	Sint32	yy = 0;
static	Sint32	zz = 0;

static	Float	mo1=0.f;
static	Float	mo2=0.f;
static	Float	mo3=0.f;
static	Float	mo4=0.f;
static	Float	mo5=0.f;
static	Float	speed=0.5f;

static	Float	inten = 0.8f;
static	Float	ambi  = 0.2f;


static	Sint32	mo = 0;

static	NJS_VECTOR	light = { 0.f, 0.f, -1.f };

static NJS_QUAD_TEXTURE quad;


static NJS_MOTION	*pMotions[MOTION_PATTERN_MAX] = {
	MOTION1,
	MOTION2,
	MOTION3,
	MOTION4,
	MOTION5,
};

static int		MenuFlg	= 0;

static TMenuItemInt		MIMotionPattern[2]	= {
	{ 0, MOTION_PATTERN_MAX-1, 1, 0 },
	{ 0, MOTION_PATTERN_MAX-1, 1, 1 },
};
static TMenuItemFloat	MIMotionFrame[2]	= {
	{ 0.0f, 100.0f, 0.5f, 0.0f },
	{ 0.0f, 100.0f, 0.5f, 0.0f },
};
static TMenuItemFloat	MIMotionLinkRate	= { 0.0f, 1.0f, 0.05f, 0.0f };

static TMenuInfo		MenuInfo[MENU_INFO_MAX] = {
	{ "Motion0 Pattern", MENU_TYPE_INT, &MIMotionPattern[0] },
	{ "Motion1 Pattern", MENU_TYPE_INT, &MIMotionPattern[1] },
	{ "Motion0 Frame", MENU_TYPE_FLOAT, &MIMotionFrame[0] },
	{ "Motion1 Frame", MENU_TYPE_FLOAT, &MIMotionFrame[1] },
	{ "MotionLinkRate", MENU_TYPE_FLOAT, &MIMotionLinkRate },
};



/*---------------------------------------------------------------------------
		初期化
		initialize
---------------------------------------------------------------------------*/
Sint32	UserInit(void)
{
	if(nuLoadTexture( TEXLIST1 ) < 0){
		return USER_EXIT;
	}
	if(nuLoadTexture( TEXLIST2 ) < 0){
		return USER_EXIT;
	}

	return USER_CONTINUE;
}

/*---------------------------------------------------------------------------
		メイン
		main
---------------------------------------------------------------------------*/
Sint32	UserMain(void)
{
	NJS_VECTOR	l;

//------------------------------------------------------------------
	njPushMatrixEx();

	njSetTexture( TEXLIST1 );

	njPushMatrixEx();
	njRotateXYZ( NULL, Per->y1*120,-Per->x1*120,0 );
	njCalcVector( NULL, &light, &l );
	njPopMatrixEx();

	
	njCnkSetSimpleLight( l.x, l.y, l.z );
	njCnkSetSimpleLightIntensity( inten, ambi );
	njCnkSetSimpleLightColor( 1.0f, 1.0f, 1.0f );
		
	njCnkSetEasyLight( l.x, l.y, l.z );
	njCnkSetEasyLightIntensity( inten, ambi );
	njCnkSetEasyLightColor( 1.0f, 1.0f, 1.0f );
		

	njTranslate( NULL, xxx, yyy, zzz );
	njRotateXYZ( NULL, xx,yy,zz );
	
	#if 0
	switch( mo ) {
	case 0:
		njCnkEasyDrawMotion( OBJECT1, MOTION1, mo1 );
		mo1 += speed;
		if( mo1 >= (MOTION1->nbFrame-1.0f) ) mo1=0.f;
		break;
	case 1:
		njCnkEasyDrawMotion( OBJECT1, MOTION2, mo2 );
		mo2 += speed;
		if( mo2 >= (MOTION2->nbFrame-1.0f) ) mo2=0.f;
		break;
	case 2:
		njCnkEasyDrawMotion( OBJECT1, MOTION3, mo3 );
		mo3 += speed;
		if( mo3 >= (MOTION3->nbFrame-1.0f) ) mo3=0.f;
		break;
	case 3:
		njCnkEasyDrawMotion( OBJECT1, MOTION4, mo4 );
		mo4 += speed;
		if( mo4 >= (MOTION4->nbFrame-1.0f) ) mo4=0.f;
		break;
	case 4:
		njCnkEasyDrawMotion( OBJECT1, MOTION5, mo5 );
		mo5 += speed;
		if( mo5 >= (MOTION5->nbFrame-1.0f) ) mo5=0.f;
		break;
	}
	#else
	{
		int					i;
		int					MotionNo[2];
		NJS_MOTION			*pMotion[2];
		Float				frame[2];
		
		for ( i = 0; i < 2; i++ ) {
			MotionNo[i]	= MIMotionPattern[i].No;
			pMotion[i]	= pMotions[ MotionNo[i] ];
			
			frame[i]	= MIMotionFrame[i].No;
			if ( frame[i] < 0.0f ) {
				frame[i]	= 0.0f;
			}
			else if ( frame[i] > pMotion[i]->nbFrame - 1.0f ) {
				frame[i]	= pMotion[i]->nbFrame - 1.0f;
			}
		}
		
		njPushMatrixEx();
		njTranslate( NULL, -100.0f, 0.0f, 0.0f );
		njCnkEasyDrawMotion( OBJECT1, pMotion[0], frame[0] );
		njPopMatrixEx();
		
		njPushMatrixEx();
		njTranslate( NULL, 100.0f, 0.0f, 0.0f );
		njCnkEasyDrawMotion( OBJECT1, pMotion[1], frame[1] );
		njPopMatrixEx();
		
		{
			NJS_MOTION_LINK		MotionLink;
			Float				rate;
			
			for ( i = 0; i < 2; i++ ) {
				MotionLink.motion[i]	= pMotion[i];
				MotionLink.frame[i]		= frame[i];
			}
			rate	= MIMotionLinkRate.No;
			njCnkEasyDrawMotionLink( OBJECT1, &MotionLink, rate );
		}
	}
	#endif
	
	
	njCnkSetSimpleLightIntensity( 0.0f, 1.0f );
	njCnkSetEasyLightIntensity( 0.0f, 1.0f );

	njSetTexture( TEXLIST2 );
	njCnkSimpleDrawObject( OBJECT2 );


	quad.x1 = 20.f;
	quad.y1 = 380.f;
	quad.x2 = 620.f;
	quad.y2 = 440.f;
	quad.u1 = 0.f;
	quad.v1 = 0.f;
	quad.u2 = 1.f;
	quad.v2 = 1.f;
	
	njQuadTextureStart( 1 );
	njSetQuadTexture( 1, 0xffffffff );
	njDrawQuadTexture( &quad, 10.f );
	njQuadTextureEnd();
	
	
	
	if ( Per->press & PDD_DGT_TB ) {
		MenuFlg	= 1 - MenuFlg;
	}
	
	if ( MenuFlg ) {
		ExecMenu( &MenuInfo[0], MENU_INFO_MAX );
	}
	else {
		if (Per->on & PDD_DGT_TA ) {
			if (Per->on & PDD_DGT_KU) 	yyy += 2.f;
			if (Per->on & PDD_DGT_KD) 	yyy -= 2.f;
			if (Per->on & PDD_DGT_KR) 	xxx += 2.f;
			if (Per->on & PDD_DGT_KL) 	xxx -= 2.f;
			if (Per->on & PDD_DGT_TR) 	zzz += 2.f;
			if (Per->on & PDD_DGT_TL) 	zzz -= 2.f;
		}
		else if (Per->on & PDD_DGT_TX ) {
			if (Per->on & PDD_DGT_KU) 	speed += 0.01f;
			if (Per->on & PDD_DGT_KD) 	speed -= 0.01f;
			if( speed <= 0.f ) speed = 0.01f;
		}
		#if 0
		else if (Per->on & PDD_DGT_TB ) {
			if (Per->on & PDD_DGT_KU) 	inten += 0.01f;
			if (Per->on & PDD_DGT_KD) 	inten -= 0.01f;
			if (Per->on & PDD_DGT_KR) 	ambi += 0.01f;
			if (Per->on & PDD_DGT_KL) 	ambi -= 0.01f;
		}
		#endif
		else {
			if (Per->on & PDD_DGT_KU) 	xx += 200;
			if (Per->on & PDD_DGT_KD) 	xx -= 200;
			if (Per->on & PDD_DGT_KR) 	yy += 200;
			if (Per->on & PDD_DGT_KL) 	yy -= 200;
			if (Per->on & PDD_DGT_TR) 	zz += 200;
			if (Per->on & PDD_DGT_TL) 	zz -= 200;
		}
		
		if (Per->press & PDD_DGT_TY) {
			mo++;
			if( mo > 4 ) mo = 0;
		}
		
		if (Per->press & PDD_DGT_ST)	{
			zzz = -300.f;
			xxx = 0.f;
			yyy = -70.f;
			xx = 0;
			yy = 0;
			zz = 0;
			speed = 0.5f;
			inten = 0.8f;
			ambi = 0.2f;
		}
	}
	
	njPopMatrixEx();
	
	return USER_CONTINUE;
}

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