⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app.cs

📁 Introduction to 3d game engine design一书的源代码!
💻 CS
📖 第 1 页 / 共 2 页
字号:
		// action functions

		/// <summary>
		/// Action to start playing
		/// </summary>
		public void Play()
		{
			m_State = GameState.GamePlay;
			GameEngine.Console.Reset();
		}

		/// <summary>
		/// Action to terminate the application
		/// </summary>
		public void Terminate()
		{
			m_bTerminate = true;
		}

		/// <summary>
		/// screen capture
		/// </summary>
		public void ScreenCapture()
		{
			m_bScreenCapture = true;
		}

		/// <summary>
		/// version of terminate for use by the console
		/// </summary>
		/// <param name="sData"></param>
		public void TerminateCommand( string sData )
		{
			Terminate();
		}

		/// <summary>
		/// Toggle the display of statistics information
		/// </summary>
		/// <param name="sData"></param>
		public void ToggleStatistics( string sData )
		{
			m_bShowStatistics = !m_bShowStatistics;
		}

		/// <summary>
		/// Action to transition to the next game state based on a mapper action
		/// </summary>
		public void NextState()
		{
			if ( m_State < GameState.AfterActionReview ) 
			{
				m_State++;
				if ( m_State == GameState.GamePlay )
				{
					m_ownship = (Ownship)Engine.GetObject("car1");
					m_ownship.Driving = true;
				}
			} 
			else 
			{
				m_State = GameState.OptionsMain;
			}
		}

		public void PointCamera( int count )
		{
			m_Engine.MoveCamera(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, count);
		}

		public void PitchCamera( int count )
		{
			m_Engine.MoveCamera(0.0f, 0.0f, 0.0f, count * 0.1f, 0.0f, 0.0f);
		}

		public void MoveCameraXP()
		{
			m_Engine.MoveCamera(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
		}

		public void MoveCameraXM()
		{
			m_Engine.MoveCamera(-0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
		}

		public void MoveCameraY()
		{
			m_Engine.MoveCamera(0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f);
		}

		public void MoveCameraZP()
		{
			m_Engine.MoveCamera(0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f);
		}

		public void MoveCameraZM()
		{
			m_Engine.MoveCamera(0.0f, 0.0f, -0.5f, 0.0f, 0.0f, 0.0f);
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void RestoreDeviceObjects(System.Object sender, System.EventArgs e)
		{
			// Restore the device objects for the meshes and fonts

			// Set the transform matrices (view and world are updated per frame)
			Matrix matProj;
			float fAspect = device.PresentationParameters.BackBufferWidth / (float)device.PresentationParameters.BackBufferHeight;
			matProj = Matrix.PerspectiveFovLH( (float)Math.PI/4, fAspect, 1.0f, 100.0f );
			device.Transform.Projection = matProj;

			// Set up the default texture states
			device.TextureState[0].ColorOperation = TextureOperation.Modulate;
			device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
			device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
			device.TextureState[0].AlphaOperation = TextureOperation.SelectArg1;
			device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
			device.SamplerState[0].MinFilter = TextureFilter.Linear;
			device.SamplerState[0].MagFilter = TextureFilter.Linear;
			device.SamplerState[0].MipFilter = TextureFilter.Linear;
			device.SamplerState[0].AddressU = TextureAddress.Clamp;
			device.SamplerState[0].AddressV = TextureAddress.Clamp;

			device.RenderState.DitherEnable = true;
		}




		/// <summary>
		/// Called when the app is exiting, or the device is being changed, this 
		/// function deletes any device-dependent objects.
		/// </summary>
		protected override void DeleteDeviceObjects(System.Object sender, System.EventArgs e)
		{
			m_Engine.Dispose();
			m_Console.Dispose();
		}

		public void LoadOptions()
		{
			try
			{
				System.Random rand = new System.Random();
				// loading of options will happen here
				m_Engine.SetTerrain(200,200,@"..\..\Resources\heightmap.jpg",@"..\..\Resources\sand1.jpg", 10.0f, 0.45f);

				for ( int i=0; i<150; i++ )
				{
					float north = (float)(rand.NextDouble() * 1900.0);
					float east  = (float)(rand.NextDouble() * 1900.0);
					BillBoard.Add( east, north, 0.0f, "cactus"+i, @"..\..\Resources\cactus.dds",1.0f, 1.0f);
				}
				for ( int i=0; i<150; i++ )
				{
					float north = (float)(rand.NextDouble() * 1900.0);
					float east  = (float)(rand.NextDouble() * 1900.0);
					BillBoard.Add( east, north, 0.0f, "tree"+i, @"..\..\Resources\palmtree.dds",6.5f, 10.0f);
				}
				GameEngine.Console.AddLine("all trees loaded");

				//			m_Engine.AddObject( new ParticleGenerator("Spray1", 2000, 2000, Color.Yellow, "Particle.bmp", new ParticleUpdate(Gravity)));

				double j = 0.0;
				double center_x = 1000.0;
				double center_z = 1000.0;
				double radius = 700.0;
				double width = 20.0;

				m_flag = new Cloth("flag", @"..\..\Resources\flag.jpg", 2, 2, 0.1, 1.0f);
				m_flag.Height = 0.6f;
				m_flag.North = 2.0f;
				m_flag.East = 0.1f;
				Cloth.EastWind = -3.0f;

				for ( double i=0.0; i<360.0; i += 1.5 )
				{
					float north = (float)(center_z + Math.Cos(i/180.0*Math.PI) * radius 
						);
					float east  = (float)(center_x + Math.Sin(i/180.0*Math.PI) * radius 
						);
					BillBoard.Add( east, north, 0.0f, "redpost"+(int)(i*2), @"..\..\Resources\redpost.dds",0.25f, 1.0f);
					j += 5.0;
					if ( j > 360.0 ) j -= 360.0;
				}
			
				j = 0.0;
				for ( double i=0.5; i<360.0; i += 1.5 )
				{
					float north = (float)(center_z + Math.Cos(i/180.0*Math.PI) * (radius+width) 
						);
					float east  = (float)(center_x + Math.Sin(i/180.0*Math.PI) * (radius+width) 
						);
					BillBoard.Add( east, north, 0.0f, "bluepost"+(int)(i*2), @"..\..\Resources\bluepost.dds",0.25f, 1.0f);
					j += 5.0;
					if ( j >= 360.0 ) j -= 360.0;
				}

				m_ownship = new Ownship(this, "car1", @"..\..\Resources\SprintRacer.x", new Vector3(0.0f, 0.8f, 0.0f), new Attitude(0.0f, (float)Math.PI, 0.0f));
				m_ownship.AddChild(m_flag);

				SoundEffect.Volume = 0.25f;

				m_Engine.AddObject( m_ownship );
/*				Opponent opp1 = new Opponent("car2", @"..\..\Resources\SprintRacer.x", new Vector3(0.0f, 0.8f, 0.0f), 
					new Attitude(0.0f, (float)Math.PI, 0.0f), "knowledge.xml");
				opp1.SetLOD( 10, 3000.0f );
				m_opponents.Add( opp1 );
				m_Engine.AddObject( opp1 );
*/
				m_ownship.North = 298.0f;
				m_ownship.East = 1000.0f;
				m_Engine.Cam.Attach(m_ownship, new Vector3(0.0f, 0.85f,-4.5f));
				m_Engine.Cam.LookAt(m_ownship);
				m_ownship.Heading = (float)Math.PI * 1.5f;
				m_ownship.SetLOD( 10, 3000.0f );

				//			Car car2 = (Car)m_Engine.GetObject("car2");
				//			car2.North = 295.0f;
				//			car2.East  = 1000.0f;
				//			car2.Height = 0.0f;
				//			car2.Heading = (float)Math.PI * 1.5f;
				//			car2.SetLOD( 10, 300.0f );
				//			car2.SetUpdateMethod( new ObjectUpdate(OpponentUpdate));

				GameEngine.GameLights.Ambient = Color.White;

				//			GameEngine.GameLights light1 = GameEngine.GameLights.AddPointLight( new Vector3(130.0f,35.0f,130.0f),Color.White,"light1");
				//					light1.Attenuation1 = 0.11f;
				//			GameEngine.GameLights light2 = GameEngine.GameLights.AddPointLight( new Vector3(40.0f,25.0f,40.0f),Color.White,"light2");
				//					light2.Attenuation1 = 0.11f;
				//			GameEngine.GameLights light3 = GameEngine.GameLights.AddPointLight( new Vector3(130.0f,35.0f,230.0f),Color.White,"light3");
				//					light3.Attenuation1 = 0.11f;
				//			GameEngine.GameLights light4 = GameEngine.GameLights.AddPointLight( new Vector3(230.0f,40.0f,230.0f),Color.White,"light4");
				//					light4.Attenuation1 = 0.11f;
				//			GameEngine.GameLights light5 = GameEngine.GameLights.AddPointLight( new Vector3(70.0f,25.0f,70.0f),Color.White,"light5");
				//					light5.Attenuation1 = 0.11f;
				GameEngine.GameLights headlights = GameEngine.GameLights.AddSpotLight(new Vector3(0.0f,0.0f,0.0f), 
					new Vector3(1.0f,0.0f,1.0f), Color.White, "headlight");
				headlights.EffectiveRange = 200.0f;
				headlights.Attenuation0 = 1.0f;
				headlights.Attenuation1 = 0.0f;
				headlights.InnerConeAngle = 1.0f;
				headlights.OuterConeAngle = 1.5f;
				headlights.PositionOffset = new Vector3(0.0f, 2.0f, 1.0f);
				headlights.DirectionOffset = new Vector3(0.0f, 0.00f, 1.0f);
				m_ownship.AddChild(headlights);
				headlights.Enabled = false;

				CGameEngine.FogColor = Color.Beige;
				CGameEngine.FogDensity = 0.5f;
				CGameEngine.FogEnable = true;
				CGameEngine.FogStart = 100.0f;
				CGameEngine.FogEnd = 900.0f;
				CGameEngine.FogTableMode = FogMode.Linear;
			}
			catch ( Exception e )
			{
				GameEngine.Console.AddLine("Exception");
				GameEngine.Console.AddLine(e.Message);
			}
		}

		public void Gravity( ref Particle Obj, float DeltaT )
		{
			Obj.m_Position   += Obj.m_Velocity * DeltaT;
			Obj.m_Velocity.Y  += -32.0f * DeltaT;
			if ( Obj.m_Position.Y < 0.0f ) Obj.m_bActive = false;
		}


		public void OwnshipUpdate( Object3D Obj, float DeltaT )
		{
		}

		public void OpponentUpdate( Object3D Obj, float DeltaT )
		{

			Obj.Height = CGameEngine.Ground.HeightOfTerrain(Obj.Position) + ((Model)Obj).Offset.Y;
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -