FleeBehavior.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // FleeBehavior.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. /// FleeBehavior is a Behavior that makes an animal run from another
  17. /// </summary>
  18. public class FleeBehavior : Behavior
  19. {
  20. #region Initialization
  21. public FleeBehavior(Animal animal)
  22. : base(animal)
  23. {
  24. }
  25. #endregion
  26. #region Update
  27. public override void Update(Animal otherAnimal, AIParameters aiParams)
  28. {
  29. base.ResetReaction();
  30. Vector2 dangerDirection = Vector2.Zero;
  31. //Vector2.Dot will return a negative result in this case if the
  32. //otherAnimal is behind the animal, in that case we don’t have to
  33. //worry about it because we’re already moving away from it.
  34. if (Vector2.Dot(
  35. Animal.Location, Animal.ReactionLocation) >= -(Math.PI / 2))
  36. {
  37. //set the animal to fleeing so that it flashes red
  38. Animal.Fleeing = true;
  39. reacted = true;
  40. dangerDirection = Animal.Location - Animal.ReactionLocation;
  41. Vector2.Normalize(ref dangerDirection, out dangerDirection);
  42. reaction = (aiParams.PerDangerWeight * dangerDirection);
  43. }
  44. }
  45. #endregion
  46. }
  47. }