Shooting Range

Where we should put method that related to shooting range in map? Yes, MapLocation class.

We would like to create a method that validate if invader is in range or not.

public bool InRangeOf(MapLocation location, int range)
{
    return distanceTo(location) <= range;
}

Back to the Tower class. Now, we want to create conditional statement to check whether invader in range of 1 or not. If yes, the invader's healt will decreased by 1.

public void FireOnInvaders(Invader[] invaders)
{
    foreach (Invader invader in invaders)
    {
        if (_location.InRangeOf(invader.Location, 1))
        {
            invader.decreaseHealth(1);
        }
    }
}

A little bit of perfection. IsActive method is to check Invader is on active mode or not, while break to exit the loop function after tower hit and decrease the heath of invader.

if (invader.IsActive && _location.InRangeOf(invader.Location, 1))
{
    invader.decreaseHealth(1);
    break;
}

results matching ""

    No results matching ""