Move everything ExternalCapture-related to Mods.Common

This commit is contained in:
penev92
2015-01-06 01:26:23 +02:00
parent 8504c233d9
commit 18ac3953cc
6 changed files with 9 additions and 13 deletions

View File

@@ -0,0 +1,84 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Activities;
using OpenRA.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
class ExternalCaptureActor : Activity
{
Target target;
public ExternalCaptureActor(Target target) { this.target = target; }
public override Activity Tick(Actor self)
{
if (target.Type != TargetType.Actor)
return NextActivity;
var capturable = target.Actor.Trait<ExternalCapturable>();
if (IsCanceled || !self.IsInWorld || self.IsDead || !target.IsValidFor(self))
{
if (capturable.CaptureInProgress)
capturable.EndCapture();
return NextActivity;
}
var mobile = self.Trait<Mobile>();
var nearest = target.Actor.OccupiesSpace.NearestCellTo(mobile.ToCell);
if ((nearest - mobile.ToCell).LengthSquared > 2)
return Util.SequenceActivities(new MoveAdjacentTo(self, target), this);
if (!capturable.CaptureInProgress)
capturable.BeginCapture(self);
else
{
if (capturable.Captor != self) return NextActivity;
if (capturable.CaptureProgressTime % 25 == 0)
{
self.World.Add(new FlashTarget(target.Actor, self.Owner));
self.World.Add(new FlashTarget(self));
}
if (capturable.CaptureProgressTime == capturable.Info.CaptureCompleteTime * 25)
{
var capturesInfo = self.Info.Traits.Get<ExternalCapturesInfo>();
self.World.AddFrameEndTask(w =>
{
if (target.Actor.IsDead)
return;
var oldOwner = target.Actor.Owner;
target.Actor.ChangeOwner(self.Owner);
foreach (var t in target.Actor.TraitsImplementing<INotifyCapture>())
t.OnCapture(target.Actor, self, oldOwner, self.Owner);
capturable.EndCapture();
if (capturesInfo != null && capturesInfo.ConsumeActor)
self.Destroy();
});
}
}
return this;
}
}
}

View File

@@ -74,6 +74,7 @@
<Compile Include="Activities\Attack.cs" />
<Compile Include="Activities\Enter.cs" />
<Compile Include="Activities\EnterTransport.cs" />
<Compile Include="Activities\ExternalCaptureActor.cs" />
<Compile Include="Activities\Heal.cs" />
<Compile Include="Activities\Hunt.cs" />
<Compile Include="Activities\Move\AttackMoveActivity.cs" />
@@ -201,6 +202,9 @@
<Compile Include="Traits\CashTrickler.cs" />
<Compile Include="Traits\Cloak.cs" />
<Compile Include="Traits\Crushable.cs" />
<Compile Include="Traits\ExternalCapturable.cs" />
<Compile Include="Traits\ExternalCapturableBar.cs" />
<Compile Include="Traits\ExternalCaptures.cs" />
<Compile Include="Traits\IgnoresDisguise.cs" />
<Compile Include="Traits\DetectCloaked.cs" />
<Compile Include="Traits\EngineerRepair.cs" />

View File

@@ -0,0 +1,95 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor can be captured by a unit with ExternalCaptures: trait.")]
public class ExternalCapturableInfo : ITraitInfo
{
[Desc("Type of actor (the ExternalCaptures: trait defines what Types it can capture).")]
public readonly string Type = "building";
public readonly bool AllowAllies = false;
public readonly bool AllowNeutral = true;
public readonly bool AllowEnemies = true;
[Desc("Seconds it takes to change the owner.", "You might want to add a ExternalCapturableBar: trait, too.")]
public readonly int CaptureCompleteTime = 15;
public bool CanBeTargetedBy(Actor captor, Player owner)
{
var c = captor.TraitOrDefault<ExternalCaptures>();
if (c == null)
return false;
var playerRelationship = owner.Stances[captor.Owner];
if (playerRelationship == Stance.Ally && !AllowAllies)
return false;
if (playerRelationship == Stance.Enemy && !AllowEnemies)
return false;
if (playerRelationship == Stance.Neutral && !AllowNeutral)
return false;
if (!c.Info.CaptureTypes.Contains(Type))
return false;
return true;
}
public object Create(ActorInitializer init) { return new ExternalCapturable(init.Self, this); }
}
public class ExternalCapturable : ITick
{
[Sync] public int CaptureProgressTime = 0;
[Sync] public Actor Captor;
private Actor self;
public ExternalCapturableInfo Info;
public bool CaptureInProgress { get { return Captor != null; } }
public ExternalCapturable(Actor self, ExternalCapturableInfo info)
{
this.self = self;
Info = info;
}
public void BeginCapture(Actor captor)
{
var building = self.TraitOrDefault<Building>();
if (building != null)
building.Lock();
Captor = captor;
}
public void EndCapture()
{
var building = self.TraitOrDefault<Building>();
if (building != null)
building.Unlock();
Captor = null;
}
public void Tick(Actor self)
{
if (Captor != null && (!Captor.IsInWorld || Captor.IsDead))
EndCapture();
if (!CaptureInProgress)
CaptureProgressTime = 0;
else
CaptureProgressTime++;
}
}
}

View File

@@ -0,0 +1,42 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Visualize the remaining CaptureCompleteTime from ExternalCapturable: trait.")]
class ExternalCapturableBarInfo : ITraitInfo, Requires<ExternalCapturableInfo>
{
public object Create(ActorInitializer init) { return new ExternalCapturableBar(init.Self); }
}
class ExternalCapturableBar : ISelectionBar
{
ExternalCapturable cap;
public ExternalCapturableBar(Actor self)
{
this.cap = self.Trait<ExternalCapturable>();
}
public float GetValue()
{
// only show when building is being captured
if (!cap.CaptureInProgress)
return 0f;
return (float)cap.CaptureProgressTime / (cap.Info.CaptureCompleteTime * 25);
}
public Color GetColor() { return Color.Orange; }
}
}

View File

@@ -0,0 +1,128 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor can capture other actors which have the ExternalCapturable: trait.")]
class ExternalCapturesInfo : ITraitInfo
{
[Desc("Types of actors that it can capture, as long as the type also exists in the ExternalCapturable Type: trait.")]
public readonly string[] CaptureTypes = { "building" };
[Desc("Destroy the unit after capturing.")]
public readonly bool ConsumeActor = false;
public object Create(ActorInitializer init) { return new ExternalCaptures(init.Self, this); }
}
class ExternalCaptures : IIssueOrder, IResolveOrder, IOrderVoice
{
public readonly ExternalCapturesInfo Info;
public ExternalCaptures(Actor self, ExternalCapturesInfo info)
{
Info = info;
}
public IEnumerable<IOrderTargeter> Orders
{
get
{
yield return new ExternalCaptureOrderTargeter();
}
}
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
{
if (order.OrderID != "ExternalCaptureActor")
return null;
if (target.Type == TargetType.FrozenActor)
return new Order(order.OrderID, self, queued) { ExtraData = target.FrozenActor.ID };
return new Order(order.OrderID, self, queued) { TargetActor = target.Actor };
}
static bool IsValidOrder(Actor self, Order order)
{
// Not targeting an actor
if (order.ExtraData == 0 && order.TargetActor == null)
return false;
if (order.ExtraData != 0)
{
// Targeted an actor under the fog
var frozenLayer = self.Owner.PlayerActor.TraitOrDefault<FrozenActorLayer>();
if (frozenLayer == null)
return false;
var frozen = frozenLayer.FromID(order.ExtraData);
if (frozen == null)
return false;
var ci = frozen.Info.Traits.GetOrDefault<ExternalCapturableInfo>();
return ci != null && ci.CanBeTargetedBy(self, frozen.Owner);
}
var c = order.TargetActor.TraitOrDefault<ExternalCapturable>();
return c != null && !c.CaptureInProgress && c.Info.CanBeTargetedBy(self, order.TargetActor.Owner);
}
public string VoicePhraseForOrder(Actor self, Order order)
{
return order.OrderString == "ExternalCaptureActor" && IsValidOrder(self, order)
? "Attack" : null;
}
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString != "ExternalCaptureActor" || !IsValidOrder(self, order))
return;
var target = self.ResolveFrozenActorOrder(order, Color.Red);
if (target.Type != TargetType.Actor)
return;
if (!order.Queued)
self.CancelActivity();
self.SetTargetLine(target, Color.Red);
self.QueueActivity(new ExternalCaptureActor(target));
}
}
class ExternalCaptureOrderTargeter : UnitOrderTargeter
{
public ExternalCaptureOrderTargeter() : base("ExternalCaptureActor", 6, "enter", true, true) { }
public override bool CanTargetActor(Actor self, Actor target, TargetModifiers modifiers, ref string cursor)
{
var c = target.TraitOrDefault<ExternalCapturable>();
var canTargetActor = c != null && !c.CaptureInProgress && c.Info.CanBeTargetedBy(self, target.Owner);
cursor = canTargetActor ? "ability" : "move-blocked";
return canTargetActor;
}
public override bool CanTargetFrozenActor(Actor self, FrozenActor target, TargetModifiers modifiers, ref string cursor)
{
var c = target.Info.Traits.GetOrDefault<ExternalCapturableInfo>();
var canTargetActor = c != null && c.CanBeTargetedBy(self, target.Owner);
cursor = canTargetActor ? "ability" : "move-blocked";
return canTargetActor;
}
}
}