I am writing a simple shoot'em up game in Java using LibGDX as the main library. I would like that any enemy in the game could perform any sort of movement, regardless their type. The problem arises when trying to isolate the logic of the movements an AI from each Enemy class.
A more graphic example:
Enemies:
What would be the best approach?
Use an entity system library like https://github.com/libgdx/ashley or https://github.com/junkdog/artemis-odb.
You would then create systems:
LinearTopBottomMovementSystem
ChasePlayerMovementSystem
ShowUpSystem
ShootSystem
(you probably also want anBulletMovementSystem
)FleeSystem
Then create components:
LinearTopBottomMovementComponent
ChasePlayerMovementComponent
ShowUpComponent
ShootComponent
FleeComponent
When creating enemies (entities) you create:
LinearTopBottomMovementComponent
ChasePlayerMovementComponent
ShowUpComponent
ShowUpComponent
is handled (by the ShowUpSystem
), remove this component and add ShootComponent
ShootComponent
is handled (by the ShootSystem
), remove this component and add FleeComponent
This allows you to create enemies with all possible component combinations and you can add multiple components to one entity.
If you decide to add circle movement and permanent fire enemy. Just create CircleMovementComponent
and PermanentFireComponent
and corresponding systems.