com.badlogic.ashley.core.Engine Java Examples

The following examples show how to use com.badlogic.ashley.core.Engine. 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: 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 #2
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 #3
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 #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: WorldManager.java    From xibalba with MIT License 5 votes vote down vote up
/**
 * Setup a whole bunch of shit.
 */
public static void setup() {
  engine = new Engine();
  log = new ActionLog();
  world = new World();
  tweens = new Array<>();

  entityFactory = new EntityFactory();
  inputHelpers = new InputHelpers();
  mapHelpers = new MapHelpers();
  entityHelpers = new EntityHelpers();
  itemHelpers = new ItemHelpers();
  combatHelpers = new CombatHelpers();

  executeTurn = false;
  turnCount = 0;

  // Setup engine (systems are run in order added)
  engine.addSystem(new AttributesSystem());
  engine.addSystem(new AbilitiesSystem());
  engine.addSystem(new MouseMovementSystem());
  engine.addSystem(new ExploreSystem());
  engine.addSystem(new BrainSystem());
  engine.addSystem(new RangeSystem());
  engine.addSystem(new MeleeSystem());
  engine.addSystem(new MovementSystem());
  engine.addSystem(new TileEffectSystem());
  engine.addSystem(new EncumberedSystem());
  engine.addSystem(new CharmedSystem());
  engine.addSystem(new CrippledSystem());
  engine.addSystem(new BleedingSystem());
  engine.addSystem(new BurningSystem());
  engine.addSystem(new DrowningSystem());
  engine.addSystem(new StuckSystem());
  engine.addSystem(new PoisonedSystem());
  engine.addSystem(new SickSystem());
  engine.addSystem(new WetSystem());
  engine.addSystem(new DeathSystem());
}
 
Example #6
Source File: IntervalSystemTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Test
public void intervalSystem () {
	Engine engine = new Engine();
	IntervalSystemSpy intervalSystemSpy = new IntervalSystemSpy();

	engine.addSystem(intervalSystemSpy);

	for (int i = 1; i <= 10; ++i) {
		engine.update(deltaTime);
		assertEquals(i / 2, intervalSystemSpy.numUpdates);
	}
}
 
Example #7
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 #8
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 #9
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 #10
Source File: SortedIteratingSystem.java    From ashley with Apache License 2.0 5 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	ImmutableArray<Entity> newEntities = engine.getEntitiesFor(family);
	sortedEntities.clear();
	if (newEntities.size() > 0) {
		for (int i = 0; i < newEntities.size(); ++i) {
			sortedEntities.add(newEntities.get(i));
		}
		sortedEntities.sort(comparator);
	}
	shouldSort = false;
	engine.addEntityListener(family, this);
}
 
Example #11
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 #12
Source File: InsertRemoveBenchmark.java    From entity-system-benchmarks with Apache License 2.0 5 votes vote down vote up
@Setup
public void init() throws Exception {
	engine = new Engine();
	engine.addSystem(new EntityManglerSystem(SEED, entityCount));
	engine.addSystem(new CompSystemA());
	engine.addSystem(new CompSystemB());
	engine.addSystem(new CompSystemC());
	engine.addSystem(new CompSystemD());
}
 
Example #13
Source File: BaselineBenchmark.java    From entity-system-benchmarks with Apache License 2.0 5 votes vote down vote up
@Setup
public void init() {
	engine = new Engine();
	engine.addSystem(new EntityDeleterSystem(SEED, entityCount, PlainPosition.class, PlainStructComponentA.class));
	engine.addSystem(new BaselinePositionSystem());
	engine.addSystem(new BaselinePositionSystem2());
	engine.addSystem(new BaselinePositionSystem3());
}
 
Example #14
Source File: PlainComponentBenchmark.java    From entity-system-benchmarks with Apache License 2.0 5 votes vote down vote up
@Setup
public void init() {
	engine = new Engine();
	engine.addSystem(new PlainPositionSystem());
	engine.addSystem(new PlainPositionSystem2());
	engine.addSystem(new PlainPositionSystem3());
	engine.addSystem(new EntityDeleterSystem(JmhSettings.SEED, entityCount, PlainPosition.class, PlainStructComponentA.class));
}
 
Example #15
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 #16
Source File: WorldBuilder.java    From Pacman_libGdx with MIT License 5 votes vote down vote up
public WorldBuilder(TiledMap tiledMap, Engine engine, World world, RayHandler rayHandler) {
    this.tiledMap = tiledMap;
    this.engine = engine;
    this.world = world;
    this.rayHandler = rayHandler;

    assetManager = GameManager.instance.assetManager;
    actorAtlas = assetManager.get("images/actors.pack", TextureAtlas.class);
}
 
Example #17
Source File: SystemPriorityTest.java    From ashley with Apache License 2.0 5 votes vote down vote up
public static void main (String[] args) {
	Engine engine = new Engine();

	engine.addSystem(new SystemA(10));
	engine.addSystem(new SystemB(5));
	engine.addSystem(new SystemA(2));

	engine.update(0);
}
 
Example #18
Source File: BasicTest.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	entities = engine.getEntitiesFor(Family.all(PositionComponent.class, MovementComponent.class).get());
	log("MovementSystem added to engine.");
}
 
Example #19
Source File: BasicTest.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void removedFromEngine (Engine engine) {
	log("PositionSystem removed from engine.");
	entities = null;
}
 
Example #20
Source File: BasicTest.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	entities = engine.getEntitiesFor(Family.all(PositionComponent.class).get());
	log("PositionSystem added to engine.");
}
 
Example #21
Source File: AshleyBenchmark.java    From ashley with Apache License 2.0 4 votes vote down vote up
private static Engine prepareEngine (int numEntities) {
	Engine engine = new Engine();

	engine.addSystem(new MovementSystem());
	engine.addSystem(new StateSystem());
	engine.addSystem(new CollisionSystem());
	engine.addSystem(new RemovalSystem());

	for (int i = 0; i < numEntities; ++i) {
		Entity entity = new Entity();

		if (Constants.shouldHaveComponent(ComponentType.POSITION, i)) {
			PositionComponent pos = new PositionComponent();
			pos.pos.x = MathUtils.random(Constants.MIN_POS, Constants.MAX_POS);
			pos.pos.y = MathUtils.random(Constants.MIN_POS, Constants.MAX_POS);
			entity.add(pos);
		}

		if (Constants.shouldHaveComponent(ComponentType.MOVEMENT, i)) {
			MovementComponent mov = new MovementComponent();
			mov.velocity.x = MathUtils.random(Constants.MIN_VEL, Constants.MAX_VEL);
			mov.velocity.y = MathUtils.random(Constants.MIN_VEL, Constants.MAX_VEL);
			mov.accel.x = MathUtils.random(Constants.MIN_ACC, Constants.MAX_ACC);
			mov.accel.y = MathUtils.random(Constants.MIN_ACC, Constants.MAX_ACC);

			entity.add(mov);
		}

		if (Constants.shouldHaveComponent(ComponentType.RADIUS, i)) {
			RadiusComponent rad = new RadiusComponent();
			rad.radius = MathUtils.random(Constants.MIN_RADIUS, Constants.MAX_RADIUS);
			entity.add(rad);
		}

		if (Constants.shouldHaveComponent(ComponentType.STATE, i)) {
			entity.add(new StateComponent());
		}

		engine.addEntity(entity);
	}

	return engine;
}
 
Example #22
Source File: AshleyBenchmark.java    From ashley with Apache License 2.0 4 votes vote down vote up
private void runEngineTest (Engine engine) {
	for (int i = 0; i < Constants.FRAMES; ++i) {
		engine.update(Constants.DELTA_TIME);
	}
}
 
Example #23
Source File: RemovalSystem.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	entities = engine.getEntitiesFor(Family.all(RemovalComponent.class).get());
}
 
Example #24
Source File: BasicTest.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void removedFromEngine (Engine engine) {
	log("MovementSystem removed from engine.");
	entities = null;
}
 
Example #25
Source File: CollisionSystem.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	entities = engine.getEntitiesFor(Family.all(RadiusComponent.class).get());
}
 
Example #26
Source File: RenderSystem.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	entities = engine.getEntitiesFor(Family.all(PositionComponent.class, VisualComponent.class).get());
}
 
Example #27
Source File: ExploreSystem.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Triggers when this system is added to Ashley engine.
 *
 * @param engine An instance of the engine
 */
public void addedToEngine(Engine engine) {
  entities = engine.getEntitiesFor(
      Family.all(PlayerComponent.class, ExploreComponent.class).get()
  );
}
 
Example #28
Source File: SortedIteratingSystemTest.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	super.addedToEngine(engine);
	this.engine = engine;
}
 
Example #29
Source File: IteratingSystemTest.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void addedToEngine (Engine engine) {
	super.addedToEngine(engine);
}
 
Example #30
Source File: IteratingSystem.java    From ashley with Apache License 2.0 4 votes vote down vote up
@Override
public void removedFromEngine (Engine engine) {
	entities = null;
}