Terrarium.Game.GameEngine.doBites C# (CSharp) Method

doBites() private method

Assumes the animal was dead (if an animal) at the time of the bite and that it is the proper food source for this animal (herbivore or carnivore) Also assumes that it is within biting range.
private doBites ( ) : void
return void
        private void doBites()
        {
            IEnumerable<EatAction> eatActions = CurrentVector.Actions.EatActions;
            foreach (EatAction action in eatActions)
            {
                // Only animals can bite
                AnimalState attackerState = (AnimalState) _newWorldState.GetOrganismState(action.OrganismID);
                if (attackerState == null || !attackerState.IsAlive)
                {
                    continue;
                }

                Boolean biteSuccessful = true;
                OrganismState defenderState = _newWorldState.GetOrganismState(action.TargetOrganism.ID);
                if (defenderState == null)
                {
                    // the defender has been removed from the game
                    biteSuccessful = false;
                }
                else
                {
                    int energyToFill =
                        (int) ((attackerState.Radius*(double) attackerState.Species.MaximumEnergyPerUnitRadius) -
                               attackerState.StoredEnergy);
                    Debug.Assert(energyToFill > 0);

                    // Determine how many chunks it would take to fill the animal completely
                    int foodChunkCount;
                    if (defenderState is PlantState)
                    {
                        foodChunkCount = (int) (energyToFill/(double) EngineSettings.EnergyPerPlantFoodChunk);
                    }
                    else
                    {
                        foodChunkCount = (int) (energyToFill/(double) EngineSettings.EnergyPerAnimalFoodChunk);
                    }

                    if (foodChunkCount == 0)
                    {
                        foodChunkCount = 1;
                    }

                    // If this is more than the animal can eat in one bite, limit it to what they can eat
                    if (foodChunkCount > attackerState.AnimalSpecies.EatingSpeedPerUnitRadius*attackerState.Radius)
                    {
                        foodChunkCount = attackerState.AnimalSpecies.EatingSpeedPerUnitRadius*attackerState.Radius;
                    }

                    if (defenderState.FoodChunks <= foodChunkCount)
                    {
                        // remove the defender from the world if we ate them all
                        removeOrganism(new KilledOrganism(defenderState));
                        foodChunkCount = defenderState.FoodChunks;
                    }
                    else
                    {
                        // Shrink the meat or plant
                        defenderState.FoodChunks = defenderState.FoodChunks - foodChunkCount;
                    }

                    // Determine how much energy we got
                    int newEnergy;
                    if (defenderState is PlantState)
                    {
                        newEnergy = EngineSettings.EnergyPerPlantFoodChunk*foodChunkCount;
                    }
                    else
                    {
                        newEnergy = EngineSettings.EnergyPerAnimalFoodChunk*foodChunkCount;
                    }

                    attackerState.StoredEnergy = attackerState.StoredEnergy + newEnergy;
                }

                // Tell the attacker what happened
                attackerState.OrganismEvents.EatCompleted = new EatCompletedEventArgs(action.ActionID, action,
                                                                                      biteSuccessful);
            }
        }