GetNeighbours

This method allows you to find and retrieve the neighbors of a particular Ball in your game. You can specify both the type of Ball you are interested in and a condition to further filter these neighbouring balls.

Syntax:

var filteredNeighbours = LevelUtils.GetNeighbours<T>(Ball ball, Func<T, bool> filter);

Parameters:

  • T : The type of the Ball that you want to find among the neighbours. This could be any class that derives from the Ball base class (like ColorBall for instance).

  • ball : The Ball whose neighbours you want to find.

  • filter : A Func<T, bool> that represents the condition to further filter the neighbours. This is a function that takes a Ball of type T and returns a boolean. If the function returns true for a specific Ball, then that Ball will be included in the returned array of neighbours.

Example:

var filteredNeighbours = LevelUtils.GetNeighbours<ColorBall>(someBall, ball => ball.GetColor() == targetColor);

In this example, GetNeighbours is called to find all neighboring ColorBalls of someBall that have the same color as targetColor. The method returns an array of these ColorBalls.

Here, ball => ball.GetColor() == targetColor is a lambda expression that represents the filter function. It checks if the color of a ColorBall is the same as targetColor.

This method is powerful because it allows you to customize the condition for filtering neighbours, resulting in more flexibility when designing game mechanics. For example, you could easily find all ColorBalls of a certain color, or BombBalls that are about to explode, etc.

Last updated