player.cs
来自「《3d游戏编程入门经典》中范例小游戏。使用C#+DirectX开发。」· CS 代码 · 共 218 行
CS
218 行
// Player.cs will be used to hold all of the player specific data,
// and rendering code
using System;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Blockers
{
/// <summary>
/// The color of the player model
/// </summary>
public enum PlayerColor
{
Blue,
Green,
Purple,
Red,
Gray,
Yellow,
Min = Blue,
Max = Yellow,
}
/// <summary>
/// The direction the player model is facing
/// </summary>
public enum PlayerDirection
{
Down,
Up,
Left,
Right
}
/// <summary>
/// Main player object
/// </summary>
public class Player : IDisposable
{
#region Constants
// Rendering constants
private const float ScaleConstant = 0.1f;
private static readonly Matrix[] RotationMatrices = new Matrix[] {
Matrix.RotationY((float)Math.PI * 3.0f / 2.0f),
Matrix.RotationY((float)Math.PI / 2.0f),
Matrix.RotationY((float)Math.PI * 2),
Matrix.RotationY((float)Math.PI) };
private static readonly Matrix ScalingMatrix = Matrix.Scaling(
ScaleConstant, ScaleConstant, ScaleConstant);
// Maximum speed the player will move per second
private const float MaxMovement = 30.0f;
// Maximum 'bounce' speed
private const float BounceSpeed = 10.0f;
private const float MaxBounceHeight = 0.4f;
#endregion
#region Instance Data
private Mesh playerMesh = null; // Mesh for the player
private Material playerMaterial; // Material
private Texture playerTexture = null; // Texture for the mesh
private Matrix rotation = RotationMatrices[0];
private Vector3 pos; // The player's real position
private Vector3 moveToPos; // Where the player is moving to
private bool isMoving = false;
private float playerHeight = 0.0f;
#endregion
#region Constructor
/// <summary>
/// Create a new player object
/// </summary>
public Player(Mesh loopyMesh, Texture loopyTex,
Material mat)
{
// Store the mesh
playerMesh = loopyMesh;
// Store the appropriate texture
playerTexture = loopyTex;
// Store the player's material
playerMaterial = mat;
}
#endregion
#region Class Properties
/// <summary>
/// The player's position
/// </summary>
public Vector3 Position
{
get { return pos; }
set { pos = moveToPos = value;}
}
#endregion
#region Class Methods
/// <summary>
/// Render the player model
/// </summary>
public void Draw(Device device, float appTime)
{
// Set the world transform
device.Transform.World = rotation * ScalingMatrix *
Matrix.Translation(pos.X, playerHeight, pos.Z);
// Set the texture for our model
device.SetTexture(0, playerTexture);
// Set the model's material
device.Material = playerMaterial;
// Render our model
playerMesh.DrawSubset(0);
}
/// <summary>
/// Set the player's direction
/// </summary>
public void SetDirection(PlayerDirection dir)
{
rotation = RotationMatrices[(byte)dir];
}
/// <summary>
/// Move the player to this new position in a fluid motion
/// </summary>
/// <param name="newPosition">The new position of the player</param>
public bool MoveTo(Vector3 newPosition)
{
if (!isMoving)
{
isMoving = true;
// Just store the new position, it will be used during update
moveToPos = newPosition;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Update the player's position based on time
/// </summary>
/// <param name="elapsed">elapsed time since last frame</param>
/// <param name="total">total time</param>
public void Update(float elapsed, float total)
{
// Calculate the new player position if needed
playerHeight = (float)(Math.Abs(Math.Sin(total * BounceSpeed))) * MaxBounceHeight;
if (pos != moveToPos)
{
Vector3 diff = moveToPos - pos;
// Are we close enough to just move there?
if (diff.LengthSq() > (MaxMovement * elapsed))
{
// No we're not, move slowly there
diff.Normalize();
diff.Scale(MaxMovement * elapsed);
pos += diff;
}
else
{
isMoving = false;
// Indeed we are, just move there
pos = moveToPos;
}
}
else
{
isMoving = false;
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Clean up any resources in case you forgot to dispose this object
/// </summary>
~Player()
{
Dispose();
}
public void Dispose()
{
// Suppress finalization
GC.SuppressFinalize(this);
// Dispose of the texture
if (playerTexture != null)
{
playerTexture.Dispose();
}
if (playerMesh != null)
{
playerMesh.Dispose();
}
playerTexture = null;
playerMesh = null;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?