Basic AI defending

This commit is contained in:
Anuken
2022-02-18 11:27:20 -05:00
parent 520b60770c
commit 027d037233
7 changed files with 210 additions and 57 deletions

View File

@@ -415,11 +415,11 @@ public class Damage{
/** Damages all entities and blocks in a radius that are enemies of the team. */
public static void damage(Team team, float x, float y, float radius, float damage, boolean complete, boolean air, boolean ground){
damage(team, x, y, radius, damage, complete, air, ground, false);
damage(team, x, y, radius, damage, complete, air, ground, false, null);
}
/** Damages all entities and blocks in a radius that are enemies of the team. */
public static void damage(Team team, float x, float y, float radius, float damage, boolean complete, boolean air, boolean ground, boolean scaled){
public static void damage(Team team, float x, float y, float radius, float damage, boolean complete, boolean air, boolean ground, boolean scaled, Bullet source){
Cons<Unit> cons = entity -> {
if(entity.team == team || !entity.within(x, y, radius + (scaled ? entity.hitSize / 2f : 0f)) || (entity.isFlying() && !air) || (entity.isGrounded() && !ground)){
return;
@@ -445,7 +445,7 @@ public class Damage{
if(ground){
if(!complete){
tileDamage(team, World.toTile(x), World.toTile(y), radius / tilesize, damage);
tileDamage(team, World.toTile(x), World.toTile(y), radius / tilesize, damage, source);
}else{
completeDamage(team, x, y, radius, damage);
}
@@ -453,6 +453,10 @@ public class Damage{
}
public static void tileDamage(Team team, int x, int y, float baseRadius, float damage){
tileDamage(team, x, y, baseRadius, damage, null);
}
public static void tileDamage(Team team, int x, int y, float baseRadius, float damage, @Nullable Bullet source){
Core.app.post(() -> {
@@ -519,7 +523,11 @@ public class Damage{
int cx = Point2.x(e.key), cy = Point2.y(e.key);
var build = world.build(cx, cy);
if(build != null){
build.damage(team, e.value);
if(source != null){
build.damage(source, team, e.value);
}else{
build.damage(team, e.value);
}
}
}
});

View File

@@ -319,7 +319,7 @@ public class BulletType extends Content implements Cloneable{
}
if(splashDamageRadius > 0 && !b.absorbed){
Damage.damage(b.team, x, y, splashDamageRadius, splashDamage * b.damageMultiplier(), false, collidesAir, collidesGround, scaledSplashDamage);
Damage.damage(b.team, x, y, splashDamageRadius, splashDamage * b.damageMultiplier(), false, collidesAir, collidesGround, scaledSplashDamage, b);
if(status != StatusEffects.none){
Damage.status(b.team, x, y, splashDamageRadius, status, statusDuration, collidesAir, collidesGround);

View File

@@ -54,6 +54,7 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
static final ObjectSet<Building> tmpTiles = new ObjectSet<>();
static final Seq<Building> tempBuilds = new Seq<>();
static final BuildTeamChangeEvent teamChangeEvent = new BuildTeamChangeEvent();
static final BuildDamageEvent bulletDamageEvent = new BuildDamageEvent();
static int sleepingEntities = 0;
@Import float x, y, health, maxHealth;
@@ -262,6 +263,19 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
data.blocks.addFirst(new BlockPlan(tile.x, tile.y, (short)rotation, block.id, overrideConfig == null ? config() : overrideConfig));
}
public @Nullable Tile findClosestEdge(Position to, Boolf<Tile> solid){
Tile best = null;
float mindst = 0f;
for(var point : Edges.getEdges(block.size)){
Tile other = Vars.world.tile(tile.x + point.x, tile.y + point.y);
if(other != null && !solid.get(other) && (best == null || to.dst2(other) < mindst)){
best = other;
mindst = other.dst2(other);
}
}
return best;
}
/** Configure with the current, local player. */
public void configure(Object value){
//save last used config
@@ -1498,6 +1512,7 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
* @return whether the bullet should be removed. */
public boolean collision(Bullet other){
damage(other.team, other.damage() * other.type().buildingDamageMultiplier);
Events.fire(bulletDamageEvent.set(self(), other));
return true;
}
@@ -1507,6 +1522,12 @@ abstract class BuildingComp implements Posc, Teamc, Healthc, Buildingc, Timerc,
damage(damage);
}
/** Handles splash damage with a bullet source. */
public void damage(Bullet bullet, Team source, float damage){
damage(source, damage);
Events.fire(bulletDamageEvent.set(self(), bullet));
}
/** Changes this building's team in a safe manner. */
public void changeTeam(Team next){
Team last = this.team;