//-----------------------------------------------------------------------------
// Purpose: Allow pre-frame adjustments on the player
//-----------------------------------------------------------------------------
void CHL1_Player::PreThink(void)
{
	CheckExplosionEffects();
	if ( player_showpredictedposition.GetBool() )
	{
		Vector	predPos;

		UTIL_PredictedPosition( this, player_showpredictedposition_timestep.GetFloat(), &predPos );

		NDebugOverlay::Box( predPos, NAI_Hull::Mins( GetHullType() ), NAI_Hull::Maxs( GetHullType() ), 0, 255, 0, 0, 0.01f );
		NDebugOverlay::Line( GetAbsOrigin(), predPos, 0, 255, 0, 0, 0.01f );
	}

	int buttonsChanged;
	buttonsChanged = m_afButtonPressed | m_afButtonReleased;

	g_pGameRules->PlayerThink( this );

	if ( g_fGameOver || IsPlayerLockedInPlace() )
		return;         // intermission or finale

	ItemPreFrame( );
	WaterMove();

	if ( g_pGameRules && g_pGameRules->FAllowFlashlight() )
		m_Local.m_iHideHUD &= ~HIDEHUD_FLASHLIGHT;
	else
		m_Local.m_iHideHUD |= HIDEHUD_FLASHLIGHT;


	// checks if new client data (for HUD and view control) needs to be sent to the client
	UpdateClientData();
	
	CheckTimeBasedDamage();

	CheckSuitUpdate();

	if (m_lifeState >= LIFE_DYING)
	{
		PlayerDeathThink();
		return;
	}

	// So the correct flags get sent to client asap.
	//
	if ( m_afPhysicsFlags & PFLAG_DIROVERRIDE )
		AddFlag( FL_ONTRAIN );
	else 
		RemoveFlag( FL_ONTRAIN );

	// Train speed control
	if ( m_afPhysicsFlags & PFLAG_DIROVERRIDE )
	{
		CBaseEntity *pTrain = GetGroundEntity();
		float vel;

		if ( pTrain )
		{
			if ( !(pTrain->ObjectCaps() & FCAP_DIRECTIONAL_USE) )
				pTrain = NULL;
		}

		if ( !pTrain )
		{
			if ( GetActiveWeapon() && (GetActiveWeapon()->ObjectCaps() & FCAP_DIRECTIONAL_USE) )
			{
				m_iTrain = TRAIN_ACTIVE | TRAIN_NEW;

				if ( m_nButtons & IN_FORWARD )
				{
					m_iTrain |= TRAIN_FAST;
				}
				else if ( m_nButtons & IN_BACK )
				{
					m_iTrain |= TRAIN_BACK;
				}
				else
				{
					m_iTrain |= TRAIN_NEUTRAL;
				}
				return;
			}
			else
			{
				trace_t trainTrace;
				// Maybe this is on the other side of a level transition
				UTIL_TraceLine( GetAbsOrigin(), GetAbsOrigin() + Vector(0,0,-38), 
					MASK_PLAYERSOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &trainTrace );

				if ( trainTrace.fraction != 1.0 && trainTrace.m_pEnt )
					pTrain = trainTrace.m_pEnt;


				if ( !pTrain || !(pTrain->ObjectCaps() & FCAP_DIRECTIONAL_USE) || !pTrain->OnControls(GetContainingEntity(pev)) )
				{
//					Warning( "In train mode with no train!\n" );
					m_afPhysicsFlags &= ~PFLAG_DIROVERRIDE;
					m_iTrain = TRAIN_NEW|TRAIN_OFF;
					return;
				}
			}
		}
		else if ( !( GetFlags() & FL_ONGROUND ) || pTrain->HasSpawnFlags( SF_TRACKTRAIN_NOCONTROL ) || (m_nButtons & (IN_MOVELEFT|IN_MOVERIGHT) ) )
		{
			// Turn off the train if you jump, strafe, or the train controls go dead
			m_afPhysicsFlags &= ~PFLAG_DIROVERRIDE;
			m_iTrain = TRAIN_NEW|TRAIN_OFF;
			return;
		}

		SetAbsVelocity( vec3_origin );
		vel = 0;
		if ( m_afButtonPressed & IN_FORWARD )
		{
			vel = 1;
			pTrain->Use( this, this, USE_SET, (float)vel );
		}
		else if ( m_afButtonPressed & IN_BACK )
		{
			vel = -1;
			pTrain->Use( this, this, USE_SET, (float)vel );
		}

		if (vel)
		{
			m_iTrain = TrainSpeed(pTrain->m_flSpeed, ((CFuncTrackTrain*)pTrain)->GetMaxSpeed());
			m_iTrain |= TRAIN_ACTIVE|TRAIN_NEW;
		}
	} 
	else if (m_iTrain & TRAIN_ACTIVE)
	{
		m_iTrain = TRAIN_NEW; // turn off train
	}

	// THIS CODE DOESN'T SEEM TO DO ANYTHING!!!
	// WHY IS IT STILL HERE? (sjb)
	if (m_nButtons & IN_JUMP)
	{
		// If on a ladder, jump off the ladder
		// else Jump
		Jump();
	}

	// If trying to duck, already ducked, or in the process of ducking
	if ((m_nButtons & IN_DUCK) || (GetFlags() & FL_DUCKING) || (m_afPhysicsFlags & PFLAG_DUCKING) )
		Duck();

	//
	// If we're not on the ground, we're falling. Update our falling velocity.
	//
	if ( !( GetFlags() & FL_ONGROUND ) )
	{
		m_Local.m_flFallVelocity = -GetAbsVelocity().z;
	}

	if ( m_afPhysicsFlags & PFLAG_ONBARNACLE )
	{
		SetAbsVelocity( vec3_origin );
	}
	// StudioFrameAdvance( );//!!!HACKHACK!!! Can't be hit by traceline when not animating?

	//Find targets for NPC to shoot if they decide to miss us
	FindMissTargets();
}
Esempio n. 2
0
int main()
{
	//
	//		
	//	Because I need it to randomize 
	//	duck's vector and spikes 
	//
	srand(time(NULL));

	//
	//	Create Window 
	//	
	sf::VideoMode videoMode(WINDOW_X, WINDOW_Y);
	sf::RenderWindow window(videoMode, WINDOW_TITLE, sf::Style::Titlebar);


	//
	// Set Window max FPS to 100
	// and play music in background
	//
	window.setFramerateLimit(100);
	sf::Music music;
	music.openFromFile( MUSIC_FOLDER "Courage.ogg" );
	music.play();



	//
	//		
	//	Create window border: line by line. 
	//
	// line thickness 10px;
	// color Black
	//
	
	sf::RectangleShape lineTOP;
	lineTOP.setFillColor(sf::Color::Black);
	lineTOP.setPosition(sf::Vector2f(0, 0));
	lineTOP.setSize(sf::Vector2f(300, 10));

	sf::RectangleShape lineLEFT;
	lineLEFT.setFillColor(sf::Color::Black);
	lineLEFT.setPosition(sf::Vector2f(0, 0));
	lineLEFT.setSize(sf::Vector2f(10, 450));

	sf::RectangleShape lineRIGHT;
	lineRIGHT.setFillColor(sf::Color::Black);
	lineRIGHT.setPosition(sf::Vector2f(290, 0));
	lineRIGHT.setSize(sf::Vector2f(10, 440));


	sf::RectangleShape LineBOTTOM;
	LineBOTTOM.setFillColor(sf::Color::Black);
	LineBOTTOM.setPosition(sf::Vector2f(0, 440));
	LineBOTTOM.setSize(sf::Vector2f(300, 10));




	//
	//		
	//	Create Circle
	//  With color RGB(242,242,242) and alpha 199 where alhpa is between 0 and 255;
	//  
	//  Circle radius : 100px;
	//  Circle position: center of window
	sf::CircleShape circle(100);
	circle.setFillColor(sf::Color(242, 242, 242, 199));
	circle.setOrigin(sf::Vector2f(100, 100));
	circle.setPosition(sf::Vector2f(WINDOW_X / 2, WINDOW_Y / 2));


	
	
	//
	//		
	//	Create 10 el array of colors
	//
	sf::Color colors[10];
	colors[0] = sf::Color(209, 250, 142);
	colors[1] = sf::Color(168, 255, 142);
	colors[2] = sf::Color(250, 255, 142);
	colors[3] = sf::Color(255, 189, 190);
	colors[4] = sf::Color(255, 189, 223);
	colors[5] = sf::Color(221, 189, 255);
	colors[6] = sf::Color(189, 190, 255);
	colors[7] = sf::Color(195, 195, 216);
	colors[8] = sf::Color(213, 213, 213);
	colors[9] = sf::Color(233, 255, 142);



	
	
	//
	// Load Font from file to font object;
	//
	// if font not exist them return FONT_NOT_FOUND;
	sf::Font font;
	if (!font.loadFromFile(WINDOW_FONT)) return FONT_NOT_FOUND;




	
	currentCOLOR = colors[0]; // set starts color
	colorbyPoint = colors[0]; // set starts color


	//
	// Create and initialize Duck object
	// 
	// @param App wios
	// @param array of randomized bool values 
	// 
	CDuck Duck(window, SpikeTab);



	// player is playing ? 	
	bool Game = false;


	//
	//		
	//	Main Game loop 
	//  executing until player don't close window.
	//
	//
	//
	while (window.isOpen())
	{


	
		//
		//  Text object 
		// @param current points 
		// @param loaded font;
		sf::Text circleText(points, font);
		setFadeColor(currentCOLOR, colorbyPoint, window, circleText);


		//
		//	Keys Support 	
		//
		sf::Event event;
		while (window.pollEvent(event))
		{
			if ((event.type == sf::Event::Closed) || (event.type == sf::Event::KeyPressed))
			{
				if (event.key.code == sf::Keyboard::Escape)  window.close();
				if (event.key.code == sf::Keyboard::Space && Game == true) Duck.Jump();
				if (event.key.code == sf::Keyboard::Space && Game == false) Game = true;
			}


		} // end While window.pollEvent


		//
		//		
		//	If player is playing
		//
		if (Game == true)
		{


			//
			// set text size to 102 		
			// set text pos to center of window
			// set text origin 
			circleText.setCharacterSize(102);
			circleText.setPosition(sf::Vector2f(WINDOW_X / 2, WINDOW_Y / 2 -25));
			circleText.setOrigin((circleText.getLocalBounds().width / 2) + 5, (circleText.getLocalBounds().height / 2));




			//
			// from unsigned to string;
			// if uPoints < 10 add 0 to string;
			points = (uPoints < 10 ? "0" + std::to_string(uPoints) : std::to_string(uPoints));


			//
			// Change next background
			//

			switch (uPoints)
			{
			case 5:
				colorbyPoint = colors[1];
				break;
			case 10:
				colorbyPoint = colors[2];
				break;
			case 15:
				colorbyPoint = colors[3];
				break;
			case 20:
				colorbyPoint = colors[4];
				break;
			case 25:
				colorbyPoint = colors[5];
				break;
			case 30:
				colorbyPoint = colors[6];
				break;
			case 40:
				colorbyPoint = colors[7];
				break;
			case 45:
				colorbyPoint = colors[8];
				break;
			case 50:
				colorbyPoint = colors[9];
				break;

			default:
				break;
			}




			//
			//	Create Left or Right spikes	
			//
			//
			for (int z = 0; z < static_cast<unsigned>(WINDOW_Y /  SPIKE_H) - 1; z++)
			{
 
 
				if (SpikeTab[z] == true)
				{

					//
					//	Create spikes  	
					// 
					// @param Window
					// @param enum Position  
					// @param x cord
					// @param y cord
					CSpike Spike(window, (Duck.cSide == 0 ? Right : Left), 10, (z * 41) + 11);
					//if (z != static_cast<unsigned>(WINDOW_Y /  SPIKE_H) - 1)
						Spike.Update();
						
						
					//
					//	Detect collision between Duck and Spike	
					//
					if (Collision::PixelPerfectTest(Duck.model, Spike.model))
					{
						Game = false; // player lose
						Duck.model.setPosition(WINDOW_X / 2, WINDOW_Y / 2); // set model pos to center
						Duck.cSide = rand() % 2; // rand duck's vector
						uPoints = 0; // set 0 points
						Duck.modelMove.y = 0; // set grav to 0
						Duck.RandSpike(); // randomize Spikes
						currentCOLOR = colors[0]; // set current color to default
					}

				}


			}


			
			for (int z = 0; z <= static_cast<unsigned>(WINDOW_X /   SPIKE_H); z++)
			{

				//
				// Top and bottom spikes
				//
				CSpike Spike2(window, Top, (z *   SPIKE_H) + 10, 10);
				Spike2.Update();

				CSpike Spike3(window, Bottom, (z *   SPIKE_H) + 10, WINDOW_Y - 10);
				if (z != static_cast<unsigned>(WINDOW_X /   SPIKE_H))
					Spike3.Update();


					
				if (Collision::PixelPerfectTest(Duck.model, Spike2.model) || Collision::PixelPerfectTest(Duck.model, Spike3.model))
				{
					Game = false;
					Duck.model.setPosition(WINDOW_X / 2, WINDOW_Y / 2);
					Duck.cSide = rand() % 2;
					uPoints = 0;
					Duck.modelMove.y = 0;
					Duck.RandSpike();
					currentCOLOR = colors[0];
				}

			}


		} // if GAME true
		else
		{
			//
			//	Create Start Screen
			//  
			//
			sf::Texture start_modelTeX; // Texture file 
			sf::Sprite start_model;	      // Model 

			
			
			// Load model
			// set texture
			//  position
			// set origin
			if (!start_modelTeX.loadFromFile(TEXTURE_FOLDER "logo.png")) exit(TEXTURE_NOT_FOUND);
			start_model.setTexture(start_modelTeX);
			start_model.setTextureRect(sf::IntRect(0, 0, 274, 291));
			start_model.setOrigin(274 / 2, 291 / 2);
			start_model.setPosition(WINDOW_X / 2, (WINDOW_Y / 2));


			
			
			// score text
			
			circleText.setCharacterSize(50);
			circleText.setPosition(sf::Vector2f(WINDOW_X / 2, WINDOW_Y / 2  -25));
			circleText.setOrigin((circleText.getLocalBounds().width / 2) + 5, (circleText.getLocalBounds().height / 2));


			
			
			//
			//
			window.draw(start_model);
			window.draw(circleText);

		} // else game false


		
		
		//
		//
		window.draw(lineTOP);
		window.draw(lineLEFT);
		window.draw(lineRIGHT);
		window.draw(LineBOTTOM);



		if (Game == true)
		{
			// layer with circle and text on it	
			window.draw(circle);
			window.draw(circleText);


			// firs layer : ONLY MOVING OBJECT
			Duck.Update();
		}


		window.display();
	} // end while window.isOpen
	return EXIT_SUCCESS;
}
Esempio n. 3
0
void CBasePlayer::PreThink()
{
	const int buttonsChanged = ( m_afButtonLast ^ GetButtons().Get() );	// These buttons have changed this frame

	// Debounced button codes for pressed/released
	// UNDONE: Do we need auto-repeat?
	m_afButtonPressed = buttonsChanged & GetButtons().Get();		// The changed ones still down are "pressed"
	m_afButtonReleased = buttonsChanged & ( ~GetButtons().Get() );	// The ones not down are "released"

	g_pGameRules->PlayerThink( this );

	bool bCheckVehicles = true;

#if USE_ANGELSCRIPT
	uint32_t uiFlags = PreThinkFlag::NONE;

	CallGlobalEvent( g_PlayerPreThinkEvent, CallFlag::NONE, this, &uiFlags );

	bCheckVehicles = !( uiFlags & PreThinkFlag::SKIP_VEHICLES );
#endif

	if( g_fGameOver )
		return;         // intermission or finale

	UTIL_MakeVectors( GetViewAngle() );             // is this still used?

	ItemPreFrame();
	WaterMove();

	if( g_pGameRules && g_pGameRules->FAllowFlashlight() )
		m_iHideHUD &= ~HIDEHUD_FLASHLIGHT;
	else
		m_iHideHUD |= HIDEHUD_FLASHLIGHT;


	// JOHN: checks if new client data (for HUD and view control) needs to be sent to the client
	UpdateClientData();

	CheckTimeBasedDamage();

	CheckSuitUpdate();

	// Observer Button Handling
	if( IsObserver() )
	{
		Observer_HandleButtons();
		Observer_CheckTarget();
		Observer_CheckProperties();
		SetImpulse( 0 );
		return;
	}

	if( GetDeadFlag() >= DEAD_DYING )
	{
		PlayerDeathThink();
		return;
	}

	// So the correct flags get sent to client asap.
	//
	if( m_afPhysicsFlags & PFLAG_ONTRAIN )
		GetFlags() |= FL_ONTRAIN;
	else
		GetFlags().ClearFlags( FL_ONTRAIN );

	if( bCheckVehicles )
	{
#if USE_OPFOR
	//We're on a rope. - Solokiller
	if( m_afPhysicsFlags & PFLAG_ONROPE && m_pRope )
	{
		SetAbsVelocity( g_vecZero );

		const Vector vecAttachPos = m_pRope->GetAttachedObjectsPosition();

		SetAbsOrigin( vecAttachPos );

		Vector vecForce;

		/*
		//TODO: This causes sideways acceleration that doesn't occur in Op4. - Solokiller
		//TODO: should be IN_MOVERIGHT and IN_MOVELEFT - Solokiller
		if( GetButtons().Any( IN_DUCK ) )
		{
			vecForce.x = gpGlobals->v_right.x;
			vecForce.y = gpGlobals->v_right.y;
			vecForce.z = 0;
			
			m_pRope->ApplyForceFromPlayer( vecForce );
		}

		if( GetButtons().Any( IN_JUMP ) )
		{
			vecForce.x = -gpGlobals->v_right.x;
			vecForce.y = -gpGlobals->v_right.y;
			vecForce.z = 0;
			m_pRope->ApplyForceFromPlayer( vecForce );
		}
		*/

		//Determine if any force should be applied to the rope, or if we should move around. - Solokiller
		if( GetButtons().Any( IN_BACK | IN_FORWARD ) )
		{
			if( ( gpGlobals->v_forward.x * gpGlobals->v_forward.x + 
				gpGlobals->v_forward.y * gpGlobals->v_forward.y - 
				gpGlobals->v_forward.z * gpGlobals->v_forward.z ) <= 0.0 )
			{
				if( m_bIsClimbing )
				{
					const float flDelta = gpGlobals->time - m_flLastClimbTime;
					m_flLastClimbTime = gpGlobals->time;
					if( GetButtons().Any( IN_FORWARD ) )
					{
						if( gpGlobals->v_forward.z < 0.0 )
						{
							if( !m_pRope->MoveDown( flDelta ) )
							{
								//Let go of the rope, detach. - Solokiller
								SetMoveType( MOVETYPE_WALK );
								SetSolidType( SOLID_SLIDEBOX );

								m_afPhysicsFlags &= ~PFLAG_ONROPE;
								m_pRope->DetachObject();
								m_pRope = nullptr;
								m_bIsClimbing = false;
							}
						}
						else
						{
							m_pRope->MoveUp( flDelta );
						}
					}
					if( GetButtons().Any( IN_BACK ) )
					{
						if( gpGlobals->v_forward.z < 0.0 )
						{
							m_pRope->MoveUp( flDelta );
						}
						else if( !m_pRope->MoveDown( flDelta ) )
						{
							//Let go of the rope, detach. - Solokiller
							SetMoveType( MOVETYPE_WALK );
							SetSolidType( SOLID_SLIDEBOX );
							m_afPhysicsFlags &= ~PFLAG_ONROPE;
							m_pRope->DetachObject();
							m_pRope = nullptr;
							m_bIsClimbing = false;
						}
					}
				}
				else
				{
					m_bIsClimbing = true;
					m_flLastClimbTime = gpGlobals->time;
				}
			}
			else
			{
				vecForce.x = gpGlobals->v_forward.x;
				vecForce.y = gpGlobals->v_forward.y;
				vecForce.z = 0.0;
				if( GetButtons().Any( IN_BACK ) )
				{
					vecForce.x = -gpGlobals->v_forward.x;
					vecForce.y = -gpGlobals->v_forward.y;
					vecForce.z = 0;
				}
				m_pRope->ApplyForceFromPlayer( vecForce );
				m_bIsClimbing = false;
			}
		}
		else
		{
			m_bIsClimbing = false;
		}

		if( m_afButtonPressed & IN_JUMP )
		{
			//We've jumped off the rope, give us some momentum - Solokiller
			SetMoveType( MOVETYPE_WALK );
			SetSolidType( SOLID_SLIDEBOX );
			this->m_afPhysicsFlags &= ~PFLAG_ONROPE;

			Vector vecDir = gpGlobals->v_up * 165.0 + gpGlobals->v_forward * 150.0;

			Vector vecVelocity = m_pRope->GetAttachedObjectsVelocity() * 2;

			vecVelocity.NormalizeInPlace();

			vecVelocity = vecVelocity * 200;

			SetAbsVelocity( vecVelocity + vecDir );

			m_pRope->DetachObject();
			m_pRope = nullptr;
			m_bIsClimbing = false;
		}
		return;
	}
#endif

		// Train speed control
		if( m_afPhysicsFlags & PFLAG_ONTRAIN )
		{
			CBaseEntity *pTrain = GetGroundEntity();

			//To match original behavior, Instance returns the world if entity is null - Solokiller
			if( !pTrain )
				pTrain = CWorld::GetInstance();

			float vel;

			if( !pTrain )
			{
				TraceResult trainTrace;
				// Maybe this is on the other side of a level transition
				UTIL_TraceLine( GetAbsOrigin(), GetAbsOrigin() + Vector( 0, 0, -38 ), ignore_monsters, ENT( pev ), &trainTrace );

				// HACKHACK - Just look for the func_tracktrain classname
				if( trainTrace.flFraction != 1.0 && trainTrace.pHit )
					pTrain = CBaseEntity::Instance( trainTrace.pHit );


				if( !pTrain || !( pTrain->ObjectCaps() & FCAP_DIRECTIONAL_USE ) || !pTrain->OnControls( this ) )
				{
					//ALERT( at_error, "In train mode with no train!\n" );
					m_afPhysicsFlags &= ~PFLAG_ONTRAIN;
					m_iTrain = TRAIN_NEW | TRAIN_OFF;
					return;
				}
			}
			else if( !GetFlags().Any( FL_ONGROUND ) || pTrain->GetSpawnFlags().Any( SF_TRACKTRAIN_NOCONTROL ) || ( GetButtons().Any( IN_MOVELEFT | IN_MOVERIGHT ) ) )
			{
				// Turn off the train if you jump, strafe, or the train controls go dead
				m_afPhysicsFlags &= ~PFLAG_ONTRAIN;
				m_iTrain = TRAIN_NEW | TRAIN_OFF;
				return;
			}

			SetAbsVelocity( g_vecZero );
			vel = 0;
			if( m_afButtonPressed & IN_FORWARD )
			{
				vel = 1;
				pTrain->Use( this, this, USE_SET, ( float ) vel );
			}
			else if( m_afButtonPressed & IN_BACK )
			{
				vel = -1;
				pTrain->Use( this, this, USE_SET, ( float ) vel );
			}

			if( vel )
			{
				m_iTrain = TrainSpeed( pTrain->GetSpeed(), pTrain->GetImpulse() );
				m_iTrain |= TRAIN_ACTIVE | TRAIN_NEW;
			}

		}
		else if( m_iTrain & TRAIN_ACTIVE )
			m_iTrain = TRAIN_NEW; // turn off train
	}

	if( GetButtons().Any( IN_JUMP ) )
	{
		// If on a ladder, jump off the ladder
		// else Jump
		Jump();
	}


	// If trying to duck, already ducked, or in the process of ducking
	if( GetButtons().Any( IN_DUCK ) || GetFlags().Any( FL_DUCKING ) || ( m_afPhysicsFlags & PFLAG_DUCKING ) )
		Duck();

	if( !GetFlags().Any( FL_ONGROUND ) )
	{
		m_flFallVelocity = -GetAbsVelocity().z;
	}

	// StudioFrameAdvance( );//!!!HACKHACK!!! Can't be hit by traceline when not animating?

	// Clear out ladder pointer
	m_hEnemy = NULL;

	if( m_afPhysicsFlags & PFLAG_ONBARNACLE )
	{
		SetAbsVelocity( g_vecZero );
	}
}