Imagine e.g. a 2D-platformer boss fight with one central enemy but with multiple phases. How would you program that?
I usually do that with a switch-case in the looped method ("void Update()" in Unity) and add a variable “status” which keeps track of the current action the boss is doing (moving to specific point, idle animation, circling, attack1, etc.). After a certain action is done I then change the status so that it goes into another “case”.
However I often run into the problem that the code gets to confusing over time because of the too long switch-case and the even longer list of methods which are declared there further downwards.
My question is, if there are better methods to code such a boss fight that consists of many linear actions but randomly chooses an action/string of actions
Here an example how I used switch-case and the status variable for a boss' behaviour:
void Update()
{
switch (status)
{
case 0: //Circle Movenment
Circling(Random.Range(360, 600), x, 1, 2.5f, 1.5f, 120f, false);
break;
case 1: //Preperation for 2. (Moving to Startingpoint)
if(fromRight)
{
MoveTo(7, 4, 3, 60, 2);
}
else
{
MoveTo(-7, 4, 3, 60, 2);
}
break;
case 2: //Flies above the ground left->right or right->left
Hyperbole-Movenment(speed, fromRight);
break;
case 3: //Move back to origin
if (fromRight)
{
MoveTo(origin.x, origin.y - 1.5f, 3, 0, 0);
}
else
{
MoveTo(origin.x, origin.y + 1.5f, 3, 0, 0);
}
break;
}