Introduce Character
This commit is huge breaking change. The Entity class has been downgraded to some generic object which can be pushed onto the ObjectLayer, whereas the former "entity" concept has been replaced with Character class.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.ai;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.character.Character;
|
||||
|
||||
public interface NPC extends Entity {
|
||||
public interface NPC extends Character {
|
||||
AI getStrategy();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.bartlomiejpluta.base.api.character;
|
||||
|
||||
import com.bartlomiejpluta.base.api.animation.Animated;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.event.Event;
|
||||
import com.bartlomiejpluta.base.api.event.EventType;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.Movable;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public interface Character extends Movable, Animated, Entity {
|
||||
|
||||
Direction getFaceDirection();
|
||||
|
||||
void setFaceDirection(Direction direction);
|
||||
|
||||
void changeCharacterSet(String characterSetUid);
|
||||
|
||||
CompletableFuture<Void> performInstantAnimation();
|
||||
|
||||
<E extends Event> void addEventListener(EventType<E> type, Consumer<E> listener);
|
||||
|
||||
<E extends Event> void removeEventListener(EventType<E> type, Consumer<E> listener);
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package com.bartlomiejpluta.base.api.context;
|
||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||
import com.bartlomiejpluta.base.api.audio.Sound;
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.character.Character;
|
||||
import com.bartlomiejpluta.base.api.event.Event;
|
||||
import com.bartlomiejpluta.base.api.event.EventType;
|
||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||
@@ -39,7 +39,7 @@ public interface Context extends Updatable, Renderable, Disposable {
|
||||
|
||||
void closeMap();
|
||||
|
||||
Entity createEntity(String entitySetUid);
|
||||
Character createCharacter(String characterSetUid);
|
||||
|
||||
Animation createAnimation(String animationUid);
|
||||
|
||||
|
||||
@@ -1,49 +1,23 @@
|
||||
package com.bartlomiejpluta.base.api.entity;
|
||||
|
||||
import com.bartlomiejpluta.base.api.animation.Animated;
|
||||
import com.bartlomiejpluta.base.api.event.Event;
|
||||
import com.bartlomiejpluta.base.api.event.EventType;
|
||||
import com.bartlomiejpluta.base.api.event.Reactive;
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.Movable;
|
||||
import com.bartlomiejpluta.base.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.internal.render.Renderable;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
public interface Entity extends Locationable, Renderable, Updatable, Reactive {
|
||||
boolean isBlocking();
|
||||
|
||||
public interface Entity extends Reactive, Movable, Animated, Renderable, Updatable {
|
||||
void setBlocking(boolean blocking);
|
||||
|
||||
Direction getFaceDirection();
|
||||
int getZIndex();
|
||||
|
||||
void setFaceDirection(Direction direction);
|
||||
|
||||
int chebyshevDistance(Entity other);
|
||||
|
||||
int manhattanDistance(Entity other);
|
||||
|
||||
Direction getDirectionTowards(Entity target);
|
||||
void setZIndex(int zIndex);
|
||||
|
||||
ObjectLayer getLayer();
|
||||
|
||||
void onAdd(ObjectLayer layer);
|
||||
|
||||
void onRemove(ObjectLayer layer);
|
||||
|
||||
boolean isBlocking();
|
||||
|
||||
void setBlocking(boolean blocking);
|
||||
|
||||
void changeEntitySet(String entitySetUid);
|
||||
|
||||
int getZIndex();
|
||||
|
||||
void setZIndex(int zIndex);
|
||||
|
||||
CompletableFuture<Void> performInstantAnimation();
|
||||
|
||||
<E extends Event> void addEventListener(EventType<E> type, Consumer<E> listener);
|
||||
|
||||
<E extends Event> void removeEventListener(EventType<E> type, Consumer<E> listener);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.base.api.location;
|
||||
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.internal.object.Placeable;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
@@ -19,7 +20,13 @@ public interface Locationable extends Placeable {
|
||||
|
||||
void setPositionOffset(float offsetX, float offsetY);
|
||||
|
||||
Direction getDirectionTowards(Locationable target);
|
||||
|
||||
int chebyshevDistance(Vector2ic coordinates);
|
||||
|
||||
int manhattanDistance(Vector2ic coordinates);
|
||||
|
||||
int chebyshevDistance(Locationable other);
|
||||
|
||||
int manhattanDistance(Locationable other);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.bartlomiejpluta.base.api.move;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.character.Character;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
@@ -9,13 +9,13 @@ import org.joml.Vector2ic;
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
public final class EntityMovement implements Movement {
|
||||
private final Entity object;
|
||||
public final class CharacterMovement implements Movement {
|
||||
private final Character object;
|
||||
private final Direction direction;
|
||||
private final Vector2ic from;
|
||||
private final Vector2ic to;
|
||||
|
||||
public EntityMovement(@NonNull Entity object, @NonNull Direction direction) {
|
||||
public CharacterMovement(@NonNull Character object, @NonNull Direction direction) {
|
||||
this.object = object;
|
||||
this.direction = direction;
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.bartlomiejpluta.base.lib.ai;
|
||||
|
||||
import com.bartlomiejpluta.base.api.ai.AI;
|
||||
import com.bartlomiejpluta.base.api.ai.NPC;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.MoveEvent;
|
||||
import com.bartlomiejpluta.base.util.path.MovementPath;
|
||||
@@ -11,7 +11,7 @@ import com.bartlomiejpluta.base.util.pathfinder.PathFinder;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
|
||||
public abstract class FollowEntityAI<N extends NPC, T extends Entity> implements AI {
|
||||
public abstract class FollowObjectAI<N extends NPC, T extends Locationable> implements AI {
|
||||
|
||||
private final PathFinder finder;
|
||||
private final PathExecutor<N> executor;
|
||||
@@ -22,7 +22,7 @@ public abstract class FollowEntityAI<N extends NPC, T extends Entity> implements
|
||||
|
||||
private MovementPath<N> path = null;
|
||||
|
||||
protected FollowEntityAI(@NonNull PathFinder finder, @NonNull N npc, @NonNull T target) {
|
||||
protected FollowObjectAI(@NonNull PathFinder finder, @NonNull N npc, @NonNull T target) {
|
||||
this.finder = finder;
|
||||
this.npc = npc;
|
||||
this.target = target;
|
||||
@@ -37,7 +37,7 @@ public abstract class FollowEntityAI<N extends NPC, T extends Entity> implements
|
||||
var movable = event.getMovable();
|
||||
|
||||
// Refresh only when target has been displaced
|
||||
// or another entity is blocking current path
|
||||
// or another object is blocking current path
|
||||
if (movable == target || (path != null && path.contains(movable))) {
|
||||
path = null;
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.bartlomiejpluta.base.lib.ai;
|
||||
|
||||
import com.bartlomiejpluta.base.api.ai.AI;
|
||||
import com.bartlomiejpluta.base.api.ai.NPC;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.MoveEvent;
|
||||
import com.bartlomiejpluta.base.util.path.MovementPath;
|
||||
@@ -15,7 +15,7 @@ import org.joml.Vector2ic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class KeepStraightDistanceAI<N extends NPC, T extends Entity> implements AI {
|
||||
public abstract class KeepStraightDistanceAI<N extends NPC, T extends Locationable> implements AI {
|
||||
private final N npc;
|
||||
@Setter(onParam = @__(@NonNull))
|
||||
private T target;
|
||||
@@ -44,7 +44,7 @@ public abstract class KeepStraightDistanceAI<N extends NPC, T extends Entity> im
|
||||
var movable = event.getMovable();
|
||||
|
||||
// Refresh only when target has been displaced
|
||||
// or another entity is blocking current path
|
||||
// or another object is blocking current path
|
||||
if (movable == target || (path != null && path.contains(movable))) {
|
||||
path = null;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.bartlomiejpluta.base.lib.ai;
|
||||
|
||||
import com.bartlomiejpluta.base.api.ai.AI;
|
||||
import com.bartlomiejpluta.base.api.ai.NPC;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -12,7 +12,7 @@ import lombok.Setter;
|
||||
import java.util.Random;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class RunawayAI<N extends NPC, T extends Entity> implements AI {
|
||||
public class RunawayAI<N extends NPC, T extends Locationable> implements AI {
|
||||
@NonNull
|
||||
private final N npc;
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bartlomiejpluta.base.lib.animation;
|
||||
|
||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.move.AnimationMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
@@ -196,6 +197,11 @@ public abstract class AnimationDelegate implements Animation {
|
||||
return animation.isMoving();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction getDirectionTowards(Locationable target) {
|
||||
return animation.getDirectionTowards(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Vector2ic coordinates) {
|
||||
return animation.chebyshevDistance(coordinates);
|
||||
@@ -206,6 +212,16 @@ public abstract class AnimationDelegate implements Animation {
|
||||
return animation.manhattanDistance(coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Locationable other) {
|
||||
return animation.chebyshevDistance(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int manhattanDistance(Locationable other) {
|
||||
return animation.manhattanDistance(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2fc getPositionOffset() {
|
||||
return animation.getPositionOffset();
|
||||
|
||||
@@ -179,7 +179,7 @@ public class BulletAnimationRunner implements AnimationRunner {
|
||||
var target = getCoordinates().add(direction.vector, new Vector2i());
|
||||
if (layer instanceof ObjectLayer) {
|
||||
for (var entity : ((ObjectLayer) layer).getEntities()) {
|
||||
var movement = entity.getMovement();
|
||||
var movement = (entity instanceof Movable) ? ((Movable) entity).getMovement() : null;
|
||||
if ((entity.getCoordinates().equals(target) || movement != null && movement.getTo().equals(target)) && entity.isBlocking()) {
|
||||
onHit.accept(movable, entity);
|
||||
return;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package com.bartlomiejpluta.base.lib.entity;
|
||||
package com.bartlomiejpluta.base.lib.character;
|
||||
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.character.Character;
|
||||
import com.bartlomiejpluta.base.api.event.Event;
|
||||
import com.bartlomiejpluta.base.api.event.EventType;
|
||||
import com.bartlomiejpluta.base.api.location.Locationable;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.CharacterMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import com.bartlomiejpluta.base.api.move.EntityMovement;
|
||||
import com.bartlomiejpluta.base.api.move.Movement;
|
||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||
import com.bartlomiejpluta.base.internal.object.Placeable;
|
||||
@@ -18,300 +19,300 @@ import org.joml.Vector2ic;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class EntityDelegate implements Entity {
|
||||
protected final Entity entity;
|
||||
public abstract class CharacterDelegate implements Character {
|
||||
protected final Character character;
|
||||
|
||||
protected EntityDelegate(Entity entity) {
|
||||
this.entity = entity;
|
||||
protected CharacterDelegate(Character character) {
|
||||
this.character = character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStepSize(float x, float y) {
|
||||
entity.setStepSize(x, y);
|
||||
character.setStepSize(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2ic getCoordinates() {
|
||||
return entity.getCoordinates();
|
||||
return character.getCoordinates();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCoordinates(Vector2ic coordinates) {
|
||||
entity.setCoordinates(coordinates);
|
||||
character.setCoordinates(coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCoordinates(int x, int y) {
|
||||
entity.setCoordinates(x, y);
|
||||
character.setCoordinates(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement prepareMovement(Direction direction) {
|
||||
return new EntityMovement(this, direction);
|
||||
return new CharacterMovement(this, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement getMovement() {
|
||||
return entity.getMovement();
|
||||
return character.getMovement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction getFaceDirection() {
|
||||
return entity.getFaceDirection();
|
||||
return character.getFaceDirection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFaceDirection(Direction direction) {
|
||||
entity.setFaceDirection(direction);
|
||||
character.setFaceDirection(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpeed(float speed) {
|
||||
entity.setSpeed(speed);
|
||||
character.setSpeed(speed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAnimationEnabled() {
|
||||
return entity.isAnimationEnabled();
|
||||
return character.isAnimationEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationEnabled(boolean enabled) {
|
||||
entity.setAnimationEnabled(enabled);
|
||||
character.setAnimationEnabled(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableAnimation() {
|
||||
entity.enableAnimation();
|
||||
character.enableAnimation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableAnimation() {
|
||||
entity.disableAnimation();
|
||||
character.disableAnimation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toggleAnimationEnabled() {
|
||||
entity.toggleAnimationEnabled();
|
||||
character.toggleAnimationEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationFrame(int frame) {
|
||||
entity.setAnimationFrame(frame);
|
||||
character.setAnimationFrame(frame);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getAnimationSpeed() {
|
||||
return entity.getAnimationSpeed();
|
||||
return character.getAnimationSpeed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAnimationSpeed(float speed) {
|
||||
entity.setAnimationSpeed(speed);
|
||||
character.setAnimationSpeed(speed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isMoving() {
|
||||
return entity.isMoving();
|
||||
return character.isMoving();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2fc getPositionOffset() {
|
||||
return entity.getPositionOffset();
|
||||
return character.getPositionOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(Vector2fc offset) {
|
||||
entity.setPositionOffset(offset);
|
||||
character.setPositionOffset(offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPositionOffset(float offsetX, float offsetY) {
|
||||
entity.setPositionOffset(offsetX, offsetY);
|
||||
character.setPositionOffset(offsetX, offsetY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Entity other) {
|
||||
return entity.chebyshevDistance(other);
|
||||
public int chebyshevDistance(Locationable other) {
|
||||
return character.chebyshevDistance(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int manhattanDistance(Entity other) {
|
||||
return entity.manhattanDistance(other);
|
||||
public int manhattanDistance(Locationable other) {
|
||||
return character.manhattanDistance(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction getDirectionTowards(Entity target) {
|
||||
return entity.getDirectionTowards(target);
|
||||
public Direction getDirectionTowards(Locationable target) {
|
||||
return character.getDirectionTowards(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Vector2fc getPosition() {
|
||||
return entity.getPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(float x, float y) {
|
||||
entity.setPosition(x, y);
|
||||
return character.getPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(Vector2fc position) {
|
||||
entity.setPosition(position);
|
||||
character.setPosition(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPosition(float x, float y) {
|
||||
character.setPosition(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePosition(float x, float y) {
|
||||
entity.movePosition(x, y);
|
||||
character.movePosition(x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void movePosition(Vector2fc position) {
|
||||
entity.movePosition(position);
|
||||
character.movePosition(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getRotation() {
|
||||
return entity.getRotation();
|
||||
return character.getRotation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRotation(float rotation) {
|
||||
entity.setRotation(rotation);
|
||||
character.setRotation(rotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveRotation(float rotation) {
|
||||
entity.moveRotation(rotation);
|
||||
character.moveRotation(rotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScaleX() {
|
||||
return entity.getScaleX();
|
||||
return character.getScaleX();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScaleX(float scale) {
|
||||
entity.setScaleX(scale);
|
||||
character.setScaleX(scale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScaleY() {
|
||||
return entity.getScaleY();
|
||||
return character.getScaleY();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScaleY(float scale) {
|
||||
entity.setScaleY(scale);
|
||||
character.setScaleY(scale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScale(float scale) {
|
||||
entity.setScale(scale);
|
||||
character.setScale(scale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScale(float scaleX, float scaleY) {
|
||||
entity.setScale(scaleX, scaleY);
|
||||
character.setScale(scaleX, scaleY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float euclideanDistance(Placeable other) {
|
||||
return entity.euclideanDistance(other);
|
||||
return character.euclideanDistance(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chebyshevDistance(Vector2ic coordinates) {
|
||||
return entity.chebyshevDistance(coordinates);
|
||||
return character.chebyshevDistance(coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int manhattanDistance(Vector2ic coordinates) {
|
||||
return entity.manhattanDistance(coordinates);
|
||||
return character.manhattanDistance(coordinates);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matrix4fc getModelMatrix() {
|
||||
return entity.getModelMatrix();
|
||||
return character.getModelMatrix();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectLayer getLayer() {
|
||||
return entity.getLayer();
|
||||
return character.getLayer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdd(ObjectLayer layer) {
|
||||
entity.onAdd(layer);
|
||||
character.onAdd(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemove(ObjectLayer layer) {
|
||||
entity.onRemove(layer);
|
||||
character.onRemove(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlocking() {
|
||||
return entity.isBlocking();
|
||||
return character.isBlocking();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlocking(boolean blocking) {
|
||||
entity.setBlocking(blocking);
|
||||
character.setBlocking(blocking);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changeEntitySet(String entitySetUid) {
|
||||
entity.changeEntitySet(entitySetUid);
|
||||
public void changeCharacterSet(String characterSetUid) {
|
||||
character.changeCharacterSet(characterSetUid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZIndex() {
|
||||
return entity.getZIndex();
|
||||
return character.getZIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setZIndex(int zIndex) {
|
||||
entity.setZIndex(zIndex);
|
||||
character.setZIndex(zIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends Event> void handleEvent(E event) {
|
||||
entity.handleEvent(event);
|
||||
character.handleEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends Event> void addEventListener(EventType<E> type, Consumer<E> listener) {
|
||||
entity.addEventListener(type, listener);
|
||||
character.addEventListener(type, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends Event> void removeEventListener(EventType<E> type, Consumer<E> listener) {
|
||||
entity.removeEventListener(type, listener);
|
||||
character.removeEventListener(type, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> performInstantAnimation() {
|
||||
return entity.performInstantAnimation();
|
||||
return character.performInstantAnimation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean move(Movement movement) {
|
||||
return entity.move(movement);
|
||||
return character.move(movement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abortMove() {
|
||||
entity.abortMove();
|
||||
character.abortMove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
entity.update(dt);
|
||||
character.update(dt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
entity.render(screen, camera, shaderManager);
|
||||
character.render(screen, camera, shaderManager);
|
||||
}
|
||||
}
|
||||
@@ -1,53 +1,53 @@
|
||||
package com.bartlomiejpluta.base.util.path;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.character.Character;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EntityPath<T extends Entity> implements Path<T> {
|
||||
public class CharacterPath<T extends Character> implements Path<T> {
|
||||
|
||||
@Getter
|
||||
private final List<PathSegment<T>> path = new ArrayList<>();
|
||||
|
||||
public EntityPath<T> add(PathSegment<T> segment) {
|
||||
public CharacterPath<T> add(PathSegment<T> segment) {
|
||||
path.add(segment);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> addFirst(PathSegment<T> segment) {
|
||||
public CharacterPath<T> addFirst(PathSegment<T> segment) {
|
||||
path.add(0, segment);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> move(Direction direction) {
|
||||
public CharacterPath<T> move(Direction direction) {
|
||||
path.add(new MoveSegment<>(direction));
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> move(Direction direction, boolean ignore) {
|
||||
public CharacterPath<T> move(Direction direction, boolean ignore) {
|
||||
path.add(new MoveSegment<>(direction, ignore));
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> turn(Direction direction) {
|
||||
public CharacterPath<T> turn(Direction direction) {
|
||||
path.add(new TurnSegment<>(direction));
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> turn(Direction direction, int newAnimationFrame) {
|
||||
public CharacterPath<T> turn(Direction direction, int newAnimationFrame) {
|
||||
path.add(new TurnSegment<>(direction, newAnimationFrame));
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> wait(float seconds) {
|
||||
public CharacterPath<T> wait(float seconds) {
|
||||
path.add(new WaitSegment<>(seconds));
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityPath<T> run(Runnable runnable) {
|
||||
public CharacterPath<T> run(Runnable runnable) {
|
||||
path.add(new RunSegment<>(runnable));
|
||||
return this;
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.bartlomiejpluta.base.util.path;
|
||||
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.character.Character;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import lombok.NonNull;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public class TurnSegment<T extends Entity> implements PathSegment<T> {
|
||||
public class TurnSegment<T extends Character> implements PathSegment<T> {
|
||||
private final Direction direction;
|
||||
private final Integer newAnimationFrame;
|
||||
|
||||
@@ -22,10 +20,10 @@ public class TurnSegment<T extends Entity> implements PathSegment<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PathProgress perform(T entity, ObjectLayer layer, float dt) {
|
||||
entity.setFaceDirection(direction);
|
||||
public PathProgress perform(T character, ObjectLayer layer, float dt) {
|
||||
character.setFaceDirection(direction);
|
||||
if (newAnimationFrame != null) {
|
||||
entity.setAnimationFrame(newAnimationFrame);
|
||||
character.setAnimationFrame(newAnimationFrame);
|
||||
}
|
||||
return PathProgress.SEGMENT_DONE;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.bartlomiejpluta.base.util.world;
|
||||
|
||||
import com.bartlomiejpluta.base.api.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.character.Character;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.event.Event;
|
||||
import com.bartlomiejpluta.base.api.event.EventType;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
@@ -22,11 +22,11 @@ import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
||||
public class EntitySpawner implements Updatable {
|
||||
public class CharacterSpawner implements Updatable {
|
||||
private static final int MAX_REPOSITION_ATTEMPTS = 10;
|
||||
private final Random random = new Random();
|
||||
private final List<Entity> spawnedEntities = new LinkedList<>();
|
||||
private final List<Supplier<Entity>> spawners = new ArrayList<>();
|
||||
private final List<Character> spawnedEntities = new LinkedList<>();
|
||||
private final List<Supplier<Character>> spawners = new ArrayList<>();
|
||||
private final Vector2ic origin;
|
||||
private final Context context;
|
||||
private final Camera camera;
|
||||
@@ -36,12 +36,12 @@ public class EntitySpawner implements Updatable {
|
||||
private int range = 4;
|
||||
private float spawnChance = 50;
|
||||
private DiceRoller countRoller = DiceRoller.of("1d4");
|
||||
private EventType<? extends Event> entityRemoveEvent;
|
||||
private EventType<? extends Event> characterRemoveEvent;
|
||||
private float accumulator = 10000f;
|
||||
private boolean spawnOutsideViewport = false;
|
||||
private float threshold;
|
||||
|
||||
public EntitySpawner(int x, int y, @NonNull Context context, @NonNull GameMap map, @NonNull ObjectLayer layer) {
|
||||
public CharacterSpawner(int x, int y, @NonNull Context context, @NonNull GameMap map, @NonNull ObjectLayer layer) {
|
||||
this.origin = new Vector2i(x, y);
|
||||
this.context = context;
|
||||
this.camera = context.getCamera();
|
||||
@@ -50,43 +50,43 @@ public class EntitySpawner implements Updatable {
|
||||
drawThreshold();
|
||||
}
|
||||
|
||||
public EntitySpawner interval(@NonNull String interval) {
|
||||
public CharacterSpawner interval(@NonNull String interval) {
|
||||
this.interval = DiceRoller.of(interval);
|
||||
drawThreshold();
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntitySpawner range(int range) {
|
||||
public CharacterSpawner range(int range) {
|
||||
this.range = range;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntitySpawner spawnChance(float change) {
|
||||
public CharacterSpawner spawnChance(float change) {
|
||||
this.spawnChance = change;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntitySpawner count(String dices) {
|
||||
public CharacterSpawner count(String dices) {
|
||||
this.countRoller = DiceRoller.of(dices);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntitySpawner trackEntities(@NonNull EventType<? extends Event> entityRemoveEvent) {
|
||||
this.entityRemoveEvent = entityRemoveEvent;
|
||||
public CharacterSpawner trackEntities(@NonNull EventType<? extends Event> characterRemoveEvent) {
|
||||
this.characterRemoveEvent = characterRemoveEvent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntitySpawner spawnOutsideViewport() {
|
||||
public CharacterSpawner spawnOutsideViewport() {
|
||||
this.spawnOutsideViewport = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntitySpawner spawnOnCreate() {
|
||||
public CharacterSpawner spawnOnCreate() {
|
||||
this.threshold = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntitySpawner spawn(@NonNull Supplier<Entity> spawner) {
|
||||
public CharacterSpawner spawn(@NonNull Supplier<Character> spawner) {
|
||||
this.spawners.add(spawner);
|
||||
return this;
|
||||
}
|
||||
@@ -151,19 +151,19 @@ public class EntitySpawner implements Updatable {
|
||||
break;
|
||||
}
|
||||
|
||||
// Draw the entity spawner
|
||||
// Draw the character spawner
|
||||
var spawner = spawners.get(random.nextInt(spawners.size()));
|
||||
|
||||
// Create the entity and push it onto the map layer
|
||||
var entity = spawner.get();
|
||||
entity.setCoordinates(coordinates);
|
||||
layer.addEntity(entity);
|
||||
// Create the character and push it onto the map layer
|
||||
var character = spawner.get();
|
||||
character.setCoordinates(coordinates);
|
||||
layer.addEntity(character);
|
||||
|
||||
// If we want to keep the number of spawned entities per spawner almost constant
|
||||
// we need to know when the entity should be removed (i.e. it has been killed by player).
|
||||
if (entityRemoveEvent != null) {
|
||||
spawnedEntities.add(entity);
|
||||
entity.addEventListener(entityRemoveEvent, e -> spawnedEntities.remove(entity));
|
||||
// we need to know when the character should be removed (i.e. it has been killed by player).
|
||||
if (characterRemoveEvent != null) {
|
||||
spawnedEntities.add(character);
|
||||
character.addEventListener(characterRemoveEvent, e -> spawnedEntities.remove(character));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user