Creating the cards: an introduction to ECS systems, part 2
The next system we are going to write is the one responsible for creating the 54 cards that form the game's dungeon deck. We name this system CardCreationSystem and define it as follows:
Note how we introduce the usage of an entity archetype for our card entities. An entity archetype is a blueprint that defines the set of components that all the entities of the archetype will have. In our case, all card entities will have a CardData component, a type-specific component and a StatData component (except for abilities). Using an entity archetype when creating an entity is more efficient than manually setting up every component independently, as the internal memory used by the entity can be arranged in one go. Our card archetypes are defined in the CardArchetypes utility class:
Note the use of the entity manager's CreateArchetype method to create the entity archetypes. This method takes a list of the component types that define the archetype.
Finally, we use several helper methods to create our 54-card dungeon deck:
- 19 monster cards
- 6 sword cards
- 6 shield cards
- 9 potion cards
- 9 coin cards
- 5 ability cards
The specific numbers are the ones used in the original Card Crawl game; you can find the details here. Nothing fancy in this part of the code; we just set the data of the created entities as appropriate.
At this point, you can already run this code; just hit Play in the Unity editor and open the Entity Debugger option located in the Window/Analysis menu:
As you can see, there is no additional setup to make our ECS systems run; they are automatically picked up by Unity.
In the entity debugger, you can see which systems (some of them are predefined ones that come with the ECS) and entities are running. You can even selectively disable certain systems and inspect the details of any particular entity. Pretty cool!
It is expected that, as the ECS matures, a full-fledged integration with the Unity editor similar to the one that already exists for GameObjects and MonoBehaviours will be added to the engine.