/** * 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; }
/** * 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; }
/** * 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 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; }
/** * 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); } }