SpriteEntity.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //-----------------------------------------------------------------------------
  2. // SpriteEntity.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace Audio3D
  10. {
  11. /// <summary>
  12. /// Base class for game entities that are displayed as billboard sprites,
  13. /// and which can emit 3D sounds. The Cat and Dog classes both derive from this.
  14. /// </summary>
  15. abstract class SpriteEntity : IAudioEmitter
  16. {
  17. /// <summary>
  18. /// Gets or sets the 3D position of the entity.
  19. /// </summary>
  20. public Vector3 Position
  21. {
  22. get { return position; }
  23. set { position = value; }
  24. }
  25. Vector3 position;
  26. /// <summary>
  27. /// Gets or sets which way the entity is facing.
  28. /// </summary>
  29. public Vector3 Forward
  30. {
  31. get { return forward; }
  32. set { forward = value; }
  33. }
  34. Vector3 forward;
  35. /// <summary>
  36. /// Gets or sets the orientation of this entity.
  37. /// </summary>
  38. public Vector3 Up
  39. {
  40. get { return up; }
  41. set { up = value; }
  42. }
  43. Vector3 up;
  44. /// <summary>
  45. /// Gets or sets how fast this entity is moving.
  46. /// </summary>
  47. public Vector3 Velocity
  48. {
  49. get { return velocity; }
  50. protected set { velocity = value; }
  51. }
  52. Vector3 velocity;
  53. /// <summary>
  54. /// Gets or sets the texture used to display this entity.
  55. /// </summary>
  56. public Texture2D Texture
  57. {
  58. get { return texture; }
  59. set { texture = value; }
  60. }
  61. Texture2D texture;
  62. /// <summary>
  63. /// Updates the position of the entity, and allows it to play sounds.
  64. /// </summary>
  65. public abstract void Update(GameTime gameTime, AudioManager audioManager);
  66. /// <summary>
  67. /// Draws the entity as a billboard sprite.
  68. /// </summary>
  69. public void Draw(QuadDrawer quadDrawer, Vector3 cameraPosition,
  70. Matrix view, Matrix projection)
  71. {
  72. Matrix world = Matrix.CreateTranslation(0, 1, 0) *
  73. Matrix.CreateScale(800) *
  74. Matrix.CreateConstrainedBillboard(Position, cameraPosition,
  75. Up, null, null);
  76. quadDrawer.DrawQuad(Texture, 1, world, view, projection);
  77. }
  78. }
  79. }