usage.adoc 2.4 KB

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