Search in sources :

Example 6 with ParametersWrapper

use of com.gemserk.componentsengine.utils.ParametersWrapper in project commons-gdx by gemserk.

the class ParametersDelegateImplTest method shouldNotClearDelegateWhenClear.

@Test
public void shouldNotClearDelegateWhenClear() {
    ParametersWrapper parameters = new ParametersWrapper();
    parameters.put("a", "b");
    ParametersDelegateImpl parametersDelegate = new ParametersDelegateImpl();
    parametersDelegate.setDelegate(parameters);
    parametersDelegate.put("c", "d");
    parametersDelegate.clear();
    assertEquals("b", parameters.get("a"));
    assertEquals("b", parametersDelegate.get("a"));
    assertEquals(null, parametersDelegate.get("c"));
}
Also used : ParametersWrapper(com.gemserk.componentsengine.utils.ParametersWrapper) Test(org.junit.Test)

Example 7 with ParametersWrapper

use of com.gemserk.componentsengine.utils.ParametersWrapper in project commons-gdx by gemserk.

the class ParametersDelegateImplTest method shouldPutAllFromMap.

@Test
public void shouldPutAllFromMap() {
    ParametersWrapper parameters = new ParametersWrapper();
    ParametersDelegateImpl parametersDelegate = new ParametersDelegateImpl();
    parametersDelegate.setDelegate(parameters);
    HashMap<String, Object> objects = new HashMap<String, Object>();
    objects.put("a", "b");
    objects.put("c", "d");
    parametersDelegate.putAll(objects);
    assertEquals("b", parametersDelegate.get("a"));
    assertEquals("d", parametersDelegate.get("c"));
}
Also used : HashMap(java.util.HashMap) ParametersWrapper(com.gemserk.componentsengine.utils.ParametersWrapper) Test(org.junit.Test)

Example 8 with ParametersWrapper

use of com.gemserk.componentsengine.utils.ParametersWrapper in project commons-gdx by gemserk.

the class ParametersWithFallBackTest method shouldReturnObjectFromParametersFallbackIfNotFoundOnParameters.

@Test
public void shouldReturnObjectFromParametersFallbackIfNotFoundOnParameters() {
    ParametersWithFallBack parametersWithFallBack = new ParametersWithFallBack();
    Parameters parameters = new ParametersWrapper();
    parametersWithFallBack.setParameters(parameters);
    parametersWithFallBack.put("a", 100f);
    Float a = parametersWithFallBack.get("a");
    assertThat(a, IsEqual.equalTo(100f));
}
Also used : Parameters(com.gemserk.componentsengine.utils.Parameters) ParametersWrapper(com.gemserk.componentsengine.utils.ParametersWrapper) Test(org.junit.Test)

Example 9 with ParametersWrapper

use of com.gemserk.componentsengine.utils.ParametersWrapper 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 10 with ParametersWrapper

use of com.gemserk.componentsengine.utils.ParametersWrapper 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)

Aggregations

ParametersWrapper (com.gemserk.componentsengine.utils.ParametersWrapper)15 Test (org.junit.Test)15 Parameters (com.gemserk.componentsengine.utils.Parameters)5 Entity (com.artemis.Entity)3 World (com.artemis.World)3 EntityFactory (com.gemserk.commons.artemis.templates.EntityFactory)3 EntityFactoryImpl (com.gemserk.commons.artemis.templates.EntityFactoryImpl)3 EntityTemplate (com.gemserk.commons.artemis.templates.EntityTemplate)3 ScriptComponent (com.gemserk.commons.artemis.components.ScriptComponent)1 SpatialComponent (com.gemserk.commons.artemis.components.SpatialComponent)1 Script (com.gemserk.commons.artemis.scripts.Script)1 ScriptJavaImpl (com.gemserk.commons.artemis.scripts.ScriptJavaImpl)1 HashMap (java.util.HashMap)1