Search in sources :

Example 1 with OwnerComponent

use of com.gemserk.commons.artemis.components.OwnerComponent in project commons-gdx by gemserk.

the class RenderableComponentComparator method compare.

@Override
public int compare(Entity o1, Entity o2) {
    EntityComponents entity1Components = factory.get(o1);
    EntityComponents entity2Components = factory.get(o2);
    RenderableComponent c1 = entity1Components.renderableComponent;
    RenderableComponent c2 = entity2Components.renderableComponent;
    if (c1.renderable.getLayer() != c2.renderable.getLayer())
        return c1.renderable.getLayer() - c2.renderable.getLayer();
    OwnerComponent ownerComponent1 = entity1Components.ownerComponent;
    OwnerComponent ownerComponent2 = entity2Components.ownerComponent;
    int id1 = o1.getId();
    int id2 = o2.getId();
    if (ownerComponent1 != null) {
        Entity owner = ownerComponent1.getOwner();
        if (owner != null)
            id1 = owner.getId();
    }
    if (ownerComponent2 != null) {
        Entity owner = ownerComponent2.getOwner();
        if (owner != null)
            id2 = owner.getId();
    }
    if (id1 != id2)
        return id1 - id2;
    return c1.renderable.getSubLayer() - c2.renderable.getSubLayer();
}
Also used : Entity(com.artemis.Entity) OwnerComponent(com.gemserk.commons.artemis.components.OwnerComponent) RenderableComponent(com.gemserk.commons.artemis.components.RenderableComponent)

Example 2 with OwnerComponent

use of com.gemserk.commons.artemis.components.OwnerComponent in project commons-gdx by gemserk.

the class RenderableSystem method enabled.

@Override
protected void enabled(Entity e) {
    RenderableComponent renderableComponent = RenderableComponent.get(e);
    OwnerComponent ownerComponent = OwnerComponent.get(e);
    Renderable renderable = renderableComponent.renderable;
    // this could be set on construction time
    renderable.setEntity(e);
    renderable.setId(e.getId());
    // if it has owner it uses the id of the owner... that was part of the original comparator
    if (ownerComponent != null && ownerComponent.getOwner() != null)
        renderable.setId(ownerComponent.getOwner().getId());
    // order the entity in the Layer, probably the same inside the layer
    for (int i = 0; i < renderLayers.size(); i++) {
        RenderLayer renderLayer = renderLayers.get(i);
        if (renderLayer.belongs(renderable)) {
            renderLayer.add(renderable);
            return;
        }
    }
}
Also used : Renderable(com.gemserk.commons.artemis.render.Renderable) OwnerComponent(com.gemserk.commons.artemis.components.OwnerComponent) RenderableComponent(com.gemserk.commons.artemis.components.RenderableComponent)

Example 3 with OwnerComponent

use of com.gemserk.commons.artemis.components.OwnerComponent in project commons-gdx by gemserk.

the class OwnerSystem method disabled.

@Override
protected void disabled(Entity e) {
    OwnerComponent ownerComponent = OwnerComponent.get(e);
    if (ownerComponent.getOwner() == null)
        return;
    ContainerComponent containerComponent = ContainerComponent.get(ownerComponent.getOwner());
    if (containerComponent == null)
        return;
    containerComponent.getChildren().remove(e);
}
Also used : OwnerComponent(com.gemserk.commons.artemis.components.OwnerComponent) ContainerComponent(com.gemserk.commons.artemis.components.ContainerComponent)

Example 4 with OwnerComponent

use of com.gemserk.commons.artemis.components.OwnerComponent in project commons-gdx by gemserk.

the class OwnerSystem method enabled.

@Override
protected void enabled(Entity e) {
    OwnerComponent ownerComponent = OwnerComponent.get(e);
    if (ownerComponent.getOwner() == null)
        return;
    ContainerComponent containerComponent = ContainerComponent.get(ownerComponent.getOwner());
    if (containerComponent == null)
        return;
    containerComponent.getChildren().add(e);
}
Also used : OwnerComponent(com.gemserk.commons.artemis.components.OwnerComponent) ContainerComponent(com.gemserk.commons.artemis.components.ContainerComponent)

Example 5 with OwnerComponent

use of com.gemserk.commons.artemis.components.OwnerComponent in project commons-gdx by gemserk.

the class ContainerSystemTest method shouldRemoveChildrenIfParentRemoved.

@Test
public void shouldRemoveChildrenIfParentRemoved() {
    World world = new World();
    WorldWrapper worldWrapper = new WorldWrapper(world);
    worldWrapper.addUpdateSystem(new ContainerSystem());
    worldWrapper.addUpdateSystem(new OwnerSystem());
    worldWrapper.init();
    Entity e1 = world.createEntity();
    e1.addComponent(new ContainerComponent());
    e1.refresh();
    Entity e2 = world.createEntity();
    e2.addComponent(new OwnerComponent(e1));
    e2.refresh();
    worldWrapper.update(10);
    world.deleteEntity(e1);
    worldWrapper.update(1);
    worldWrapper.update(1);
    Entity e3 = world.getEntity(e2.getId());
    assertThat(e3, IsNull.nullValue());
}
Also used : Entity(com.artemis.Entity) WorldWrapper(com.gemserk.commons.artemis.WorldWrapper) OwnerComponent(com.gemserk.commons.artemis.components.OwnerComponent) ContainerComponent(com.gemserk.commons.artemis.components.ContainerComponent) World(com.artemis.World) Test(org.junit.Test)

Aggregations

OwnerComponent (com.gemserk.commons.artemis.components.OwnerComponent)18 Entity (com.artemis.Entity)15 World (com.artemis.World)14 Test (org.junit.Test)14 RenderableComponent (com.gemserk.commons.artemis.components.RenderableComponent)13 ContainerComponent (com.gemserk.commons.artemis.components.ContainerComponent)5 WorldWrapper (com.gemserk.commons.artemis.WorldWrapper)3 EntityProcessingSystem (com.artemis.EntityProcessingSystem)1 StoreComponent (com.gemserk.commons.artemis.components.StoreComponent)1 Renderable (com.gemserk.commons.artemis.render.Renderable)1 EntityStore (com.gemserk.commons.artemis.utils.EntityStore)1