From a673b749a5816392652785457dd1cf3603368628 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 8 Feb 2026 12:33:16 -0800 Subject: chore: Use actors --- core/src/main/java/coffee/liz/ecs/DAGWorld.java | 272 ++++++++++----------- core/src/main/java/coffee/liz/ecs/math/Mat2.java | 148 +++++------ core/src/main/java/coffee/liz/ecs/math/Vec2.java | 134 +++++----- core/src/main/java/coffee/liz/ecs/math/Vec2f.java | 86 +++---- core/src/main/java/coffee/liz/ecs/math/Vec2i.java | 76 +++--- .../main/java/coffee/liz/ecs/model/Component.java | 6 +- .../src/main/java/coffee/liz/ecs/model/Entity.java | 154 ++++++------ .../src/main/java/coffee/liz/ecs/model/System.java | 36 +-- core/src/main/java/coffee/liz/ecs/model/World.java | 80 +++--- 9 files changed, 496 insertions(+), 496 deletions(-) (limited to 'core/src/main/java/coffee/liz/ecs') diff --git a/core/src/main/java/coffee/liz/ecs/DAGWorld.java b/core/src/main/java/coffee/liz/ecs/DAGWorld.java index b5b54c2..24d5adc 100644 --- a/core/src/main/java/coffee/liz/ecs/DAGWorld.java +++ b/core/src/main/java/coffee/liz/ecs/DAGWorld.java @@ -26,140 +26,140 @@ import java.util.stream.Collectors; @Log4j2 @RequiredArgsConstructor public class DAGWorld implements World { - /** All entities in the world. */ - protected final Set entities = Collections.synchronizedSet(new HashSet<>()); - - /** Cache mapping component types to entities having that component. */ - private final Map, Set> componentCache = Collections - .synchronizedMap(new HashMap<>()); - - /** Deterministic ID's for spawned entities. */ - private final AtomicInteger nextEntityId = new AtomicInteger(0); - - /** All registered systems. */ - protected final Map>, System> systems; - - /** Ordered list of systems for execution. */ - private final List> systemExecutionOrder; - - public DAGWorld(final Map>, System> systems) { - this.systems = systems; - this.systemExecutionOrder = buildExecutionOrder(systems.values().stream().toList()); - log.debug("Executing in order: {}", systemExecutionOrder); - } - - @Override - public Entity createEntity() { - final Entity entity = Entity.builder().id(nextEntityId.incrementAndGet()).build(); - entities.add(entity); - return entity; - } - - @Override - public void removeEntity(final Entity entity) { - entity.getComponentMap().keySet().forEach(componentType -> { - final Set cachedEntities = componentCache.get(componentType); - if (cachedEntities != null) { - cachedEntities.remove(entity); - } - }); - entities.remove(entity); - } - - @Override - public Set query(final Collection> components) { - if (components.isEmpty()) { - return Set.copyOf(entities); - } - - final Class firstType = components.iterator().next(); - final Set candidates = componentCache.get(firstType); - if (candidates == null) { - return Collections.emptySet(); - } - - return candidates.stream().filter(entity -> components.stream().allMatch(entity::has)) - .collect(Collectors.toSet()); - } - - @Override - public void update(final T state, final Duration duration) { - systemExecutionOrder.forEach(system -> { - refreshComponentCache(); - system.update(this, state, duration); - }); - refreshComponentCache(); - } - - @SuppressWarnings("unchecked") - @Override - public > S getSystem(final Class system) { - return (S) systems.get(system); - } - - private void refreshComponentCache() { - componentCache.clear(); - entities.forEach(entity -> entity.getComponentMap().keySet().forEach( - componentType -> componentCache.computeIfAbsent(componentType, _ -> new HashSet<>()).add(entity))); - } - - private List> buildExecutionOrder(final Collection> systems) { - if (systems.isEmpty()) { - return Collections.emptyList(); - } - - final Map, System> systemMap = systems.stream() - .collect(Collectors.toMap(System::getClass, system -> system)); - final Map, Integer> inDegree = new HashMap<>(); - final Map, Set>> adjacencyList = new HashMap<>(); - - systems.forEach(system -> { - final Class systemClass = system.getClass(); - inDegree.put(systemClass, 0); - adjacencyList.put(systemClass, new HashSet<>()); - }); - - systems.forEach(system -> { - system.getDependencies().forEach(dependency -> { - if (systemMap.containsKey(dependency)) { - adjacencyList.get(dependency).add(system.getClass()); - inDegree.merge(system.getClass(), 1, Integer::sum); - } - }); - }); - - // Kahn's algorithm - final List> result = new ArrayList<>(); - - final Queue> queue = new LinkedList<>( - inDegree.entrySet().stream().filter(entry -> entry.getValue() == 0).map(Map.Entry::getKey).toList()); - - while (!queue.isEmpty()) { - final Class currentClass = queue.poll(); - result.add(systemMap.get(currentClass)); - - adjacencyList.getOrDefault(currentClass, Collections.emptySet()).forEach(dependent -> { - final int newInDegree = inDegree.get(dependent) - 1; - inDegree.put(dependent, newInDegree); - if (newInDegree == 0) { - queue.add(dependent); - } - }); - } - - if (result.size() != systems.size()) { - throw new IllegalStateException("Circular dependency detected in systems"); - } - - return Collections.unmodifiableList(result); - } - - @Override - public void close() throws Exception { - for (final System system : systemExecutionOrder) { - system.close(); - } - componentCache.clear(); - entities.clear(); - } + /** All entities in the world. */ + protected final Set entities = Collections.synchronizedSet(new HashSet<>()); + + /** Cache mapping component types to entities having that component. */ + private final Map, Set> componentCache = Collections + .synchronizedMap(new HashMap<>()); + + /** Deterministic ID's for spawned entities. */ + private final AtomicInteger nextEntityId = new AtomicInteger(0); + + /** All registered systems. */ + protected final Map>, System> systems; + + /** Ordered list of systems for execution. */ + private final List> systemExecutionOrder; + + public DAGWorld(final Map>, System> systems) { + this.systems = systems; + this.systemExecutionOrder = buildExecutionOrder(systems.values().stream().toList()); + log.debug("Executing in order: {}", systemExecutionOrder); + } + + @Override + public Entity createEntity() { + final Entity entity = Entity.builder().id(nextEntityId.incrementAndGet()).build(); + entities.add(entity); + return entity; + } + + @Override + public void removeEntity(final Entity entity) { + entity.getComponentMap().keySet().forEach(componentType -> { + final Set cachedEntities = componentCache.get(componentType); + if (cachedEntities != null) { + cachedEntities.remove(entity); + } + }); + entities.remove(entity); + } + + @Override + public Set query(final Collection> components) { + if (components.isEmpty()) { + return Set.copyOf(entities); + } + + final Class firstType = components.iterator().next(); + final Set candidates = componentCache.get(firstType); + if (candidates == null) { + return Collections.emptySet(); + } + + return candidates.stream().filter(entity -> components.stream().allMatch(entity::has)) + .collect(Collectors.toSet()); + } + + @Override + public void update(final T state, final Duration duration) { + systemExecutionOrder.forEach(system -> { + refreshComponentCache(); + system.update(this, state, duration); + }); + refreshComponentCache(); + } + + @SuppressWarnings("unchecked") + @Override + public > S getSystem(final Class system) { + return (S) systems.get(system); + } + + private void refreshComponentCache() { + componentCache.clear(); + entities.forEach(entity -> entity.getComponentMap().keySet().forEach( + componentType -> componentCache.computeIfAbsent(componentType, _ -> new HashSet<>()).add(entity))); + } + + private List> buildExecutionOrder(final Collection> systems) { + if (systems.isEmpty()) { + return Collections.emptyList(); + } + + final Map, System> systemMap = systems.stream() + .collect(Collectors.toMap(System::getClass, system -> system)); + final Map, Integer> inDegree = new HashMap<>(); + final Map, Set>> adjacencyList = new HashMap<>(); + + systems.forEach(system -> { + final Class systemClass = system.getClass(); + inDegree.put(systemClass, 0); + adjacencyList.put(systemClass, new HashSet<>()); + }); + + systems.forEach(system -> { + system.getDependencies().forEach(dependency -> { + if (systemMap.containsKey(dependency)) { + adjacencyList.get(dependency).add(system.getClass()); + inDegree.merge(system.getClass(), 1, Integer::sum); + } + }); + }); + + // Kahn's algorithm + final List> result = new ArrayList<>(); + + final Queue> queue = new LinkedList<>( + inDegree.entrySet().stream().filter(entry -> entry.getValue() == 0).map(Map.Entry::getKey).toList()); + + while (!queue.isEmpty()) { + final Class currentClass = queue.poll(); + result.add(systemMap.get(currentClass)); + + adjacencyList.getOrDefault(currentClass, Collections.emptySet()).forEach(dependent -> { + final int newInDegree = inDegree.get(dependent) - 1; + inDegree.put(dependent, newInDegree); + if (newInDegree == 0) { + queue.add(dependent); + } + }); + } + + if (result.size() != systems.size()) { + throw new IllegalStateException("Circular dependency detected in systems"); + } + + return Collections.unmodifiableList(result); + } + + @Override + public void close() throws Exception { + for (final System system : systemExecutionOrder) { + system.close(); + } + componentCache.clear(); + entities.clear(); + } } diff --git a/core/src/main/java/coffee/liz/ecs/math/Mat2.java b/core/src/main/java/coffee/liz/ecs/math/Mat2.java index 9975227..ed9493c 100644 --- a/core/src/main/java/coffee/liz/ecs/math/Mat2.java +++ b/core/src/main/java/coffee/liz/ecs/math/Mat2.java @@ -7,82 +7,82 @@ import java.util.function.Function; import java.util.function.Supplier; public final class Mat2 { - private Mat2() { - } + private Mat2() { + } - /** - * Initializes a mutable 2d matrix of given type. - * - * @param dimensions - * the dimensions - * @param constructor - * the constructor - * @return row-indexed 2d matrix of {@param dimensions} - */ - public static List> init(final Vec2 dimensions, final Function, T> constructor) { - final List> rows = new ArrayList<>(); - for (int y = 0; y < dimensions.getY(); y++) { - final List row = new ArrayList<>(dimensions.getX()); - for (int x = 0; x < dimensions.getX(); x++) - row.add(constructor.apply(Vec2i.builder().y(y).x(x).build())); - rows.add(row); - } - return rows; - } + /** + * Initializes a mutable 2d matrix of given type. + * + * @param dimensions + * the dimensions + * @param constructor + * the constructor + * @return row-indexed 2d matrix of {@param dimensions} + */ + public static List> init(final Vec2 dimensions, final Function, T> constructor) { + final List> rows = new ArrayList<>(); + for (int y = 0; y < dimensions.getY(); y++) { + final List row = new ArrayList<>(dimensions.getX()); + for (int x = 0; x < dimensions.getX(); x++) + row.add(constructor.apply(Vec2i.builder().y(y).x(x).build())); + rows.add(row); + } + return rows; + } - /** - * Convolves a {@link Convolver} across a matrix reaching neighbors around the - * grid like a torus. - * - * @param mat - * is the row-indexed 2d matrix to convolve. - * @param axes - * are the x/y major/minor axes. - * @param init - * is the initial value of the convolution. - * @param convolver - * is the {@link Convolver}. - * @param finalReduction - * to apply after {@param convolver}. - * @return result of {@param convolver} applied along axes at each cell. - * @param - * is the type of the matrix to convolve. - * @param - * is the type of the resulting type of each convolution. - */ - public static List> convolveTorus(final List> mat, final Vec2 axes, - final Supplier init, final Convolver convolver, final BiFunction finalReduction) { - final List> rows = new ArrayList<>(); - for (int y = 0; y < mat.size(); y++) { - final List row = new ArrayList<>(mat.get(y).size()); - for (int x = 0; x < mat.get(y).size(); x++) { - final T center = mat.get(y).get(x); - R result = init.get(); - for (int dy = -axes.getY(); dy <= axes.getY(); dy++) { - final int ry = Math.floorMod(y + dy, mat.size()); - for (int dx = -axes.getX(); dx <= axes.getX(); dx++) { - final int rx = Math.floorMod(x + dx, mat.get(ry).size()); - result = convolver.convolve(mat.get(ry).get(rx), Vec2i.builder().x(dx).y(dy).build(), result); - } - } - row.add(result); - } - rows.add(row); - } + /** + * Convolves a {@link Convolver} across a matrix reaching neighbors around the + * grid like a torus. + * + * @param mat + * is the row-indexed 2d matrix to convolve. + * @param axes + * are the x/y major/minor axes. + * @param init + * is the initial value of the convolution. + * @param convolver + * is the {@link Convolver}. + * @param finalReduction + * to apply after {@param convolver}. + * @return result of {@param convolver} applied along axes at each cell. + * @param + * is the type of the matrix to convolve. + * @param + * is the type of the resulting type of each convolution. + */ + public static List> convolveTorus(final List> mat, final Vec2 axes, + final Supplier init, final Convolver convolver, final BiFunction finalReduction) { + final List> rows = new ArrayList<>(); + for (int y = 0; y < mat.size(); y++) { + final List row = new ArrayList<>(mat.get(y).size()); + for (int x = 0; x < mat.get(y).size(); x++) { + final T center = mat.get(y).get(x); + R result = init.get(); + for (int dy = -axes.getY(); dy <= axes.getY(); dy++) { + final int ry = Math.floorMod(y + dy, mat.size()); + for (int dx = -axes.getX(); dx <= axes.getX(); dx++) { + final int rx = Math.floorMod(x + dx, mat.get(ry).size()); + result = convolver.convolve(mat.get(ry).get(rx), Vec2i.builder().x(dx).y(dy).build(), result); + } + } + row.add(result); + } + rows.add(row); + } - final List> reductions = new ArrayList<>(); - for (int y = 0; y < mat.size(); y++) { - final List reduction = new ArrayList<>(mat.get(y).size()); - for (int x = 0; x < mat.get(y).size(); x++) { - reduction.add(finalReduction.apply(mat.get(y).get(x), rows.get(y).get(x))); - } - reductions.add(reduction); - } - return reductions; - } + final List> reductions = new ArrayList<>(); + for (int y = 0; y < mat.size(); y++) { + final List reduction = new ArrayList<>(mat.get(y).size()); + for (int x = 0; x < mat.get(y).size(); x++) { + reduction.add(finalReduction.apply(mat.get(y).get(x), rows.get(y).get(x))); + } + reductions.add(reduction); + } + return reductions; + } - @FunctionalInterface - public interface Convolver { - R convolve(final T center, final Vec2 rel, final R reduction); - } + @FunctionalInterface + public interface Convolver { + R convolve(final T center, final Vec2 rel, final R reduction); + } } diff --git a/core/src/main/java/coffee/liz/ecs/math/Vec2.java b/core/src/main/java/coffee/liz/ecs/math/Vec2.java index 7620395..dde6e51 100644 --- a/core/src/main/java/coffee/liz/ecs/math/Vec2.java +++ b/core/src/main/java/coffee/liz/ecs/math/Vec2.java @@ -9,80 +9,80 @@ import java.util.function.Function; * the numeric type of vector components */ public interface Vec2 { - /** - * @return the x coordinate - */ - T getX(); + /** + * @return the x coordinate + */ + T getX(); - /** - * @return the y coordinate - */ - T getY(); + /** + * @return the y coordinate + */ + T getY(); - /** - * Adds another vector to this vector. - * - * @param other - * the vector to add - * @return a new vector with the result - */ - Vec2 plus(final Vec2 other); + /** + * Adds another vector to this vector. + * + * @param other + * the vector to add + * @return a new vector with the result + */ + Vec2 plus(final Vec2 other); - /** - * Subtracts another vector from this vector. - * - * @param other - * the vector to subtract - * @return a new vector with the result - */ - Vec2 minus(final Vec2 other); + /** + * Subtracts another vector from this vector. + * + * @param other + * the vector to subtract + * @return a new vector with the result + */ + Vec2 minus(final Vec2 other); - /** - * Scales this vector by the given factors. - * - * @param scaleX - * the x scale factor - * @param scaleY - * the y scale factor - * @return a new scaled vector - */ - Vec2 scale(final T scaleX, final T scaleY); + /** + * Scales this vector by the given factors. + * + * @param scaleX + * the x scale factor + * @param scaleY + * the y scale factor + * @return a new scaled vector + */ + Vec2 scale(final T scaleX, final T scaleY); - /** - * Scales this vector by the given vector. - * - * @param scale - * scale vec. - * @return a new scaled vector - */ - default Vec2 scale(final Vec2 scale) { - return scale(scale.getX(), scale.getY()); - } + /** + * Scales this vector by the given vector. + * + * @param scale + * scale vec. + * @return a new scaled vector + */ + default Vec2 scale(final Vec2 scale) { + return scale(scale.getX(), scale.getY()); + } - /** - * Length of the vector. - * - * @return length. - */ - float length(); + /** + * Length of the vector. + * + * @return length. + */ + float length(); - /** - * @return Vec2 components of {@link Vec2} - */ - Vec2 intValue(); + /** + * @return Vec2 components of {@link Vec2} + */ + Vec2 intValue(); - /** - * @return Vec2 components of {@link Vec2} - */ - Vec2 floatValue(); + /** + * @return Vec2 components of {@link Vec2} + */ + Vec2 floatValue(); - /** - * @param xTransform - * transform of x component. - * @param yTransform - * transform of y component. - * @return transformed vec applying {@param xTransform} to x component, - * {@param yTransform} to y component. - */ - Vec2 transform(final Function xTransform, final Function yTransform); + /** + * @param xTransform + * transform of x component. + * @param yTransform + * transform of y component. + * @return transformed vec applying {@param xTransform} to x component, + * {@param yTransform} to y component. + */ + Vec2 transform(final Function xTransform, final Function yTransform); } diff --git a/core/src/main/java/coffee/liz/ecs/math/Vec2f.java b/core/src/main/java/coffee/liz/ecs/math/Vec2f.java index 5b91a2d..42b73e7 100644 --- a/core/src/main/java/coffee/liz/ecs/math/Vec2f.java +++ b/core/src/main/java/coffee/liz/ecs/math/Vec2f.java @@ -15,47 +15,47 @@ import java.util.function.Function; @Data @Builder public final class Vec2f implements Vec2 { - /** X coordinate. */ - private final Float x; - - /** Y coordinate. */ - private final Float y; - - @Override - public Vec2 plus(final Vec2 other) { - return new Vec2f(x + other.getX(), y + other.getY()); - } - - @Override - public Vec2 minus(final Vec2 other) { - return new Vec2f(x - other.getX(), y - other.getY()); - } - - @Override - public Vec2 scale(final Float scaleX, final Float scaleY) { - return new Vec2f(x * scaleX, y * scaleY); - } - - @Override - public float length() { - return (float) sqrt(x * x + y * y); - } - - @Override - public Vec2 floatValue() { - return this; - } - - @Override - public Vec2 transform(final Function xTransform, final Function yTransform) { - return new Vec2f(xTransform.apply(getX()), yTransform.apply(getY())); - } - - @Override - public Vec2 intValue() { - return Vec2i.builder().x(this.x.intValue()).y(this.y.intValue()).build(); - } - - /** Zero float vec */ - public static Vec2 ZERO = Vec2f.builder().x(0f).y(0f).build(); + /** X coordinate. */ + private final Float x; + + /** Y coordinate. */ + private final Float y; + + @Override + public Vec2 plus(final Vec2 other) { + return new Vec2f(x + other.getX(), y + other.getY()); + } + + @Override + public Vec2 minus(final Vec2 other) { + return new Vec2f(x - other.getX(), y - other.getY()); + } + + @Override + public Vec2 scale(final Float scaleX, final Float scaleY) { + return new Vec2f(x * scaleX, y * scaleY); + } + + @Override + public float length() { + return (float) sqrt(x * x + y * y); + } + + @Override + public Vec2 floatValue() { + return this; + } + + @Override + public Vec2 transform(final Function xTransform, final Function yTransform) { + return new Vec2f(xTransform.apply(getX()), yTransform.apply(getY())); + } + + @Override + public Vec2 intValue() { + return Vec2i.builder().x(this.x.intValue()).y(this.y.intValue()).build(); + } + + /** Zero float vec */ + public static Vec2 ZERO = Vec2f.builder().x(0f).y(0f).build(); } diff --git a/core/src/main/java/coffee/liz/ecs/math/Vec2i.java b/core/src/main/java/coffee/liz/ecs/math/Vec2i.java index b07e481..326a5df 100644 --- a/core/src/main/java/coffee/liz/ecs/math/Vec2i.java +++ b/core/src/main/java/coffee/liz/ecs/math/Vec2i.java @@ -15,51 +15,51 @@ import java.util.function.Function; @Builder @Data public final class Vec2i implements Vec2 { - /** X coordinate. */ - private final Integer x; + /** X coordinate. */ + private final Integer x; - /** Y coordinate. */ - private final Integer y; + /** Y coordinate. */ + private final Integer y; - @Override - public Vec2 plus(final Vec2 other) { - return new Vec2i(x + other.getX(), y + other.getY()); - } + @Override + public Vec2 plus(final Vec2 other) { + return new Vec2i(x + other.getX(), y + other.getY()); + } - @Override - public Vec2 minus(final Vec2 other) { - return new Vec2i(x - other.getX(), y - other.getY()); - } + @Override + public Vec2 minus(final Vec2 other) { + return new Vec2i(x - other.getX(), y - other.getY()); + } - @Override - public Vec2 scale(final Integer scaleX, final Integer scaleY) { - return new Vec2i(x * scaleX, y * scaleY); - } + @Override + public Vec2 scale(final Integer scaleX, final Integer scaleY) { + return new Vec2i(x * scaleX, y * scaleY); + } - @Override - public Vec2 floatValue() { - return Vec2f.builder().x(this.x.floatValue()).y(this.y.floatValue()).build(); - } + @Override + public Vec2 floatValue() { + return Vec2f.builder().x(this.x.floatValue()).y(this.y.floatValue()).build(); + } - @Override - public Vec2 transform(final Function xTransform, - final Function yTransform) { - return new Vec2i(xTransform.apply(getX()), yTransform.apply(getY())); - } + @Override + public Vec2 transform(final Function xTransform, + final Function yTransform) { + return new Vec2i(xTransform.apply(getX()), yTransform.apply(getY())); + } - @Override - public Vec2 intValue() { - return this; - } + @Override + public Vec2 intValue() { + return this; + } - @Override - public float length() { - return (float) sqrt(x * x + y * y); - } + @Override + public float length() { + return (float) sqrt(x * x + y * y); + } - public static final Vec2 NORTH = new Vec2i(0, 1); - public static final Vec2 SOUTH = new Vec2i(0, -1); - public static final Vec2 EAST = new Vec2i(1, 0); - public static final Vec2 WEST = new Vec2i(-1, 0); - public static final Vec2 ZERO = new Vec2i(0, 0); + public static final Vec2 NORTH = new Vec2i(0, 1); + public static final Vec2 SOUTH = new Vec2i(0, -1); + public static final Vec2 EAST = new Vec2i(1, 0); + public static final Vec2 WEST = new Vec2i(-1, 0); + public static final Vec2 ZERO = new Vec2i(0, 0); } diff --git a/core/src/main/java/coffee/liz/ecs/model/Component.java b/core/src/main/java/coffee/liz/ecs/model/Component.java index f96ba95..2d3a8e7 100644 --- a/core/src/main/java/coffee/liz/ecs/model/Component.java +++ b/core/src/main/java/coffee/liz/ecs/model/Component.java @@ -2,7 +2,7 @@ package coffee.liz.ecs.model; /** Component of an {@link Entity}. */ public interface Component { - default Class getKey() { - return getClass(); - } + default Class getKey() { + return getClass(); + } } diff --git a/core/src/main/java/coffee/liz/ecs/model/Entity.java b/core/src/main/java/coffee/liz/ecs/model/Entity.java index e820e57..7dab667 100644 --- a/core/src/main/java/coffee/liz/ecs/model/Entity.java +++ b/core/src/main/java/coffee/liz/ecs/model/Entity.java @@ -18,88 +18,88 @@ import java.util.Set; @AllArgsConstructor @Data public class Entity { - /** Unique id. */ - private final int id; + /** Unique id. */ + private final int id; - /** Instances of {@link Component}s. */ - @Builder.Default - private Map, Component> componentMap = Collections.synchronizedMap(new HashMap<>()); + /** Instances of {@link Component}s. */ + @Builder.Default + private Map, Component> componentMap = Collections.synchronizedMap(new HashMap<>()); - /** - * Check if entity has component type. - * - * @param componentType - * the {@link Component} class - * @return true if component exists - */ - public boolean has(final Class componentType) { - return componentMap.containsKey(componentType); - } + /** + * Check if entity has component type. + * + * @param componentType + * the {@link Component} class + * @return true if component exists + */ + public boolean has(final Class componentType) { + return componentMap.containsKey(componentType); + } - /** - * Check if entity has all component types. - * - * @param components - * collection of {@link Component} classes - * @return true if all components exist - */ - public boolean hasAll(final Collection> components) { - return components.stream().allMatch(this::has); - } + /** + * Check if entity has all component types. + * + * @param components + * collection of {@link Component} classes + * @return true if all components exist + */ + public boolean hasAll(final Collection> components) { + return components.stream().allMatch(this::has); + } - /** - * Get component by type. - * - * @param componentType - * the {@link Component} class - * @param - * component type - * @return the component or throw {@link IllegalArgumentException} - */ - @SuppressWarnings("unchecked") - public C get(final Class componentType) { - final C component = (C) componentMap.get(componentType); - if (component == null) { - throw new IllegalArgumentException( - "Entity with id " + getId() + " does not have required component " + componentType.getSimpleName()); - } - return component; - } + /** + * Get component by type. + * + * @param componentType + * the {@link Component} class + * @param + * component type + * @return the component or throw {@link IllegalArgumentException} + */ + @SuppressWarnings("unchecked") + public C get(final Class componentType) { + final C component = (C) componentMap.get(componentType); + if (component == null) { + throw new IllegalArgumentException( + "Entity with id " + getId() + " does not have required component " + componentType.getSimpleName()); + } + return component; + } - /** - * Add component to entity. - * - * @param component - * the {@link Component} to add - * @param - * component type - * @return this {@link Entity} for chaining - */ - public Entity add(final C component) { - componentMap.put(component.getKey(), component); - return this; - } + /** + * Add component to entity. + * + * @param component + * the {@link Component} to add + * @param + * component type + * @return this {@link Entity} for chaining + */ + public Entity add(final C component) { + componentMap.put(component.getKey(), component); + return this; + } - /** - * Remove component from entity. - * - * @param componentType - * the {@link Component} class to remove - * @param - * component type - * @return this {@link Entity} for chaining - */ - public Entity remove(final Class componentType) { - componentMap.remove(componentType); - return this; - } + /** + * Remove component from entity. + * + * @param componentType + * the {@link Component} class to remove + * @param + * component type + * @return this {@link Entity} for chaining + */ + public Entity remove(final Class componentType) { + componentMap.remove(componentType); + return this; + } - /** - * Get all component types. - * - * @return set of {@link Component} classes - */ - public Set> componentTypes() { - return componentMap.keySet(); - } + /** + * Get all component types. + * + * @return set of {@link Component} classes + */ + public Set> componentTypes() { + return componentMap.keySet(); + } } diff --git a/core/src/main/java/coffee/liz/ecs/model/System.java b/core/src/main/java/coffee/liz/ecs/model/System.java index ac99818..c4b0bc0 100644 --- a/core/src/main/java/coffee/liz/ecs/model/System.java +++ b/core/src/main/java/coffee/liz/ecs/model/System.java @@ -10,24 +10,24 @@ import java.util.Collection; * is the state of the stuff outside the {@link World}. */ public interface System extends AutoCloseable { - /** - * {@link System} clazzes that must run before this system. - * - * @return {@link Collection} of dependencies. - */ - Collection>> getDependencies(); + /** + * {@link System} clazzes that must run before this system. + * + * @return {@link Collection} of dependencies. + */ + Collection>> getDependencies(); - /** - * @param world - * Is the {@link World}. - * @param state - * Is the {@link T} state outside the {@param world}. - * @param dt - * Is the timestep. - */ - void update(final World world, final T state, final Duration dt); + /** + * @param world + * Is the {@link World}. + * @param state + * Is the {@link T} state outside the {@param world}. + * @param dt + * Is the timestep. + */ + void update(final World world, final T state, final Duration dt); - @Override - default void close() { - } + @Override + default void close() { + } } diff --git a/core/src/main/java/coffee/liz/ecs/model/World.java b/core/src/main/java/coffee/liz/ecs/model/World.java index 05363e8..c59a0d7 100644 --- a/core/src/main/java/coffee/liz/ecs/model/World.java +++ b/core/src/main/java/coffee/liz/ecs/model/World.java @@ -11,48 +11,48 @@ import java.util.Set; * is the state of the stuff outside the world. */ public interface World extends AutoCloseable { - /** - * Create unique {@link Entity} in the {@link World}. - * - * @return created {@link Entity}. - */ - Entity createEntity(); + /** + * Create unique {@link Entity} in the {@link World}. + * + * @return created {@link Entity}. + */ + Entity createEntity(); - /** - * Remove an entity from the {@link World}. - * - * @param entity - * to remove. - */ - void removeEntity(final Entity entity); + /** + * Remove an entity from the {@link World}. + * + * @param entity + * to remove. + */ + void removeEntity(final Entity entity); - /** - * Get entities with {@link Component}s. - * - * @param components - * to query for. - * @return All entities with all {@param components}. - */ - Set query(final Collection> components); + /** + * Get entities with {@link Component}s. + * + * @param components + * to query for. + * @return All entities with all {@param components}. + */ + Set query(final Collection> components); - /** - * Integrate the {@link World}. - * - * @param state - * Is the state outside the world. - * @param duration - * Is the time step. - */ - void update(final T state, final Duration duration); + /** + * Integrate the {@link World}. + * + * @param state + * Is the state outside the world. + * @param duration + * Is the time step. + */ + void update(final T state, final Duration duration); - /** - * Get world {@link System}. - * - * @param system - * is the Clazz. - * @param - * is the {@link System} type. - * @return {@link System} instance of {@param system}. - */ - > S getSystem(final Class system); + /** + * Get world {@link System}. + * + * @param system + * is the Clazz. + * @param + * is the {@link System} type. + * @return {@link System} instance of {@param system}. + */ + > S getSystem(final Class system); } -- cgit v1.3