/** * Create and return a new or reused entity id. Entity is * automatically added to the world. * * @return assigned entity id, where id >= 0. */ public int create() { int entityId = em.create(); batchProcessor.changed.unsafeSet(entityId); return entityId; }
/** * Get entity with the specified id. * * Resolves entity id to the unique entity instance. <em>This method may * return an entity even if it isn't active in the world.</em> Make sure to * not retain id's of deleted entities. * * @param entityId * the entities id * @return the specific entity */ public Entity getEntity(int entityId) { return em.getEntity(entityId); }
/** * Get entity editor for entity. * @return a fast albeit verbose editor to perform batch changes to entities. * @param entityId entity to fetch editor for. */ public EntityEdit edit(int entityId) { if (!em.isActive(entityId)) throw new RuntimeException("Issued edit on deleted " + entityId); return batchProcessor.obtainEditor(entityId); }
/** * Create a new entity. * * @return a new entity */ protected Entity createEntityInstance() { return obtain(); }
/** * Create and return a new or reused entity instance. Entity is * automatically added to the world. * * @return entity * @see #create() recommended alternative. */ public Entity createEntity() { Entity e = em.createEntityInstance(); batchProcessor.changed.unsafeSet(e.getId()); return e; }
private Entity obtain() { if (limbo.isEmpty()) { return createEntity(nextId++); } else { int id = limbo.popFirst(); recycled.unsafeClear(id); return entities.get(id); } }
/** * Process all non-passive systems. * @see InvocationStrategy to control and extend how systems are invoked. */ public void process() { invocationStrategy.process(); IntBag pendingPurge = batchProcessor.getPendingPurge(); if (!pendingPurge.isEmpty()) { cm.clean(pendingPurge); em.clean(pendingPurge); batchProcessor.purgeComponents(); } }
void clean(IntBag pendingDeletion) { int[] ids = pendingDeletion.getData(); for(int i = 0, s = pendingDeletion.size(); s > i; i++) { int id = ids[i]; // usually never happens but: // this happens when an entity is deleted before // it is added to the world, ie; created and deleted // before World#process has been called if (!recycled.unsafeGet(id)) { free(id); } } }
/** * Instantiates an Entity without registering it into the world. * @param id The ID to be set on the Entity */ private Entity createEntity(int id) { Entity e = new Entity(world, id); if (e.id >= entities.getCapacity()) { growEntityStores(); } // can't use unsafe set, as we need to track highest id // for faster iteration when syncing up new subscriptions // in ComponentManager#synchronize entities.set(e.id, e); return e; }
/** * Creates a new world. * <p> * {@link com.artemis.EntityManager}, {@link ComponentManager} and {@link AspectSubscriptionManager} are * available by default, on top of your own systems. * </p> * @see WorldConfigurationBuilder * @see WorldConfiguration */ public World(WorldConfiguration configuration) { partition = new WorldSegment(configuration); systemsBag = configuration.systems; final ComponentManager lcm = (ComponentManager) systemsBag.get(COMPONENT_MANAGER_IDX); final EntityManager lem = (EntityManager) systemsBag.get(ENTITY_MANAGER_IDX); final AspectSubscriptionManager lasm = (AspectSubscriptionManager) systemsBag.get(ASPECT_SUBSCRIPTION_MANAGER_IDX); cm = lcm == null ? new ComponentManager(configuration.expectedEntityCount()) : lcm; em = lem == null ? new EntityManager(configuration.expectedEntityCount()) : lem; asm = lasm == null ? new AspectSubscriptionManager() : lasm; batchProcessor = new BatchChangeProcessor(this); alwaysDelayComponentRemoval = configuration.isAlwaysDelayComponentRemoval(); configuration.initialize(this, partition.injector, asm); }
/** * Create a new entity. * * @return a new entity id */ protected int create() { return obtain().id; }
/** * Create and return a new or reused entity instance. Entity is * automatically added to the world. * * @return entity * @see #create() recommended alternative. */ public Entity createEntity() { Entity e = em.createEntityInstance(); batchProcessor.changed.unsafeSet(e.getId()); return e; }
private Entity obtain() { if (limbo.isEmpty()) { return createEntity(nextId++); } else { int id = limbo.popFirst(); recycled.unsafeClear(id); return entities.get(id); } }
/** * Process all non-passive systems. * @see InvocationStrategy to control and extend how systems are invoked. */ public void process() { invocationStrategy.process(); IntBag pendingPurge = batchProcessor.getPendingPurge(); if (!pendingPurge.isEmpty()) { cm.clean(pendingPurge); em.clean(pendingPurge); batchProcessor.purgeComponents(); } }
void clean(IntBag pendingDeletion) { int[] ids = pendingDeletion.getData(); for(int i = 0, s = pendingDeletion.size(); s > i; i++) { int id = ids[i]; // usually never happens but: // this happens when an entity is deleted before // it is added to the world, ie; created and deleted // before World#process has been called if (!recycled.unsafeGet(id)) { free(id); } } }
/** * Instantiates an Entity without registering it into the world. * @param id The ID to be set on the Entity */ private Entity createEntity(int id) { Entity e = new Entity(world, id); if (e.id >= entities.getCapacity()) { growEntityStores(); } // can't use unsafe set, as we need to track highest id // for faster iteration when syncing up new subscriptions // in ComponentManager#synchronize entities.set(e.id, e); return e; }
/** * Creates a new world. * <p> * {@link com.artemis.EntityManager}, {@link ComponentManager} and {@link AspectSubscriptionManager} are * available by default, on top of your own systems. * </p> * @see WorldConfigurationBuilder * @see WorldConfiguration */ public World(WorldConfiguration configuration) { partition = new WorldSegment(configuration); systemsBag = configuration.systems; final ComponentManager lcm = (ComponentManager) systemsBag.get(COMPONENT_MANAGER_IDX); final EntityManager lem = (EntityManager) systemsBag.get(ENTITY_MANAGER_IDX); final AspectSubscriptionManager lasm = (AspectSubscriptionManager) systemsBag.get(ASPECT_SUBSCRIPTION_MANAGER_IDX); cm = lcm == null ? new ComponentManager(configuration.expectedEntityCount()) : lcm; em = lem == null ? new EntityManager(configuration.expectedEntityCount()) : lem; asm = lasm == null ? new AspectSubscriptionManager() : lasm; batchProcessor = new BatchChangeProcessor(this); alwaysDelayComponentRemoval = configuration.isAlwaysDelayComponentRemoval(); configuration.initialize(this, partition.injector, asm); }
/** * Get entity editor for entity. * @return a fast albeit verbose editor to perform batch changes to entities. * @param entityId entity to fetch editor for. */ public EntityEdit edit(int entityId) { if (!em.isActive(entityId)) throw new RuntimeException("Issued edit on deleted " + entityId); return batchProcessor.obtainEditor(entityId); }
/** * Create a new entity. * * @return a new entity id */ protected int create() { return obtain().id; }
/** * Create and return an {@link Entity} wrapping a new or reused entity instance. * Entity is automatically added to the world. * * Use {@link Entity#edit()} to set up your newly created entity. * * You can also create entities using: * <ul> * <li>{@link com.artemis.utils.EntityBuilder} Convenient entity creation. Not useful when pooling.</li> * <li>{@link com.artemis.Archetype} Fastest, low level, no parameterized components.</li> * <li><a href="https://github.com/junkdog/artemis-odb/wiki/Serialization">Serialization</a>, * with a simple prefab-like class to parameterize the entities.</li> * </ul> * * @see #create() recommended alternative. * @return entity */ public Entity createEntity(Archetype archetype) { Entity e = em.createEntityInstance(); int id = e.getId(); archetype.transmuter.perform(id); cm.setIdentity(e.id, archetype.compositionId); batchProcessor.changed.unsafeSet(id); return e; }