Skip to content

Commit

Permalink
Add Type tags to *Supplies traits
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperrr committed Mar 20, 2017
1 parent 311e6b5 commit 8eeb6e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 12 additions & 2 deletions OpenRA.Mods.Common/Traits/AcceptsSupplies.cs
Expand Up @@ -9,12 +9,22 @@
*/
#endregion

using System.Collections.Generic;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits
{
[Desc("Tag trait for actors with `DeliversSupplies`.")]
class AcceptsSuppliesInfo : TraitInfo<AcceptsSupplies> { }
public class AcceptsSuppliesInfo : ITraitInfo
{
[Desc("Accepted `DeliversSupplies` types. Leave empty to accept all types.")]
public readonly HashSet<string> ValidTypes = new HashSet<string>();

class AcceptsSupplies { }
public object Create(ActorInitializer init) { return new AcceptsSupplies(init.Self, this); }
}

public class AcceptsSupplies
{
public AcceptsSupplies(Actor self, AcceptsSuppliesInfo info) { }
}
}
17 changes: 11 additions & 6 deletions OpenRA.Mods.Common/Traits/DeliversSupplies.cs
Expand Up @@ -26,6 +26,9 @@ class DeliversSuppliesInfo : ITraitInfo
[Desc("The amount of experience the donating player receives.")]
public readonly int PlayerExperience = 0;

[Desc("Identifier checked against AcceptsSupplies.ValidTypes. Only needed if the latter is not empty.")]
public readonly string Type = null;

[VoiceReference] public readonly string Voice = "Action";

public object Create(ActorInitializer init) { return new DeliversSupplies(this); }
Expand Down Expand Up @@ -77,21 +80,23 @@ public void ResolveOrder(Actor self, Order order)
self.QueueActivity(new DonateSupplies(self, target.Actor, info.Payload, info.PlayerExperience));
}

class DeliversSuppliesOrderTargeter : UnitOrderTargeter
public class DeliversSuppliesOrderTargeter : UnitOrderTargeter
{
public DeliversSuppliesOrderTargeter()
: base("DeliverSupplies", 5, "enter", false, true)
{
}
: base("DeliverSupplies", 5, "enter", false, true) { }

public override bool CanTargetActor(Actor self, Actor target, TargetModifiers modifiers, ref string cursor)
{
return target.Info.HasTraitInfo<AcceptsSuppliesInfo>();
var type = self.Info.TraitInfo<DeliversSuppliesInfo>().Type;
var targetInfo = target.Info.TraitInfoOrDefault<AcceptsSuppliesInfo>();
return targetInfo != null && (targetInfo.ValidTypes.Count == 0 || (!string.IsNullOrEmpty(type) && targetInfo.ValidTypes.Contains(type)));
}

public override bool CanTargetFrozenActor(Actor self, FrozenActor target, TargetModifiers modifiers, ref string cursor)
{
return target.Info.HasTraitInfo<AcceptsSuppliesInfo>();
var type = self.Info.TraitInfo<DeliversSuppliesInfo>().Type;
var targetInfo = target.Info.TraitInfoOrDefault<AcceptsSuppliesInfo>();
return targetInfo != null && (targetInfo.ValidTypes.Count == 0 || (!string.IsNullOrEmpty(type) && targetInfo.ValidTypes.Contains(type)));
}
}
}
Expand Down

0 comments on commit 8eeb6e7

Please sign in to comment.