FleeBehavior.cs 1.5 KB

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