📄 obstacle.cs
字号:
using System;
using System.Collections;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Dodger
{
/// <summary>
/// This class will hold a single obstacle
/// </summary>
public class Obstacle : IDisposable
{
// Object constants
private const int NumberMeshTypes = 5;
private const float ObjectLength = 3.0f;
private const float ObjectRadius = ObjectLength / 2.0f;
private const int ObjectStacksSlices = 18;
// obstacle colors
private static readonly Color[] ObstacleColors = {
Color.Red, Color.Blue, Color.Green,
Color.Bisque, Color.Cyan, Color.DarkKhaki,
Color.OldLace, Color.PowderBlue, Color.DarkTurquoise,
Color.Azure, Color.Violet, Color.Tomato,
Color.Yellow, Color.Purple, Color.AliceBlue,
Color.Honeydew, Color.Crimson, Color.Firebrick };
// Mesh information
private Mesh obstacleMesh = null;
private Material obstacleMaterial;
private Vector3 position;
private bool isTeapot;
// Rotation information
private float rotation = 0;
private float rotationspeed = 0.0f;
private Vector3 rotationVector;
public Obstacle(Device device, float x, float y, float z)
{
// Store our position
position = new Vector3(x, y, z);
// It's not a teapot
isTeapot = false;
rotationspeed = (float)Utility.Rnd.NextDouble() * (float)Math.PI;
rotationVector = new Vector3((float)Utility.Rnd.NextDouble(),
(float)Utility.Rnd.NextDouble(),(float)Utility.Rnd.NextDouble());
// Create a new obstacle
switch (Utility.Rnd.Next(NumberMeshTypes))
{
case 0:
obstacleMesh = Mesh.Sphere(device, ObjectRadius, ObjectStacksSlices, ObjectStacksSlices);
break;
case 1:
obstacleMesh = Mesh.Box(device, ObjectLength, ObjectLength, ObjectLength);
break;
case 2:
obstacleMesh = Mesh.Teapot(device);
isTeapot = true;
break;
case 3:
obstacleMesh = Mesh.Cylinder(device, ObjectRadius, ObjectRadius, ObjectLength, ObjectStacksSlices, ObjectStacksSlices);
break;
case 4:
obstacleMesh = Mesh.Torus(device, ObjectRadius / 3.0f, ObjectRadius / 2.0f, ObjectStacksSlices, ObjectStacksSlices);
break;
}
// Set the obstacle color
obstacleMaterial = new Material();
Color objColor = ObstacleColors[Utility.Rnd.Next(ObstacleColors.Length)];
obstacleMaterial.Ambient = objColor;
obstacleMaterial.Diffuse = objColor;
}
public void Update(float elapsedTime, float speed)
{
position.Z += (speed * elapsedTime);
rotation += (rotationspeed * elapsedTime);
}
public void Draw(Device device)
{
if (isTeapot)
{
device.Transform.World = Matrix.RotationAxis(rotationVector, rotation)
* Matrix.Scaling(ObjectRadius, ObjectRadius, ObjectRadius)
* Matrix.Translation(position);
}
else
{
device.Transform.World = Matrix.RotationAxis(rotationVector, rotation)
* Matrix.Translation(position);
}
device.Material = obstacleMaterial;
device.SetTexture(0, null);
obstacleMesh.DrawSubset(0);
}
public float Depth
{
get { return position.Z; }
}
public bool IsHittingCar(float carLocation, float carDiameter)
{
// In order for the obstacle to be hitting the car,
// it must be on the same side of the road and
// hitting the car
if (position.Z > (Car.Depth - (carDiameter / 2.0f)))
{
// are we on the right side of the car
if ((carLocation < 0) && (position.X < 0))
return true;
if ((carLocation > 0) && (position.X > 0))
return true;
}
return false;
}
#region Cleanup Code
/// <summary>
/// Dispose the mesh and any related information
/// </summary>
public void Dispose()
{
obstacleMesh.Dispose();
System.GC.SuppressFinalize(this);
}
/// <summary>
/// Finalizer for this object
/// </summary>
~Obstacle()
{
// Just call dispose
this.Dispose();
}
#endregion
}
public class Obstacles : IEnumerable
{
private ArrayList obstacleList = new ArrayList();
/// <summary>
/// Indexer for this class
/// </summary>
public Obstacle this[int index]
{
get
{
return (Obstacle)obstacleList[index];
}
}
// Get the enumerator from our arraylist
public IEnumerator GetEnumerator()
{
return obstacleList.GetEnumerator();
}
/// <summary>
/// Add an obstacle to our list
/// </summary>
/// <param name="obstacle">The obstacle to add</param>
public void Add(Obstacle obstacle)
{
obstacleList.Add(obstacle);
}
/// <summary>
/// Remove an obstacle from our list
/// </summary>
/// <param name="obstacle">The obstacle to remove</param>
public void Remove(Obstacle obstacle)
{
obstacleList.Remove(obstacle);
}
/// <summary>
/// Clear the obstacle list
/// </summary>
public void Clear()
{
obstacleList.Clear();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -