Loops and Final Touches
We already have all we need in Invader class. Now, let's start code in Tower class.
First, we need to have a location from MapLocation class. Then, we assign a constructor to it.
class Tower
{
private readonly MapLocation _location;
public Tower(MapLocation location)
{
_location = location;
}
}
Tower should has an ability to shot the invader. Let's create a method to shot the invader.
The idea is checking all of the invaders and determine if they are in range or not. Then, shoot at them and decrease their health. Therfore, we need looping statement to loop for all the invaders.
public void FireOnInvaders(Invader[] invaders)
{
for (int i = 0; i < invaders.Length; i++)
{
Invader invader = invaders[i];
}
}
or could be like this.
public void FireOnInvaders(Invader[] invaders)
{
foreach (Invader invader in invaders)
{
}
}