usage.adoc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. = JMonkey Entity System Usage
  2. :revnumber: 2.0
  3. :revdate: 2020/07/24
  4. == Initialize the Entity System
  5. [source,java]
  6. ----
  7. EntitySystem entitySystem = new EntitySystem(new DefaultEntityData());
  8. ----
  9. == Writing an own Component
  10. [source,java]
  11. ----
  12. public class MovementComponent extends Component {
  13. private Vector3f movement;
  14. public MovementComponent(Vector3f movement)
  15. {
  16. this.movement=movement;
  17. }
  18. public Vector3f getMovement()
  19. {
  20. return movement;
  21. }
  22. }
  23. ----
  24. == Adding a new Entity
  25. [source,java]
  26. ----
  27. Component[] components = new Component[6];
  28. components[0] = new VisualRepComponent("Models/HoverTank/Tank2.mesh.xml");
  29. components[1] = new PositionComponent(v3f,new Quaternion());
  30. components[2] = new MovementComponent(Vector3f.ZERO);
  31. components[3] = new InSceneComponent(true);
  32. components[4] = new PlayerComponent();
  33. components[5] = new CollisionComponent(5);
  34. entitySystem.newEntity(components);
  35. ----
  36. == Creating a new EntitySet
  37. [source,java]
  38. ----
  39. Entity Set entitySet = entitySystem.getEntitySet(MovementComponent.class, PositionComponent.class);
  40. ----
  41. For the single parts of your program who deal with special components it is recommended to use xref:core:app/state/application_states.adoc[AppStates].
  42. == Loop through all entities
  43. [source,java]
  44. ----
  45. entitySet.applyChanges()
  46. Iterator<Entity> iterator = entitySet.getIterator();
  47. while (iterator.hasNext()) {
  48. Entity entity = iterator.next();
  49. //do something
  50. }
  51. ----
  52. == Only get changed Entities
  53. [source,java]
  54. ----
  55. if(entitySet.applyChanges())
  56. {
  57. entitySet.getAddedEntities(); //Get the Set of new added entities
  58. entitySet.getChangedEntities(); //Get the Set of changed entities
  59. entitySet.getRemovedEntities(); //Get the Set of removed entities
  60. }
  61. ----
  62. == Update a component
  63. [source,java]
  64. ----
  65. entity.setComponent(new PositionComponent(new Vector3f()));
  66. ----
  67. == Destroy an entity
  68. [source,java]
  69. ----
  70. entity.destroy();
  71. ----
  72. == How to display Spatials if it not allowed to save them into the components?
  73. Use an EntityControl class which is able to display Entitys with visual components.
  74. In an AppState with a Map containing Entities and and EntityControls they can be merged together and updated.
  75. //Have a look at the example:
  76. //link:http://peeeq.de/uploads/ogerlord/EntityTest.rar[http://peeeq.de/uploads/ogerlord/EntityTest.rar]