GameplayObject.cs 6.1 KB

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