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);
|
||||
|
||||
Reference in New Issue
Block a user