Animal.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Animal.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 System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input;
  15. #endregion
  16. namespace Flocking
  17. {
  18. public enum AnimalType
  19. {
  20. // no type
  21. Generic,
  22. // flies around and reacts
  23. Bird,
  24. // controled by teh thumbstick, birds flee from it
  25. Cat
  26. }
  27. /// <summary>
  28. /// base class for moveable, drawable critters onscreen
  29. /// </summary>
  30. public class Animal
  31. {
  32. #region Fields
  33. /// <summary>
  34. /// texture drawn to represent this animal
  35. /// </summary>
  36. protected Texture2D texture;
  37. /// <summary>
  38. /// tint color to draw the texture with
  39. /// </summary>
  40. protected Color color = Color.White;
  41. /// <summary>
  42. /// center of the draw texture
  43. /// </summary>
  44. protected Vector2 textureCenter;
  45. /// <summary>
  46. /// movement speed in pixels/second
  47. /// </summary>
  48. protected float moveSpeed;
  49. /// <summary>
  50. /// All the behavior that this animal has
  51. /// </summary>
  52. protected Dictionary<AnimalType, Behaviors> behaviors;
  53. /// <summary>
  54. /// The animal type
  55. /// </summary>
  56. public AnimalType AnimalType
  57. {
  58. get
  59. {
  60. return animaltype;
  61. }
  62. }
  63. protected AnimalType animaltype = AnimalType.Generic;
  64. /// <summary>
  65. /// Reaction distance
  66. /// </summary>
  67. public float ReactionDistance
  68. {
  69. get
  70. {
  71. return reactionDistance;
  72. }
  73. }
  74. protected float reactionDistance;
  75. /// <summary>
  76. /// Reaction location
  77. /// </summary>
  78. public Vector2 ReactionLocation
  79. {
  80. get
  81. {
  82. return reactionLocation;
  83. }
  84. }
  85. protected Vector2 reactionLocation;
  86. public bool Fleeing
  87. {
  88. get
  89. {
  90. return fleeing;
  91. }
  92. set
  93. {
  94. fleeing = value;
  95. }
  96. }
  97. protected bool fleeing = false;
  98. public int BoundryWidth
  99. {
  100. get
  101. {
  102. return boundryWidth;
  103. }
  104. }
  105. protected int boundryWidth;
  106. public int BoundryHeight
  107. {
  108. get
  109. {
  110. return boundryHeight;
  111. }
  112. }
  113. protected int boundryHeight;
  114. /// <summary>
  115. /// Direction the animal is moving in
  116. /// </summary>
  117. public Vector2 Direction
  118. {
  119. get
  120. {
  121. return direction;
  122. }
  123. }
  124. protected Vector2 direction;
  125. /// <summary>
  126. /// Location on screen
  127. /// </summary>
  128. public Vector2 Location
  129. {
  130. get
  131. {
  132. return location;
  133. }
  134. set
  135. {
  136. location = value;
  137. }
  138. }
  139. protected Vector2 location;
  140. #endregion
  141. #region Initialization
  142. /// <summary>
  143. /// Sets the boundries the animal can move in the texture used in Draw
  144. /// </summary>
  145. /// <param name="tex">Texture to use</param>
  146. /// <param name="screenSize">Size of the sample screen</param>
  147. public Animal(Texture2D tex, int screenWidth, int screenHeight)
  148. {
  149. if (tex != null)
  150. {
  151. texture = tex;
  152. textureCenter = new Vector2(texture.Width / 2, texture.Height / 2);
  153. }
  154. boundryWidth = screenWidth;
  155. boundryHeight = screenHeight;
  156. moveSpeed = 0.0f;
  157. behaviors = new Dictionary<AnimalType, Behaviors>();
  158. }
  159. #endregion
  160. #region Update and Draw
  161. /// <summary>
  162. /// Empty update function
  163. /// </summary>
  164. /// <param name="gameTime"></param>
  165. public virtual void Update(GameTime gameTime)
  166. {
  167. }
  168. /// <summary>
  169. /// Draw the Animal with the specified SpriteBatch
  170. /// </summary>
  171. /// <param name="spriteBatch"></param>
  172. /// <param name="gameTime"></param>
  173. public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime)
  174. {
  175. float rotation = (float)Math.Atan2(direction.Y, direction.X);
  176. spriteBatch.Draw(texture, location, null, color,
  177. rotation, textureCenter, 1.0f, SpriteEffects.None, 0.0f);
  178. }
  179. #endregion
  180. }
  181. }