Collections

MakersDefense should be able to handle a group of objects, i.e. many of invaders and many of towers. Therfore, we need to use a collection of objects.

We want to create a ion of path that will be passed in the game.

try
{
    MapLocation[] path =
    {
        new MapLocation(0, 2, map),
        new MapLocation(1, 2, map),
        new MapLocation(2, 2, map),
        new MapLocation(3, 2, map),
        new MapLocation(4, 2, map),
        new MapLocation(5, 2, map),
        new MapLocation(6, 2, map),
        new MapLocation(7, 2, map)
    };
}

But, path is just a variable, we should define how pathis being used. In short, we need to create an pathas an object.

It brings us to next OOP principles, encapsulation.

A good example of encapsulation in the physical world is a car. Cars are complex machine with lots of moving parts inside. The good point is we don't have to know how all this parts work, we only have to know to use brake, steering, pedal, gear shifter. So, if we use another car with different engine, we can still drive it.

In order to use our patharray as easy as we want, we have to encapsulate the array into class. Code in path class looks like this.

class Path
{
    private readonly MapLocation[] _path;

    public Path(MapLocation[] path)
    {
        //this.path = path;
        _path = path;
    }
}

results matching ""

    No results matching ""