123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- = Zay-ES Beginner Tutorial
- :revnumber: 2.0
- :revdate: 2020/07/25
- In this article we explain the first steps how to work with Zay-ES and what you should know about the background.
- If you are new Entity Systems please read the xref:es/entitysystem/introduction.adoc[Entity System Introduction] first.
- == Sample Code
- [source,java]
- ----
- import com.simsilica.es.Entity;
- import com.simsilica.es.EntityData;
- import com.simsilica.es.EntityId;
- import com.simsilica.es.base.DefaultEntityData;
- public class Main {
- public static void main(String[] args) {
- //Creating the EntityData
- EntityData entityData = new DefaultEntityData();
- //Creates a new EntityId, the id is handled as an object to prevent botching
- EntityId entityId = entityData.createEntity();
- //A new TestComponent is added to the Entity
- entityData.setComponent(entityId, new TestComponent("Hello World"));
- //Get a new Entity Object with TestComponents
- Entity entity = entityData.getEntity(entityId, TestComponent.class);
- //Get the Component and display the value
- TestComponent testComponent = entity.get(TestComponent.class);
- System.out.println(testComponent.getValue());
- //Overwrite the existing component
- entity.set(new TestComponent("New Value"));
- System.out.println(testComponent.getValue());
- //Remove the Entity from the data
- entityData.removeEntity(entity.getId());
- }
- }
- ----
- === Description of the sample
- == Create a Component Class
- You simply create a component by implementing the EntityComponent interface:
- [source,java]
- ----
- public class TestComponent implements EntityComponent {
- private String value;
- public TestComponent(String value)
- {
- this.value=value;
- }
- public String getValue()
- {
- return value;
- }
- }
- ----
- But there are some rules you must consider:
- Components only have a constructor and getter.
- It is important that you only store pure data in the components because otherwise you would no longer know where you can find the logic and the approach of a clear software design would get lost.
- Besides an entity can only have one component of the same class.
- [IMPORTANT]
- ====
- Beginners tend to add large objects like spatials and geometries to a component.
- This is a big mistake because such objects contain logic and in our approach components are data only. Abstract it to a general level or store it completely in the systems.
- ====
- [IMPORTANT]
- ====
- Never subclass Component classes.
- ====
- == Initialize the Entity System
- [source,java]
- ----
- EntityData entityData = new DefaultEntityData();
- ----
- The EntityData is the main class of the entity system. All the data are stored here.
- How the entityData works:
- For every component class a new Hashmap is created which contains the entityId as a key and the component as a value. Therefore, if you need to know all entities which own a component of a certain type, the system will search in these Hashmaps for the required entityIds.
- == Creating Entitys and adding Components
- [source,java]
- ----
- //Creates a new EntityId, the id is handled as an object to prevent botching
- EntityId entityId = entityData.createEntity();
- //A new TestComponent is added to the Entity
- entityData.setComponent(entityId, new TestComponent("Hello World"));
- ----
- EntityIds are a objects which contain a long value. Zay-ES uses this objects to prevent users from writing dirty code.
- Every entity has a unique id which is a long.
- Ids are not reused because, if they were, they would not be unique anymore, which would cause a huge penalty.
- [TIP]
- ====
- Often people are scared that they will run out of ids:
- If you create a new entity every nano second, you would need roughly 585 years before it wraps.
- ====
- == The Entity Class
- [source,java]
- ----
- //Get a new Entity Object with TestComponents
- Entity entity = entityData.getEntity(entityId, TestComponent.class);
- //Get the Component and display the value
- TestComponent testComponent = entity.get(TestComponent.class);
- System.out.println(testComponent.getValue());
- ----
- If you want to work with entities, the EntityData is able to create Entity objects. This objects contains
- all the Components of the classes you are interested in. In this example it is only the TestComponent.class.
- You can have multiple Entity objects for the same entity.
- [IMPORTANT]
- ====
- The data of this Entity objects will not be updated if other classes change the components for this entity
- ====
- == Replacing a component
- [source,java]
- ----
- //Overwrite the existing component
- entity.set(new TestComponent("New Value"));
- System.out.println(testComponent.getValue());
- ----
- == Delete an entity
- [source,java]
- ----
- //Remove the Entity from the data
- entityData.removeEntity(entity.getId());
- ----
- === Entity Sets
- The most important feature of Zay-ES are the Entity Sets.
- It is strongly recommended that you read the xref:es/entitysystem/entityset.adoc[Entity Set tutorial] after reading this article.
- [IMPORTANT]
- ====
- Read the xref:es/entitysystem/entityset.adoc[tutorial] about entity sets
- ====
|