Files
OpenRA/OpenRA.Mods.RA/AttackMove.cs
2011-01-26 21:00:28 +13:00

106 lines
2.5 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion
using System.Drawing;
using OpenRA.Effects;
using OpenRA.Mods.RA.Move;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA
{
class AttackMoveInfo : TraitInfo<AttackMove>
{
public readonly bool JustMove = false;
}
class AttackMove : IResolveOrder, IOrderVoice, ITick, ISync
{
[Sync] public int2 TargetLocation = int2.Zero;
[Sync] public bool AttackMoving = false;
public string VoicePhraseForOrder(Actor self, Order order)
{
if (order.OrderString == "AttackMove")
return "AttackMove";
return null;
}
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "AttackMove")
{
self.CancelActivity();
//if we are just moving, we don't turn on attackmove and this becomes a regular move order
if (self.Info.Traits.Get<AttackMoveInfo>().JustMove)
{
self.QueueActivity(self.Trait<Mobile>().MoveTo(order.TargetLocation, 1));
AttackMoving = false;
}
else
{
self.QueueActivity(new AttackMoveActivity(order.TargetLocation));
AttackMoving = true;
TargetLocation = order.TargetLocation;
}
self.SetTargetLine(Target.FromOrder(order), Color.Red);
}
else
{
AttackMoving = false;
}
}
class AttackMoveActivity : CancelableActivity
{
readonly int2 target;
IActivity inner;
public AttackMoveActivity( int2 target ) { this.target = target; }
public override IActivity Tick( Actor self )
{
self.Trait<AttackBase>().ScanAndAttack(self, true);
if( inner == null )
{
if( IsCanceled )
return NextActivity;
inner = self.Trait<Mobile>().MoveTo( target, 1 );
}
inner = Util.RunActivity( self, inner );
return this;
}
protected override bool OnCancel( Actor self )
{
if( inner != null )
inner.Cancel( self );
return base.OnCancel( self );
}
}
public void Tick(Actor self)
{
if (!self.IsInWorld) return;
if (AttackMoving && self.IsIdle)
{
self.CancelActivity();
self.QueueActivity(new AttackMoveActivity(TargetLocation));
}
}
}
}