/// <summary> /// React to an Animal based on it's type /// </summary> /// <param name="animal"></param> public void ReactTo(Animal animal, ref AIParameters AIparams) { if (animal != null) { //setting the the reactionLocation and reactionDistance here is //an optimization, many of the possible reactions use the distance //and location of theAnimal, so we might as well figure them out //only once ! Vector2 otherLocation = animal.Location; ClosestLocation(ref location, ref otherLocation, out reactionLocation); reactionDistance = Vector2.Distance(location, reactionLocation); //we only react if theAnimal is close enough that we can see it if (reactionDistance < AIparams.DetectionDistance) { Behaviors.Behaviors reactions = behaviors[animal.AnimalType]; foreach (Behavior reaction in reactions) { reaction.Update(animal, AIparams); if (reaction.Reacted) { aiNewDir += reaction.Reaction; aiNumSeen++; } } } } }
/// <summary> /// Instantiates all the behaviors that this Bird knows about /// </summary> public void BuildBehaviors() { Behaviors.Behaviors catReactions = new Behaviors.Behaviors(); catReactions.Add(new FleeBehavior(this)); behaviors.Add(AnimalType.Cat, catReactions); Behaviors.Behaviors birdReactions = new Behaviors.Behaviors(); birdReactions.Add(new AlignBehavior(this)); birdReactions.Add(new CohesionBehavior(this)); birdReactions.Add(new SeparationBehavior(this)); behaviors.Add(AnimalType.Bird, birdReactions); }