Programming a centered bee swarm

Finished version of the centered bee swarm following the player


The bee swarm in “Game of Beelonging” is one of the main parts of the game concept. Whilst playing
the game, there is a bee swarm constantly chasing the player, helping you out when enemies come to
close and assigning a new player on death of the main bee. The swarm is supposed to always be
vertically centered in a compact swarm, even if one of the bees die, which makes programming the
swarm quite difficult.
As I was aware that setting up the bee swarm was the most difficult part of creating “Game of
Beelonging” I started off with it right away.

I began creating ten empty game objects called “slots” representing  all the positions, each of the non
playable bees could go to and added a script to each of the slots containing a pointer to their current
bee, a pointer to the next slot and a boolean that tells if the slot is occupied or if it is not. The idea was
that each bee will constantly check if the next slot is occupied and if it is not, the bee will move to the
next slot. This also changes the pointer to the current bee occupying each slot.
However, there were two problems. First, the last slots pointer to the next slot was always empty,
which means that it would always detect the next slot as unoccupied and every bee in the last slot
would just move away making the next bee follow and triggering a chain reaction that makes every
bee leave the play screen. On the other hand the bee swarm was never centered. Although the bees
move up, when another bee dies, they would always just move one direction making it seem like the
swarm is attached either attached to the top or the bottom of the visible playfield.

To solve the first issue, I created another slot including a bee, that follows the player outside the visible
area. As no enemies appear at this location, this slot will always be occupied and prevent the bee in
the last slot to move up to the next slot, which is the one outside the playfield. I realise, that this is a
rather unconventional solution to a problem like this, but it does not only prevent bees from moving
out of the screen, but also removes null pointer exceptions.
the additional bee following the player outside the visible play area

The second problem was solved by putting all slots into a parent game object called “swarm” this game
object gets the average position of every bee in the swarm on the y-axis and according to the resulting
value adjusts the vertical position of every slot. I.e., if the average y-position of the bees is -1, the
script in the swarm will set the position of the slots to their initial position + 1. This way, the bees will
always vertically stay in the center of the screen.
All in all, I especially learned that it is important to try things out yourself and rely on the principle of
trial and error while solving a problem like this, instead of following tutorials and copying codes without
actually knowing what you are doing. Setting up the bee swarm for the game made me learn to not
only solve problems in creative ways, but also investing time in miscalculation and failure as a way of
understanding a problem at whole.

About Jan Plähn

2017 Programming