com.badlogic.ashley.core.Family Java Examples

The following examples show how to use com.badlogic.ashley.core.Family. 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: WorldRenderer.java    From xibalba with MIT License 6 votes vote down vote up
private void renderItems() {
  ImmutableArray<Entity> entities =
      WorldManager.engine.getEntitiesFor(
          Family.all(ItemComponent.class, PositionComponent.class, VisualComponent.class).get()
      );

  for (Entity entity : entities) {
    PositionComponent position = ComponentMappers.position.get(entity);

    if (Main.tweenManager.getRunningTimelinesCount() == 0) {
      WorldManager.entityHelpers.updateSprite(entity, position.pos.x, position.pos.y);
    }

    if (WorldManager.entityHelpers.isVisible(entity)) {
      ComponentMappers.visual.get(entity).sprite.draw(batch);
    }
  }
}
 
Example #2
Source File: IntervalIteratingTest.java    From ashley with Apache License 2.0 6 votes vote down vote up
@Test
public void intervalSystem () {
	Engine engine = new Engine();
	IntervalIteratingSystemSpy intervalSystemSpy = new IntervalIteratingSystemSpy();
	ImmutableArray<Entity> entities = engine.getEntitiesFor(Family.all(IntervalComponentSpy.class).get());
	ComponentMapper<IntervalComponentSpy> im = ComponentMapper.getFor(IntervalComponentSpy.class);

	engine.addSystem(intervalSystemSpy);

	for (int i = 0; i < 10; ++i) {
		Entity entity = new Entity();
		entity.add(new IntervalComponentSpy());
		engine.addEntity(entity);
	}

	for (int i = 1; i <= 10; ++i) {
		engine.update(deltaTime);

		for (int j = 0; j < entities.size(); ++j) {
			assertEquals(i / 2, im.get(entities.get(j)).numUpdates);
		}
	}
}
 
Example #3
Source File: GameState.java    From StockSimulator with GNU General Public License v2.0 6 votes vote down vote up
public void printUserData() {
    Family market = Family.all(MarketItemComponent.class, PriceComponent.class, NameComponent.class, PriceHistoryComponent.class).get();
    ImmutableArray<Entity> marketItemEntities = entityEngine.getEntitiesFor(market);

    Family players = Family.all(WalletComponent.class, NameComponent.class, InventoryComponent.class).get();
    for (Entity player : entityEngine.getEntitiesFor(players)) {
        NameComponent nc = player.getComponent(NameComponent.class);
        WalletComponent wc = player.getComponent(WalletComponent.class);
        InventoryComponent ic = player.getComponent(InventoryComponent.class);
        System.out.println("Name: " + nc.name);
        ic.printInventory(marketItemEntities);
        long value = ic.getInventoryValue(marketItemEntities);
        System.out.println("Inventory Value: $" + value);
        System.out.println(" Wallet Balance: $" + wc.balance);
        System.out.println("    Total Value: $" + (value + wc.balance));
    }
}
 
Example #4
Source File: EntityDeleterSystem.java    From entity-system-benchmarks with Apache License 2.0 6 votes vote down vote up
@Override
	public void addedToEngine(Engine engine) {
		this.engine = engine;
		entities = engine.getEntitiesFor(Family.all(PlainPosition.class).get());
//		engine.addEntityListener(new EntityListener() {
//			
//			@Override
//			public void entityRemoved(Entity entity) {
//				engine.remove(entity.getIndex());
//			}
//			
//			@Override
//			public void entityAdded(Entity entity) {
//				entities.put(entity.getIndex(), entity);
//			}
//		});
		for (int i = 0; ENTITY_COUNT > i; i++)
			createEntity();
	}
 
Example #5
Source File: WorldRenderer.java    From xibalba with MIT License 6 votes vote down vote up
private void renderEnemies() {
  ImmutableArray<Entity> entities =
      WorldManager.engine.getEntitiesFor(Family.all(EnemyComponent.class).get());

  for (Entity entity : entities) {
    PositionComponent position = ComponentMappers.position.get(entity);

    if (Main.tweenManager.getRunningTimelinesCount() == 0) {
      WorldManager.entityHelpers.updateSprite(entity, position.pos.x, position.pos.y);
    }

    if (WorldManager.entityHelpers.isVisible(entity)) {
      ComponentMappers.visual.get(entity).sprite.draw(batch);
    } else if (WorldManager.entityHelpers.canHear(WorldManager.player, entity)) {
      question.setPosition(
          position.pos.x * Main.SPRITE_WIDTH, position.pos.y * Main.SPRITE_HEIGHT
      );

      question.draw(batch);
    }
  }
}
 
Example #6
Source File: EntityManglerSystem.java    From entity-system-benchmarks with Apache License 2.0 6 votes vote down vote up
@Override
public void addedToEngine(Engine engine) {
	for (int i = 0; permutations.length > i; i++) {
		Array<Class<? extends Component>> components = new Array<Class<? extends Component>>();
		for (int classIndex = 0, s = (int)(rng.nextFloat() * 7) + 3; s > classIndex; classIndex++) {
			components.add(types.get((int)(rng.nextFloat() * types.size)));
		}
		permutations[i] = components;
	}
	
	this.engine = engine;
	entities = engine.getEntitiesFor(Family.all().get());
	
	for (int i = 0; ENTITY_COUNT > i; i++)
		createEntity();
}
 
Example #7
Source File: PooledEntityDeleterSystem.java    From entity-system-benchmarks with Apache License 2.0 6 votes vote down vote up
@Override
	public void addedToEngine(Engine engine) {
		this.engine = (PooledEngine) engine;
		entities = engine.getEntitiesFor(Family.all(PlainPosition.class).get());
//		engine.addEntityListener(new EntityListener() {
//			
//			@Override
//			public void entityRemoved(Entity entity) {
//				entities.remove(entity.getIndex());
//			}
//			
//			@Override
//			public void entityAdded(Entity entity) {
//				entities.put(entity.getIndex(), entity);
//			}
//		});
		for (int i = 0; ENTITY_COUNT > i; i++)
			createEntity();
	}
 
Example #8
Source File: WorldRenderer.java    From xibalba with MIT License 6 votes vote down vote up
private void renderStairs() {
  ImmutableArray<Entity> entrances =
      WorldManager.engine.getEntitiesFor(Family.all(EntranceComponent.class).get());
  ImmutableArray<Entity> exits =
      WorldManager.engine.getEntitiesFor(Family.all(ExitComponent.class).get());

  Object[] entities = ArrayUtils.addAll(entrances.toArray(), exits.toArray());

  for (Object e : entities) {
    Entity entity = (Entity) e;

    if (WorldManager.entityHelpers.isVisible(entity)) {
      ComponentMappers.visual.get(entity).sprite.draw(batch);
    }
  }
}
 
Example #9
Source File: MapHelpers.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Get trap at given position.
 *
 * @param position The position to check
 * @return The trap if one is found, null if not
 */
public Entity getTrapAt(Vector2 position) {
  ImmutableArray<Entity> entities =
      WorldManager.engine.getEntitiesFor(
          Family.all(TrapComponent.class).get()
      );

  for (Entity entity : entities) {
    PositionComponent entityPosition = ComponentMappers.position.get(entity);

    if (entityPosition.pos.epsilonEquals(position, 0.00001f)) {
      return entity;
    }
  }

  return null;
}
 
Example #10
Source File: RenderingSystem.java    From ashley-superjumper with Apache License 2.0 6 votes vote down vote up
public RenderingSystem(SpriteBatch batch) {
	super(Family.all(TransformComponent.class, TextureComponent.class).get());
	
	textureM = ComponentMapper.getFor(TextureComponent.class);
	transformM = ComponentMapper.getFor(TransformComponent.class);
	
	renderQueue = new Array<Entity>();
	
	comparator = new Comparator<Entity>() {
		@Override
		public int compare(Entity entityA, Entity entityB) {
			return (int)Math.signum(transformM.get(entityB).pos.z -
									transformM.get(entityA).pos.z);
		}
	};
	
	this.batch = batch;
	
	cam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
	cam.position.set(FRUSTUM_WIDTH / 2, FRUSTUM_HEIGHT / 2, 0);
}
 
Example #11
Source File: MapHelpers.java    From xibalba with MIT License 6 votes vote down vote up
/**
 * Try to find an entity at x, y.
 *
 * @param cellX x
 * @param cellY y
 * @return Either the entity or null if none were found
 */
public Entity getEntityAt(float cellX, float cellY) {
  ImmutableArray<Entity> entities =
      WorldManager.engine.getEntitiesFor(
          Family.all(PositionComponent.class).exclude(
              DecorationComponent.class, LightComponent.class
          ).get()
      );

  for (Entity entity : entities) {
    PositionComponent entityPosition = ComponentMappers.position.get(entity);

    if (entityPosition.pos.x == cellX && entityPosition.pos.y == cellY) {
      return entity;
    }
  }

  return null;
}
 
Example #12
Source File: AnimationSystem.java    From ashley-superjumper with Apache License 2.0 5 votes vote down vote up
public AnimationSystem() {
	super(Family.all(TextureComponent.class,
						AnimationComponent.class,
						StateComponent.class).get());
	
	tm = ComponentMapper.getFor(TextureComponent.class);
	am = ComponentMapper.getFor(AnimationComponent.class);
	sm = ComponentMapper.getFor(StateComponent.class);
}
 
Example #13
Source File: MapWeather.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Rainfall in the forest.
 *
 * <p>Generate 250 rain drops in random positions on the map.
 *
 * @param mapIndex The map we're working on
 */
public MapWeather(int mapIndex) {
  for (int i = 0; i < 250; i++) {
    Entity drop = WorldManager.entityFactory.createRainDrop();
    WorldManager.world.entities.get(mapIndex).add(drop);
  }

  rainDrops = WorldManager.engine.getEntitiesFor(Family.all(RainDropComponent.class).get());

  falling = Main.asciiAtlas.createSprite("1502");
  falling.setColor(Colors.get("CYAN"));
  splash = Main.asciiAtlas.createSprite("0700");
  splash.setColor(Colors.get("CYAN"));
  fading = Main.asciiAtlas.createSprite("0900");
}
 
Example #14
Source File: MapHelpers.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Similar to getEntityAt, but only return an entity if it contains EnemyComponent.
 *
 * @param cellX x
 * @param cellY y
 * @return Either an enemy or null if none were found
 */
public Entity getEnemyAt(int cellX, int cellY) {
  ImmutableArray<Entity> entities =
      WorldManager.engine.getEntitiesFor(Family.all(EnemyComponent.class).get());

  for (Entity entity : entities) {
    PositionComponent entityPosition = ComponentMappers.position.get(entity);

    if (entityPosition.pos.x == cellX && entityPosition.pos.y == cellY) {
      return entity;
    }
  }

  return null;
}
 
Example #15
Source File: WorldRenderer.java    From xibalba with MIT License 5 votes vote down vote up
private void renderDecorations() {
  ImmutableArray<Entity> entities =
      WorldManager.engine.getEntitiesFor(Family.all(DecorationComponent.class).get());

  for (Entity entity : entities) {
    if (WorldManager.entityHelpers.isVisible(entity)) {
      ComponentMappers.visual.get(entity).sprite.draw(batch);
    }
  }
}
 
Example #16
Source File: WorldRenderer.java    From xibalba with MIT License 5 votes vote down vote up
private void renderTraps() {
  ImmutableArray<Entity> entities =
      WorldManager.engine.getEntitiesFor(Family.all(TrapComponent.class).get());

  for (Entity entity : entities) {
    if (ComponentMappers.visual.has(entity) && WorldManager.entityHelpers.isVisible(entity)) {
      ComponentMappers.visual.get(entity).sprite.draw(batch);
    }
  }
}
 
Example #17
Source File: DrowningSystem.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Handles drowning. </p> Take 5 damage for every turn you're in deep water. Once you leave deep
 * water we remove the DrowningComponent as you're no longer drowning.
 */
public DrowningSystem() {
  super(
      Family.all(
          DrowningComponent.class, AttributesComponent.class, PositionComponent.class
      ).get()
  );
}
 
Example #18
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 #19
Source File: GameState.java    From StockSimulator with GNU General Public License v2.0 5 votes vote down vote up
public void printMarketPrices() {
    Family market = Family.all(MarketItemComponent.class, PriceComponent.class, NameComponent.class).get();
    ImmutableArray<Entity> entities = entityEngine.getEntitiesFor(market);
    System.out.println("Current Prices: ");
    for (Entity e : entities) {
        PriceComponent p = e.getComponent(PriceComponent.class);
        NameComponent n = e.getComponent(NameComponent.class);
        System.out.println("Name: " + n.name + " Price: " + p.price);
    }
}
 
Example #20
Source File: SortedIteratingSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Test
public void entityOrder () {
	Engine engine = new Engine();

	final Family family = Family.all(OrderComponent.class).get();
	final SortedIteratingSystemMock system = new SortedIteratingSystemMock(family);
	engine.addSystem(system);

	Entity a = createOrderEntity("A", 0);
	Entity b = createOrderEntity("B", 1);
	Entity c = createOrderEntity("C", 3);
	Entity d = createOrderEntity("D", 2);

	engine.addEntity(a);
	engine.addEntity(b);
	engine.addEntity(c);
	system.expectedNames.addLast("A");
	system.expectedNames.addLast("B");
	system.expectedNames.addLast("C");
	engine.update(0);

	engine.addEntity(d);
	system.expectedNames.addLast("A");
	system.expectedNames.addLast("B");
	system.expectedNames.addLast("D");
	system.expectedNames.addLast("C");
	engine.update(0);

	orderMapper.get(a).zLayer = 3;
	orderMapper.get(b).zLayer = 2;
	orderMapper.get(c).zLayer = 1;
	orderMapper.get(d).zLayer = 0;
	system.forceSort();
	system.expectedNames.addLast("D");
	system.expectedNames.addLast("C");
	system.expectedNames.addLast("B");
	system.expectedNames.addLast("A");
	engine.update(0);
}
 
Example #21
Source File: GameState.java    From StockSimulator with GNU General Public License v2.0 5 votes vote down vote up
public MarketItemEntity getMarketItemByName(String item) {
    Family marketItems = Family.all(MarketItemComponent.class, NameComponent.class).get();
    for (Entity e : entityEngine.getEntitiesFor(marketItems)) {
        if (e.getComponent(NameComponent.class).name.equalsIgnoreCase(item)) {
            return (MarketItemEntity) e;
        }
    }
    return null;

}
 
Example #22
Source File: MarketSystem.java    From StockSimulator with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void addedToEngine(Engine engine) {
    this.engine = engine;
    this.marketItemFamily = Family.all(MarketItemComponent.class, MarketVolatilityComponent.class, PriceComponent.class, PriceHistoryComponent.class).get();
    playerFamily = Family.all(WalletComponent.class, NameComponent.class, InventoryComponent.class).get();

    this.gameStateFamily = Family.all(GameStateComponent.class).get();
    this.stockImpactEventComponent = Family.all(StockImpactEventComponent.class).get();
}
 
Example #23
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 #24
Source File: CollisionSystem.java    From ashley-superjumper with Apache License 2.0 5 votes vote down vote up
@Override
public void addedToEngine(Engine engine) {
	this.engine = engine;
	
	bobs = engine.getEntitiesFor(Family.all(BobComponent.class, BoundsComponent.class, TransformComponent.class, StateComponent.class).get());
	coins = engine.getEntitiesFor(Family.all(CoinComponent.class, BoundsComponent.class).get());
	squirrels = engine.getEntitiesFor(Family.all(SquirrelComponent.class, BoundsComponent.class).get());
	springs = engine.getEntitiesFor(Family.all(SpringComponent.class, BoundsComponent.class, TransformComponent.class).get());
	castles = engine.getEntitiesFor(Family.all(CastleComponent.class, BoundsComponent.class).get());
	platforms = engine.getEntitiesFor(Family.all(PlatformComponent.class, BoundsComponent.class, TransformComponent.class).get());
}
 
Example #25
Source File: MapHelpers.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Returns whether or not the given position is blocked.
 *
 * @param position Position to check
 * @return Is it blocked?
 */
public boolean isBlocked(int mapIndex, Vector2 position) {
  MapCell[][] map = WorldManager.world.getMap(mapIndex).getCellMap();

  boolean blocked = map[(int) position.x][(int) position.y].isWall()
      || map[(int) position.x][(int) position.y].isNothing();

  if (!blocked) {
    ImmutableArray<Entity> entities =
        WorldManager.engine.getEntitiesFor(
            Family.all(PositionComponent.class).exclude(LightComponent.class).get()
        );

    for (Entity entity : entities) {
      PositionComponent ep = ComponentMappers.position.get(entity);

      if (ep != null && ep.pos.epsilonEquals(position, 0.00001f)) {
        if (ComponentMappers.decoration.has(entity)) {
          if (ComponentMappers.decoration.get(entity).blocks) {
            blocked = true;
            break;
          }
        } else if (ComponentMappers.trap.has(entity)) {
          blocked = false;
        } else {
          blocked = true;
          break;
        }
      }
    }
  }

  return blocked;
}
 
Example #26
Source File: CompSystemA.java    From entity-system-benchmarks with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public CompSystemA() {
	super(Family.all(Comp1.class, Comp4.class, Comp5.class).get());
}
 
Example #27
Source File: CompSystemB.java    From entity-system-benchmarks with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public CompSystemB() {
	super(Family.all(Comp2.class, Comp8.class, Comp9.class).get());
}
 
Example #28
Source File: BaselinePositionSystem.java    From entity-system-benchmarks with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public BaselinePositionSystem() {
	super(Family.all(PlainPosition.class).get());
}
 
Example #29
Source File: CompSystemE.java    From entity-system-benchmarks with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public CompSystemE() {
	super(Family.all(Comp10.class, Comp11.class, Comp12.class).exclude(Comp9.class).get());
}
 
Example #30
Source File: MovementSystem.java    From ashley-superjumper with Apache License 2.0 4 votes vote down vote up
public MovementSystem() {
	super(Family.all(TransformComponent.class, MovementComponent.class).get());
	
	tm = ComponentMapper.getFor(TransformComponent.class);
	mm = ComponentMapper.getFor(MovementComponent.class);
}