/** * Cleans up deleted Entry's components. Need to do it separately * to avoid freeing Components while other observers are processing * the removal of an entity. */ public void clean() { if (deletedEntities.size > 0) { for (Entity entity : deletedEntities) { removeComponentsOfEntity(entity); } deletedEntities.clear(); } cleanRemovedComponents(); }
/** * Create and return a new or reused component instance of specified type. * * @param <T> Type of component * @param type Type of component to return * @return Created component */ public <T extends Component> T createComponent(Class<T> type) { return cm.createComponent(type); }
/** * Slower retrieval of components from this entity. Minimize usage of this, * but is fine to use e.g. when creating new entities and setting data in * components. Use mappers instead. * * @param <T> * the expected return component type. * @param type * the expected return component type. * @return component that matches, or null if none is found. */ public <T extends Component> T getComponent(Class<T> type) { return componentManager.getComponent(this, type); }
/** * Returns a filter where an entity must possess one of the specified component types. * @param type one of the types the entity must possess * @param types one of the types the entity must possess * @return a filter that can be matched against entities */ public Filter any(Class<? extends Component> type, Class<? extends Component>... types) { anySet.set(ComponentManager.getComponentClassIndex(type)); for (Class<? extends Component> t : types) { anySet.set(ComponentManager.getComponentClassIndex(t)); } return this; }
/** * @return An array of Entity components. This is a generated array, * and modifying it will not have an effect on components belonging * to this entity. * * WARNING: This is an efficient way to access entitie's components. * Use with care. ComponentMapper is a faster and more efficient way to * access components. */ public Array<Component> getComponents() { return componentManager.getComponents(this); }
/** * Retrieves a ComponentMapper instance for fast retrieval of * components from entities. * * @param <T> Type of component * @param type of component to get mapper for. * @return mapper for specified component type. */ public <T extends Component> ComponentMapper<T> getMapper(Class<T> type) { return cm.getMapper(type); }
public World() { this(new ComponentManager(), new EntityManager()); }
/** * Add a component to this entity. * * @param component to add to this entity * * @return this entity for chaining. */ public Entity addComponent(Component component) { componentManager.addComponent(this, component); if (isActive()) { world.changedEntity(this); } return this; }
@Override public void dispose() { em.dispose(); cm.dispose(); added.clear(); changed.clear(); deleted.clear(); enable.clear(); disable.clear(); for (Manager manager : managers) { manager.dispose(); } managers.clear(); systems.clear(); if (eventSystem != null) { eventSystem.dispose(); eventSystem = null; } }
/** * Process all non-passive systems. */ public void process() { check(added, addedPerformer); check(changed, changedPerformer); check(disable, disablePerformer); check(enable, enablePerformer); check(deleted, deletedPerformer); cm.clean(); em.clean(); if (eventSystem != null) { eventSystem.update(); } for(int i = 0; i < systems.size; i++) { EntitySystem system = systems.get(i); if(!system.isPassive()) { system.process(); } } }
/** * Returns a filter where an entity must possess all of the specified component types. * @param type a required component type * @param types a required component type * @return a filter that can be matched against entities */ public Filter all(Class<? extends Component> type, Class<? extends Component>... types) { allSet.set(ComponentManager.getComponentClassIndex(type)); for (Class<? extends Component> t : types) { allSet.set(ComponentManager.getComponentClassIndex(t)); } return this; }
/** * Populates provided Array with this Entity's components. * * WARNING: This is an efficient way to access entitie's components. * Use with care. ComponentMapper is a faster and more efficient way to * access components. * * @param array to put the components into. */ public void getComponents(Array<Component> array) { componentManager.getComponents(this, array); }
/** * Excludes all of the specified component types from the filter. A system will not be * interested in an entity that possesses one of the specified exclusion component types. * * @param type component type to exclude * @param types component type to exclude * @return a filter that can be matched against entities */ public Filter exclude(Class<? extends Component> type, Class<? extends Component>... types) { exclusionSet.set(ComponentManager.getComponentClassIndex(type)); for (Class<? extends Component> t : types) { exclusionSet.set(ComponentManager.getComponentClassIndex(t)); } return this; }
/** * Returns an array of components for the specified entity. * The Array is generated newly every time and making changes to its * contents will not affect the components belonging to the entity. * * @param e Entity to get Components with. * @return an array of components belonging to entity. */ public Array<Component> getComponents(Entity e) { returnedComponents.clear(); getComponents(e, returnedComponents); return returnedComponents; }
/** * Returns Component of the specified type belonging to specified Entity. * Null if not found. * * @param <T> Type of component * @param e Entity to return Component for. * @param type Type of Component to return. * @return Component or null if not found. */ @SuppressWarnings("unchecked") public <T extends Component> T getComponent(Entity e, Class<T> type) { int classIndex = getComponentClassIndex(type); Array<T> components = (Array<T>) componentsByType.get(classIndex); if(components != null) { return components.get(e.id); } return null; }
public ComponentMapper(Class<A> type, World world) { components = world.getComponentManager().getComponents(type); }
/** * Returns an Array of all Components of specified type. * * @param <T> Type of component * @param type Type of Componets to return * @return an Array of said components. */ @SuppressWarnings("unchecked") public <T extends Component> Array<T> getComponents(Class<T> type) { int classIndex = getComponentClassIndex(type); Array<T> components = (Array<T>) componentsByType.get(classIndex); if(components == null) { components = new SafeArray<T>(); componentsByType.set(classIndex, components); } return components; }
/** * Adds a Component bellonging to the specified Entity to the manager. * * @param <T> Type of component * @param e Entity the component belongs to * @param component Component to add */ public <T extends Component> void addComponent(Entity e, T component) { int classIndex = getComponentClassIndex(component.getClass()); @SuppressWarnings("unchecked") Array<T> components = (Array<T>) componentsByType.get(classIndex); if(components == null) { components = new SafeArray<T>(); componentsByType.set(classIndex, components); } // clean up existing component belonging to the entity Component current = components.get(e.id); if (current != null && current != component) { Pools.free(current); } components.set(e.id, component); e.getComponentBits().set(classIndex); }
/** * Remove Component of specified class for a given Entity. * * @param e Entity to remove the component for. * @param type Component class to remove. */ public void removeComponent(Entity e, Class<? extends Component> type) { int classIndex = getComponentClassIndex(type); if(e.getComponentBits().get(classIndex)) { e.getComponentBits().clear(classIndex); if (!componentsToDelete.containsKey(e)) { componentsToDelete.put(e, Pools.obtain(IntArray.class)); } componentsToDelete.get(e).add(classIndex); } }