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

📄 gameclass.cs

📁 Beginning C# Game Programming 的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX.DirectPlay;
using Microsoft.DirectX.Direct3D;
using Direct3D=Microsoft.DirectX.Direct3D;


	public delegate void MessageDelegate(byte message); // Delegate for messages arriving via DirectPlay.
	public delegate void AudioDelegate(); // Delegate to handle audio playback.
	public delegate void PeerCloseCallback();           // This delegate will be called when the session terminated event is fired.

public class GameClass : GraphicsSample
{
	private GraphicsFont drawingFont = null;
	private Point destination = new Point(0, 0);
	private InputClass input = null;
	private MouseInput mouseInput = null;

	private bool networkEnabled;
	private PlayClass peer = null;
	private bool remotePlayerActive = false;
	private string statusMessage;
	private float statusMessageTimer;
	private PositionedMesh spaceSphere = null;
	private Ship playerShip = null;
	private Ship opponentShip = null;
	private HullColors hullColor = HullColors.White;
	private Camera camera = null;
	private CameraMode cameraMode = CameraMode.ChaseMode;
	private Matrix viewMatrix;
	private Vector2 screenCenter;
	private Vector2 mouseLoc;
	private SoundHandler soundHandler = null;
	private bool kbThrust = false;
	private bool cPressed = false;
	private bool f5Pressed = false;
	private bool f6Pressed = false;
	private bool f7Pressed = false;
	private bool f8Pressed = false;
	private bool f9Pressed = false;
	private bool f10Pressed = false;
	private bool spacePressed = false;

	private Texture bgPointerTexture = null;	
	private Texture vectorPanel = null;
	private RenderToSurface rts = null;
	private Surface renderSurface = null;
	private BGPointer bgPointer = null;
	private int range = 0;

	private GameStates gameState;
	public GameStates GameState { get { return gameState; } set { gameState = value; } }

	private string debugText;
	public string DebugText { get { return debugText; } set { debugText = value; } }
	private bool debugging = true;

	public GameClass(bool startFullScreen, Size size, bool enableNetwork)
	{
		this.startFullscreen = startFullScreen;
		this.Size = size;
		this.networkEnabled = enableNetwork;
		this.Text = "SpaceWar3D-Step11";
		statusMessageTimer = Constants.StatusMessageDisplayTime;

		drawingFont = new GraphicsFont( "Verdana", System.Drawing.FontStyle.Regular);

		input = new InputClass(this);
		mouseInput = new MouseInput(this);
		this.MouseMove +=new MouseEventHandler(GameClass_MouseMove);
		this.Cursor = Cursors.NoMove2D;

		camera = new Camera();
		soundHandler = new SoundHandler(this);

		if (networkEnabled)
		{
			peer = new PlayClass(this);
			if (peer.IsHost)
			{
				hullColor = HullColors.Red;
			}
		}

	}

	private void ProcessInput()
	{
		//Get input
		KeyboardState kbState = input.GetInputState();
		if (kbState == null)
			return;
		if (kbState[Key.W] || kbState[Key.Up])
		{
			kbThrust = true;
		}
		else
		{
			kbThrust = false;
		}
		if (kbState[Key.C] && !cPressed)
		{
			cPressed = true;
			SelectNextCameraMode();
		}
		if (!kbState[Key.C])
		{
			cPressed = false;
		}

		if (kbState[Key.F5] && !f5Pressed)
		{
			f5Pressed = true;
			playerShip.Sounds |= Sounds.Taunt;
		}
		if (!kbState[Key.F5])
		{
			f5Pressed = false;
		}
		if (kbState[Key.F6] && !f6Pressed)
		{
			f6Pressed = true;
			playerShip.Sounds |= Sounds.Dude1;
		}
		if (!kbState[Key.F6])
		{
			f6Pressed = false;
		}
		if (kbState[Key.F7] && !f7Pressed)
		{
			f7Pressed = true;
			playerShip.Sounds |= Sounds.Dude2;
		}
		if (!kbState[Key.F7])
		{
			f7Pressed = false;
		}
		if (kbState[Key.F8] && !f8Pressed)
		{
			f8Pressed = true;
			playerShip.Sounds |= Sounds.Dude3;
		}
		if (!kbState[Key.F8])
		{
			f8Pressed = false;
		}
		if (kbState[Key.F9] && !f9Pressed) {
			f9Pressed = true;
			playerShip.Sounds |= Sounds.Dude4;
		}
		if (!kbState[Key.F9]) {
			f9Pressed = false;
		}
		if (kbState[Key.F10] && !f10Pressed) {
			f10Pressed = true;
			playerShip.Sounds |= Sounds.Dude1;
		}
		if (!kbState[Key.F10]) {
			f10Pressed = false;
		}
		if (kbState[Key.Space] && !spacePressed) {
			spacePressed = true;
			playerShip.EnterHyper();
		}
		if (!kbState[Key.Space]) {
			spacePressed = false;
		}
	}

	protected override void FrameMove()
	{
		statusMessageTimer += elapsedTime;

		int fps = (int) framePerSecond;
		debugText = "FPS:  " + fps.ToString() + "\r\n";
		ProcessInput();
		MouseControlValues v = mouseInput.Values;
		if (v.FireButtonPushed)
			playerShip.Shoot();
			
		float yawAmount = mouseLoc.X - screenCenter.X;
		float pitchAmount = mouseLoc.Y - screenCenter.Y;

		playerShip.YawPitchRoll(yawAmount, pitchAmount, elapsedTime);
		playerShip.SetThrust(v.ThrustButtonPushed | kbThrust, elapsedTime);
		playerShip.UpdatePosition(elapsedTime);
		playerShip.UpdateState(elapsedTime);
		playerShip.TestShip(opponentShip);
		//Send our ship update to the remote player
		SendMyPlayerUpdate();

		// If there is no remote player, fly the o ther ship around in a circle for target practice.
		// Ideally, we would derive an AI controlled ship from the ship class and use it instead.
		if (!remotePlayerActive)
		{
			opponentShip.SetThrust(true, elapsedTime);
			opponentShip.YawPitchRoll(250,0,elapsedTime);
			opponentShip.TestShip(playerShip);
		}	
		opponentShip.UpdatePosition(elapsedTime);
		opponentShip.UpdateState(elapsedTime);

		/*Here we set up the view matrix and space dome location.  The space dome moves but not rotates with the player
			 * and is alway drawn first, so it looks like it is infinitely distant.
			 * 
			 * In chase mode, the chaseMatrix determines the offset from the ship.  If you want to move our viewpoint
			 * back from the ship more, increase the negative z value.
			 * 
			 * The fixed mode camera sits at the origin and always tracks the player ship.  Very hard to control from 
			 * this viewpoint, but cool to watch.
			 */

		Vector3 spaceSphereLocation = new Vector3(0, 0, 0);
		switch (cameraMode)
		{
			case CameraMode.ChaseMode:
			{
				Matrix chaseMatrix = Matrix.Translation(0, 6, -14);
				chaseMatrix *= playerShip.Position.WorldMatrix;
				viewMatrix = Matrix.Invert(chaseMatrix);
				spaceSphereLocation = playerShip.Position.Location;
				break;
			}
			case CameraMode.CockpitMode:
			{
				viewMatrix = Matrix.Invert(playerShip.Position.WorldMatrix);
				spaceSphereLocation = playerShip.Position.Location;
				break;
			}
			case CameraMode.Fixed:
			{
				camera.Point(0,0,0, 
					playerShip.Position.XPos,
					playerShip.Position.YPos,
					playerShip.Position.ZPos);
				viewMatrix = camera.ViewMatrix;
				spaceSphereLocation = new Vector3(0, 0, 0);
				break;
			}
		}
		device.Transform.View = viewMatrix;
		spaceSphere.Position.Location = spaceSphereLocation;
			
		//rotate space very slowly for that nice twinkly star effect
		spaceSphere.Position.RotateRel(-.001f * elapsedTime,-0.0001f * elapsedTime, 0);

		//Calculate range and direction to target
		range = (int) Vector3.Length(
			playerShip.Position.Location - opponentShip.Position.Location); 
		bgPointer.Point(playerShip.Position, opponentShip.Position);

		//Play the sounds
		soundHandler.Play(opponentShip.Sounds | playerShip.Sounds);
		opponentShip.Sounds = (Sounds) 0;
		playerShip.Sounds = (Sounds) 0;
	}


	/// <summary>
	/// Called once per frame, the call is the entry point for 3d rendering. This 
	/// function sets up render states, clears the viewport, and renders the scene.
	/// </summary>
	protected override void Render()
	{
		RenderBGPointer();

		//Clear the backbuffer to a Blue color 
		device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);

		//Set the view matrix
		device.Transform.View = viewMatrix;

		//Begin the scene
		device.BeginScene();
		device.RenderState.ZBufferEnable = false;
		device.RenderState.Lighting = false;
		spaceSphere.Render();
		device.RenderState.Lighting = true;
		device.RenderState.ZBufferEnable = true;

		device.Transform.World = playerShip.Position.WorldMatrix;
		playerShip.Render();
		opponentShip.Render();

		//Render our targeting pointer

⌨️ 快捷键说明

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