### Run Hello Dominion Example Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-examples/README.md Command to execute the 'Hello Dominion' application. This is a basic example to get started with Dominion ECS. ```bash java -jar dominion-ecs-examples/target/dominion-ecs-examples-0.9.0-SNAPSHOT.jar ``` -------------------------------- ### Creating Entities with Prepared Compositions in Java Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt This example demonstrates how to define specific component compositions using the Dominion API and use them to efficiently instantiate large batches of entities. It covers both 2-component and 3-component structures, showing how to inject initial values during creation. ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Composition; import dev.dominion.ecs.api.Entity; public class PreparedCompositions { record Position(double x, double y) {} record Velocity(double x, double y) {} record Health(int value) {} record Damage(int value) {} record Enemy() {} public static void main(String[] args) { Dominion world = Dominion.create(); Composition composition = world.composition(); Composition.Of2 bulletComposition = composition.of(Position.class, Velocity.class); for (int i = 0; i < 1000; i++) { Entity bullet = world.createPreparedEntity( bulletComposition.withValue( new Position(0, i), new Velocity(10, 0) ) ); } Composition.Of3 enemyComposition = composition.of(Position.class, Health.class, Enemy.class); for (int i = 0; i < 100; i++) { world.createPreparedEntity( enemyComposition.withValue( new Position(i * 10, 100), new Health(50 + i), new Enemy() ) ); } world.close(); } } ``` -------------------------------- ### Run Dark Entities Example Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-examples/README.md Command to execute the 'Dark Entities' application. This example showcases more advanced features, suitable for turn-based rogue-like games, including entity-component-system management and a lighting system. ```bash java -cp dominion-ecs-examples/target/dominion-ecs-examples-0.9.0-SNAPSHOT.jar dev.dominion.ecs.examples.dark.DarkEntities ``` -------------------------------- ### Java: Create and Schedule ECS Systems with Dominion Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates creating entities with components, defining systems (movement and rendering) that process these entities, and scheduling their execution using Dominion ECS. It covers setting up a fixed-rate tick, suspending and resuming systems, and managing the scheduler's lifecycle. This example requires the Dominion ECS library. ```Java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Scheduler; public class SystemScheduling { static class Position { double x, y; public Position(double x, double y) { this.x = x; this.y = y; } } record Velocity(double x, double y) {} record Health(int value) {} public static void main(String[] args) throws InterruptedException { Dominion world = Dominion.create(); // Create entities world.createEntity("player", new Position(0, 0), new Velocity(1, 0.5)); world.createEntity("enemy", new Position(100, 100), new Velocity(-1, -1)); // Create scheduler Scheduler scheduler = world.createScheduler(); // Movement system - updates positions based on velocity Runnable movementSystem = scheduler.schedule(() -> { world.findEntitiesWith(Position.class, Velocity.class) .stream() .forEach(result -> { Position pos = result.comp1(); Velocity vel = result.comp2(); // Scale by deltaTime for frame-independent movement double dt = scheduler.deltaTime(); pos.x += vel.x() * dt * 60; // 60 units per second pos.y += vel.y() * dt * 60; }); }); // Render system - displays entity positions Runnable renderSystem = scheduler.schedule(() -> { System.out.println("--- Frame (dt=" + String.format("%.3f", scheduler.deltaTime()) + "s) ---"); world.findEntitiesWith(Position.class) .stream() .forEach(result -> { Position pos = result.comp1(); String name = result.entity().getName(); System.out.printf("%s at (%.1f, %.1f)%n", name, pos.x, pos.y); }); }); // Start fixed-rate ticking at 2 ticks per second scheduler.tickAtFixedRate(2); // Let it run for 2 seconds Thread.sleep(2000); // Suspend movement system (render continues) scheduler.suspend(movementSystem); System.out.println("\n*** Movement suspended ***\n"); Thread.sleep(1000); // Resume movement system scheduler.resume(movementSystem); System.out.println("\n*** Movement resumed ***\n"); Thread.sleep(1000); // Stop ticking (set to 0) scheduler.tickAtFixedRate(0); // Orderly shutdown scheduler.shutDown(); world.close(); } } ``` -------------------------------- ### Complete Game Loop Example Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt A comprehensive example illustrating the typical Entity-Component-System (ECS) game loop pattern. It shows how entities, components, systems, and the scheduler interact to simulate movement and rendering. ```APIDOC ## Complete Game Loop Example ### Description A complete example demonstrating the typical ECS game loop pattern with entities, components, systems, and the scheduler working together for a movement simulation. ### Method N/A (Illustrative Code) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Scheduler; public class GameLoopExample { // Components static class Position { double x, y; public Position(double x, double y) { this.x = x; this.y = y; } @Override public String toString() { return String.format("(%.1f, %.1f)", x, y); } } record Velocity(double x, double y) {} record Bounds(double minX, double maxX, double minY, double maxY) {} record Player() {} // Tag component public static void main(String[] args) throws InterruptedException { // Create game world Dominion game = Dominion.create("game"); // Create player entity game.createEntity("Hero", new Player(), new Position(50, 50), new Velocity(2, 1), new Bounds(0, 100, 0, 100) ); // Create some NPCs for (int i = 0; i < 3; i++) { game.createEntity("NPC-" + i, new Position(Math.random() * 100, Math.random() * 100), new Velocity(Math.random() * 2 - 1, Math.random() * 2 - 1), new Bounds(0, 100, 0, 100) ); } // Create scheduler Scheduler scheduler = game.createScheduler(); // Movement system scheduler.schedule(() -> { double dt = scheduler.deltaTime(); game.findEntitiesWith(Position.class, Velocity.class) .stream() .forEach(result -> { Position pos = result.comp1(); Velocity vel = result.comp2(); pos.x += vel.x() * dt * 60; pos.y += vel.y() * dt * 60; }); }); // Boundary collision system scheduler.schedule(() -> { game.findEntitiesWith(Position.class, Bounds.class) .stream() .forEach(result -> { Position pos = result.comp1(); Bounds bounds = result.comp2(); // Wrap around boundaries if (pos.x < bounds.minX()) pos.x = bounds.maxX(); if (pos.x > bounds.maxX()) pos.x = bounds.minX(); if (pos.y < bounds.minY()) pos.y = bounds.maxY(); if (pos.y > bounds.maxY()) pos.y = bounds.minY(); }); }); // Render system (runs last) scheduler.schedule(() -> { System.out.println("\n=== Tick " + String.format("(dt=%.3fs)", scheduler.deltaTime()) + " ==="); // Render player first game.findEntitiesWith(Position.class, Player.class) .stream() .forEach(r -> System.out.println("[PLAYER] " + r.entity().getName() + " @ " + r.comp1())); // Then NPCs game.findEntitiesWith(Position.class) .without(Player.class) .stream() .forEach(r -> System.out.println(" [NPC] " + r.entity().getName() + " @ " + r.comp1())); }); // Run at 5 ticks per second for demonstration scheduler.tickAtFixedRate(5); // Run for 2 seconds Thread.sleep(2000); // Clean shutdown scheduler.tickAtFixedRate(0); scheduler.shutDown(); game.close(); System.out.println("\nGame ended."); } } ``` ``` -------------------------------- ### Find Entities by Component (Java) Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Provides examples of finding entities based on their components. This includes finding all entities, entities with a specific component type, or entities with multiple specified component types. ```java Results allEntities = dominion.findAllEntities(); Results entitiesWithComponentA = dominion.findEntitiesWith(ComponentA.class); Results entitiesWithAandB = dominion.findEntitiesWith(ComponentA.class, ComponentB.class); ``` -------------------------------- ### Parallel System Execution with Dominion ECS Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates parallel execution of independent systems using `parallelSchedule()` and forking subsystems within a system using `forkAndJoinAll()`. This example showcases how to set up entities, schedule sequential and parallel tasks, and execute scheduler ticks for processing. ```Java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Scheduler; public class ParallelSystems { static class Position { double x, y; public Position(double x, double y) { this.x = x; this.y = y; } } record Velocity(double x, double y) {} record Physics() {} // Tag for physics-enabled entities record AI() {} // Tag for AI-controlled entities public static void main(String[] args) throws InterruptedException { Dominion world = Dominion.create(); // Create entities for (int i = 0; i < 100; i++) { world.createEntity(new Position(i, i), new Velocity(1, 1), new Physics()); } for (int i = 0; i < 50; i++) { world.createEntity("ai-" + i, new Position(i * 2, i * 2), new AI()); } Scheduler scheduler = world.createScheduler(); // Input system runs first (sequential) scheduler.schedule(() -> { // Process input... System.out.println("1. Input processed"); }); // Physics and AI can run in parallel (independent systems) Runnable[] parallelSystems = scheduler.parallelSchedule( () -> { world.findEntitiesWith(Position.class, Physics.class) .stream() .forEach(r -> { /* physics calculations */ }); System.out.println("2a. Physics updated (parallel)"); }, () -> { world.findEntitiesWith(Position.class, AI.class) .stream() .forEach(r -> { /* AI decisions */ }); System.out.println("2b. AI updated (parallel)"); } ); // Render system with forked subsystems scheduler.schedule(() -> { System.out.println("3. Starting render..."); // Fork parallel subsystems for different render passes scheduler.forkAndJoinAll( () -> { // Shadow pass System.out.println(" 3a. Shadow pass (forked)"); }, () -> { // Geometry pass System.out.println(" 3b. Geometry pass (forked)"); }, () -> { // Lighting pass System.out.println(" 3c. Lighting pass (forked)"); } ); System.out.println("3. Render complete (all passes joined)"); }); // Execute one tick manually scheduler.tick(); // Or run with manual delta time (in nanoseconds) scheduler.tick(16_666_667); // ~60 FPS delta scheduler.shutDown(); world.close(); } } ``` -------------------------------- ### Query Entities with Components in Java Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates querying entities that possess specific component types using the Dominion ECS API. It shows how to retrieve entities with one, two, or three components, and how to iterate or stream the results. The code also includes examples of creating entities with various components and finding all entities. ```Java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Results.With1; import dev.dominion.ecs.api.Results.With2; import dev.dominion.ecs.api.Results.With3; public class QueryingEntities { static class Position { double x, y; public Position(double x, double y) { this.x = x; this.y = y; } } record Velocity(double x, double y) {} record Health(int value) {} record Enemy() {} // Tag component public static void main(String[] args) { Dominion world = Dominion.create(); // Create various entities world.createEntity("player", new Position(0, 0), new Velocity(1, 1), new Health(100)); world.createEntity("enemy-1", new Position(10, 10), new Velocity(-1, 0), new Health(50), new Enemy()); world.createEntity("enemy-2", new Position(20, 5), new Velocity(0, -1), new Health(30), new Enemy()); world.createEntity("static-object", new Position(5, 5)); // Query entities with single component type System.out.println("=== Entities with Position ==="); for (With1 result : world.findEntitiesWith(Position.class)) { Position pos = result.comp(); System.out.println("Entity at: " + pos.x + ", " + pos.y); } // Query with two component types using stream System.out.println("\n=== Moving entities (Position + Velocity) ==="); world.findEntitiesWith(Position.class, Velocity.class) .stream() .forEach(result -> { Position pos = result.comp1(); Velocity vel = result.comp2(); String name = result.entity().getName(); System.out.println(name + ": pos=(" + pos.x + "," + pos.y + ") vel=(" + vel.x + "," + vel.y + ")"); }); // Query with three component types System.out.println("\n=== Enemies with health ==="); world.findEntitiesWith(Position.class, Health.class, Enemy.class) .stream() .forEach(result -> { String name = result.entity().getName(); Health health = result.comp2(); System.out.println(name + " HP: " + health.value()); }); // Find all entities System.out.println("\n=== All entities ==="); world.findAllEntities().stream() .forEach(entity -> System.out.println("- " + entity.getName())); world.close(); } } ``` -------------------------------- ### Java: Filter ECS Query Results with without(), withAlso(), withState() Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt This Java code snippet demonstrates filtering entity query results in Dominion ECS. It shows how to exclude entities with specific components using without(), require additional components using withAlso(), and filter by entity state using withState(). The example creates several entities with different components and states, then applies various filter combinations to retrieve precise subsets of entities. ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Entity; public class FilteringResults { record Position(double x, double y) {} record Velocity(double x, double y) {} record Health(int value) {} record Shield(int value) {} record Frozen() {} // Tag for frozen entities enum UnitState { ACTIVE, STUNNED, DEAD } public static void main(String[] args) { Dominion world = Dominion.create(); // Create entities with various states Entity warrior = world.createEntity("warrior", new Position(0, 0), new Velocity(1, 1), new Health(100), new Shield(50)); warrior.setState(UnitState.ACTIVE); Entity mage = world.createEntity("mage", new Position(5, 5), new Velocity(0, 1), new Health(60)); mage.setState(UnitState.ACTIVE); Entity frozenKnight = world.createEntity("frozen-knight", new Position(10, 10), new Velocity(0, 0), new Health(80), new Frozen()); frozenKnight.setState(UnitState.STUNNED); Entity deadEnemy = world.createEntity("dead-enemy", new Position(15, 15), new Health(0)); deadEnemy.setState(UnitState.DEAD); // Find movable entities WITHOUT Frozen component System.out.println("=== Movable units (not frozen) ==="); world.findEntitiesWith(Position.class, Velocity.class) .without(Frozen.class) .stream() .forEach(r -> System.out.println("- " + r.entity().getName())); // Output: // - warrior // - mage // Find entities WITH Health that ALSO have Shield System.out.println("\n=== Units with health AND shield ==="); world.findEntitiesWith(Health.class) .withAlso(Shield.class) .stream() .forEach(r -> System.out.println("- " + r.entity().getName())); // Output: // - warrior // Find only ACTIVE entities System.out.println("\n=== Active units ==="); world.findEntitiesWith(Position.class, Health.class) .withState(UnitState.ACTIVE) .stream() .forEach(r -> System.out.println("- " + r.entity().getName())); // Output: // - warrior // - mage // Combine filters System.out.println("\n=== Active movable units without shield ==="); world.findEntitiesWith(Position.class, Velocity.class) .without(Shield.class, Frozen.class) .withState(UnitState.ACTIVE) .stream() .forEach(r -> System.out.println("- " + r.entity().getName())); // Output: // - mage world.close(); } } ``` -------------------------------- ### Initialize Dominion World and System Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/README.md Demonstrates how to create a Dominion world, define components using classes and records, create entities, and schedule a system to process entity updates at a fixed rate. ```java public class HelloDominion { public static void main(String[] args) { Dominion hello = Dominion.create(); hello.createEntity( "my-entity", new Position(0, 0), new Velocity(1, 1) ); Runnable system = () -> { hello.findEntitiesWith(Position.class, Velocity.class) .stream().forEach(result -> { Position position = result.comp1(); Velocity velocity = result.comp2(); position.x += velocity.x; position.y += velocity.y; System.out.printf("Entity %s moved with %s to %s\n", result.entity().getName(), velocity, position); }); }; Scheduler scheduler = hello.createScheduler(); scheduler.schedule(system); scheduler.tickAtFixedRate(3); } static class Position { double x, y; public Position(double x, double y) {} @Override public String toString() { return ""; } } record Velocity(double x, double y) {} } ``` -------------------------------- ### System Properties Configuration Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates how to configure Dominion's startup behavior and logging levels using Java system properties. ```APIDOC ## System Properties Configuration ### Description Dominion supports configuration through system properties for controlling startup behavior and logging levels. ### Method N/A (Configuration) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ```java public class ConfigurationExample { public static void main(String[] args) { // Disable startup banner System.setProperty("dominion.show-banner", "false"); // Set global logging level (TRACE, DEBUG, INFO, WARNING, ERROR) System.setProperty("dominion.logging-level", "INFO"); // Set logging level for specific dominion instance System.setProperty("dominion.my-game.logging-level", "DEBUG"); // Now create dominion instances var game = Dominion.create("my-game"); // Application code... game.close(); } } ``` ``` -------------------------------- ### Create Entities with Components Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Shows how to instantiate entities and attach POJO or record components. It covers creating empty entities, named entities, and using existing entities as prefabs. ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Entity; public class EntityCreation { static class Position { double x, y; public Position(double x, double y) { this.x = x; this.y = y; } } record Velocity(double x, double y) {} record Health(int current, int max) {} record Name(String value) {} public static void main(String[] args) { Dominion world = Dominion.create(); Entity emptyEntity = world.createEntity(); Entity player = world.createEntity(new Position(0, 0), new Velocity(1, 1), new Health(100, 100)); Entity namedPlayer = world.createEntity("player-1", new Position(10, 20), new Velocity(0, 0), new Health(100, 100)); Entity enemy = world.createEntityAs(player, new Name("Goblin")); world.close(); } } ``` -------------------------------- ### Configure System Properties Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates how to set system properties to control Dominion instance behavior, such as disabling the startup banner and adjusting logging verbosity. ```java public class ConfigurationExample { public static void main(String[] args) { System.setProperty("dominion.show-banner", "false"); System.setProperty("dominion.logging-level", "INFO"); System.setProperty("dominion.my-game.logging-level", "DEBUG"); var game = Dominion.create("my-game"); game.close(); } } ``` -------------------------------- ### Create Dominion Instance (Java) Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Demonstrates how to create new Dominion instances, either with an automatically assigned name or a custom name. The Dominion acts as a container for all ECS data. ```java Dominion dominion = Dominion.create(); Dominion namedDominion = Dominion.create("MyDominion"); ``` -------------------------------- ### Creating and Modifying Entities with Composition Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Demonstrates how to use the Composition class to prepare entity creation with specific component types and how to modify existing entities by adding or removing components. ```java Dominion dominion = Dominion.create(); Composition composition = dominion.composition(); // prepared entity creations var compositionOf1 = composition.of(Comp.class); Entity entity1 = dominion.createPreparedEntity(compositionOf1.withValue(new Comp(0))); Entity entity2 = dominion.createPreparedEntity(compositionOf1.withValue(new Comp(1))); // prepared entity changes var modifyAdding1 = composition.byAdding1AndRemoving(Comp2.class); dominion.modifyEntity(modifyAdding1.withValue(entity1, new Comp2())); dominion.modifyEntity(modifyAdding1.withValue(entity2, new Comp2())); ``` -------------------------------- ### POST /composition/createPreparedEntity Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Creates an entity using a pre-defined composition template for optimized performance. ```APIDOC ## POST /composition/createPreparedEntity ### Description Creates a new entity in the Dominion world using a pre-compiled composition template. This is significantly faster than standard entity creation when spawning large numbers of entities with identical component sets. ### Method POST ### Endpoint world.createPreparedEntity(Composition.OfN<...>.withValue(...)) ### Parameters #### Path Parameters - **None** #### Request Body - **Composition** (Object) - Required - The pre-defined composition template (e.g., Of2, Of3). - **Values** (Object) - Required - The component instances corresponding to the composition types. ### Request Example { "composition": "bulletComposition", "values": [ {"x": 0, "y": 10}, {"x": 10, "y": 0} ] } ### Response #### Success Response (200) - **Entity** (Object) - The created entity instance. #### Response Example { "status": "success", "entityId": "12345" } ``` -------------------------------- ### Initialize and Manage Dominion Instances Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates how to create, name, and close a Dominion ECS instance. This serves as the primary container for managing entities and components within a specific simulation context. ```java import dev.dominion.ecs.api.Dominion; public class DominionCreation { public static void main(String[] args) { Dominion world = Dominion.create(); System.out.println("Created: " + world.getName()); Dominion gameWorld = Dominion.create("game-world"); System.out.println("Created: " + gameWorld.getName()); System.out.println("Is closed: " + gameWorld.isClosed()); gameWorld.close(); System.out.println("Is closed: " + gameWorld.isClosed()); } } ``` -------------------------------- ### Entity Creation (Java) Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Shows different methods for creating entities within a Dominion. Entities can be created with initial components, from a prepared composition, or by using another entity as a prefab. ```java Entity entity = dominion.createEntity(new ComponentA(), new ComponentB()); Composition.OfTypes composition = Composition.ofTypes(ComponentA.class, ComponentB.class); Entity preparedEntity = dominion.createPreparedEntity(composition.withValues(new ComponentA(), new ComponentB())); Entity prefabEntity = dominion.createEntityAs(prefabEntity, new ComponentC()); ``` -------------------------------- ### Scheduling Systems in Dominion ECS Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Demonstrates how to schedule sequential and parallel systems, manage their lifecycle with suspend/resume, and utilize fork-join patterns for concurrent sub-tasks. ```java Scheduler scheduler = new Scheduler(); // Schedule sequential systems scheduler.schedule(() -> System.out.println("System A")); // Schedule parallel systems scheduler.parallelSchedule(() -> System.out.println("System B"), () -> System.out.println("System C")); // Suspend and Resume Runnable systemB = () -> System.out.println("System B"); scheduler.parallelSchedule(systemB); scheduler.suspend(systemB); scheduler.resume(systemB); // Fork and Join scheduler.schedule(() -> { scheduler.forkAndJoinAll(() -> System.out.println("Subsystem 1"), () -> System.out.println("Subsystem 2")); }); // Start ticking scheduler.tickAtFixedRate(60); ``` -------------------------------- ### Find Compositions by Component (Java) Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Demonstrates how to find compositions that contain specific component types. This is useful for querying entities based on their component makeup. ```java Results compositionsWithA = dominion.findCompositionsWith(ComponentA.class); Results compositionsWithAandB = dominion.findCompositionsWith(ComponentA.class, ComponentB.class); ``` -------------------------------- ### Parallel System Execution Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates parallel execution of independent systems using parallelSchedule() and forked subsystems using forkAndJoinAll(). ```APIDOC ## Parallel System Execution The Scheduler supports parallel execution through `parallelSchedule()` for independent systems and `forkAndJoinAll()` for spawning subsystems within a system. This enables efficient utilization of multicore CPUs while maintaining execution order guarantees. ### Usage Example This example shows how to set up parallel execution for physics and AI systems, and how to use `forkAndJoinAll` for rendering passes. ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Scheduler; public class ParallelSystems { static class Position { double x, y; public Position(double x, double y) { this.x = x; this.y = y; } } record Velocity(double x, double y) {} record Physics() {} record AI() {} public static void main(String[] args) throws InterruptedException { Dominion world = Dominion.create(); // Create entities for (int i = 0; i < 100; i++) { world.createEntity(new Position(i, i), new Velocity(1, 1), new Physics()); } for (int i = 0; i < 50; i++) { world.createEntity("ai-" + i, new Position(i * 2, i * 2), new AI()); } Scheduler scheduler = world.createScheduler(); // Input system runs first (sequential) scheduler.schedule(() -> { System.out.println("1. Input processed"); }); // Physics and AI can run in parallel (independent systems) scheduler.parallelSchedule( () -> { world.findEntitiesWith(Position.class, Physics.class).stream().forEach(r -> { /* physics calculations */ }); System.out.println("2a. Physics updated (parallel)"); }, () -> { world.findEntitiesWith(Position.class, AI.class).stream().forEach(r -> { /* AI decisions */ }); System.out.println("2b. AI updated (parallel)"); } ); // Render system with forked subsystems scheduler.schedule(() -> { System.out.println("3. Starting render..."); scheduler.forkAndJoinAll( () -> { System.out.println(" 3a. Shadow pass (forked)"); }, () -> { System.out.println(" 3b. Geometry pass (forked)"); }, () -> { System.out.println(" 3c. Lighting pass (forked)"); } ); System.out.println("3. Render complete (all passes joined)"); }); scheduler.tick(); // Execute one tick manually scheduler.tick(16_666_667); // Or run with manual delta time (~60 FPS) scheduler.shutDown(); world.close(); } } ``` ### Key Methods - **`parallelSchedule(Runnable... systems)`**: Schedules multiple independent systems to run in parallel. Returns an array of `Runnable` representing the scheduled systems. - **`forkAndJoinAll(Runnable... subsystems)`**: Spawns multiple subsystems that run in parallel and waits for all of them to complete before continuing. ### Concepts - **Independent Systems**: Systems that do not depend on each other's results within the same tick can be run in parallel using `parallelSchedule()`. - **Subsystems**: For tasks that can be broken down into smaller, parallelizable parts within a larger system (e.g., rendering passes), `forkAndJoinAll()` is used. - **Execution Guarantees**: While enabling parallelism, the scheduler maintains execution order guarantees between sequential and parallel system groups. ``` -------------------------------- ### Implement ECS Game Loop with Dominion Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates a complete ECS simulation loop including entity creation, component definition, system scheduling for movement and collision, and lifecycle management. It utilizes the Dominion API to process entities based on component composition. ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Scheduler; public class GameLoopExample { static class Position { double x, y; public Position(double x, double y) { this.x = x; this.y = y; } @Override public String toString() { return String.format("(%.1f, %.1f)", x, y); } } record Velocity(double x, double y) {} record Bounds(double minX, double maxX, double minY, double maxY) {} record Player() {} public static void main(String[] args) throws InterruptedException { Dominion game = Dominion.create("game"); game.createEntity("Hero", new Player(), new Position(50, 50), new Velocity(2, 1), new Bounds(0, 100, 0, 100) ); for (int i = 0; i < 3; i++) { game.createEntity("NPC-" + i, new Position(Math.random() * 100, Math.random() * 100), new Velocity(Math.random() * 2 - 1, Math.random() * 2 - 1), new Bounds(0, 100, 0, 100) ); } Scheduler scheduler = game.createScheduler(); scheduler.schedule(() -> { double dt = scheduler.deltaTime(); game.findEntitiesWith(Position.class, Velocity.class) .stream() .forEach(result -> { Position pos = result.comp1(); Velocity vel = result.comp2(); pos.x += vel.x() * dt * 60; pos.y += vel.y() * dt * 60; }); }); scheduler.schedule(() -> { game.findEntitiesWith(Position.class, Bounds.class) .stream() .forEach(result -> { Position pos = result.comp1(); Bounds bounds = result.comp2(); if (pos.x < bounds.minX()) pos.x = bounds.maxX(); if (pos.x > bounds.maxX()) pos.x = bounds.minX(); if (pos.y < bounds.minY()) pos.y = bounds.maxY(); if (pos.y > bounds.maxY()) pos.y = bounds.minY(); }); }); scheduler.schedule(() -> { System.out.println("\n=== Tick " + String.format("(dt=%.3fs)", scheduler.deltaTime()) + " ==="); game.findEntitiesWith(Position.class, Player.class) .stream() .forEach(r -> System.out.println("[PLAYER] " + r.entity().getName() + " @ " + r.comp1())); game.findEntitiesWith(Position.class) .without(Player.class) .stream() .forEach(r -> System.out.println(" [NPC] " + r.entity().getName() + " @ " + r.comp1())); }); scheduler.tickAtFixedRate(5); Thread.sleep(2000); scheduler.tickAtFixedRate(0); scheduler.shutDown(); game.close(); } } ``` -------------------------------- ### Entity Creation API Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt API methods for creating entities, attaching components, and utilizing prefabs within a Dominion world. ```APIDOC ## Dominion.createEntity() ### Description Creates a new entity within the world, optionally assigning a name and an initial set of components. ### Method Instance Method ### Parameters #### Request Body - **name** (String) - Optional - The first string argument acts as the entity name. - **components** (Object...) - Optional - Variable number of POJO or record components to attach to the entity. ### Request Example world.createEntity("player", new Position(0, 0), new Health(100)); ### Response #### Success Response (200) - **Entity** (Object) - The created entity instance. ## Dominion.createEntityAs() ### Description Creates a new entity using an existing entity as a template (prefab), copying its component types. ### Method Instance Method ### Parameters #### Request Body - **prefab** (Entity) - Required - The entity to copy component types from. - **components** (Object...) - Optional - Additional components to add to the new entity. ### Response #### Success Response (200) - **Entity** (Object) - The new entity instance. ``` -------------------------------- ### Manage Entities and Components in Dominion ECS Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates how to create entities, add or remove components dynamically, check for component existence, manage entity states using enums, and toggle entity lifecycle status. This requires the Dominion ECS API dependency. ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Entity; public class EntityManagement { record Position(double x, double y) {} record Velocity(double x, double y) {} record Health(int value) {} record Poison(int damage) {} enum EntityState { IDLE, MOVING, ATTACKING, DEAD } public static void main(String[] args) { Dominion world = Dominion.create(); Entity entity = world.createEntity("hero", new Position(0, 0)); entity.add(new Velocity(1, 0)); entity.add(new Health(100)); System.out.println("Has Position: " + entity.has(Position.class)); Position pos = entity.get(Position.class); System.out.println("Position: " + pos); Health health = entity.get(Health.class); System.out.println("Contains health: " + entity.contains(health)); entity.remove(health); System.out.println("Has Health after remove: " + entity.has(Health.class)); entity.removeType(Velocity.class); entity.setState(EntityState.IDLE); entity.setState(EntityState.MOVING); entity.setState(null); entity.setEnabled(false); System.out.println("Is enabled: " + entity.isEnabled()); entity.setEnabled(true); boolean deleted = world.deleteEntity(entity); System.out.println("Deleted: " + deleted); System.out.println("Is deleted: " + entity.isDeleted()); world.close(); } } ``` -------------------------------- ### Dominion Instance Management Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt API methods for initializing, naming, and lifecycle management of a Dominion ECS world instance. ```APIDOC ## Dominion.create() ### Description Creates a new instance of the Dominion ECS world. This instance acts as a container for all entities, components, and systems. ### Method Static Factory ### Parameters #### Query Parameters - **name** (String) - Optional - A unique identifier for the Dominion instance. ### Request Example Dominion world = Dominion.create("my-world"); ### Response #### Success Response (200) - **Dominion** (Object) - The initialized Dominion world instance. ## Dominion.close() ### Description Closes the Dominion instance, releasing resources and off-heap memory associated with the world. ### Method Instance Method ### Response #### Success Response (200) - **void** - Closes the instance and sets isClosed() to true. ``` -------------------------------- ### Shutdown Method Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Initiates an orderly shutdown of the system. Previously submitted systems will be executed, but no new systems will be accepted. ```APIDOC ## shutDown ### Description Initiates an orderly shutdown in which previously submitted systems are executed, but no new systems will be accepted. ### Method boolean ### Endpoint N/A (This is a method call, not a REST endpoint) ### Parameters None ### Request Example N/A ### Response #### Success Response (true) - **boolean** (boolean) - Returns true if the shutdown was initiated successfully. #### Response Example true ``` -------------------------------- ### Maven Dependency Configuration Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Instructions on how to add the Dominion ECS Java library to your Maven project by including the necessary dependency in your pom.xml file. Requires Java 17 or newer. ```APIDOC ## Maven Dependency Configuration ### Description Add Dominion to your Java project by including the dependency in your pom.xml. Requires Java 17 or newer. ### Method N/A (Configuration) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A ```xml 17 dev.dominion.ecs dominion-ecs-engine 0.9.0 ``` ``` -------------------------------- ### Iterating and Streaming Dominion Results Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Demonstrates how to retrieve entities from a Results instance using both standard Java iterators and the Stream API. This allows for flexible processing of query results within the Dominion ECS framework. ```java Results results = dominion.findEntitiesWith(Position.class); // Using Iterator for (Entity entity : results) { System.out.println(entity); } // Using Stream results.stream().filter(e -> e.has(Velocity.class)).forEach(System.out::println); ``` -------------------------------- ### Configure Maven Dependency Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Required Maven configuration to include the Dominion ECS engine in a Java project. Requires Java 17 or higher. ```xml 17 dev.dominion.ecs dominion-ecs-engine 0.9.0 ``` -------------------------------- ### Entity Deletion (Java) Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Illustrates how to delete an entity from a Dominion. Deleting an entity frees its ID and cancels references to its components. ```java boolean deleted = dominion.deleteEntity(entity); ``` -------------------------------- ### Initiate ECS Shutdown Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md The shutDown method initiates an orderly shutdown process. It ensures that previously submitted systems complete their execution while preventing the acceptance of any new systems. ```java boolean shutDown(); ``` -------------------------------- ### Batch Modify Entity Components with Dominion ECS Source: https://context7.com/dominion-dev/dominion-ecs-java/llms.txt Demonstrates how to use composition modifiers to add and remove components from entities. It covers single component updates and batch operations where multiple components are added or removed in a single transaction. ```java import dev.dominion.ecs.api.Dominion; import dev.dominion.ecs.api.Composition; import dev.dominion.ecs.api.Entity; public class ModifyingComposition { record Position(double x, double y) {} record Velocity(double x, double y) {} record Health(int value) {} record Stunned(double duration) {} record Dead() {} record Invincible() {} public static void main(String[] args) { Dominion world = Dominion.create(); Composition composition = world.composition(); Entity player = world.createEntity("player", new Position(0, 0), new Velocity(1, 1), new Health(100) ); var addStunned = composition.byAdding1AndRemoving(Stunned.class); world.modifyEntity(addStunned.withValue(player, new Stunned(3.0))); var removeStunned = composition.byRemoving(Stunned.class); world.modifyEntity(removeStunned.withValue(player)); var becomeInvincible = composition.byAdding1AndRemoving(Invincible.class, Health.class); world.modifyEntity(becomeInvincible.withValue(player, new Invincible())); var die = composition.byAdding1AndRemoving(Dead.class, Velocity.class, Invincible.class); world.modifyEntity(die.withValue(player, new Dead())); world.close(); } } ``` -------------------------------- ### Results API Methods Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Methods available on the Results instance to iterate, stream, or filter entity compositions. ```APIDOC ## Results API Methods ### Description The Results class provides an interface to interact with entity compositions found via Dominion queries. It supports functional-style streaming, standard iteration, and dynamic filtering. ### Methods - **iterator()**: Returns an Iterator to traverse found entities. - **stream()**: Returns a Stream for functional processing. - **without(Class... componentTypes)**: Returns a new Results instance excluding specified component types. - **withAlso(Class... componentTypes)**: Returns a new Results instance including additional component types. - **withState(S state)**: Returns a new Results instance filtered by the specified entity state. ### Usage Example ```java Results results = dominion.findEntitiesWith(Position.class); results.stream() .filter(e -> e.has(Velocity.class)) .forEach(System.out::println); ``` ``` -------------------------------- ### Filtering Dominion Results Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/dominion-ecs-api/README.md Shows how to refine a Results set by excluding or including specific component types. These methods return a new Results instance, allowing for fluent query chaining. ```java Results filtered = results .withAlso(Velocity.class) .without(Disabled.class); ``` -------------------------------- ### Add Dominion ECS Dependency to Maven Source: https://github.com/dominion-dev/dominion-ecs-java/blob/main/README.md Configures the Dominion ECS engine dependency in a Java project using Maven. This is required to access the core engine classes like DataComposition and ChunkedPool. ```xml dev.dominion.ecs dominion-ecs-engine 0.9.0 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.