Once your properties are configured you have a choice about how your enemy will be designed. There are two patterns you can use for a standard enemy: the Basic Enemy pattern and the AI Enemy pattern.
For more complex enemies, like bosses, you may want to consider the Sequence Driven Enemy.
Note: you can also build very complex enemies as an extension of the Character class but that is outside the scope of this discussion. Drop us a support request to find out more.
A basic enemy pattern uses two controlling components:
- the Enemy component which contains details of the enemy such as its health and collision boundaries,
- the Movement component which is a subclass of the EnemyMovement class and controls how the character moves.
Each frame the Enemy object passes control to them movement in a similar manner to a Character movement.
You may optionally add an EnemyDeathMovement which will take over movement control when the enemy is damaged or when the enemy dies.
This pattern allows you to quickly create enemies with simple behaviour like patrolling back and forth. You may also use this pattern to create more complex behaviour by extending the EnemyMovement class with your own implementation.
The drawback of this approach for complex enemies is that the most of the behaviour is embedded in the movement class, making it difficult to share only parts of the behaviour between enemies. For example you might want many enemies to detect and shoot at the character in a similar way, even though they move in many different ways (fly, run, walk, swim, etc).
Instead of having one movement the AI Enemy has a MovementDistributor which associates different enemy states with many different movements. The enemies state is decided by an EnemyAI component which picks an enemy state based on its own logic (for example an enemy AI might change to the shooting state when a Character is close by).
This pattern allows you to create reusable AI components and reusable movement components which you can combine to create new enemies.
The drawback of this approach is that it can be more complex to configure and debug your enemies. The following diagram might help you better understand the control flow of an AI Enemy:
Tip: The Enemy class also provides a debugging view which can help with this.
Choosing an Enemy Pattern
Most of the sample use the AI Enemy pattern so that their component parts can be easily reused in your own games but this does not mean you need use this pattern.
There are no fixed rules but here are some dot points:
- If your enemies all have very distinct movements with very little shared behaviours you should consider the Basic Enemy pattern. You can still paramatize aspects of the movement to allow for some variation.
- If your enemies sense the character you should consider the AI Enemy pattern as there are examples of sensing the character already available.
- If your enemies often have some shared movements (for example they all shoot in a similar way) but other different movements (some fly, some walk, etc) you should consider the AI Enemy pattern.
- For more complex enemies, like bosses, you may want to consider the Sequence Driven Enemy.