diff --git a/OpenRA.Game/Orders/GenericSelectTarget.cs b/OpenRA.Game/Orders/GenericSelectTarget.cs index cecdcaf52c..1bff25ab91 100644 --- a/OpenRA.Game/Orders/GenericSelectTarget.cs +++ b/OpenRA.Game/Orders/GenericSelectTarget.cs @@ -19,6 +19,7 @@ #endregion using System.Collections.Generic; +using System.Linq; namespace OpenRA.Orders { @@ -48,9 +49,28 @@ namespace OpenRA.Orders yield return new Order(order, subject, xy); } - public void Tick(World world) { } + public virtual void Tick(World world) { } public void Render(World world) { } public string GetCursor(World world, int2 xy, MouseInput mi) { return cursor; } } + + // variant that requires a tag trait (T) to be present on some actor owned + // by the activating player + public class GenericSelectTargetWithBuilding : GenericSelectTarget + { + public GenericSelectTargetWithBuilding(Actor subject, string order, string cursor) + : base(subject, order, cursor) { } + + public override void Tick(World world) + { + var hasStructure = world.Queries.OwnedBy[world.LocalPlayer] + .WithTrait() + .Any(); + + if (!hasStructure) + Game.controller.CancelInputMode(); + } + } + } diff --git a/OpenRA.Mods.Cnc/IonCannonPower.cs b/OpenRA.Mods.Cnc/IonCannonPower.cs index 3d679b9eb0..8d3466854f 100644 --- a/OpenRA.Mods.Cnc/IonCannonPower.cs +++ b/OpenRA.Mods.Cnc/IonCannonPower.cs @@ -22,6 +22,7 @@ using System.Collections.Generic; using System.Linq; using OpenRA.Mods.Cnc.Effects; using OpenRA.Traits; +using OpenRA.Orders; namespace OpenRA.Mods.Cnc { @@ -34,36 +35,6 @@ namespace OpenRA.Mods.Cnc { public IonCannonPower(Actor self, IonCannonPowerInfo info) : base(self, info) { } - class SelectTarget : IOrderGenerator - { - public IEnumerable Order(World world, int2 xy, MouseInput mi) - { - if (mi.Button == MouseButton.Right) - Game.controller.CancelInputMode(); - return OrderInner(world, xy, mi); - } - - IEnumerable OrderInner(World world, int2 xy, MouseInput mi) - { - if (mi.Button == MouseButton.Left) - yield return new Order("IonCannon", world.LocalPlayer.PlayerActor, xy); - } - - public void Tick(World world) - { - var hasStructure = world.Queries.OwnedBy[world.LocalPlayer] - .WithTrait() - .Any(); - - if (!hasStructure) - Game.controller.CancelInputMode(); - } - - public void Render(World world) { } - - public string GetCursor(World world, int2 xy, MouseInput mi) { return "ability"; } - } - public void ResolveOrder(Actor self, Order order) { if (order.OrderString == "IonCannon") @@ -84,7 +55,8 @@ namespace OpenRA.Mods.Cnc protected override void OnActivate() { - Game.controller.orderGenerator = new SelectTarget(); + Game.controller.orderGenerator = + new GenericSelectTargetWithBuilding(Owner.PlayerActor, "IonCannon", "ability"); Sound.PlayToPlayer(Owner, Info.SelectTargetSound); } }