/// <summary> /// Animals may listen, to detect which direction the loudest sound is /// coming from. This method works by examining the SoundLevels in the air /// in all immediately-surrounding cells and picking the one that has the greatest /// sound level. /// /// If a number of directions have equal SoundLevels, any one of them may be returned. /// /// But in the special case when all the SoundLevels are zero, null is returned. /// </summary> /// <returns> the Direction of the loudest sound, or null if all the SoundLevels are zero. </returns> public Direction Listen() { int loudestSound = 0; Direction loudestSoundDirection = Direction.NORTH; for (int i = 0; i < Direction.NUMBER_POSSIBLE; i++) { Direction direction = Direction.GetAdjacentCellDirection(i); Cell adjacentCell = Cell.GetAdjacentCell(direction); if (adjacentCell != null) { Air adjacentAir = adjacentCell.Air; int adjacentSoundLevel = adjacentAir.SoundLevel; if (loudestSound < adjacentSoundLevel) { loudestSound = adjacentSoundLevel; loudestSoundDirection = direction; } } } if (loudestSound == 0) //0 being default value { return(null); } else { return(loudestSoundDirection); } }
/// <summary> /// This method is available to all Things that wish to make a sound in the /// pigWorld. Calling this method starts a chain reaction in which this air /// object sends a TransmitSound message to all surrounding air objects, /// and each of those in turn send a TransmitSound message to their own /// surrounding air objects, until the sound dies out or reaches a dead-end. /// /// Sound does not pass THROUGH walls, but it will propagate AROUND a wall /// whenever there is a path from one Cell to another. /// /// Each time sound is transmitted to surrounding air objects, the /// passed-on soundLevel is decreased by one. /// </summary> /// <param name="soundLevel"> the intensity of sound to transmit to this particular /// piece of air. </param> public void TransmitSound(int soundLevel) { if (soundLevel <= this.soundLevel) { return; } this.soundLevel = soundLevel; cell.AirChanged(); for (int i = 0; i < Direction.NUMBER_POSSIBLE; i++) { Direction direction = Direction.GetAdjacentCellDirection(i); Cell adjacentCell = Cell.GetAdjacentCell(direction); if (adjacentCell != null) { Air adjacentAir = adjacentCell.Air; adjacentAir.TransmitSound(soundLevel - 1); } } }
/// <summary> /// Constructs a new Cell. /// </summary> /// <param name="pigWorld"></param> /// <param name="position"></param> public Cell(PigWorld pigWorld, Position position) { this.pigWorld = pigWorld; this.position = position; air = new Air(this); }