Behavior.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Behavior.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. #endregion
  13. namespace Flocking
  14. {
  15. /// <summary>
  16. /// Behavior is the base class for the four flock behaviors in this sample:
  17. /// aligning, cohesion, separation and fleeing. It is an abstract class,
  18. /// leaving the implementation of Update up to its subclasses. Animal objects
  19. /// can have an arbitrary number of behaviors, after the entity calls Update
  20. /// on the behavior the reaction results are stored in reaction so the owner
  21. /// can query it.
  22. /// </summary>
  23. public abstract class Behavior
  24. {
  25. #region Fields
  26. /// <summary>
  27. /// Keep track of the animal that this behavior belongs to.
  28. /// </summary>
  29. public Animal Animal
  30. {
  31. get { return animal; }
  32. set { animal = value; }
  33. }
  34. private Animal animal;
  35. /// <summary>
  36. /// Store the behavior reaction here.
  37. /// </summary>
  38. public Vector2 Reaction
  39. {
  40. get { return reaction; }
  41. }
  42. protected Vector2 reaction;
  43. /// <summary>
  44. /// Store if the behavior has reaction results here.
  45. /// </summary>
  46. public bool Reacted
  47. {
  48. get { return reacted; }
  49. }
  50. protected bool reacted;
  51. #endregion
  52. #region Initialization
  53. protected Behavior(Animal animal)
  54. {
  55. this.animal = animal;
  56. }
  57. #endregion
  58. #region Update
  59. /// <summary>
  60. /// Abstract function that the subclass must impliment. Figure out the
  61. /// Behavior reaction here.
  62. /// </summary>
  63. /// <param name="otherAnimal">the Animal to react to</param>
  64. /// <param name="aiParams">the Behaviors' parameters</param>
  65. public abstract void Update(Animal otherAnimal, AIParameters aiParams);
  66. /// <summary>
  67. /// Reset the behavior reactions from the last Update
  68. /// </summary>
  69. protected void ResetReaction()
  70. {
  71. reacted = false;
  72. reaction = Vector2.Zero;
  73. }
  74. #endregion
  75. }
  76. }