If you remember earlier when you first defined the tank class, plenty of variables declared were private and you didn't have any public way to access them. Naturally, you need to rectify this situation because the game engine (and player objects you will be creating) need to manipulate the tanks. Add the properties in Listing 13.10 to your tank class to round out its implementation. Listing 13.10. Tank Propertiespublic Vector3 Position { get { return tankPosition; } set { tankPosition = value; } } public float GunTurretAngle { get { return turretAngle; } set { if (value > MaxTurretAngle) turretAngle = (float)MaxTurretAngle; else if (value < MinTurretAngle) turretAngle = (float)MinTurretAngle; else turretAngle = value; } } public float GunBarrelAngle { get { return barrelAngle; } set { if (value > MaxBarrelAngle) barrelAngle = (float)MaxBarrelAngle; else if (value < MinBarrelAngle) barrelAngle = (float)MinBarrelAngle; else barrelAngle = value; } } public float WheelRotation { get { return wheelAngle; } set { wheelAngle = value; } } public float TankRotation { get { return rotationAngle; } set { // Set the new rotation angle rotationAngle = value; // Update the rotation matrix rotationMatrix = Matrix.RotationY(rotationAngle); // Calculate the direction you're facing facingDirection = new Vector3(rotationMatrix.M31, rotationMatrix.M32 , rotationMatrix.M33); facingDirection.Normalize(); } } public Matrix TankRotationMatrix { get { return rotationMatrix; } } public Vector3 TankDirection { get { return facingDirection; } } public float Radius { get { return tankRadius; } } public Vector3 Center { get { return tankCenter; } } public Vector3 BarrelDirection { get { return gunDirection; } } public Vector3 BarrelPosition { get { return gunPosition; } } public TankColors Color { get { return currentColor; } set { // Set the color currentColor = value; // Update the textures tankTexture = TankSelectionModel.GetTankTexture(device, currentColor); } } You'll notice that the majority of the properties are simple public accessors (either read-only or normal), but a few actually do a little extra, normally when the value is set. For example, setting the color updates the texture using the method you wrote in the last chapter. Modifying the gun angles enforces the maximum and minimum limits on them. Finally, updating the tank rotation builds the rotation matrix and stores the direction the tank is currently facing. Just like the gun turret, the entire tank is rotated on the Y axis. |