//---------------------------------------------------------------------------
//	Ninja Sample
//	COPYRIGHT (C) SEGA ENTERPRISES, LTD.
//	
//	CameraEx:	WalkDemo
//---------------------------------------------------------------------------
#include	<shinobi.h>
#include	"nj.h"
#include	"Common.h"

static float		frame_no	= 0.0f;

static NJS_POINT3		CameraPos;
static NJS_POINT3		CharaPos;
static Angle			CharaAngle;

void WalkDemo( int State )
{
	if ( State == STATE_ATTACH ) {
		frame_no	= 0.0f;
		
		nuLoadTexture( CHARA_TEXLIST );
		
		CameraPos.x		= 0.0f;
		CameraPos.y		= 0.0f;
		CameraPos.z		= 0.0f;
		
		CharaPos.x		= CHARA_POS_X;
		CharaPos.y		= CHARA_POS_Y;
		CharaPos.z		= CHARA_POS_Z;
		CharaAngle		= 0;
	}
	else if ( State == STATE_DETACH ) {
		njReleaseTexture( CHARA_TEXLIST );
	}
	else {
		const PDS_PERIPHERAL		*per;
		static NJS_POINT3			Up		= { 0.0f, 1.0f, 0.0f };
		
		per		= pdGetPeripheral(PDD_PORT_A0);
		
		#if		0
		CharaAngle	-= per->x1 * 4;
		CharaPos.x	+= njSin( CharaAngle ) * per->y1 * 0.01f;
		CharaPos.z	+= njCos( CharaAngle ) * per->y1 * 0.01f;
		#else
		{
			Float				FwX;
			Float				FwZ;
			Float				RtX;
			Float				RtZ;
			Float				DstX;
			Float				DstZ;
			Float				f;
			Angle				DstAng;
			Float				Speed;
			Sint32				DefAngle;
			
			FwX	= CharaPos.x - CameraPos.x;
			FwZ	= CharaPos.z - CameraPos.z;
			f	= 1.0f / njSqrt( FwX * FwX + FwZ * FwZ );
			FwX	*= f;
			FwZ	*= f;
			RtX	= -FwZ;
			RtZ	= FwX;
			
			//	目標方向ベクトル
			DstX	= RtX * per->x1 + FwX * (-per->y1);
			DstZ	= RtZ * per->x1 + FwZ * (-per->y1);
			//	速度
			Speed	= njSqrt( per->x1 * per->x1 + per->y1 * per->y1 );
			if ( Speed != 0.0f ) {
				DstX	/= Speed;
				DstZ	/= Speed;
			}
			
			//	目標角度 :	−Ｚ軸を基線に取った目標方向の角度
			DstAng		= njArcTan2( -DstX, -DstZ );
			
			
			
			//	目標角度と現在の角度の違い
			DefAngle	= (Sint16)( ((Sint16)DstAng) - ((Sint16)CharaAngle) );
			
			if ( Speed >= 2.0f ) {
				Angle			Step		= Speed * 8;
				
				if ( DefAngle >= Step ) {
					CharaAngle	+= Step;
				}
				else if ( DefAngle <= -Step ) {
					CharaAngle	-= Step;
				}
			}
			
			#if 0
			if ( DefAngle >= -0x1000 && DefAngle <= 0x1000 ) {
				CharaPos.x	-= njSin( CharaAngle ) * Speed * 0.01f;
				CharaPos.z	-= njCos( CharaAngle ) * Speed * 0.01f;
			}
			else if ( DefAngle >= -0x2000 && DefAngle <= 0x2000 ) {
				CharaPos.x	-= njSin( CharaAngle ) * Speed * 0.0075f;
				CharaPos.z	-= njCos( CharaAngle ) * Speed * 0.0075f;
			}
			else if ( DefAngle >= -0x3000 && DefAngle <= 0x3000 ) {
				CharaPos.x	-= njSin( CharaAngle ) * Speed * 0.005f;
				CharaPos.z	-= njCos( CharaAngle ) * Speed * 0.005f;
			}
			else {
				CharaPos.x	-= njSin( CharaAngle ) * Speed * 0.0025f;
				CharaPos.z	-= njCos( CharaAngle ) * Speed * 0.0025f;
			}
			#else
			if ( Speed >= 1.0f ) {
				Float				Speed2;
				
				Speed2	= DstX * (-njSin(CharaAngle)) + DstZ * (-njCos(CharaAngle));
				if ( Speed2 < 0.0f ) {
					Speed2	= 0.0f;
				}
				else {
				//	Speed2	= Speed2 * Speed2;
				}
				
				Speed2	*= Speed * 0.01f;
				
				CharaPos.x	+= DstX * Speed2;
				CharaPos.z	+= DstZ * Speed2;
			}
			#endif
			
			if(per->on & PDD_DGT_ST){
				CharaPos.x		= CHARA_POS_X;
				CharaPos.y		= CHARA_POS_Y;
				CharaPos.z		= CHARA_POS_Z;
				CharaAngle		= 0;
			}
			
			#if		0
			njPrint( NJM_LOCATION(1,5),  "Dst      = %g,%g", DstX, DstZ );
			njPrint( NJM_LOCATION(1,6),  "DstAng   = %g", (Float)NJM_ANG_DEG(DstAng) );
			njPrint( NJM_LOCATION(1,7),  "CharaAng = %g", (Float)NJM_ANG_DEG(CharaAngle) );
			njPrint( NJM_LOCATION(1,8),  "DefAng   = %g", (Float)NJM_ANG_DEG(DefAngle) );
			njPrint( NJM_LOCATION(1,9),  "CharaPos = %g,%g,%g", CharaPos.x, CharaPos.y, CharaPos.z );
			njPrint( NJM_LOCATION(1,10), "Speed    = %g", Speed );
			njPrint( NJM_LOCATION(1,11), "FwX,FwZ  = %g,%g", FwX, FwZ );
			njPrint( NJM_LOCATION(1,12), "RtX,RtZ  = %g,%g", RtX, RtZ );
			#endif
		}
		#endif
		
		//	ライトの設定
		#if 0
		njCnkSetEasyLight( 0.0f, -0.7f, -0.7f );
		njCnkSetEasyLightIntensity( 1.0f, 0.0f );
		njCnkSetEasyLightColor( 255, 255, 255 );
		#else
		njCnkSetSimpleLight( 0.0f, -0.7f, -0.7f );
		njCnkSetSimpleLightIntensity( 1.0f, 0.0f );
		njCnkSetSimpleLightColor( 255, 255, 255 );
		#endif
		
		
		njPushMatrix( &_nj_unit_matrix_ );
		njCameraExLookAt( &CameraPos, &CharaPos, &Up );
		
		
		//	描画
		DrawWorldAxis();
		
		njSetTexture( CHARA_TEXLIST );
		
		
		njPushMatrix( NULL );
		njTranslateV( NULL, &CharaPos );
		njRotateY( NULL, CharaAngle + 0x8000 );
		njScale( NULL, 0.8f, 0.8f, 0.8f );
		njCnkSimpleDrawMotion( CHARA_OBJECT , CHARA_MOTION , frame_no );
		njPopMatrix( 1 );
		
		
		njPopMatrix( 1 );
		
		
		
		frame_no	+= 1.0f;
		
		if( frame_no >= CHARA_MOTION->nbFrame - 1.0f ) {
			frame_no	= 0.0f;
		}
	}
}
