Animal.cs 4.7 KB

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