www.pudn.com > sxdl.zip > Player.cpp


#include "Player.h" 
#include "Game.h" 
 
extern Game game ; 
 
CLife::CLife ( CBasicSprite * Sprite, int x , int y , int cx , int cy )  
{ 
	Mass     = 0.0f ;  
	Position = Vector3 ( ( float )  x , ( float )  y , 0.0f ) ;  
	Scale    = Vector3 ( ( float ) cx , ( float ) cy , 0.0f ) ;  
	//LifeTime = -1.0f; 
	//InitialScale  = Scale;  
	WillRenderLike ( Sprite ) ;  
	//DoWorldMatrix ( ) ;  
	Activate (true , true)  ;  
}  
 
/*bool CLife::OnAnimate ( float ElapsedTime , float AbsoluteTime )  
{ 
// Si on vient de perdre une vie le coeur retrecit avant de disparaitre 
//	if (LifeTime > 0.0f) 
//		Scale = InitialScale * ( 1.0f - (Age/LifeTime) ); 
	 
// 	Pas de gestion supplementaire, on veut pas que ca bouge ! 
	return (false); 
}*/ 
 
///////////////////////////////////: 
 
 
CPlayer::CPlayer( CMovieSprite * * Sprites, int PIndex ) 
{ 
	//Mass = 200; 
	Family   = ( int ) Game::FPlayer ; 
	//Position = Vector3(  0.0f, -300.0f, 0.0f);  
	if (PIndex == 0) 
		Position = Vector3( -100.0f, -277.0f, 0.0f);  
	else 
		Position = Vector3(  100.0f, -277.0f, 0.0f);  
	Scale = Vector3 ( 30.0f , 32.0f , 0.0f ); 
 
// Index du joueur 0 ou 1	 
	PlayerIndex = PIndex; 
	 
// Nombre de point de vie	 
	HitPoints = 4; 
	for(int i=0;i 0.0f ) OnUp ( ) ;  
		if ( Value < 0.0f ) OnDown ( ) ;  
	} ; */ 
 
//vers la gauche ou la droite 
	if ( ( VirtualKey == CInput::LeftRight_1 ) || ( VirtualKey == CInput::LeftRight_2 ) )  
	{ 
		if ( Value > 0.0f ) OnLeft ( ) ;  
		if ( Value < 0.0f ) OnRight ( ) ;  
	} ;  
	 
//Fire !!!	mais seulement si il ya moins de 3 projectile deja lancé 
	if ( ( VirtualKey == CInput::FirePrimary || VirtualKey == CInput::FireSecondary) && ProjectileLaunched < 3) 
	{ 
		new CProjectile( game.ProjectileSprites, this ); 
		// on affiche le joueur en train de tirer 
		WillRenderLike ( SSprite [ 3 ] , 0.250 ); 
		// on bouge pas quand on tire ! 
		Velocity.x = 0.0f; 
	} 
}  
 
inline void CPlayer::OnUp ( void )  
{ 
	if ( Moving != Up )  
	{  
		// On change de direction -> on change de sprite 
		Moving = Up ;  
		WillRenderLike ( SSprite [ 3 ] , 0.250 ) ;  
		_Sprite = SSprite [ 3 ]; 
	} ; 
// Deplacement 
	//Velocity = 50.0f * ( Vector3 ( 0.0f , 1.0f , 0.0f ) ) ;  
	Velocity.y = 50.0f; 
}  
inline void CPlayer::OnDown ( void )  
{ 
	if ( Moving != Down )  
	{  
		// On change de direction -> on change de sprite		 
		Moving = Down ;  
		WillRenderLike ( SSprite [ 2 ] , 0.250 ) ;  
		_Sprite = SSprite [ 2 ]; 
	} ; 
// Deplacement	 
	//Velocity = 50.0f * ( Vector3 ( 0.0f , -1.0f , 0.0f ) ) ;  
	Velocity.y = -50.0f; 
}  
inline void CPlayer::OnLeft ( void )  
{ 
	if ( Moving != Left )  
	{  
		// On change de direction -> on change de sprite		 
		Moving = Left ;  
		WillRenderLike ( SSprite [ 0 ] , 0.250 ) ;  
		_Sprite = SSprite [ 0 ]; 
	} ; 
// Deplacement		 
	//Velocity = 50.0f * ( Vector3 ( -1.0f , 0.0f , 0.0f ) ) ;  
	Velocity.x = -MaxSpeed; 
}  
inline void CPlayer::OnRight ( void )  
{ 
	if ( Moving != Right )  
	{  
		// On change de direction -> on change de sprite		 
		Moving = Right ;  
		WillRenderLike ( SSprite [ 1 ] , 0.250 ) ;  
		_Sprite = SSprite [ 1 ]; 
	} ; 
// Deplacement		 
	//Velocity = 50.0f * ( Vector3 ( 1.0f , 0.0f , 0.0f ) ) ; 
	Velocity.x = MaxSpeed; 
}  
 
bool CPlayer::OnAnimate ( float ElapsedTime , float AbsoluteTime )  
{ 
	//  on bouge plus si on avance trop peu 
	if ( Speed ( ) < 2.5f )  
	{ 
		//on bouge pas 
		Moving = Idle ;  
		// Plus d'animation 
		SetRenderStyle ( 0.0f ) ;  
	} ;  
 
// On ralenti peu à peu 
// (Code repris de CEntity) 
	Vector3 Drag ;   
	Vector3 Force = Vector3 ( 0.0f , 0.0f , 0.0f ) ; 
	D3DXVec3Normalize ( & Drag , & Velocity ) ;  
	float Length = D3DXVec3Length ( & Velocity ) * 3.5f ;  
	Force -= Drag * Length ;  
	Velocity += Force * ElapsedTime ;  
	 
//  Si on avance et qu'on a pas la bonne sprite (à cause du tir) on change 
	if ( Speed ( ) >= 2.5f && Invincible != true) 
	{ 
		if (GetRenderer() != _Sprite) 
			WillRenderLike ( _Sprite , 0.250 ) ; 
	}	 
	 
// Si on est invicible, on clignote une fois sur deux 
	 
	if (AbsoluteTime - Time >= 0.1f && Invincible == true) 
	{ 
		Time = AbsoluteTime; 
		if (count == 0 ) 
		{ 
			count = 1 - count; 
			WillRenderLike (game.NullRenderer);  
		} 
		else 
		{ 
			WillRenderLike ( _Sprite , 0.250 ) ; 
			count = 1 - count; 
		} 
	} 
 
	return true ;  
}  
 
 
void CPlayer::OnCollide ( CEntity *  CollidingEntity )  
{ 
// Si on se fais toucher par une balle et qu'on est pas invincible	 
	if ( CollidingEntity->Family == Game::FBall && Invincible != true )  
	{ 
		CBall * Ball = ( CBall * ) CollidingEntity ;  
		// On ne se fais toucher que par les vrais balles (pas les residus) 
		if (Ball->Mass > 0) 
		{ 
			// Aïe ca fais mal 
			HitPoints--; 
			// on perd un coeur 
			if (HitPoints >= 0) 
			{ 
				Life[(int)HitPoints]->LifeTime = 0.3f; 
				//Life[(int)HitPoints]->DoAging(0.0f); 
				Life[(int)HitPoints]->Age = 0.0f; 
			} 
 
			// on ne peux plus se faire toucher 
			Invincible = true; 
			// mais seulement 0.8 seconde  
			// Je sais, c'est pas beau, je tacherais de faire mieux plus tard 
			if (PlayerIndex == 0) 
				game.Timing.SetTimer ( Game::P1NoInvincible , 0.8f , true ) ; 
			else 
				game.Timing.SetTimer ( Game::P2NoInvincible , 0.8f , true ) ; 
		} 
	} 
//Pour pas qu'on traverse le mur 
	else if ( CollidingEntity->Family == Game::FWall )  
	{ 
		CWall * Wall = ( CWall * ) CollidingEntity ;  
		Position += 2.0f * Wall->Normal ;  
	} 
//On ne traverse pas l'autre joueur, chacun de son coté !!!	 
	else if ( CollidingEntity->Family == Game::FPlayer )  
	{ 
		//CPlayer * Player = ( CPlayer * ) CollidingEntity ; 
	//On bouge plus 
		//Player->Velocity = 0.0f; 
		Velocity = Vector3 ( 0.0f , 0.0f , 0.0f ); 
	}	 
//On prend un bonus 
	else if ( CollidingEntity->Family == Game::FBonus )  
	{ 
		CBonus * Bonus = ( CBonus * ) CollidingEntity ; 
		int type = (int)Bonus->Mass - 100; 
		if (Bonus->LifeTime != 0.0f) 
		{ 
			if (type == 0 && HitPoints < 15) 
			{ 
				Life[(int)HitPoints] = new CLife ( game.LifeSprites, -350+700*(PlayerIndex) , -320+40*((int)HitPoints+1), 32 , 32 ) ; 
				HitPoints++; 
 
			} 
			else if (type == 1) 
			{ 
				MaxSpeed *= 1.02f; 
			} 
			Bonus->LifeTime = 0.0f; 
		} 
	}		 
	 
} 	 
 
void CPlayer::OnDying ( void )  
{ 
// Mes yeux se brouille, je ne vois plus rien, arrrrggggg je meurs	... 
	WillRenderLike ( game.NullRenderer ) ; 
	// C'est fini... 
	//game.GameMode = false ;  
	game.Timing.SetTimer ( Game::Dead , 0.1f ) ; 	 
} 
 
 
//////////////////////////////////////// 
 
 
CProjectile::CProjectile ( CBasicSprite * Sprite , CPlayer * Player)  
{  
	Family   = ( int ) Game::FProjectile ; 
	 
	PlayerParent = Player; 
	PlayerParent->ProjectileLaunched++; 
	 
// Le projectile part de la position du joueur	 
	Position = Player->Position ;  
	Position.y += 16; 
 
// Il va vers le haut		 
	Velocity.x = 0.0f; 
	Velocity.y = 700.0f; 
	Velocity.z = 0.0f; 
 
	Scale = Vector3 ( 7.0f , 16.0f , 0.0f ); 
 
	PlayerParent->point -= 2; 
	 
	WillRenderLike ( Sprite ) ;  
	Activate ( ) ;  
} ;	 
	 
bool CProjectile::OnAnimate ( float ElapsedTime , float AbsoluteTime )   
{	 
// Sil ill est trop haut on le fais disparaitre	 
	if (Position.y > 280.0f) 
	{ 
		LifeTime = 0.0f; 
		PlayerParent->ProjectileLaunched--; 
 
		for(int i=0;i<6;i++) 
			new CEclat ( game.EclatSprites, (int)Position.x , 290 , 5 , 5) ; 
			//new CEclat ( game.EclatSprites, (int)Position.x , (int)Position.y+8 , 5 , 5) ; 
	} 
	 
	return true ;  
}  
 
 
void CProjectile::OnCollide ( CEntity *  CollidingEntity  )  
{ 
// Si on touche une balle	 
	if ( CollidingEntity->Family == Game::FBall )  
	{ 
		CBall * Ball = ( CBall * ) CollidingEntity ;  
		 
		// On ne touche que les vrais balles (pas les residus) 
		if (Ball->Mass > 0) 
		{ 
			if (LifeTime != 0.0f) 
			{ 
				// La balle doit mourrir			 
				Ball->LifeTime = 0.0f; 
				PlayerParent->ProjectileLaunched--; 
 
				PlayerParent->point += 12; 
			} 
	//	Le projectile disparait (un projectile ne peut toucher qu'une seule balle) 
			LifeTime = 0.0f; 
		} 
	} 
} 	 
 
///////////////////////: 
 
CEclat::CEclat ( CBasicSprite * Sprite, int x , int y , int cx , int cy)  
{ 
	Mass     = 100.0f;  
	Position = Vector3 ( ( float )  x , ( float )  y , 0.0f ) ;  
	Scale    = Vector3 ( ( float ) cx , ( float ) cy , 0.0f ) ;  
	LifeTime = 0.4f; 
	InitialScale  = Scale;  
	WillRenderLike ( Sprite , 0.08f) ;  
	//DoWorldMatrix ( ) ;  
 
	Velocity.x += game.Tools.randf ( -15.0f , 15.0f ) ; 
	Velocity.y += game.Tools.randf ( -15.0f , -5.0f ) ; 
	Activate ()  ;  
}  
 
 
bool CEclat::OnAnimate ( float ElapsedTime , float AbsoluteTime )  
{ 
	if (Age > 0.3f) 
		Scale = InitialScale * (LifeTime-Age) * 10 ; 
	 
	return (true); 
}