void SampleCarnivore_Idle(object sender, IdleEventArgs e) { try { if (CanReproduce) { BeginReproduction(null); } if (CanEat && !IsEating) { if (_targetAnimal != null) { if (WithinEatingRange(_targetAnimal)) { BeginEating(_targetAnimal); if (IsMoving) { StopMoving(); } } else { if (!IsMoving) { BeginMoving(new MovementVector(_targetAnimal.Position, 2)); } } } else { if (!ScanForTargetAnimal()) { if (!IsMoving) { int randomX = OrganismRandom.Next(0, WorldWidth - 1); int randomY = OrganismRandom.Next(0, WorldHeight - 1); BeginMoving(new MovementVector(new Point(randomX, randomY), 2)); } } } } else { if (IsMoving) { StopMoving(); } } } catch (Exception exc) { WriteTrace(exc.ToString()); } }
// Fired if we're being attacked private void MyAnimal_Attacked(object sender, AttackedEventArgs e) { if (e.Attacker.IsAlive) { AnimalState TheAttacker = e.Attacker; BeginDefending(TheAttacker); //defend against the attacker WriteTrace("Run away to some random point"); int X = OrganismRandom.Next(0, WorldWidth - 1); int Y = OrganismRandom.Next(0, WorldHeight - 1); BeginMoving(new MovementVector(new Point(X, Y), 10)); } }
// Fired after all other events are fired during a turn private void MyAnimal_Idle(object sender, IdleEventArgs e) { try { // Reproduce as often as possible if (CanReproduce) { BeginReproduction(null); } // If we can eat and we have a target plant, eat if (CanEat) { WriteTrace("Hungry."); if (!IsEating) { WriteTrace("Not eating: Have target plant?"); if (targetPlant != null) { WriteTrace("Yes, Have target plant already."); if (WithinEatingRange(targetPlant)) { WriteTrace("Within Range, Start eating."); BeginEating(targetPlant); if (IsMoving) { WriteTrace("Stop while eating."); StopMoving(); } } else { if (!IsMoving) { WriteTrace("Move to Target Plant"); BeginMoving(new MovementVector(targetPlant.Position, 2)); } } } else { WriteTrace("Don't have target plant."); if (!ScanForTargetPlant()) { if (!IsMoving) { WriteTrace("No plant found, so pick a random point and move there"); int RandomX = OrganismRandom.Next(0, WorldWidth - 1); int RandomY = OrganismRandom.Next(0, WorldHeight - 1); BeginMoving(new MovementVector(new Point(RandomX, RandomY), 2)); } else { WriteTrace("Moving and Looking..."); } } } } else { WriteTrace("Eating."); if (IsMoving) { WriteTrace("Stop moving while eating."); StopMoving(); } } } else { WriteTrace("Full: do nothing."); if (IsMoving) { StopMoving(); } } } catch (Exception exc) { WriteTrace(exc.ToString()); } }
// Fired after all other events are fired during a turn void IdleEvent(object sender, IdleEventArgs e) { try { // Our Creature will reproduce as often as possible so // every turn it checks CanReproduce to know when it // is capable. If so we call BeginReproduction with // a null Dna parameter to being reproduction. if (CanReproduce) { BeginReproduction(null); } // Check to see if we are capable of eating // If we are then try to eat or find food, // else we'll just stop moving. if (CanEat && !IsEating) { // If we have a Target Plant we can try // to either eat it, or move towards it. // else we'll move to a random vector // in search of a plant. if (targetPlant != null) { // If we are within range start eating // and stop moving. Else we'll try // to move towards the plant. if (WithinEatingRange(targetPlant)) { BeginEating(targetPlant); if (IsMoving) { StopMoving(); } } else { if (!IsMoving) { BeginMoving(new MovementVector(targetPlant.Position, 2)); } } } else { // We'll try try to find a target plant // If we don't find one we'll move to // a random vector if (!ScanForTargetPlant()) { if (!IsMoving) { int RandomX = OrganismRandom.Next(0, WorldWidth - 1); int RandomY = OrganismRandom.Next(0, WorldHeight - 1); BeginMoving(new MovementVector(new Point(RandomX, RandomY), 2)); } } } } else { // Since we are Full or we are already eating // We should stop moving. if (IsMoving) { StopMoving(); } } } catch (Exception exc) { WriteTrace(exc.ToString()); } }