FuelCarrier.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Audio;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6. using System;
  7. namespace FuelCell
  8. {
  9. public class FuelCarrier : GameObject
  10. {
  11. public float ForwardDirection { get; set; }
  12. public int MaxRange { get; set; }
  13. private Vector3 startPosition = new Vector3(0, GameConstants.HeightOffset, 0);
  14. private SoundEffect engineRumble;
  15. public FuelCarrier()
  16. : base()
  17. {
  18. ForwardDirection = 0.0f;
  19. Position = startPosition;
  20. MaxRange = GameConstants.MaxRange;
  21. }
  22. public void LoadContent(ContentManager content, string modelName)
  23. {
  24. Model = content.Load<Model>(modelName);
  25. BoundingSphere = CalculateBoundingSphere();
  26. engineRumble = content.Load<SoundEffect>("Audio/engine-rumble");
  27. BoundingSphere scaledSphere;
  28. scaledSphere = BoundingSphere;
  29. scaledSphere.Radius *= GameConstants.FuelCarrierBoundingSphereFactor;
  30. BoundingSphere = new BoundingSphere(scaledSphere.Center, scaledSphere.Radius);
  31. }
  32. internal void Reset()
  33. {
  34. Position = startPosition;
  35. ForwardDirection = 0f;
  36. }
  37. public void Update(IInputState inputState, Barrier[] barriers)
  38. {
  39. Vector3 futurePosition = Position;
  40. ForwardDirection += inputState.GetPlayerTurn(PlayerIndex.One) * GameConstants.TurnSpeed;
  41. Matrix orientationMatrix = Matrix.CreateRotationY(ForwardDirection);
  42. Vector3 speed = Vector3.Transform(inputState.GetPlayerMove(PlayerIndex.One), orientationMatrix);
  43. if (speed != Vector3.Zero)
  44. {
  45. engineRumble.Play();
  46. }
  47. speed *= GameConstants.Velocity;
  48. futurePosition = Position + speed;
  49. if (ValidateMovement(futurePosition, barriers))
  50. {
  51. Position = futurePosition;
  52. BoundingSphere updatedSphere;
  53. updatedSphere = BoundingSphere;
  54. updatedSphere.Center.X = Position.X;
  55. updatedSphere.Center.Z = Position.Z;
  56. BoundingSphere = new BoundingSphere(updatedSphere.Center, updatedSphere.Radius);
  57. }
  58. }
  59. public void Draw(Matrix view, Matrix projection)
  60. {
  61. Matrix worldMatrix = Matrix.Identity;
  62. Matrix rotationYMatrix = Matrix.CreateRotationY(ForwardDirection);
  63. Matrix translateMatrix = Matrix.CreateTranslation(Position);
  64. worldMatrix = rotationYMatrix * translateMatrix;
  65. foreach (ModelMesh mesh in Model.Meshes)
  66. {
  67. foreach (BasicEffect effect in mesh.Effects)
  68. {
  69. effect.World = worldMatrix;
  70. effect.View = view;
  71. effect.Projection = projection;
  72. effect.EnableDefaultLighting();
  73. effect.PreferPerPixelLighting = true;
  74. }
  75. mesh.Draw();
  76. }
  77. }
  78. private bool ValidateMovement(Vector3 futurePosition, Barrier[] barriers)
  79. {
  80. BoundingSphere futureBoundingSphere = BoundingSphere;
  81. futureBoundingSphere.Center.X = futurePosition.X;
  82. futureBoundingSphere.Center.Z = futurePosition.Z;
  83. //Do not allow off-terrain driving
  84. if ((Math.Abs(futurePosition.X) > MaxRange) || (Math.Abs(futurePosition.Z) > MaxRange))
  85. return false;
  86. //Do not allow driving through a barrier
  87. if (CheckForBarrierCollision(futureBoundingSphere, barriers))
  88. {
  89. return false;
  90. }
  91. return true;
  92. }
  93. private bool CheckForBarrierCollision(BoundingSphere vehicleBoundingSphere, Barrier[] barriers)
  94. {
  95. for (int curBarrier = 0; curBarrier < barriers.Length; curBarrier++)
  96. {
  97. if (vehicleBoundingSphere.Intersects(barriers[curBarrier].BoundingSphere))
  98. {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. }
  105. }