Make Captures Upgradeable

This commit is contained in:
Mustafa Alperen Seki
2017-03-22 19:03:12 +02:00
committed by abcdefg30
parent 287290bbdf
commit f7983692ae
3 changed files with 40 additions and 18 deletions

View File

@@ -9,6 +9,8 @@
*/ */
#endregion #endregion
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Traits; using OpenRA.Traits;
@@ -19,7 +21,7 @@ namespace OpenRA.Mods.Common.Activities
readonly Actor actor; readonly Actor actor;
readonly Building building; readonly Building building;
readonly Capturable capturable; readonly Capturable capturable;
readonly CapturesInfo capturesInfo; readonly Captures[] captures;
readonly Health health; readonly Health health;
public CaptureActor(Actor self, Actor target) public CaptureActor(Actor self, Actor target)
@@ -27,7 +29,7 @@ namespace OpenRA.Mods.Common.Activities
{ {
actor = target; actor = target;
building = actor.TraitOrDefault<Building>(); building = actor.TraitOrDefault<Building>();
capturesInfo = self.Info.TraitInfo<CapturesInfo>(); captures = self.TraitsImplementing<Captures>().ToArray();
capturable = target.Trait<Capturable>(); capturable = target.Trait<Capturable>();
health = actor.Trait<Health>(); health = actor.Trait<Health>();
} }
@@ -50,9 +52,12 @@ namespace OpenRA.Mods.Common.Activities
if (building != null && building.Locked) if (building != null && building.Locked)
building.Unlock(); building.Unlock();
if (actor.IsDead || capturable.BeingCaptured) var activeCaptures = captures.FirstOrDefault(c => !c.IsTraitDisabled);
if (actor.IsDead || capturable.BeingCaptured || activeCaptures == null)
return; return;
var capturesInfo = activeCaptures.Info;
var lowEnoughHealth = health.HP <= capturable.Info.CaptureThreshold * health.MaxHP / 100; var lowEnoughHealth = health.HP <= capturable.Info.CaptureThreshold * health.MaxHP / 100;
if (!capturesInfo.Sabotage || lowEnoughHealth || actor.Owner.NonCombatant) if (!capturesInfo.Sabotage || lowEnoughHealth || actor.Owner.NonCombatant)
{ {
@@ -82,5 +87,13 @@ namespace OpenRA.Mods.Common.Activities
self.Dispose(); self.Dispose();
}); });
} }
public override Activity Tick(Actor self)
{
if (captures.All(c => c.IsTraitDisabled))
Cancel(self);
return base.Tick(self);
}
} }
} }

View File

@@ -9,6 +9,8 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using System.Linq;
using Eluant; using Eluant;
using OpenRA.Mods.Common.Activities; using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
@@ -20,25 +22,33 @@ namespace OpenRA.Mods.Common.Scripting
[ScriptPropertyGroup("Ability")] [ScriptPropertyGroup("Ability")]
public class CaptureProperties : ScriptActorProperties public class CaptureProperties : ScriptActorProperties
{ {
readonly CapturesInfo normalInfo; readonly Captures[] captures;
readonly ExternalCapturesInfo externalInfo; readonly ExternalCapturesInfo externalInfo;
public CaptureProperties(ScriptContext context, Actor self) public CaptureProperties(ScriptContext context, Actor self)
: base(context, self) : base(context, self)
{ {
normalInfo = Self.Info.TraitInfoOrDefault<CapturesInfo>(); captures = Self.TraitsImplementing<Captures>().ToArray();
externalInfo = Self.Info.TraitInfoOrDefault<ExternalCapturesInfo>(); externalInfo = Self.Info.TraitInfoOrDefault<ExternalCapturesInfo>();
} }
[Desc("Captures the target actor.")] [Desc("Captures the target actor.")]
public void Capture(Actor target) public void Capture(Actor target)
{ {
var normalCapturable = target.Info.TraitInfoOrDefault<CapturableInfo>(); var capturable = target.Info.TraitInfoOrDefault<CapturableInfo>();
if (capturable != null)
{
if (captures.Any(x => !x.IsTraitDisabled && x.Info.CaptureTypes.Contains(capturable.Type)))
{
Self.QueueActivity(new CaptureActor(Self, target));
return;
}
}
var externalCapturable = target.Info.TraitInfoOrDefault<ExternalCapturableInfo>(); var externalCapturable = target.Info.TraitInfoOrDefault<ExternalCapturableInfo>();
if (normalInfo != null && normalCapturable != null && normalInfo.CaptureTypes.Contains(normalCapturable.Type)) if (externalInfo != null && externalCapturable != null && externalInfo.CaptureTypes.Contains(externalCapturable.Type))
Self.QueueActivity(new CaptureActor(Self, target));
else if (externalInfo != null && externalCapturable != null && externalInfo.CaptureTypes.Contains(externalCapturable.Type))
Self.QueueActivity(new ExternalCaptureActor(Self, Target.FromActor(target))); Self.QueueActivity(new ExternalCaptureActor(Self, Target.FromActor(target)));
else else
throw new LuaException("Actor '{0}' cannot capture actor '{1}'!".F(Self, target)); throw new LuaException("Actor '{0}' cannot capture actor '{1}'!".F(Self, target));

View File

@@ -18,7 +18,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("This actor can capture other actors which have the Capturable: trait.")] [Desc("This actor can capture other actors which have the Capturable: trait.")]
public class CapturesInfo : ITraitInfo public class CapturesInfo : ConditionalTraitInfo
{ {
[Desc("Types of actors that it can capture, as long as the type also exists in the Capturable Type: trait.")] [Desc("Types of actors that it can capture, as long as the type also exists in the Capturable Type: trait.")]
public readonly HashSet<string> CaptureTypes = new HashSet<string> { "building" }; public readonly HashSet<string> CaptureTypes = new HashSet<string> { "building" };
@@ -41,22 +41,21 @@ namespace OpenRA.Mods.Common.Traits
[VoiceReference] public readonly string Voice = "Action"; [VoiceReference] public readonly string Voice = "Action";
public object Create(ActorInitializer init) { return new Captures(this); } public override object Create(ActorInitializer init) { return new Captures(this); }
} }
public class Captures : IIssueOrder, IResolveOrder, IOrderVoice public class Captures : ConditionalTrait<CapturesInfo>, IIssueOrder, IResolveOrder, IOrderVoice
{ {
public readonly CapturesInfo Info;
public Captures(CapturesInfo info) public Captures(CapturesInfo info)
{ : base(info) { }
Info = info;
}
public IEnumerable<IOrderTargeter> Orders public IEnumerable<IOrderTargeter> Orders
{ {
get get
{ {
if (IsTraitDisabled)
yield break;
yield return new CaptureOrderTargeter(Info); yield return new CaptureOrderTargeter(Info);
} }
} }
@@ -79,7 +78,7 @@ namespace OpenRA.Mods.Common.Traits
public void ResolveOrder(Actor self, Order order) public void ResolveOrder(Actor self, Order order)
{ {
if (order.OrderString != "CaptureActor") if (order.OrderString != "CaptureActor" || IsTraitDisabled)
return; return;
var target = self.ResolveFrozenActorOrder(order, Color.Red); var target = self.ResolveFrozenActorOrder(order, Color.Red);