Skip to content

Commit

Permalink
feat: guntower AI priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Sep 24, 2023
1 parent 7064a0b commit 40d433f
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions core/src/me/vinceh121/wanderer/guntower/GuntowerAiController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.vinceh121.wanderer.guntower;

import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
Expand All @@ -12,7 +14,7 @@
import me.vinceh121.wanderer.util.MathUtilsW;

public class GuntowerAiController extends AIController<AbstractGuntower> {
private final Set<String> targets = new HashSet<>();
private final List<String> priorities = new ArrayList<>();
private float range = 500, turnSpeed = 3;

public GuntowerAiController(Wanderer game, AbstractGuntower target) {
Expand All @@ -21,22 +23,38 @@ public GuntowerAiController(Wanderer game, AbstractGuntower target) {

@Override
public void tick(float delta) {
AbstractEntity closest = null;
final Queue<AbstractEntity> around = new PriorityQueue<>((e1, e2) -> {
final int typeCmp = Integer.compare(this.priorities.indexOf(e1.getClass().getCanonicalName()),
this.priorities.indexOf(e2.getClass().getCanonicalName()));

if (typeCmp != 0) {
return typeCmp;
} else {
return Float.compare(e1.getTranslation().dst2(this.target.getTranslation()),
e2.getTranslation().dst2(this.target.getTranslation()));
}
});

float minDist = Float.MAX_VALUE;

for (final AbstractEntity e : this.game.getEntities()) {
final float dist = this.target.getTranslation().dst(e.getTranslation());

if (dist < minDist && e != this.target && targets.contains(e.getClass().getCanonicalName())) {
closest = e;
if (dist < minDist) {
minDist = dist;
}

if (e != this.target && priorities.contains(e.getClass().getCanonicalName())) {
around.add(e);
}
}

if (minDist > range || closest == null) {
if (minDist > range || around.size() == 0) {
return;
}

final AbstractEntity closest = around.element();

final Vector3 closestDir = closest.getMidPoint()
.cpy()
.sub(this.target.getTranslation().add(0, this.target.getAverageTurretPosition().y, 0))
Expand Down

0 comments on commit 40d433f

Please sign in to comment.