Entities
Open JavadocEntities are unique elements inside a world that can have components. Every entity has a Transform
component, which contains data about its position, parent and children.
Creating entities via prefabs
Entities can be created by instantiated prefabs into the world. Prefabs serve as a blueprint for entities, they determine which components an entity is initialized with, which tag, etc.
Here is an example:
-
Create a new sprite prefab.
SpritePrefab spritePrefab = new SpritePrefab("sprite.png"); -
Set properties of the prefab
spritePrefab.setColor(new Color(1f, 1f, 1f, 0.5f));spritePrefab.setScale(2f); -
Instantiate the prefab
Entity sprite = spritePrefab.instantiate(world);// OrEntity sprite = spritePrefab.instantiate(world, 0, 0);// OrEntity sprite = world.addEntity(spritePrefab);// OrEntity sprite = world.addEntity(spritePrefab, 0, 0); -
You will now have an entity at (0, 0) with a sprite component attached with a semi-transparent color and 2x scale.
Alternatively, if you want to add the entity as a child of another entity, you can use the method addChild(prefab)
, which will instantiate the prefab and add it as a child:
Entity sprite = parentEntity.addChild(spritePrefab);
Adding an entity to the world will also register its components to all systems and triggers any initialisation system that requires one of the classes of the entity’s components.
Toggling entities
Entities can be activated and deactivated, which will temporarily make all systems ignore their components. Once an inactive entity is activated, it will trigger any initialisation system that requires any classes of the entity’s components for said entity.
entity.setActive(false);entity.isActive(); // false
entity.setActive(true);entity.isActive(); // true