beginner.adoc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. = Zay-ES Beginner Tutorial
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :relfileprefix: ../../../
  6. :imagesdir: ../../..
  7. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  8. In this article we explain the first steps how to work with Zay-ES and what you should know about the background.
  9. If you are new Entity Systems please read the <<jme3/contributions/entitysystem/introduction#,Entity System Introduction>> first.
  10. == Sample Code
  11. [source,java]
  12. ----
  13. import com.simsilica.es.Entity;
  14. import com.simsilica.es.EntityData;
  15. import com.simsilica.es.EntityId;
  16. import com.simsilica.es.base.DefaultEntityData;
  17. public class Main {
  18. public static void main(String[] args) {
  19. //Creating the EntityData
  20. EntityData entityData = new DefaultEntityData();
  21. //Creates a new EntityId, the id is handled as an object to prevent botching
  22. EntityId entityId = entityData.createEntity();
  23. //A new TestComponent is added to the Entity
  24. entityData.setComponent(entityId, new TestComponent("Hello World"));
  25. //Get a new Entity Object with TestComponents
  26. Entity entity = entityData.getEntity(entityId, TestComponent.class);
  27. //Get the Component and display the value
  28. TestComponent testComponent = entity.get(TestComponent.class);
  29. System.out.println(testComponent.getValue());
  30. //Overwrite the existing component
  31. entity.set(new TestComponent("New Value"));
  32. System.out.println(testComponent.getValue());
  33. //Remove the Entity from the data
  34. entityData.removeEntity(entity.getId());
  35. }
  36. }
  37. ----
  38. === Description of the sample
  39. == Create a Component Class
  40. You simply create a component by implementing the EntityComponent interface:
  41. [source,java]
  42. ----
  43. public class TestComponent implements EntityComponent {
  44. private String value;
  45. public TestComponent(String value)
  46. {
  47. this.value=value;
  48. }
  49. public String getValue()
  50. {
  51. return value;
  52. }
  53. }
  54. ----
  55. But there are some rules you must consider:
  56. Components only have a constructor and getter.
  57. 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.
  58. Besides an entity can only have one component of the same class.
  59. [IMPORTANT]
  60. ====
  61. Beginners tend to add large objects like spatials and geometries to a component.
  62. 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.
  63. ====
  64. [IMPORTANT]
  65. ====
  66. Never subclass Component classes.
  67. ====
  68. == Initialize the Entity System
  69. [source,java]
  70. ----
  71. EntityData entityData = new DefaultEntityData();
  72. ----
  73. The EntityData is the main class of the entity system. All the data are stored here.
  74. How the entityData works:
  75. 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.
  76. == Creating Entitys and adding Components
  77. [source,java]
  78. ----
  79. //Creates a new EntityId, the id is handled as an object to prevent botching
  80. EntityId entityId = entityData.createEntity();
  81. //A new TestComponent is added to the Entity
  82. entityData.setComponent(entityId, new TestComponent("Hello World"));
  83. ----
  84. EntityIds are a objects which contain a long value. Zay-ES uses this objects to prevent users from writing dirty code.
  85. Every entity has a unique id which is a long.
  86. Ids are not reused because, if they were, they would not be unique anymore, which would cause a huge penalty.
  87. [TIP]
  88. ====
  89. Often people are scared that they will run out of ids:
  90. If you create a new entity every nano second, you would need roughly 585 years before it wraps.
  91. ====
  92. == The Entity Class
  93. [source,java]
  94. ----
  95. //Get a new Entity Object with TestComponents
  96. Entity entity = entityData.getEntity(entityId, TestComponent.class);
  97. //Get the Component and display the value
  98. TestComponent testComponent = entity.get(TestComponent.class);
  99. System.out.println(testComponent.getValue());
  100. ----
  101. If you want to work with entities, the EntityData is able to create Entity objects. This objects contains
  102. all the Components of the classes you are interested in. In this example it is only the TestComponent.class.
  103. You can have multiple Entity objects for the same entity.
  104. [IMPORTANT]
  105. ====
  106. The data of this Entity objects will not be updated if other classes change the components for this entity
  107. ====
  108. == Replacing a component
  109. [source,java]
  110. ----
  111. //Overwrite the existing component
  112. entity.set(new TestComponent("New Value"));
  113. System.out.println(testComponent.getValue());
  114. ----
  115. == Delete an entity
  116. [source,java]
  117. ----
  118. //Remove the Entity from the data
  119. entityData.removeEntity(entity.getId());
  120. ----
  121. === Entity Sets
  122. The most important feature of Zay-ES are the Entity Sets.
  123. It is strongly recommended that you read the <<jme3/contributions/entitysystem/entityset#,Entity Set tutorial>> after reading this article.
  124. [IMPORTANT]
  125. ====
  126. Read the <<jme3/contributions/entitysystem/entityset#,tutorial>> about entity sets
  127. ====