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

static float		frame_no;

static NJS_POINT3		CameraPos;


void UFOManDemo( int State )
{
	if ( State == STATE_ATTACH ) {
		nuLoadTexture( UFOMAN_TEXLIST );
		
		njPushMatrix( &_nj_unit_matrix_ );
		
		frame_no	= 0.0f;
	}
	else if ( State == STATE_DETACH ) {
		njPopMatrix( 1 );
		
		njReleaseTexture( UFOMAN_TEXLIST );
	}
	else {
		//njCameraMotion( UFOMAN_CAMERA, UFOMAN_CAMOTION, frame_no );
		
		//	ライトの設定
		njCnkSetEasyLight( 0.0f, -0.7f, -0.7f );
		njCnkSetEasyLightIntensity( 0.8f, 0.2f );
		njCnkSetEasyLightColor( 1.0f, 1.0f, 1.0f );
		
		njCnkSetSimpleLight( 0.0f, -0.7f, -0.7f );
		njCnkSetSimpleLightIntensity( 0.8f, 0.2f );
		njCnkSetSimpleLightColor( 1.0f, 1.0f, 1.0f );
		
		
		{
			const PDS_PERIPHERAL		*per;
			per		= pdGetPeripheral(PDD_PORT_A0);
			
			if(per->on & PDD_DGT_ST){
#if DEBUG
				njPrint( NJM_LOCATION(10,8), "BUTTON START" );
#endif
				njUnitMatrix( NULL );
			}
			
			{
				Float				TransX;
				Float				TransY;
				Float				TransZ;
				
				TransX		= 0.0f;
				TransY		= 0.0f;
				TransZ		= 0.0f;
				
				TransX	= per->x1 * 0.01f;
				TransZ	= per->y1 * 0.01f;
				
#if DEBUG
				njPrint( NJM_LOCATION(10,9), "x=%d,y=%d", per->x1, per->y1 );
#endif				
				if(per->on & PDD_DGT_TY){
#if DEBUG
					njPrint( NJM_LOCATION(10,10), "BUTTON Y" );
#endif

					TransY	= 128.0f * 0.01f;
				}
				if(per->on & PDD_DGT_TA){
#if DEBUG
					njPrint( NJM_LOCATION(10,11), "BUTTON A" );
#endif
					TransY	= -128.0f * 0.01f;
				}
				
				njTranslateCameraEx( TransX, TransY, TransZ );
			}
			
			if(per->on & PDD_DGT_TX){
#if DEBUG
				njPrint( NJM_LOCATION(10,13), "BUTTON X" );
#endif
			}
			
			if(per->on & PDD_DGT_KL){
#if DEBUG
				njPrint( NJM_LOCATION(20,10), "DIGITAL DIR LEFT" );
#endif
				njRotateYCameraEx( 0x80 );
			}
			if(per->on & PDD_DGT_KR){
#if DEBUG
				njPrint( NJM_LOCATION(20,11), "DIGITAL DIR RIGHT" );
#endif
				njRotateYCameraEx( -0x80 );
			}
			if(per->on & PDD_DGT_KU){
#if DEBUG
				njPrint( NJM_LOCATION(20,12), "DIGITAL DIR UP" );
#endif
				njRotateXCameraEx( 0x80 );
			}
			if(per->on & PDD_DGT_KD){
#if DEBUG
				njPrint( NJM_LOCATION(20,13), "DIGITAL DIR DOWN" );
#endif
				njRotateXCameraEx( -0x80 );
			}
			if(per->on & PDD_DGT_TL){
#if DEBUG
				njPrint( NJM_LOCATION(20,14), "BUTTON L" );
#endif
				njRotateZCameraEx( 0x80 );
			}
			if(per->on & PDD_DGT_TR){
#if DEBUG
				njPrint( NJM_LOCATION(20,15), "BUTTON R" );
#endif
				njRotateZCameraEx( -0x80 );
			}
		}
		
		
		njPushMatrix( &_nj_unit_matrix_ );
		MakeMotionCameraMatrix( UFOMAN_OBJECT, UFOMAN_MOTION, frame_no, UFOMAN_ID );
		njGetMatrix( &pTmpMat[0] );
		njPopMatrix( 1 );
		
		
		njPushMatrix( NULL );
		njMultiMatrix( NULL, &pTmpMat[0] );
		
		
		//	描画
		njPushMatrix( NULL );
		//njTranslate( NULL, 0.0f, 0.0f, -2000.0f );
		njSetTexture( UFOMAN_TEXLIST );
		njCnkSimpleDrawShapeMotion( UFOMAN_OBJECT, UFOMAN_MOTION, UFOMAN_SHAPE, frame_no );
		njPopMatrix( 1 );
		
		
		njPopMatrix( 1 );
		
		frame_no	+= 0.5f;
		
		if( frame_no >= UFOMAN_MOTION->nbFrame - 1.0f ) {
			frame_no	= 0.0f;
		}
	}
}
