com.artemis.World Java Examples

The following examples show how to use com.artemis.World. 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: SequenceOperationTest.java    From artemis-odb-orion with Apache License 2.0 6 votes vote down vote up
@Test
public void reports_complete_immediately_when_done() {
	World world = new World(new WorldConfiguration()
		.setSystem(OperationSystem.class));

	int entityId = world.create();

	SequenceOperation seq = sequence(
		delayTick(1),
		operation(MySingleUseOperation.class)
	);
	seq.register(world, entityId);

	process(world);
	assertEquals(1, operatives(world).size());

	process(world);
	assertEquals(0, operatives(world).size());
	assertSame(seq, sequence());
}
 
Example #2
Source File: RepeatOperationTest.java    From artemis-odb-orion with Apache License 2.0 6 votes vote down vote up
@Test
public void repeated_operations_are_recycled() {
	IntArray instances = new IntArray();

	World world = new World(
		new WorldConfiguration()
			.register("instances", instances)
			.setSystem(OperationSystem.class));

	repeat(100,
		operation(SelfTrackingOperation.class)
	).register(world, world.create());

	process(world);
	assertEquals(2, instances.size);

	process(world); // just making sure nothing new happens
	assertEquals(2, instances.size);

	assertTrue(instances.contains(
		System.identityHashCode(operation(SelfTrackingOperation.class))));
}
 
Example #3
Source File: RepeatOperationTest.java    From artemis-odb-orion with Apache License 2.0 6 votes vote down vote up
@Test
public void repeat_thrice_over_one_frame() {
	World world = new World(
		new WorldConfiguration()
			.setSystem(OperationSystem.class));

	repeat(3, operation(InstantOperation.class))
		.register(world, world.create());

	assertEquals(0, InstantOperation.invocations);

	process(world);
	assertEquals(3, InstantOperation.invocations);

	process(world);
	assertEquals(3, InstantOperation.invocations);
}
 
Example #4
Source File: GameScreen.java    From libgdx-artemis-quickstart with MIT License 6 votes vote down vote up
protected World createWorld() {
    return new World(new WorldConfigurationBuilder()
            // keeps components available until all listeners have been called.
            // Use this if your systems need to access components to clean up after removal.
            .alwaysDelayComponentRemoval(true)
            // Describes dependencies on plugins. You can find more example plugins commented out in build.gradle.
            .dependsOn(
                    //EntityLinkManager.class,
                    //OperationsPlugin.class,
                    ProfilerPlugin.class,
                    FluidEntityPlugin.class)
            .with(
                    // put your own systems here! With the default InvocationStrategy they are called in order each frame.
                    new ExampleSystem()
            ).build());
}
 
Example #5
Source File: OperationTest.java    From artemis-odb-orion with Apache License 2.0 6 votes vote down vote up
@Test
public void test_hiearhical_clean_up() {
	World world = new World(new WorldConfiguration()
		.setSystem(OperationSystem.class));

	KillOperation killOperation = killEntity();

	sequence(
		sequence(
			sequence(
				parallel(
					sequence(),
					sequence(killOperation)
				)
			)
		)
	).register(world, world.create());

	world.delta = .25f;
	world.process();

	assertSame(killOperation, killEntity());
	assertNotSame(killOperation, killEntity());
}
 
Example #6
Source File: IfElseOperationTest.java    From artemis-odb-orion with Apache License 2.0 6 votes vote down vote up
@Test
public void ifElse_true_test() {
	World world = new World(new WorldConfiguration()
		.setSystem(new OperationSystem()));

	MySingleUseOperation trueOp = operation(MySingleUseOperation.class);
	MySingleUseOperation falseOp = operation(MySingleUseOperation.class);

	ifTrue(true, trueOp).elseDo(falseOp).register(world, world.create());

	world.delta = 1f;
	world.process();

	assertEquals(1, trueOp.timesRun);
	assertEquals(0, falseOp.timesRun);
}
 
Example #7
Source File: IfElseOperationTest.java    From artemis-odb-orion with Apache License 2.0 6 votes vote down vote up
@Test
public void ifElse_false_test() {
	World world = new World(new WorldConfiguration()
		.setSystem(new OperationSystem()));

	MySingleUseOperation trueOp = operation(MySingleUseOperation.class);
	MySingleUseOperation falseOp = operation(MySingleUseOperation.class);

	ifTrue(false, trueOp).elseDo(falseOp).register(world, world.create());

	world.delta = 1f;
	world.process();

	assertEquals(0, trueOp.timesRun);
	assertEquals(1, falseOp.timesRun);
}
 
Example #8
Source File: OperationTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleUseOperation() {
	OperationSystem operationSystem = new OperationSystem();
	World world = new World(new WorldConfiguration()
		.setSystem(operationSystem));

	MySingleUseOperation op = operation(MySingleUseOperation.class);
	op.register(world, world.create());

	process(world);

	assertEquals(0, operatives(world).size());
	assertSame(op, operation(MySingleUseOperation.class));
}
 
Example #9
Source File: RepeatOperationTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void repeat_thrice_over_three_frames() {
	int[] counter = new int[1];

	World world = new World(new WorldConfiguration()
		.register("counter", counter)
		.setSystem(OperationSystem.class));

	repeat(3,
		sequence(
			operation(SingleFrameOperation.class)
		)
	).register(world);

	assertEquals(0, counter[0]);

	process(world);
	assertEquals(1, counter[0]);

	process(world);
	assertEquals(2, counter[0]);

	process(world);
	assertEquals(3, counter[0]);

	process(world);
	assertEquals(3, counter[0]);
}
 
Example #10
Source File: OperationTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void testWireInjection() {
	World world = new World(new WorldConfiguration()
			.setSystem(new GroupManager())
			.setSystem(new TagManager())
			.setSystem(new OperationSystem()));

	ManagerOperation action = operation(ManagerOperation.class);
	action.register(world, world.create());

	world.process();
	world.process();
}
 
Example #11
Source File: DelayTickTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void test_delay() {
	World w = new World(new WorldConfiguration()
		.setSystem(OperationSystem.class));

	DelayTickOperation op1 = delayTick(1);
	DelayTickOperation op2 = delayTick(2);
	DelayTickOperation op3 = delayTick(1);

	sequence(op1, op2, op3).register(w.createEntity());
	assertEquals(false, op1.isComplete());
	assertEquals(false, op2.isComplete());
	assertEquals(false, op3.isComplete());

	w.delta = .01f;
	w.process();
	assertEquals(true, op1.isComplete());
	assertEquals(false, op2.isComplete());
	assertEquals(false, op3.isComplete());

	w.delta = .01f;
	w.process();
	assertEquals(true, op1.isComplete());
	assertEquals(false, op2.isComplete());
	assertEquals(false, op3.isComplete());

	w.delta = .01f;
	w.process();
	assertEquals(true, op1.isComplete());
	assertEquals(true, op2.isComplete());
	assertEquals(false, op3.isComplete());

	w.delta = .01f;
	w.process();
	assertEquals(true, op1.isComplete());
	assertEquals(true, op2.isComplete());
	assertEquals(true, op3.isComplete());
}
 
Example #12
Source File: UniqueOperationTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void replace_non_entity_operation() {
	WorldConfiguration wc = new WorldConfiguration()
			.setSystem(OperationSystem.class);
	World world = new World(wc);

	unique("hello",
		sequence(
			delayTick(3),
			run(new Runnable() {
				@Override
				public void run() {
					fail();
				}
			})
		)
	).register(world);

	OperationTestUtil.process(world);
	assertFalse(bool2);

	unique("hello",
		run(new Runnable() {
			@Override
			public void run() {
				bool2 = true;
			}
		})
	).register(world);

	OperationTestUtil.process(world);
	OperationTestUtil.process(world);
	assertTrue(bool2);

	assertEquals(0, operatives(world).size());
}
 
Example #13
Source File: UniqueOperationTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void replace_operation() {
	WorldConfiguration wc = new WorldConfiguration()
			.setSystem(OperationSystem.class);
	World world = new World(wc);

	Entity e = world.createEntity();
	unique("hello",
		sequence(
			delayTick(3),
			run(new Runnable() {
				@Override
				public void run() {
					fail();
				}
			})
		)
	).register(e);

	OperationTestUtil.process(world);
	assertFalse(bool2);

	unique("hello",
		run(new Runnable() {
			@Override
			public void run() {
				bool2 = true;
			}
		})
	).register(e);

	OperationTestUtil.process(world);
	OperationTestUtil.process(world);
	assertTrue(bool2);


	assertEquals(0, operatives(world).size());
}
 
Example #14
Source File: UniqueOperationTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void run_single_operation() {
	WorldConfiguration wc = new WorldConfiguration()
			.setSystem(OperationSystem.class);
	World world = new World(wc);

	sequence(
		unique("hello",
			run(new Runnable() {
				@Override
				public void run() {
					bool1 = true;
				}
			})
		),
		run(new Runnable() {
			@Override
			public void run() {
				bool2 = true;
			}
		})
	).register(world.createEntity());

	OperationTestUtil.process(world);

	assertTrue(bool1);
	assertTrue(bool2);
}
 
Example #15
Source File: OperationTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test(expected = MundaneWireException.class)
public void no_wire_failed_inject_test() {
	World world = new World(new WorldConfiguration()
		.setSystem(OperationSystem.class));

	operation(NoWireNoInjectOperation.class).register(world);

	process(world);
}
 
Example #16
Source File: OperationsPluginTest.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
@Test
public void When_registering_operation_plugin_Should_register_operationsystem() {
    World world = new World(new WorldConfigurationBuilder()
            .dependsOn(OperationsPlugin.class)
            .build());
    Assert.assertNotNull(world.getSystem(OperationSystem.class));
    world.process();
}
 
Example #17
Source File: Operation.java    From artemis-odb-orion with Apache License 2.0 5 votes vote down vote up
public final void register(World world, int entityId) {
	OperationSystem os = world.getSystem(OperationSystem.class);
	if (os == null) {
		String name = OperationSystem.class.getSimpleName();
		String error = "Failed to register operation as " + name
			+ " isn't registered with the World instance.";

		throw new RuntimeException(error);
	}

	os.register(entityId, toNode());
}
 
Example #18
Source File: SystemProfiler.java    From riiablo with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(BaseSystem baseSystem, World world) {
  system = baseSystem;
  gpu = ClassUtils.hasAnnotation(baseSystem.getClass(), GpuSystem.class);
  if (name == null) {
    name = toString();
  }
  if (color == null) {
    calculateColor(toString().hashCode(), color = new Color());
  }
}
 
Example #19
Source File: JsonSerializationTest.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
@Test
public void save_load_entity_with_operation() {
	String[] string = new String[1];
	int[] entity = new int[1];

	World world = new World(new WorldConfiguration()
		.register("string", string)
		.register("entity", entity)
		.setSystem(OperationSystem.class)
		.setSystem(WorldSerializationManager.class));

	WorldSerializationManager worldSerializer =
		world.getSystem(WorldSerializationManager.class);

	JsonArtemisSerializer backend = new JsonArtemisSerializer(world);
	backend.prettyPrint(true);
	InterpolationSerializer.registerAll(backend);
	backend.register(OperationTree.class, new OperationTreeSerializer());
	worldSerializer.setSerializer(backend);

	int e0 = world.create();
	int e1 = world.create();

	process(world);


	assertEquals(null, string[0]);
	assertEquals(0, entity[0]);

	sequence(
		delayTick(1),
		operation(EntityWriterOperation.class)
	).register(world, e1);

	process(world);
	String saveE1 = save(world, all(Operative.class));

	process(world);

	assertNotNull(string[0]);
	assertEquals(1, entity[0]);

	ByteArrayInputStream is = new ByteArrayInputStream(
			saveE1.getBytes(StandardCharsets.UTF_8));
	SaveFileFormat load = worldSerializer.load(is, SaveFileFormat.class);

	int e2 = load.entities.get(0);
	assertEquals(1, load.entities.size());
	assertNotEquals(e1, e2);

	// making sure values haven't changed due to loading
	assertNotNull(string[0]);
	assertEquals(1, entity[0]);

	world.process();

	assertNotNull(string[0]);
	assertEquals(e2, entity[0]);
}
 
Example #20
Source File: OperationTest.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
@Test
public void testParallelProcessing() {
	OperationSystem operationSystem = new OperationSystem();
	World world = new World(new WorldConfiguration()
			.setSystem(operationSystem));

	DelayOperation[] ds = new DelayOperation[] {
		delay(0.25f),
		delay(0.25f),
		delay(0.25f),
		delay(0.75f)
	};

	parallel(ds[0], ds[1], ds[2], ds[3]).register(world, world.create());

	world.delta = 0.000001f;
	world.process();

	for (int i = 0; i < ds.length - 1; i++) {
		assertFalse(ds[i].isComplete());
		assertEquals(.25f, ds[i].remaining(), .001f);
	}

	world.delta = 0.15f;
	world.process();

	for (int i = 0; i < ds.length - 1; i++) {
		assertFalse(ds[i].isComplete());
		assertEquals(.25f - 0.15f, ds[i].remaining(), .001f);
	}
	assertEquals(.75f - 0.15f, ds[3].remaining(), .001f);

	world.delta = 0.5f;
	world.process();

	for (int i = 0; i < ds.length - 1; i++) {
		assertTrue(ds[i].isComplete());
		assertEquals(0f, ds[i].remaining(), 0.0001f);
	}
	assertEquals(0.75f - 0.65f, ds[3].remaining(), 0.0001f);
	assertFalse(ds[3].isComplete());

	world.delta = 0.5f;
	world.process();
	world.process();

	// orion should be evicted from the system here.
	assertEquals(0, operatives(world).size());
}
 
Example #21
Source File: OperationTest.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
private static void process(World world) {
	world.delta = 1f / 60f;
	world.process();
}
 
Example #22
Source File: OperationTest.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
@Test
	public void testSequentialProcessing() {

		OperationSystem operationSystem = new OperationSystem();
		World world = new World(new WorldConfiguration()
				.setSystem(operationSystem));

		DelayOperation[] ds = new DelayOperation[] {
			delay(0.25f),
			delay(0.25f),
			delay(0.25f),
			delay(0.25f)
		};

		sequence(ds[0], ds[1], ds[2], ds[3]).register(world, world.create());

		world.delta = 0;
		world.process();

		assertEquals(1, operatives(world).size());

		for (int i = 0; i < ds.length; i++) {
			assertFalse(ds[i].isComplete());
			assertEquals(.25f, ds[i].remaining(), .001f);
		}

		world.delta = 0.15f;
		world.process();

		assertFalse(ds[0].isComplete());
		assertEquals(.25f - 0.15f, ds[0].remaining(), .001f);
		for (int i = 1; i < ds.length; i++) {
			assertFalse(ds[i].isComplete());
			assertEquals(.25f, ds[i].remaining(), .001f);
		}

		world.delta = 0.75f;
		world.process();

		for (int i = 0; i < ds.length - 1; i++) {
			assertTrue(ds[i].isComplete());
			assertEquals(0f, ds[i].remaining(), 0.0000000f);
		}

		assertFalse(ds[3].isComplete());
		assertEquals(.25f - 0.15f, ds[3].remaining(), .001f);

		world.delta = 0.25f;
		world.process();
		world.process();

		// orion should be evicted from the system here.
		assertEquals(0, operatives(world).size());

//		assertEquals(0, operationSystem.getOperations().size);
	}
 
Example #23
Source File: PComp1.java    From entity-system-benchmarks with Apache License 2.0 4 votes vote down vote up
public PComp1(World world) {
	this.world = world;
}
 
Example #24
Source File: ArtemisBenchmark.java    From ashley with Apache License 2.0 4 votes vote down vote up
private void runWorldTest (World world) {
	for (int i = 0; i < Constants.FRAMES; ++i) {
		world.setDelta(Constants.DELTA_TIME);
		world.process();
	}
}
 
Example #25
Source File: ArtemisBenchmark.java    From ashley with Apache License 2.0 4 votes vote down vote up
private static World prepareWorld (int numEntities) {
	World world = new World();

	world.setSystem(new MovementSystem());
	world.setSystem(new StateSystem());
	world.setSystem(new CollisionSystem());
	world.setSystem(new RemovalSystem());

	world.initialize();

	for (int i = 0; i < numEntities; ++i) {
		Entity entity = world.createEntity();

		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.addComponent(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.addComponent(mov);
		}

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

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

		world.addEntity(entity);
	}

	return world;
}
 
Example #26
Source File: OperationTestUtil.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
public static void process(World world) {
	world.delta = 1f / 60f;
	world.process();
}
 
Example #27
Source File: OperationTestUtil.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
public static IntBag operatives(World world) {
	return world.getAspectSubscriptionManager()
		.get(all(Operative.class))
		.getEntities();
}
 
Example #28
Source File: JsonSerializationTest.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
private static String save(World world, Aspect.Builder aspect) {
	EntitySubscription subscription =
		world.getAspectSubscriptionManager().get(aspect);

	return save(world.getSystem(WorldSerializationManager.class), subscription);
}
 
Example #29
Source File: ProfilerManager.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public SystemProfiler createFor(BaseSystem system, World world) {
  return add(new SystemProfiler(system, world));
}
 
Example #30
Source File: VoidEntityOperationTest.java    From artemis-odb-orion with Apache License 2.0 4 votes vote down vote up
@Test
public void test_when_killing_entities() {
	World w = new World(new WorldConfiguration()
		.setSystem(new OperationSystem()));

	HiThereOperation op = operation(HiThereOperation.class).s("hi");
	HiThereOperation op2 = operation(HiThereOperation.class).s("hepp");

	HiThereOperation eOp = operation(HiThereOperation.class).s("eOp");
	HiThereOperation eOp2 = operation(HiThereOperation.class).s("eOp2");

	sequence(
		// 1
		delayTick(1),

		// 2
		op,
		delayTick(1),

		// 3
		delayTick(1),
		op2,

		// 4
		delayTick(1)
	).register(w);

	int entity = w.create();
	sequence(
		// 1
		delayTick(1),

		// 2
		eOp,
		delayTick(1),

		// 3
		delayTick(1),
		eOp2,

		// 4
		delayTick(1)
	).register(w, entity);

			// 1
	w.delta = .1f;
	w.process();

	assertEquals("hi", op.s);
	assertEquals("hepp", op2.s);
	assertEquals("eOp", eOp.s);
	assertEquals("eOp2", eOp2.s);

	// 2
	w.delta = .1f;
	w.process();

	assertEquals("hi!!!", op.s);
	assertEquals("hepp", op2.s);
	assertEquals("eOp!!!", eOp.s);
	assertEquals("eOp2", eOp2.s);

	// 3
	w.delta = .1f;
	w.delete(entity);
	w.process();

	assertEquals("hi!!!", op.s);
	assertEquals("hepp", op2.s);
	assertEquals(null, eOp.s); // reclaimed
	assertEquals(null, eOp2.s);

	w.delta = .1f;
	w.process(); // 4

	assertEquals("hi!!!", op.s);
	assertEquals("hepp!!!", op2.s);
	assertEquals(null, eOp.s); // reclaimed
	assertEquals(null, eOp2.s);
}