GameplayObject.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //-----------------------------------------------------------------------------
  2. // GameplayObject.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10. namespace NetRumble
  11. {
  12. /// <summary>
  13. /// A base public class for all gameplay objects.
  14. /// </summary>
  15. abstract public class GameplayObject
  16. {
  17. /// <summary>
  18. /// If true, the object is active in the world.
  19. /// </summary>
  20. protected bool active = false;
  21. public bool Active
  22. {
  23. get { return active; }
  24. }
  25. protected Vector2 position = Vector2.Zero;
  26. public Vector2 Position
  27. {
  28. get { return position; }
  29. set
  30. {
  31. position = value;
  32. }
  33. }
  34. protected Vector2 velocity = Vector2.Zero;
  35. public Vector2 Velocity
  36. {
  37. get { return velocity; }
  38. set
  39. {
  40. if (Single.IsNaN(value.X) || Single.IsNaN(value.Y))
  41. {
  42. throw new ArgumentException("Velocity was NaN");
  43. }
  44. velocity = value;
  45. }
  46. }
  47. protected float rotation = 0f;
  48. public float Rotation
  49. {
  50. get { return rotation; }
  51. set { rotation = value; }
  52. }
  53. protected float radius = 1f;
  54. public float Radius
  55. {
  56. get { return radius; }
  57. set { radius = value; }
  58. }
  59. protected float mass = 1f;
  60. public float Mass
  61. {
  62. get { return mass; }
  63. }
  64. protected bool collidedThisFrame = false;
  65. public bool CollidedThisFrame
  66. {
  67. get { return collidedThisFrame; }
  68. set { collidedThisFrame = value; }
  69. }
  70. /// <summary>
  71. /// Constructs a new gameplay object.
  72. /// </summary>
  73. protected GameplayObject() { }
  74. /// <summary>
  75. /// Initialize the object to it's default gameplay states.
  76. /// </summary>
  77. public virtual void Initialize()
  78. {
  79. if (!active)
  80. {
  81. active = true;
  82. CollisionManager.Collection.Add(this);
  83. }
  84. }
  85. /// <summary>
  86. /// Update the gameplay object.
  87. /// </summary>
  88. /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
  89. public virtual void Update(float elapsedTime)
  90. {
  91. collidedThisFrame = false;
  92. }
  93. /// <summary>
  94. /// Draw the gameplay object.
  95. /// </summary>
  96. /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
  97. /// <param name="spriteBatch">The SpriteBatch object used to draw.</param>
  98. /// <param name="sprite">The texture used to draw this object.</param>
  99. /// <param name="sourceRectangle">The source rectangle.</param>
  100. /// <param name="color">The color of the sprite.</param>
  101. public virtual void Draw(float elapsedTime, SpriteBatch spriteBatch,
  102. Texture2D sprite, Rectangle? sourceRectangle, Color color)
  103. {
  104. if ((spriteBatch != null) && (sprite != null))
  105. {
  106. spriteBatch.Draw(sprite, position, sourceRectangle, color, rotation,
  107. new Vector2(sprite.Width / 2f, sprite.Height / 2f),
  108. 2f * radius / MathHelper.Min(sprite.Width, sprite.Height),
  109. SpriteEffects.None, 0f);
  110. }
  111. }
  112. /// <summary>
  113. /// Defines the interaction between this GameplayObject and
  114. /// a target GameplayObject when they touch.
  115. /// </summary>
  116. /// <param name="target">The GameplayObject that is touching this one.</param>
  117. /// <returns>True if the objects meaningfully interacted.</returns>
  118. public virtual bool Touch(GameplayObject target)
  119. {
  120. return true;
  121. }
  122. /// <summary>
  123. /// Damage this object by the amount provided.
  124. /// </summary>
  125. /// <remarks>
  126. /// This function is provided in lieu of a Life mutation property to allow
  127. /// classes of objects to restrict which kinds of objects may damage them,
  128. /// and under what circumstances they may be damaged.
  129. /// </remarks>
  130. /// <param name="source">The GameplayObject responsible for the damage.</param>
  131. /// <param name="damageAmount">The amount of damage.</param>
  132. /// <returns>If true, this object was damaged.</returns>
  133. public virtual bool Damage(GameplayObject source, float damageAmount)
  134. {
  135. return false;
  136. }
  137. /// <summary>
  138. /// Kills this object, in response to the given GameplayObject.
  139. /// </summary>
  140. /// <param name="source">The GameplayObject responsible for the kill.</param>
  141. /// <param name="cleanupOnly">
  142. /// If true, the object dies without any further effects.
  143. /// </param>
  144. public virtual void Die(GameplayObject source, bool cleanupOnly)
  145. {
  146. // deactivate the object
  147. if (active)
  148. {
  149. active = false;
  150. CollisionManager.Collection.QueuePendingRemoval(this);
  151. }
  152. }
  153. }
  154. }