FuelCell.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Audio;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. namespace FuelCell
  6. {
  7. public class FuelCell : GameObject
  8. {
  9. public bool Retrieved { get; set; }
  10. private SoundEffect fuelCellCollect;
  11. public FuelCell()
  12. : base()
  13. {
  14. Retrieved = false;
  15. }
  16. public void LoadContent(ContentManager content, string modelName)
  17. {
  18. Model = content.Load<Model>(modelName);
  19. BoundingSphere = CalculateBoundingSphere();
  20. Position = Vector3.Down;
  21. fuelCellCollect = content.Load<SoundEffect>("Audio/fuelcell-collect");
  22. BoundingSphere scaledSphere;
  23. scaledSphere = BoundingSphere;
  24. scaledSphere.Radius *= GameConstants.FuelCellBoundingSphereFactor;
  25. BoundingSphere = new BoundingSphere(scaledSphere.Center, scaledSphere.Radius);
  26. }
  27. public void Draw(Matrix view, Matrix projection)
  28. {
  29. Matrix translateMatrix = Matrix.CreateTranslation(Position);
  30. Matrix worldMatrix = translateMatrix;
  31. if (!Retrieved)
  32. {
  33. foreach (ModelMesh mesh in Model.Meshes)
  34. {
  35. foreach (BasicEffect effect in mesh.Effects)
  36. {
  37. effect.World = worldMatrix;
  38. effect.View = view;
  39. effect.Projection = projection;
  40. effect.EnableDefaultLighting();
  41. effect.PreferPerPixelLighting = true;
  42. }
  43. mesh.Draw();
  44. }
  45. }
  46. }
  47. internal void Update(BoundingSphere vehicleBoundingSphere)
  48. {
  49. if (vehicleBoundingSphere.Intersects(this.BoundingSphere)
  50. && !this.Retrieved)
  51. {
  52. this.Retrieved = true;
  53. fuelCellCollect.Play();
  54. }
  55. }
  56. }
  57. }