diff --git a/core/src/mindustry/content/UnitTypes.java b/core/src/mindustry/content/UnitTypes.java index 0fa43990fe..98f00ea233 100644 --- a/core/src/mindustry/content/UnitTypes.java +++ b/core/src/mindustry/content/UnitTypes.java @@ -76,7 +76,7 @@ public class UnitTypes{ //special block unit type public static @EntityDef({Unitc.class, BlockUnitc.class}) UnitType block; - //special tethered (has payload capability, because it's necessary sometimes) + //special building tethered (has payload capability, because it's necessary sometimes) public static @EntityDef({Unitc.class, BuildingTetherc.class, Payloadc.class}) UnitType manifold, assemblyDrone; //tank diff --git a/core/src/mindustry/entities/comp/UnitTetherComp.java b/core/src/mindustry/entities/comp/UnitTetherComp.java new file mode 100644 index 0000000000..7c1a703997 --- /dev/null +++ b/core/src/mindustry/entities/comp/UnitTetherComp.java @@ -0,0 +1,39 @@ +package mindustry.entities.comp; + +import arc.util.*; +import mindustry.annotations.Annotations.*; +import mindustry.game.*; +import mindustry.gen.*; +import mindustry.type.*; + +/** A unit that depends on a units's existence; if that unit is removed, it despawns. */ +@Component +abstract class UnitTetherComp implements Unitc{ + @Import UnitType type; + @Import Team team; + + //spawner unit cannot be read directly for technical reasons. + public transient @Nullable Unit spawner; + public int spawnerUnitId = -1; + + @Override + public void afterRead(){ + if(spawnerUnitId != -1) spawner = Groups.unit.getByID(spawnerUnitId); + spawnerUnitId = -1; + } + + @Override + public void afterSync(){ + if(spawnerUnitId != -1) spawner = Groups.unit.getByID(spawnerUnitId); + spawnerUnitId = -1; + } + + @Override + public void update(){ + if(spawner == null || !spawner.isValid() || spawner.team != team){ + Call.unitDespawn(self()); + }else{ + spawnerUnitId = spawner.id; + } + } +}