I'm implementing something like the electric balls seen in this video https://www.youtube.com/watch?v=MWyB56AlAWw (skip to 1:55 and you will see this electrical balls follow tile edges in the tilemap).
I have an algorithm working but I feel like it is a bit of a mess. I could summarize it as:
- The ball starts always falling
- I have an enum holding what side the ball is engaged to (side of tile it is traveling. It could be Top,Bottom,Left, Right and None). This var is inited to "None" at start.
2) While the ball is falling, as soon as it touches a tile we set the engaged var to bottom (the ball is engaged to a tile that is at the bottom) and we make it move to the left.
3) Now we have 2 cases
- the ball collides with another tile while moving to the left. In this case we set the engaged var to Left and start moving it upwards.
- the ball gets into a situation where there's no tile at the bottom to continue going to the left. In this situation we use the engaged var to know what edge of the tile we were moving along. In this particular case as we where engaged to a bottom tile we fix the ball position to be right at the left of the last valid tile and start moving it down.
This applied to all possibilities makes the ball run across all the tilemap.
I would like to know if there's a more clean way to do this without all this "mini cases" or a better way to accomplish this.