Search in sources :

Example 31 with Entity

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

the class EntityTemplateTest method weaponInstantiateBulletWithWeaponParameters.

@Test
public void weaponInstantiateBulletWithWeaponParameters() {
    // get the component from the weapon (the current entity)
    WeaponComponent weaponComponent = new WeaponComponent(560f, null);
    // custom template parameters of the weapon to be used when building a new bullet
    // this parameters are stored in each weapon instance.
    ParametersWrapper weaponBulletParameters = new ParametersWrapper();
    weaponBulletParameters.put("damage", weaponComponent.getDamage());
    // the interesting part here is that I could change it dynamically without changing the template or the instantiation call
    EntityTemplate bulletTemplate = new BulletEntityTemplate();
    EntityFactory entityFactory = new EntityFactoryImpl(new World());
    Entity bullet = entityFactory.instantiate(bulletTemplate, weaponBulletParameters);
    DamageComponent damageComponent = bullet.getComponent(DamageComponent.class);
    assertThat(damageComponent.getDamage(), IsEqual.equalTo(560f));
}
Also used : Entity(com.artemis.Entity) EntityFactoryImpl(com.gemserk.commons.artemis.templates.EntityFactoryImpl) EntityTemplate(com.gemserk.commons.artemis.templates.EntityTemplate) World(com.artemis.World) EntityFactory(com.gemserk.commons.artemis.templates.EntityFactory) ParametersWrapper(com.gemserk.componentsengine.utils.ParametersWrapper) Test(org.junit.Test)

Example 32 with Entity

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

the class EntityTemplateTest method testModifyPositionByHand.

@Test
public void testModifyPositionByHand() {
    // default parameters through a custom template, could be created when the level starts with custom level information
    EntityTemplate customShipTemplate = new ShipEntityTemplate() {

        {
            parameters.put("health", new Container(250f, 250f));
        }
    };
    EntityFactory entityFactory = new EntityFactoryImpl(new World());
    Entity entity = entityFactory.instantiate(customShipTemplate);
    SpatialComponent spatialComponent = entity.getComponent(SpatialComponent.class);
    Spatial spatial = spatialComponent.getSpatial();
    spatial.setPosition(100f, 100f);
}
Also used : Entity(com.artemis.Entity) Container(com.gemserk.componentsengine.utils.Container) EntityFactoryImpl(com.gemserk.commons.artemis.templates.EntityFactoryImpl) Spatial(com.gemserk.commons.gdx.games.Spatial) EntityTemplate(com.gemserk.commons.artemis.templates.EntityTemplate) SpatialComponent(com.gemserk.commons.artemis.components.SpatialComponent) World(com.artemis.World) EntityFactory(com.gemserk.commons.artemis.templates.EntityFactory) Test(org.junit.Test)

Example 33 with Entity

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

the class EntityTemplateTest method weaponInstantiateBulletWithWeaponParametersWithStyle.

@Test
public void weaponInstantiateBulletWithWeaponParametersWithStyle() {
    World world = new World();
    final EntityFactory entityFactory = new EntityFactoryImpl(world);
    EntityTemplate shipTemplate = new ShipEntityTemplate() {

        {
            parameters.put("x", 750f);
            parameters.put("y", 125f);
        }
    };
    Entity ship = entityFactory.instantiate(shipTemplate);
    EntityTemplate weaponTemplate = new WeaponEntityTemplate();
    EntityTemplate bulletTemplate = new BulletEntityTemplate();
    ParametersWrapper weaponParameters = new ParametersWrapper();
    weaponParameters.put("damage", 30f);
    weaponParameters.put("bulletTemplate", bulletTemplate);
    weaponParameters.put("owner", ship);
    weaponParameters.put("script", new ScriptJavaImpl() {

        ParametersWrapper bulletParameters = new ParametersWrapper();

        @Override
        public void update(World world, Entity e) {
            WeaponComponent weaponComponent = e.getComponent(WeaponComponent.class);
            OwnerComponent ownerComponent = e.getComponent(OwnerComponent.class);
            Entity owner = ownerComponent.getOwner();
            SpatialComponent spatialComponent = owner.getComponent(SpatialComponent.class);
            bulletParameters.put("damage", weaponComponent.getDamage());
            bulletParameters.put("position", spatialComponent.getPosition());
            EntityTemplate bulletTemplate = weaponComponent.getBulletTemplate();
            Entity bullet = entityFactory.instantiate(bulletTemplate, bulletParameters);
            DamageComponent damageComponent = bullet.getComponent(DamageComponent.class);
            SpatialComponent bulletSpatialComponent = bullet.getComponent(SpatialComponent.class);
            // part of the test
            assertThat(damageComponent.getDamage(), IsEqual.equalTo(30f));
            assertThat(bulletSpatialComponent.getSpatial().getX(), IsEqual.equalTo(750f));
            assertThat(bulletSpatialComponent.getSpatial().getY(), IsEqual.equalTo(125f));
        }
    });
    Entity weapon = entityFactory.instantiate(weaponTemplate, weaponParameters);
    // ... on script system
    ScriptComponent scriptComponent = weapon.getComponent(ScriptComponent.class);
    ArrayList<Script> scripts = scriptComponent.getScripts();
    for (int i = 0; i < scripts.size(); i++) scripts.get(i).update(world, weapon);
}
Also used : Entity(com.artemis.Entity) Script(com.gemserk.commons.artemis.scripts.Script) ScriptJavaImpl(com.gemserk.commons.artemis.scripts.ScriptJavaImpl) ScriptComponent(com.gemserk.commons.artemis.components.ScriptComponent) EntityTemplate(com.gemserk.commons.artemis.templates.EntityTemplate) SpatialComponent(com.gemserk.commons.artemis.components.SpatialComponent) World(com.artemis.World) ParametersWrapper(com.gemserk.componentsengine.utils.ParametersWrapper) EntityFactoryImpl(com.gemserk.commons.artemis.templates.EntityFactoryImpl) EntityFactory(com.gemserk.commons.artemis.templates.EntityFactory) Test(org.junit.Test)

Example 34 with Entity

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

the class GroupManagerTest method entityNotRemovedFromGroupWhenDeleted.

@Test
public void entityNotRemovedFromGroupWhenDeleted() {
    String group = "EntityGroup";
    World world = new World();
    Entity e = world.createEntity();
    e.setGroup(group);
    e.refresh();
    world.setDelta(10);
    world.loopStart();
    ImmutableBag<Entity> entities = world.getGroupManager().getEntities(group);
    assertThat(entities.size(), IsEqual.equalTo(1));
    world.deleteEntity(e);
    world.setDelta(10);
    world.loopStart();
    entities = world.getGroupManager().getEntities(group);
    assertThat(entities.size(), IsEqual.equalTo(0));
}
Also used : Entity(com.artemis.Entity) World(com.artemis.World) Test(org.junit.Test)

Example 35 with Entity

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

the class MultipleImplementationsSameComponentTest method shouldNotHandleSpecificComponentImplementation.

@Test
public void shouldNotHandleSpecificComponentImplementation() {
    World world = new World();
    WorldWrapper worldWrapper = new WorldWrapper(world);
    worldWrapper.addUpdateSystem(new SecondImplementationSystem());
    worldWrapper.init();
    Entity e1 = world.createEntity();
    e1.addComponent(new FirstImplementationComponent());
    e1.refresh();
    worldWrapper.update(100);
}
Also used : Entity(com.artemis.Entity) WorldWrapper(com.gemserk.commons.artemis.WorldWrapper) World(com.artemis.World) Test(org.junit.Test)

Aggregations

Entity (com.artemis.Entity)59 World (com.artemis.World)38 Test (org.junit.Test)31 RenderableComponent (com.gemserk.commons.artemis.components.RenderableComponent)16 OwnerComponent (com.gemserk.commons.artemis.components.OwnerComponent)15 WorldWrapper (com.gemserk.commons.artemis.WorldWrapper)8 EntityFactory (com.gemserk.commons.artemis.templates.EntityFactory)6 EntityFactoryImpl (com.gemserk.commons.artemis.templates.EntityFactoryImpl)6 EntityTemplate (com.gemserk.commons.artemis.templates.EntityTemplate)6 Spatial (com.gemserk.commons.gdx.games.Spatial)6 SpatialComponent (com.gemserk.commons.artemis.components.SpatialComponent)5 Body (com.badlogic.gdx.physics.box2d.Body)4 ContainerComponent (com.gemserk.commons.artemis.components.ContainerComponent)4 StoreComponent (com.gemserk.commons.artemis.components.StoreComponent)4 ParametersWrapper (com.gemserk.componentsengine.utils.ParametersWrapper)3 ComponentType (com.artemis.ComponentType)2 PreviousStateSpatialComponent (com.gemserk.commons.artemis.components.PreviousStateSpatialComponent)2 SpriteComponent (com.gemserk.commons.artemis.components.SpriteComponent)2 Script (com.gemserk.commons.artemis.scripts.Script)2 Container (com.gemserk.componentsengine.utils.Container)2