Java Code Examples for com.badlogic.ashley.core.Entity#add()

The following examples show how to use com.badlogic.ashley.core.Entity#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: World.java    From ashley-superjumper with Apache License 2.0 6 votes vote down vote up
private void createSpring(float x, float y) {
	Entity entity = engine.createEntity();
	
	SpringComponent spring = engine.createComponent(SpringComponent.class);
	BoundsComponent bounds = engine.createComponent(BoundsComponent.class);
	TransformComponent position = engine.createComponent(TransformComponent.class);
	TextureComponent texture = engine.createComponent(TextureComponent.class);
	
	bounds.bounds.width = SpringComponent.WIDTH;
	bounds.bounds.height = SpringComponent.HEIGHT;
	
	position.pos.set(x, y, 2.0f);
	
	texture.region = Assets.spring;
	
	entity.add(spring);
	entity.add(bounds);
	entity.add(position);
	entity.add(texture);
	
	engine.addEntity(entity);
}
 
Example 2
Source File: Bleed.java    From xibalba with MIT License 6 votes vote down vote up
@Override
public void act(Entity caster, Entity target) {
  if (ComponentMappers.bleeding.has(target)) {
    return;
  }

  if (MathUtils.random() < chance / 100) {
    target.add(new BleedingComponent(damage, life));

    if (ComponentMappers.player.has(target)) {
      WorldManager.log.add("effects.bleeding.started", "You", "are");
    } else {
      AttributesComponent attributes = ComponentMappers.attributes.get(target);

      WorldManager.log.add("effects.bleeding.started", attributes.name, "is");
    }
  }
}
 
Example 3
Source File: Poison.java    From xibalba with MIT License 6 votes vote down vote up
@Override
public void act(Entity caster, Entity target) {
  if (ComponentMappers.poisoned.has(target)) {
    return;
  }

  if (MathUtils.random() < chance / 100) {
    target.add(new PoisonedComponent(damage, life));

    if (ComponentMappers.player.has(target)) {
      WorldManager.log.add("effects.poisoned.started", "You", "are");
    } else {
      AttributesComponent attributes = ComponentMappers.attributes.get(target);

      WorldManager.log.add("effects.poisoned.started", attributes.name, "is");
    }
  }
}
 
Example 4
Source File: EntityFactory.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Create skin.
 *
 * @param corpse   The corpse we're skinning
 * @param position Where to place the skin
 * @return The skin
 */
public Entity createSkin(Entity corpse, Vector2 position) {
  Entity entity = createItem("skin", position);

  ComponentMappers.item.get(entity).name
      = ComponentMappers.corpse.get(corpse).entity + " skin";

  CorpseComponent body = ComponentMappers.corpse.get(corpse);

  if (body.wearableBodyParts != null && body.wearableBodyParts.get("skin") != null) {
    EffectsComponent effects = new EffectsComponent();
    effects.effects.addAll(body.wearableBodyParts.get("skin"));

    entity.add(effects);
  }

  return entity;
}
 
Example 5
Source File: EntityFactory.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Create trap.
 *
 * @param name     Name of the trap we're creating
 * @param position Where to place it
 * @return The trap
 */
public Entity createTrap(String name, Vector2 position) {
  Entity entity = new Entity();

  entity.add(new TrapComponent());
  entity.add(new PositionComponent(position));

  switch (name) {
    case "spiderWeb":
      entity.add(new SpiderWebComponent());

      entity.add(new VisualComponent(
          Main.asciiAtlas.createSprite("0302"), position, Color.WHITE,
          WorldManager.entityHelpers.hasTrait(WorldManager.player, "Perceptive") ? .5f : .1f
      ));

      break;
    default:
  }

  return entity;
}
 
Example 6
Source File: EntityFactory.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Create exit entity.
 *
 * @param mapIndex Map to place it on
 * @return The exit entity
 */
public Entity createExit(int mapIndex) {
  Map map = WorldManager.world.getMap(mapIndex);

  int cellX;
  int cellY;

  do {
    cellX = MathUtils.random(0, map.width - 1);
    cellY = MathUtils.random(0, map.height - 1);
  } while (WorldManager.mapHelpers.isBlocked(mapIndex, new Vector2(cellX, cellY))
      && WorldManager.mapHelpers.getWallNeighbours(mapIndex, cellX, cellY) >= 4);

  Vector2 position = new Vector2(cellX, cellY);
  Entity entity = new Entity();
  entity.add(new ExitComponent());
  entity.add(new PositionComponent(position));

  entity.add(new VisualComponent(
      Main.asciiAtlas.createSprite("1403"), position
  ));

  return entity;
}
 
Example 7
Source File: SortedIteratingSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldIterateEntitiesWithCorrectFamily () {
	final Engine engine = new Engine();

	final Family family = Family.all(OrderComponent.class, ComponentB.class).get();
	final SortedIteratingSystemMock system = new SortedIteratingSystemMock(family);
	final Entity e = new Entity();

	engine.addSystem(system);
	engine.addEntity(e);

	// When entity has OrderComponent
	e.add(new OrderComponent("A", 0));
	engine.update(deltaTime);

	// When entity has OrderComponent and ComponentB
	e.add(new ComponentB());
	system.expectedNames.addLast("A");
	engine.update(deltaTime);

	// When entity has OrderComponent, ComponentB and ComponentC
	e.add(new ComponentC());
	system.expectedNames.addLast("A");
	engine.update(deltaTime);

	// When entity has ComponentB and ComponentC
	e.remove(OrderComponent.class);
	e.add(new ComponentC());
	engine.update(deltaTime);
}
 
Example 8
Source File: World.java    From ashley-superjumper with Apache License 2.0 5 votes vote down vote up
private void createSquirrel(float x, float y) {
	Entity entity = engine.createEntity();
	
	AnimationComponent animation = engine.createComponent(AnimationComponent.class);
	SquirrelComponent squirrel = engine.createComponent(SquirrelComponent.class);
	BoundsComponent bounds = engine.createComponent(BoundsComponent.class);
	MovementComponent movement = engine.createComponent(MovementComponent.class);
	TransformComponent position = engine.createComponent(TransformComponent.class);
	StateComponent state = engine.createComponent(StateComponent.class);
	TextureComponent texture = engine.createComponent(TextureComponent.class);
	
	movement.velocity.x = rand.nextFloat() > 0.5f ? SquirrelComponent.VELOCITY : -SquirrelComponent.VELOCITY;
	
	animation.animations.put(SquirrelComponent.STATE_NORMAL, Assets.squirrelFly);
	
	bounds.bounds.width = SquirrelComponent.WIDTH;
	bounds.bounds.height = SquirrelComponent.HEIGHT;
	
	position.pos.set(x, y, 2.0f);
	
	state.set(SquirrelComponent.STATE_NORMAL);
	
	entity.add(animation);
	entity.add(squirrel);
	entity.add(bounds);
	entity.add(movement);
	entity.add(position);
	entity.add(state);
	entity.add(texture);
	
	engine.addEntity(entity);
}
 
Example 9
Source File: MarketSystem.java    From StockSimulator with GNU General Public License v2.0 5 votes vote down vote up
private StockImpactEventComponent injectMarketEvents() {
    if (random.nextDouble() > 0.1f) return null;
    Entity e = new Entity();
    StockImpactEventComponent impact = generateRandom();
    e.add(impact);
    engine.addEntity(e);
    bus.post(impact);
    return impact;
}
 
Example 10
Source File: IteratingSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldIterateEntitiesWithCorrectFamily () {
	final Engine engine = new Engine();

	final Family family = Family.all(ComponentA.class, ComponentB.class).get();
	final IteratingSystemMock system = new IteratingSystemMock(family);
	final Entity e = new Entity();

	engine.addSystem(system);
	engine.addEntity(e);

	// When entity has ComponentA
	e.add(new ComponentA());
	engine.update(deltaTime);

	assertEquals(0, system.numUpdates);

	// When entity has ComponentA and ComponentB
	system.numUpdates = 0;
	e.add(new ComponentB());
	engine.update(deltaTime);

	assertEquals(1, system.numUpdates);

	// When entity has ComponentA, ComponentB and ComponentC
	system.numUpdates = 0;
	e.add(new ComponentC());
	engine.update(deltaTime);

	assertEquals(1, system.numUpdates);

	// When entity has ComponentB and ComponentC
	system.numUpdates = 0;
	e.remove(ComponentA.class);
	e.add(new ComponentC());
	engine.update(deltaTime);

	assertEquals(0, system.numUpdates);
}
 
Example 11
Source File: MouseMovementSystem.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Get next step in moving path, add a movement component with that position, remove step.
 *
 * @param deltaTime Time between now and previous frame
 */
public void update(float deltaTime) {
  for (Entity entity : entities) {
    PlayerComponent playerDetails = ComponentMappers.player.get(WorldManager.player);
    AttributesComponent attributes = ComponentMappers.attributes.get(WorldManager.player);

    // Remove mouse movement component once path is empty
    if (playerDetails.path == null || playerDetails.path.isEmpty()) {
      attributes.energy -= MovementComponent.COST;

      entity.remove(MouseMovementComponent.class);
      WorldManager.state = WorldManager.State.PLAYING;
    } else {
      if (attributes.energy >= MovementComponent.COST) {
        // Start walking
        GridCell cell = playerDetails.path.get(0);

        entity.add(new MovementComponent(new Vector2(cell.getX(), cell.getY())));

        List<GridCell> newPath = new ArrayList<>(playerDetails.path);
        newPath.remove(cell);

        playerDetails.path = newPath;
      }
    }
  }
}
 
Example 12
Source File: EntityFactory.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Create rain drop.
 *
 * @return The rain drop
 */
public Entity createRainDrop() {
  Entity entity = new Entity();
  Vector2 position = new Vector2(0, 0);

  entity.add(new DecorationComponent(false));
  entity.add(new RainDropComponent());
  entity.add(new PositionComponent(position));
  entity.add(new VisualComponent(
      Main.asciiAtlas.createSprite("1502"), position, Colors.get("CYAN")
  ));

  return entity;
}
 
Example 13
Source File: World.java    From ashley-superjumper with Apache License 2.0 5 votes vote down vote up
private void createPlatform(int type, float x, float y) {
	Entity entity = new Entity();//engine.createEntity();
	
	AnimationComponent animation = engine.createComponent(AnimationComponent.class);
	PlatformComponent platform = engine.createComponent(PlatformComponent.class);
	BoundsComponent bounds = engine.createComponent(BoundsComponent.class);
	MovementComponent movement = engine.createComponent(MovementComponent.class);
	TransformComponent position = engine.createComponent(TransformComponent.class);
	StateComponent state = engine.createComponent(StateComponent.class);
	TextureComponent texture = engine.createComponent(TextureComponent.class);
	
	animation.animations.put(PlatformComponent.STATE_NORMAL, Assets.platform);
	animation.animations.put(PlatformComponent.STATE_PULVERIZING, Assets.breakingPlatform);
	
	bounds.bounds.width = PlatformComponent.WIDTH;
	bounds.bounds.height = PlatformComponent.HEIGHT;
	
	position.pos.set(x, y, 1.0f);
	
	state.set(PlatformComponent.STATE_NORMAL);
	
	platform.type = type;
	if (type == PlatformComponent.TYPE_MOVING) {
		movement.velocity.x = rand.nextBoolean() ? PlatformComponent.VELOCITY : -PlatformComponent.VELOCITY;
	}
	
	entity.add(animation);
	entity.add(platform);
	entity.add(bounds);
	entity.add(movement);
	entity.add(position);
	entity.add(state);
	entity.add(texture);
	
	engine.addEntity(entity);
}
 
Example 14
Source File: EntityFactory.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Create a corpse.
 *
 * @param enemy    The enemy who's corpse we're creating
 * @param position Where to place it
 * @return The corpse
 */
public Entity createCorpse(Entity enemy, Vector2 position) {
  Entity entity = createItem("corpse", position);
  String name = ComponentMappers.attributes.get(enemy).name;

  ComponentMappers.item.get(entity).name = name + " corpse";

  BodyComponent body = ComponentMappers.body.get(enemy);
  AttributesComponent.Type type = ComponentMappers.attributes.get(enemy).type;
  entity.add(new CorpseComponent(name, type, body.bodyParts, body.wearableBodyParts));

  return entity;
}
 
Example 15
Source File: EntityManglerSystem.java    From entity-system-benchmarks with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private final void createEntity() {
	Entity e = new Entity();
	Array<Class<? extends Component>> components = permutations[cmp[cmpIndex++]];
	if (cmpIndex == cmp.length) cmpIndex = 0;
	
	Object[] data = components.items;
	for (int i = 0, s = components.size; s > i; i++) {
		e.add(newInstance(data[i]));
	}

	engine.addEntity(e);
}
 
Example 16
Source File: RenderSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Override
public void create () {
	OrthographicCamera camera = new OrthographicCamera(640, 480);
	camera.position.set(320, 240, 0);
	camera.update();

	Texture crateTexture = new Texture("assets/crate.png");
	Texture coinTexture = new Texture("assets/coin.png");

	engine = new PooledEngine();
	engine.addSystem(new RenderSystem(camera));
	engine.addSystem(new MovementSystem());

	Entity crate = engine.createEntity();
	crate.add(new PositionComponent(50, 50));
	crate.add(new VisualComponent(new TextureRegion(crateTexture)));

	engine.addEntity(crate);

	TextureRegion coinRegion = new TextureRegion(coinTexture);

	for (int i = 0; i < 100; i++) {
		Entity coin = engine.createEntity();
		coin.add(new PositionComponent(MathUtils.random(640), MathUtils.random(480)));
		coin.add(new MovementComponent(10.0f, 10.0f));
		coin.add(new VisualComponent(coinRegion));
		engine.addEntity(coin);
	}
}
 
Example 17
Source File: EntityFactory.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Create limb.
 *
 * @param corpse   The corpse we're dismembering
 * @param part     What part of the body we're dismembering
 * @param position Where to place the limb
 * @return The skin
 */
public Entity createLimb(Entity corpse, String part, Vector2 position) {
  Entity entity = createItem("limb", position);

  ItemComponent item = ComponentMappers.item.get(entity);

  item.name = ComponentMappers.corpse.get(corpse).entity
      + " " + part.replace("left ", "").replace("right ", "");

  entity.add(new LimbComponent(ComponentMappers.corpse.get(corpse).type));

  CorpseComponent body = ComponentMappers.corpse.get(corpse);

  if (body.wearableBodyParts != null && body.wearableBodyParts.get(part) != null) {
    item.location = part;
    item.actions.add("wear");

    EffectsComponent effects = new EffectsComponent();
    effects.effects.addAll(body.wearableBodyParts.get(part));

    entity.add(effects);
  }

  return entity;
}
 
Example 18
Source File: SortedIteratingSystemTest.java    From ashley with Apache License 2.0 4 votes vote down vote up
private static Entity createOrderEntity (String name, int zLayer) {
	Entity entity = new Entity();
	entity.add(new OrderComponent(name, zLayer));
	return entity;
}
 
Example 19
Source File: ItemHelpers.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Add an item to an entity's inventory.
 *
 * @param entity Entity we wanna to give shit to
 * @param item   The item itself
 * @param log    Whether or not to log this action
 */
public void addToInventory(Entity entity, Entity item, boolean log) {
  InventoryComponent inventory = ComponentMappers.inventory.get(entity);
  AttributesComponent attributes = ComponentMappers.attributes.get(entity);

  if (inventory != null) {
    item.remove(PositionComponent.class);
    inventory.items.add(item);

    EquipmentComponent equipment = ComponentMappers.equipment.get(entity);
    ItemComponent itemDetails = ComponentMappers.item.get(item);

    if (log) {
      WorldManager.log.add(
          "inventory.pickedUp", WorldManager.itemHelpers.getName(entity, item)
      );
    }

    if (itemDetails.twoHanded && WorldManager.entityHelpers.hasDefect(entity, "One arm")) {
      return;
    }

    if (Objects.equals(itemDetails.type, "weapon")) {
      if (!itemDetails.twoHanded || !WorldManager.entityHelpers.hasDefect(entity, "One arm")) {
        if (equipment.slots.get("right hand") == null) {
          hold(entity, item);

          if (log) {
            WorldManager.log.add(
                "inventory.holding", WorldManager.itemHelpers.getName(entity, item)
            );
          }
        }
      }
    }

    if (Objects.equals(itemDetails.type, "armor")) {
      if (equipment.slots.get(itemDetails.location) == null) {
        wear(entity, item);
      }

      if (log) {
        WorldManager.log.add(
            "inventory.wearing", WorldManager.itemHelpers.getName(entity, item)
        );
      }
    }

    if (getTotalWeight(entity) > attributes.strength * 5) {
      entity.add(new EncumberedComponent());

      if (ComponentMappers.player.has(entity)) {
        WorldManager.log.add("effects.encumbered.started");
      }
    }
  }
}
 
Example 20
Source File: MapFire.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Update FIRES.
 *
 * @param delta Time since last frame
 * @param flood Keep spreading?
 */
public void update(float delta, boolean flood) {
  animCounter += delta;

  if (floodedCount < MathUtils.random(100, 300)) {
    if (flood) {
      flood(lastX + MathUtils.random(0, 2), lastY);
      flood(lastX - MathUtils.random(0, 2), lastY);
      flood(lastX, lastY + MathUtils.random(0, 2));
      flood(lastX, lastY - MathUtils.random(0, 2));
    }
  }

  if (animCounter >= .5f) {
    animCounter = 0;

    for (int x = 0; x < flooded.length; x++) {
      for (int y = 0; y < flooded[0].length; y++) {
        if (flooded[x][y] == MapCell.Type.FLOOR) {
          MapCell cell = map.getCellMap()[x][y];
          cell.description = "fire";

          String spriteKey = MathUtils.random() > 0.5 ? "1405" : "1407";
          Sprite sprite = Main.asciiAtlas.createSprite(spriteKey);
          sprite.setColor(Colors.get("fire-" + MathUtils.random(1, 3)));
          cell.sprite.set(sprite);
          cell.sprite.setPosition(x * Main.SPRITE_WIDTH, y * Main.SPRITE_HEIGHT);

          if (!cell.onFire) {
            ArrayList<Color> fireColors = new ArrayList<>();
            fireColors.add(Colors.get("fire-1"));
            fireColors.add(Colors.get("fire-2"));
            fireColors.add(Colors.get("fire-3"));

            Entity fireLight = new Entity();
            fireLight.add(
                new LightComponent(MathUtils.random(1, 3), true, fireColors)
            );
            fireLight.add(new PositionComponent(x, y));
            WorldManager.world.addEntity(fireLight);
          }

          cell.onFire = true;
        }
      }
    }
  }
}