An easy to use Java Library for creating games using the Entity Component System architecture.
Introduction
Students often want to build games when learning how to code. Games can be a very useful medium for exploring topics such as object oriented programming, graphics and optimisation however the learning curve to starting to make games can be steep. I was working with a student in Java and although there are multiple Java Game Development Libraries out there I found that they all had significant boiler plate which would either need a lot of explaining or would simply need to be brushed past in order to start making the game. I decided to create a simple game engine which would allow my student to focus more on simple concepts like variables and functions.
Entity Component System
I took inspiration from Unreal and Unity which both use their own versions of the Entity Component System architecture (ECS). I decided to use ECS for three reasons:
1. It would allow for an easier transition to other engines that use the same architecture
2. I could create as many components and systems as necessary and my student could focus on creating entities.
3. I was interested in implementing a simple ECS in Java
Before continuing I will briefly explain how an ECS works.
Entity
The enties are the objects you see and interact with in your game. For example the player, the pokemon or the tennis ball. In their most basic form they are an id which is associated with a list of components. This means that when creating an entity the student can pick and choose which components the entity has and what their values are, they don't have to worry about how the components and their associated systems are implemented, not to mention entities don't include components they don't need.
Components
A component is a property of an entity for example sprite, acceleration, collision box. These are fairly common so most entities will have these although it's worth noting you might have an entity that is not visible so does not need a sprite component. A component should be exclusively data, for example the Transform component is just an (x, y) vector. The way the data is acted upon should only be controlled by the systems. The engine should include some standard components but every game will need its own custom ones, for example a health component.
Systems
Systems are the driving force behind the game engine that act upon components. You might have a rendering system which uses the sprite component and the position component and draws the sprite at the given position. You might have a movement system which updates the position component using the velocity component. Importantly systems should store as little data as possible and will almost exclusively be functions / methods. As with components there can be a mixture of built in common systems and custom ones.
Usage
The engine was a huge success with my student and we made a Zelda-like game together where a player could explore a tilemap and interact with various items (entities) that they found.
I also found unexpected benefits from using the ECS archictecture. For example every scene has a camera which is an entity. Normally you could set the camera to follow the player's transform component as they move so the player is centered on the screen, but if you wanted to you could have it follow another entities' transform component or indeed give the camera it's own transform component with a custom path to follow allowing for cut scenes.
Conclusion
It is worth noting that although this engine removes the boilerplate offered by a lot of other engines, it does involve a fairly lengthy process of setting up, for example, animations which need to be hand coded into mildly complicated ArrayLists (this was partly due to ArrayLists being a topic I was teaching so was a good opportunity for practice). Perhaps an even more user friendly solution would be to bundle a small app with the engine which gives users a GUI to make custom animations which can then be referred to from within their code.
All in all I consider the project a success for my student as well as for myself as it taught me a considerable amount about Entity Component Systems.
Technologies Used