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

📄 car.cs

📁 学习Managed directx3d的很好的资料,VC#源码实例,共包括6章,其中第四章为:创建三维地形
💻 CS
字号:
using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;

namespace Dodger
{
	/// <summary>
	/// Will hold all of the information on the car
	/// </summary>
	public class Car
	{
        // Car constants
        public const float Height = 2.5f;
        public const float Depth = 3.0f;
        public const float SpeedIncrement = 0.1f;
        private const float Scale = 0.85f;

        // Car data information
        private float carLocation = DodgerGame.RoadLocationLeft;
        private float carDiameter;
        private float carSpeed = 10.0f;
        private bool movingLeft = false;
        private bool movingRight = false;

        // Our car mesh information
        private Mesh carMesh = null;
        private Material[] carMaterials = null;
        private Texture[] carTextures = null;

        /// <summary>
        /// Create a new car device, and load the mesh data for it
        /// </summary>
        /// <param name="device">D3D device to use</param>
        public Car(Device device)
		{
            // Create our car mesh
            carMesh = DodgerGame.LoadMesh(device, @"..\..\car.x", ref carMaterials, ref carTextures);

            // We need to calculate a bounding sphere for our car
            VertexBuffer vb = carMesh.VertexBuffer;
            try
            {
                // We need to lock the entire buffer to calculate this
                GraphicsStream stm = vb.Lock(0, 0, LockFlags.None);
                Vector3 center; // We won't use the center, but it's required
                float radius = Geometry.ComputeBoundingSphere(stm, 
                    carMesh.NumberVertices, carMesh.VertexFormat, out center);

                // All we care about is the diameter.  Store that
                carDiameter = (radius * 2) * Scale;
            }
            finally
            {
                // No matter what, make sure we unlock and dispose this vertex
                // buffer.
                vb.Unlock();
                vb.Dispose();
            }
		}

        /// <summary>
        /// Render the car given the current properties
        /// </summary>
        /// <param name="device">The device used to render the car</param>
        public void Draw(Device device)
        {
            // The car is a little bit too big, scale it down
            device.Transform.World = Matrix.Scaling(Scale, 
                Scale, Scale) *  Matrix.Translation(carLocation, Height, Depth);

            for (int i = 0; i < carMaterials.Length; i++)
            {
                device.Material = carMaterials[i];
                device.SetTexture(0, carTextures[i]);
                carMesh.DrawSubset(i);
            }
        }

        /// <summary>
        /// Increment the movement speed of the car
        /// </summary>
        public void IncrementSpeed()
        {
            carSpeed += SpeedIncrement;
        }

        /// <summary>
        /// Update the cars state based on the elapsed time
        /// </summary>
        /// <param name="elapsedTime">Amount of time that has elapsed</param>
        public void Update(float elapsedTime)
        {
            if (movingLeft)
            {
                // Move the car
                carLocation += (carSpeed * elapsedTime);
                // Is the car all the way to the left?
                if (carLocation >= DodgerGame.RoadLocationLeft)
                {
                    movingLeft = false;
                    carLocation = DodgerGame.RoadLocationLeft;
                }
            }

            if (movingRight)
            {
                // Move the car
                carLocation -= (carSpeed * elapsedTime);
                // Is the car all the way to the right?
                if (carLocation <= DodgerGame.RoadLocationRight)
                {
                    movingRight = false;
                    carLocation = DodgerGame.RoadLocationRight;
                }
            }
        }

        // Public properties for car data
        public float Location 
        {
            get { return carLocation; } 
            set { carLocation = value; } 
        }
        public float Diameter 
        { 
            get { return carDiameter; } 
        }
        public float Speed 
        { 
            get { return carSpeed; } 
            set { carSpeed = value; } 
        }
        public bool IsMovingLeft 
        { 
            get { return movingLeft; } 
            set { movingLeft = value; } 
        }
        public bool IsMovingRight 
        { 
            get { return movingRight; } 
            set { movingRight = value; } 
        }
    }
}

⌨️ 快捷键说明

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