Create AI scaffolding
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package com.bartlomiejpluta.base.api.game.ai;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AI {
|
||||
void nextActivity(ObjectLayer layer, float dt);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.bartlomiejpluta.base.api.game.ai;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||
|
||||
public interface NPC extends Entity {
|
||||
AI getStrategy();
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.bartlomiejpluta.base.api.game.ai;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.entity.Direction;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomMovementAI implements AI {
|
||||
private final Random random = new Random();
|
||||
private final NPC npc;
|
||||
|
||||
private final float intervalSeconds;
|
||||
private float accumulator = 0.0f;
|
||||
private float threshold = 0.0f;
|
||||
|
||||
public RandomMovementAI(NPC npc, float intervalSeconds) {
|
||||
this.npc = npc;
|
||||
this.intervalSeconds = intervalSeconds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nextActivity(ObjectLayer layer, float dt) {
|
||||
if (!npc.isMoving()) {
|
||||
if (accumulator > threshold) {
|
||||
Direction direction = Direction.values()[random.nextInt(4)];
|
||||
layer.pushMovement(npc.prepareMovement(direction));
|
||||
accumulator = 0.0f;
|
||||
threshold = random.nextFloat() * intervalSeconds;
|
||||
}
|
||||
|
||||
accumulator += dt;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,4 +36,8 @@ public interface Entity extends Placeable, Renderable, Updatable {
|
||||
void onAdd(ObjectLayer layer);
|
||||
|
||||
void onRemove(ObjectLayer layer);
|
||||
|
||||
boolean isBlocking();
|
||||
|
||||
void setBlocking(boolean blocking);
|
||||
}
|
||||
|
||||
@@ -171,6 +171,16 @@ public abstract class EntityDelegate implements Entity {
|
||||
entity.onRemove(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlocking() {
|
||||
return entity.isBlocking();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlocking(boolean blocking) {
|
||||
entity.setBlocking(blocking);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
entity.update(dt);
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.bartlomiejpluta.base.engine.world.entity.config.EntitySpriteConfigura
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
@@ -35,6 +36,10 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
return isMoving();
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean blocking;
|
||||
|
||||
@Override
|
||||
public Vector2f[] getSpriteAnimationFramesPositions() {
|
||||
var row = spriteDirectionRows.get(faceDirection);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.base.engine.world.map.layer.object;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.ai.NPC;
|
||||
import com.bartlomiejpluta.base.api.game.camera.Camera;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Direction;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||
@@ -108,6 +109,12 @@ public class DefaultObjectLayer implements ObjectLayer {
|
||||
default -> true;
|
||||
};
|
||||
|
||||
for (var entity : entities) {
|
||||
if (entity.isBlocking() && entity.getCoordinates().equals(target)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return isTargetReachable && canMoveFromCurrentTile;
|
||||
}
|
||||
|
||||
@@ -131,6 +138,10 @@ public class DefaultObjectLayer implements ObjectLayer {
|
||||
}
|
||||
|
||||
entity.update(dt);
|
||||
|
||||
if (entity instanceof NPC) {
|
||||
((NPC) entity).getStrategy().nextActivity(this, dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user