Behavior.cs 2.2 KB

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