Create common AI strategy which supports all weapon types
This commit is contained in:
Binary file not shown.
55
src/main/java/com/bartlomiejpluta/demo/ai/WeaponBasedAI.java
Normal file
55
src/main/java/com/bartlomiejpluta/demo/ai/WeaponBasedAI.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package com.bartlomiejpluta.demo.ai;
|
||||
|
||||
import lombok.*;
|
||||
import com.bartlomiejpluta.base.api.ai.AI;
|
||||
import com.bartlomiejpluta.base.api.move.MoveEvent;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.lib.ai.*;
|
||||
|
||||
import com.bartlomiejpluta.demo.entity.Character;
|
||||
import com.bartlomiejpluta.demo.entity.Enemy;
|
||||
import com.bartlomiejpluta.demo.world.weapon.*;
|
||||
|
||||
public class WeaponBasedAI implements AI {
|
||||
private static final int RANGE = 10;
|
||||
private static final int MIN_RANGE = 4;
|
||||
private static final int MAX_RANGE = 8;
|
||||
private final Enemy enemy;
|
||||
private final Character target;
|
||||
private final RunawayAI runawayAI;
|
||||
private final SimpleEnemyAI meleeAI;
|
||||
private final ArcherAI archerAI;
|
||||
|
||||
public WeaponBasedAI(@NonNull Enemy enemy, @NonNull Character target) {
|
||||
this.enemy = enemy;
|
||||
this.target = target;
|
||||
this.runawayAI = new RunawayAI<>(enemy, target);
|
||||
this.meleeAI = new SimpleEnemyAI(enemy, target, RANGE);
|
||||
this.archerAI = new ArcherAI(enemy, target, MIN_RANGE, MAX_RANGE, RANGE);
|
||||
}
|
||||
|
||||
public void recomputePath() {
|
||||
meleeAI.recomputePath();
|
||||
archerAI.recomputePath();
|
||||
}
|
||||
|
||||
public void recomputePath(@NonNull MoveEvent event) {
|
||||
meleeAI.recomputePath(event);
|
||||
archerAI.recomputePath(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nextActivity(ObjectLayer layer, float dt) {
|
||||
if(enemy.getWeapon() == null) {
|
||||
runawayAI.nextActivity(layer, dt);
|
||||
}
|
||||
|
||||
if(enemy.getWeapon() instanceof MeleeWeapon) {
|
||||
meleeAI.nextActivity(layer, dt);
|
||||
}
|
||||
|
||||
if(enemy.getWeapon() instanceof RangedWeapon) {
|
||||
archerAI.nextActivity(layer, dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ public abstract class Character extends EntityDelegate {
|
||||
@Getter
|
||||
protected int hp;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private Weapon weapon;
|
||||
|
||||
|
||||
@@ -103,4 +103,15 @@ public class Enemy extends Character implements NPC {
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Enemy defaultAI() {
|
||||
var ai = new WeaponBasedAI(this, runner.getPlayer());
|
||||
|
||||
addEventListener(MoveEvent.TYPE, ai::recomputePath);
|
||||
addEventListener(EnemyDiedEvent.TYPE, e -> ai.recomputePath());
|
||||
|
||||
this.ai = ai;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user