From b74ae302ec0186cc334b477920a0f72e5225bf67 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 27 Sep 2018 17:40:08 -0400 Subject: [PATCH] Ground unit retreat attacking / Patrol attacking --- .../mindustry/entities/units/FlyingUnit.java | 24 +++++++--- .../mindustry/entities/units/GroundUnit.java | 44 ++++++++----------- .../world/blocks/power/PowerNode.java | 2 +- 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/core/src/io/anuke/mindustry/entities/units/FlyingUnit.java b/core/src/io/anuke/mindustry/entities/units/FlyingUnit.java index 4d5b8b2fc7..475378c64d 100644 --- a/core/src/io/anuke/mindustry/entities/units/FlyingUnit.java +++ b/core/src/io/anuke/mindustry/entities/units/FlyingUnit.java @@ -58,6 +58,12 @@ public abstract class FlyingUnit extends BaseUnit implements CarryTrait{ if(target == null){ retarget(() -> { targetClosest(); + + if(target == null && isCommanded() && getCommand() == UnitCommand.patrol){ + setState(patrol); + return; + } + if(target == null) targetClosestEnemyFlag(BlockFlag.target); if(target == null) targetClosestEnemyFlag(BlockFlag.producer); if(target == null) targetClosestEnemyFlag(BlockFlag.turret); @@ -82,9 +88,18 @@ public abstract class FlyingUnit extends BaseUnit implements CarryTrait{ }, patrol = new UnitState(){ public void update(){ - target = getClosestCore(); + retarget(() -> { + targetClosest(); + + if(target != null){ + setState(attack); + } + + target = getClosestCore(); + }); + if(target != null){ - circle(60f + Mathf.absin(Timers.time() + id*23525, 70f, 1200f)); + circle(60f + Mathf.absin(Timers.time() + id * 23525, 70f, 1200f)); } } }, @@ -107,11 +122,6 @@ public abstract class FlyingUnit extends BaseUnit implements CarryTrait{ } }; - //instantiation only - public FlyingUnit(){ - - } - @Override public void onCommand(UnitCommand command){ state.set(command == UnitCommand.retreat ? retreat : diff --git a/core/src/io/anuke/mindustry/entities/units/GroundUnit.java b/core/src/io/anuke/mindustry/entities/units/GroundUnit.java index 662ad48ccf..bce9bbc398 100644 --- a/core/src/io/anuke/mindustry/entities/units/GroundUnit.java +++ b/core/src/io/anuke/mindustry/entities/units/GroundUnit.java @@ -46,40 +46,16 @@ public abstract class GroundUnit extends BaseUnit{ if(core != null && dst < getWeapon().getAmmo().getRange() / 1.1f){ target = core; - }else{ - retarget(() -> targetClosest()); } - if(target != null){ - if(core != null){ - if(dst > getWeapon().getAmmo().getRange() * 0.5f){ - moveToCore(); - } - - }else{ - moveToCore(); - } - - if(distanceTo(target) < getWeapon().getAmmo().getRange()){ - rotate(angleTo(target)); - - if(Mathf.angNear(angleTo(target), rotation, 13f)){ - AmmoType ammo = getWeapon().getAmmo(); - - Vector2 to = Predict.intercept(GroundUnit.this, target, ammo.bullet.speed); - - getWeapon().update(GroundUnit.this, to.x, to.y); - } - } - - }else{ + if(dst > getWeapon().getAmmo().getRange() * 0.5f){ moveToCore(); } } }, patrol = new UnitState(){ public void update(){ - target = getClosestCore(); + TileEntity target = getClosestCore(); if(target != null){ if(distanceTo(target) > 400f){ moveAwayFromCore(); @@ -214,6 +190,20 @@ public abstract class GroundUnit extends BaseUnit{ if(health <= health * type.retreatPercent && !isCommanded()){ setState(retreat); } + + if(!Units.invalidateTarget(target, this)){ + if(distanceTo(target) < getWeapon().getAmmo().getRange()){ + rotate(angleTo(target)); + + if(Mathf.angNear(angleTo(target), rotation, 13f)){ + AmmoType ammo = getWeapon().getAmmo(); + + Vector2 to = Predict.intercept(GroundUnit.this, target, ammo.bullet.speed); + + getWeapon().update(GroundUnit.this, to.x, to.y); + } + } + } } @Override @@ -223,6 +213,8 @@ public abstract class GroundUnit extends BaseUnit{ if(Units.invalidateTarget(target, team, x, y, Float.MAX_VALUE)){ target = null; } + + retarget(this::targetClosest); } @Override diff --git a/core/src/io/anuke/mindustry/world/blocks/power/PowerNode.java b/core/src/io/anuke/mindustry/world/blocks/power/PowerNode.java index 83716aacea..a69049b986 100644 --- a/core/src/io/anuke/mindustry/world/blocks/power/PowerNode.java +++ b/core/src/io/anuke/mindustry/world/blocks/power/PowerNode.java @@ -50,7 +50,7 @@ public class PowerNode extends PowerBlock{ @Remote(targets = Loc.both, called = Loc.server, forward = true) public static void linkPowerNodes(Player player, Tile tile, Tile other){ - if(tile.entity.power == null) return; + if(tile.entity.power == null || !((PowerNode)tile.block()).linkValid(tile, other)) return; TileEntity entity = tile.entity();