use of de.gurkenlabs.litiengine.entities.Trigger in project litiengine by gurkenlabs.
the class MapObjectLoaderTests method testTriggerMapObjectLoader.
@Test
public void testTriggerMapObjectLoader() {
TriggerMapObjectLoader loader = new TriggerMapObjectLoader();
IEnvironment environment = mock(IEnvironment.class);
IMapObject mapObject = mock(IMapObject.class);
when(mapObject.getType()).thenReturn(MapObjectType.TRIGGER.name());
when(mapObject.getId()).thenReturn(111);
when(mapObject.getName()).thenReturn("testTrigger");
when(mapObject.getLocation()).thenReturn(new Point(100, 100));
when(mapObject.getWidth()).thenReturn(200);
when(mapObject.getHeight()).thenReturn(200);
when(mapObject.getDimension()).thenReturn(new Dimension(200, 200));
when(mapObject.getCustomProperty(MapObjectProperty.TRIGGER_MESSAGE)).thenReturn("message");
when(mapObject.getCustomProperty(MapObjectProperty.TRIGGER_ACTIVATION)).thenReturn(TriggerActivation.INTERACT.name());
when(mapObject.getCustomProperty(MapObjectProperty.TRIGGER_TARGETS)).thenReturn("1,2,3");
when(mapObject.getCustomProperty(MapObjectProperty.TRIGGER_ACTIVATORS)).thenReturn("4,5,6");
when(mapObject.getCustomProperty(MapObjectProperty.TRIGGER_ONETIME)).thenReturn("false");
Collection<IEntity> entities = loader.load(environment, mapObject);
Optional<IEntity> opt = entities.stream().findFirst();
assertTrue(opt.isPresent());
IEntity entity = entities.stream().findFirst().get();
assertNotNull(entity);
assertEquals(entity.getMapId(), 111);
assertEquals(entity.getName(), "testTrigger");
assertEquals(entity.getLocation().getX(), 100, 0.0001);
assertEquals(entity.getLocation().getY(), 100, 0.0001);
Trigger trigger = (Trigger) entity;
assertFalse(trigger.isOneTimeTrigger());
assertEquals(TriggerActivation.INTERACT, trigger.getActivationType());
assertArrayEquals(new Integer[] { 1, 2, 3 }, trigger.getTargets().toArray());
assertArrayEquals(new Integer[] { 4, 5, 6 }, trigger.getActivators().toArray());
assertEquals(200.0, trigger.getCollisionBoxWidth(), 0.0001);
assertEquals(200.0, trigger.getCollisionBoxHeight(), 0.0001);
}
use of de.gurkenlabs.litiengine.entities.Trigger in project litiengine by gurkenlabs.
the class Environment method add.
@Override
public void add(final IEntity entity) {
if (entity == null) {
return;
}
// set local map id if none is set for the entity
if (entity.getMapId() == 0) {
entity.setMapId(this.getLocalMapId());
}
if (entity instanceof Emitter) {
Emitter emitter = (Emitter) entity;
this.getGroundRenderables().add(emitter.getGroundRenderable());
this.getOverlayRenderables().add(emitter.getOverlayRenderable());
this.emitters.add(emitter);
}
if (entity instanceof ICombatEntity) {
this.combatEntities.put(entity.getMapId(), (ICombatEntity) entity);
}
if (entity instanceof IMobileEntity) {
this.mobileEntities.put(entity.getMapId(), (IMobileEntity) entity);
}
if (entity instanceof Prop) {
this.props.add((Prop) entity);
}
if (entity instanceof Creature) {
this.creatures.add((Creature) entity);
}
if (entity instanceof CollisionBox) {
this.colliders.add((CollisionBox) entity);
}
if (entity instanceof LightSource) {
this.lightSources.add((LightSource) entity);
}
if (entity instanceof Trigger) {
this.triggers.add((Trigger) entity);
}
if (entity instanceof Spawnpoint) {
this.spawnPoints.add((Spawnpoint) entity);
}
if (entity instanceof StaticShadow) {
this.staticShadows.add((StaticShadow) entity);
} else if (entity instanceof MapArea) {
this.mapAreas.add((MapArea) entity);
}
for (String rawTag : entity.getTags()) {
if (rawTag == null) {
continue;
}
final String tag = rawTag.trim().toLowerCase();
if (tag.isEmpty()) {
continue;
}
if (this.entitiesByTag.containsKey(tag)) {
this.entitiesByTag.get(tag).add(entity);
continue;
}
this.entitiesByTag.put(tag, new CopyOnWriteArrayList<>());
this.entitiesByTag.get(tag).add(entity);
}
// we need to load the new entity manually
if (this.loaded) {
this.load(entity);
}
this.entities.get(entity.getRenderType()).put(entity.getMapId(), entity);
for (Consumer<IEntity> cons : this.entityAddedConsumers) {
cons.accept(entity);
}
}
use of de.gurkenlabs.litiengine.entities.Trigger in project litiengine by gurkenlabs.
the class TriggerMapObjectLoader method load.
@Override
public Collection<IEntity> load(IEnvironment environment, IMapObject mapObject) {
if (MapObjectType.get(mapObject.getType()) != MapObjectType.TRIGGER) {
throw new IllegalArgumentException("Cannot load a mapobject of the type " + mapObject.getType() + " with a loader of the type " + TriggerMapObjectLoader.class);
}
final String message = mapObject.getCustomProperty(MapObjectProperty.TRIGGER_MESSAGE);
final TriggerActivation act = mapObject.getCustomProperty(MapObjectProperty.TRIGGER_ACTIVATION) != null ? TriggerActivation.valueOf(mapObject.getCustomProperty(MapObjectProperty.TRIGGER_ACTIVATION)) : TriggerActivation.COLLISION;
final boolean oneTime = mapObject.getCustomPropertyBool(MapObjectProperty.TRIGGER_ONETIME);
final int coolDown = mapObject.getCustomPropertyInt(MapObjectProperty.TRIGGER_COOLDOWN);
final Trigger trigger = this.createTrigger(mapObject, act, message, oneTime, coolDown, mapObject);
loadDefaultProperties(trigger, mapObject);
this.loadTargets(mapObject, trigger);
this.loadActivators(mapObject, trigger);
Collection<IEntity> entities = super.load(environment, mapObject);
entities.add(trigger);
return entities;
}
use of de.gurkenlabs.litiengine.entities.Trigger in project litiengine by gurkenlabs.
the class MapSelectionPanel method populateMapObjectTree.
private void populateMapObjectTree() {
this.nodeRoot.setUserObject(new IconTreeListItem(Game.getEnvironment().getEntities().size() + " " + Resources.get("panel_mapselection_entities"), Icons.FOLDER));
for (DefaultMutableTreeNode node : this.entityNodes) {
node.removeAllChildren();
}
this.nodeLights.setUserObject(new IconTreeListItem(Game.getEnvironment().getLightSources().size() + " " + Resources.get("panel_mapselection_lights"), Icons.LIGHT));
this.nodeProps.setUserObject(new IconTreeListItem(Game.getEnvironment().getProps().size() + " " + Resources.get("panel_mapselection_props"), Icons.PROP));
this.nodeCreatures.setUserObject(new IconTreeListItem(Game.getEnvironment().getCreatures().size() + " " + Resources.get("panel_mapselection_creatures"), Icons.CREATURE));
this.nodeTriggers.setUserObject(new IconTreeListItem(Game.getEnvironment().getTriggers().size() + " " + Resources.get("panel_mapselection_triggers"), Icons.TRIGGER));
this.nodeSpawnpoints.setUserObject(new IconTreeListItem(Game.getEnvironment().getSpawnPoints().size() + " " + Resources.get("panel_mapselection_spawnpoints"), Icons.SPAWMPOINT));
this.nodeCollisionBoxes.setUserObject(new IconTreeListItem(Game.getEnvironment().getCollisionBoxes().size() + " " + Resources.get("panel_mapselection_collboxes"), Icons.COLLISIONBOX));
this.nodeMapAreas.setUserObject(new IconTreeListItem(Game.getEnvironment().getAreas().size() + " " + Resources.get("panel_mapselection_areas"), Icons.MAPAREA));
this.nodeStaticShadows.setUserObject(new IconTreeListItem(Game.getEnvironment().getStaticShadows().size() + " " + Resources.get("panel_mapselection_shadow"), Icons.SHADOWBOX));
this.nodeEmitter.setUserObject(new IconTreeListItem(Game.getEnvironment().getEmitters().size() + " " + Resources.get("panel_mapselection_emitter"), Icons.EMITTER));
for (LightSource light : Game.getEnvironment().getLightSources()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(light));
this.nodeLights.add(node);
}
for (Prop prop : Game.getEnvironment().getProps()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(prop));
this.nodeProps.add(node);
}
for (Creature creature : Game.getEnvironment().getCreatures()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(creature));
this.nodeCreatures.add(node);
}
for (Trigger trigger : Game.getEnvironment().getTriggers()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(trigger));
this.nodeTriggers.add(node);
}
for (Spawnpoint spawn : Game.getEnvironment().getSpawnPoints()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(spawn));
this.nodeSpawnpoints.add(node);
}
for (CollisionBox coll : Game.getEnvironment().getCollisionBoxes()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(coll));
this.nodeCollisionBoxes.add(node);
}
for (MapArea area : Game.getEnvironment().getAreas()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(area));
this.nodeMapAreas.add(node);
}
for (StaticShadow shadow : Game.getEnvironment().getStaticShadows()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(shadow));
this.nodeStaticShadows.add(node);
}
for (Emitter emitter : Game.getEnvironment().getEmitters()) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new IconTreeListItem(emitter));
this.nodeEmitter.add(node);
}
this.entitiesTreeModel.reload();
}
use of de.gurkenlabs.litiengine.entities.Trigger in project litiengine by gurkenlabs.
the class EnvironmentTests method testTrigger.
@Test
public void testTrigger() {
Trigger testTrigger = new Trigger(TriggerActivation.COLLISION, "test", "testmessage");
testTrigger.setMapId(1);
this.testEnvironment.add(testTrigger);
assertNotNull(this.testEnvironment.getTrigger("test"));
assertNotNull(this.testEnvironment.getTrigger(1));
assertNotNull(this.testEnvironment.get(1));
assertNotNull(this.testEnvironment.get("test"));
assertEquals(1, this.testEnvironment.getByType(Trigger.class).size());
assertEquals(1, this.testEnvironment.getEntities().size());
this.testEnvironment.remove(testTrigger);
assertNull(this.testEnvironment.getTrigger("test"));
assertNull(this.testEnvironment.getTrigger(1));
assertNull(this.testEnvironment.get(1));
assertNull(this.testEnvironment.get("test"));
assertEquals(0, this.testEnvironment.getByType(Trigger.class).size());
assertEquals(0, this.testEnvironment.getEntities().size());
}
Aggregations