make more activities cancelable. remove many uses of CurrentActivity is T

This commit is contained in:
Bob
2010-09-22 11:53:58 +12:00
parent e2eae7973b
commit c16a515224
19 changed files with 95 additions and 101 deletions

View File

@@ -8,7 +8,7 @@
*/ */
#endregion #endregion
using OpenRA.Traits; using System.Collections.Generic;
namespace OpenRA.Traits.Activities namespace OpenRA.Traits.Activities
{ {
@@ -48,5 +48,10 @@ namespace OpenRA.Traits.Activities
else else
NextActivity = activity; NextActivity = activity;
} }
public IEnumerable<float2> GetCurrentPath()
{
yield return endLocation;
}
} }
} }

View File

@@ -10,19 +10,8 @@
namespace OpenRA.Traits.Activities namespace OpenRA.Traits.Activities
{ {
class Idle : IActivity class Idle : CancelableActivity
{ {
IActivity NextActivity { get; set; } public override IActivity Tick(Actor self) { return NextActivity; }
public IActivity Tick(Actor self) { return NextActivity; }
public void Cancel(Actor self) {}
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -247,9 +247,17 @@ namespace OpenRA.Traits.Activities
return nextCell; return nextCell;
} }
protected override void OnCancel() protected override bool OnCancel()
{ {
path = new List<int2>(); path = new List<int2>();
return true;
}
public override IEnumerable<float2> GetCurrentPath()
{
if( path == null )
return new float2[ 0 ];
return Enumerable.Reverse(path).Select( c => Util.CenterOfCell(c) );
} }
abstract class MovePart : IActivity abstract class MovePart : IActivity
@@ -312,6 +320,11 @@ namespace OpenRA.Traits.Activities
} }
protected abstract MovePart OnComplete( Actor self, Mobile mobile, Move parent ); protected abstract MovePart OnComplete( Actor self, Mobile mobile, Move parent );
public IEnumerable<float2> GetCurrentPath()
{
return move.GetCurrentPath();
}
} }
class MoveFirstHalf : MovePart class MoveFirstHalf : MovePart

View File

@@ -8,6 +8,8 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
namespace OpenRA.Traits.Activities namespace OpenRA.Traits.Activities
{ {
class Sell : IActivity class Sell : IActivity
@@ -63,5 +65,10 @@ namespace OpenRA.Traits.Activities
else else
NextActivity = activity; NextActivity = activity;
} }
public IEnumerable<float2> GetCurrentPath()
{
yield break;
}
} }
} }

View File

@@ -321,14 +321,6 @@ namespace OpenRA.Traits
return Info.Speed * Info.TerrainSpeeds[type].Speed * modifier / 100f; return Info.Speed * Info.TerrainSpeeds[type].Speed * modifier / 100f;
} }
public IEnumerable<float2> GetCurrentPath(Actor self)
{
var move = self.GetCurrentActivity() as Move;
if (move == null || move.path == null) return new float2[] { };
return Enumerable.Reverse(move.path).Select( c => Util.CenterOfCell(c) );
}
public void AddInfluence() public void AddInfluence()
{ {
uim.Add( self, this ); uim.Add( self, this );

View File

@@ -164,7 +164,7 @@ namespace OpenRA.Traits
if (mobile != null) if (mobile != null)
{ {
var alt = new float2(0, -mobile.Altitude); var alt = new float2(0, -mobile.Altitude);
var path = mobile.GetCurrentPath(self); var path = self.GetCurrentActivity().GetCurrentPath();
var start = self.CenterLocation + alt; var start = self.CenterLocation + alt;
var c = Color.Green; var c = Color.Green;

View File

@@ -113,7 +113,6 @@ namespace OpenRA.Traits
public interface IMove : ITeleportable public interface IMove : ITeleportable
{ {
float MovementSpeedForCell(Actor self, int2 cell); float MovementSpeedForCell(Actor self, int2 cell);
IEnumerable<float2> GetCurrentPath(Actor self);
int Altitude { get; set; } int Altitude { get; set; }
} }
@@ -173,6 +172,7 @@ namespace OpenRA.Traits
IActivity Tick(Actor self); IActivity Tick(Actor self);
void Cancel(Actor self); void Cancel(Actor self);
void Queue(IActivity activity); void Queue(IActivity activity);
IEnumerable<float2> GetCurrentPath();
} }
public abstract class CancelableActivity : IActivity public abstract class CancelableActivity : IActivity
@@ -181,13 +181,15 @@ namespace OpenRA.Traits
protected bool IsCanceled { get; private set; } protected bool IsCanceled { get; private set; }
public abstract IActivity Tick( Actor self ); public abstract IActivity Tick( Actor self );
protected virtual void OnCancel() {} protected virtual bool OnCancel() { return true; }
public void Cancel( Actor self ) public void Cancel( Actor self )
{ {
IsCanceled = true; IsCanceled = OnCancel();
NextActivity = null; if( IsCanceled )
OnCancel(); NextActivity = null;
else
NextActivity.Cancel( self );
} }
public void Queue( IActivity activity ) public void Queue( IActivity activity )
@@ -197,6 +199,11 @@ namespace OpenRA.Traits
else else
NextActivity = activity; NextActivity = activity;
} }
public virtual IEnumerable<float2> GetCurrentPath()
{
yield break;
}
} }
public interface IRenderOverlay { void Render(); } public interface IRenderOverlay { void Render(); }

View File

@@ -9,6 +9,7 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
@@ -48,5 +49,10 @@ namespace OpenRA.Mods.RA.Activities
else else
NextActivity = activity; NextActivity = activity;
} }
public IEnumerable<float2> GetCurrentPath()
{
yield break;
}
} }
} }

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Traits.Activities; using OpenRA.Traits.Activities;
@@ -60,5 +61,10 @@ namespace OpenRA.Mods.RA.Activities
else else
NextActivity = activity; NextActivity = activity;
} }
public IEnumerable<float2> GetCurrentPath()
{
yield break;
}
} }
} }

View File

@@ -9,7 +9,7 @@
#endregion #endregion
using System; using System;
using System.Linq; using System.Collections.Generic;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
@@ -43,6 +43,11 @@ namespace OpenRA.Mods.RA.Activities
FlyUtil.Fly(self, cruiseAltitude); FlyUtil.Fly(self, cruiseAltitude);
return this; return this;
} }
public override IEnumerable<float2> GetCurrentPath()
{
yield return Pos;
}
} }
public static class FlyUtil public static class FlyUtil

View File

@@ -28,35 +28,21 @@ namespace OpenRA.Mods.RA.Activities
} }
} }
public class FlyOffMap : IActivity public class FlyOffMap : CancelableActivity
{ {
IActivity NextActivity { get; set; }
bool isCanceled;
public bool Interruptible = true; public bool Interruptible = true;
public IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
var targetAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude; var targetAltitude = self.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
if (isCanceled || !self.World.Map.IsInMap(self.Location)) return NextActivity; if (IsCanceled || !self.World.Map.IsInMap(self.Location)) return NextActivity;
FlyUtil.Fly(self, targetAltitude); FlyUtil.Fly(self, targetAltitude);
return this; return this;
} }
public void Cancel(Actor self) protected override bool OnCancel()
{ {
if (Interruptible) return Interruptible;
{
isCanceled = true;
NextActivity = null;
}
}
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
} }
} }
} }

View File

@@ -15,14 +15,14 @@ using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Harvest : IActivity public class Harvest : CancelableActivity
{ {
IActivity NextActivity { get; set; }
bool isHarvesting = false; bool isHarvesting = false;
public IActivity Tick( Actor self ) public override IActivity Tick( Actor self )
{ {
if( isHarvesting ) return this; if( isHarvesting ) return this;
if( IsCanceled ) return NextActivity;
if( NextActivity != null ) return NextActivity; if( NextActivity != null ) return NextActivity;
var harv = self.Trait<Harvester>(); var harv = self.Trait<Harvester>();
@@ -72,15 +72,5 @@ namespace OpenRA.Mods.RA.Activities
})); }));
self.QueueActivity(new Harvest()); self.QueueActivity(new Harvest());
} }
public void Cancel(Actor self) { }
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
}
} }
} }

View File

@@ -9,7 +9,7 @@
#endregion #endregion
using System; using System;
using System.Linq; using System.Collections.Generic;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
@@ -53,5 +53,10 @@ namespace OpenRA.Mods.RA.Activities
return this; return this;
} }
public override IEnumerable<float2> GetCurrentPath()
{
yield return Dest;
}
} }
} }

View File

@@ -6,11 +6,11 @@
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
* see LICENSE. * see LICENSE.
*/ */
#endregion #endregion
using System; using System.Collections.Generic;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Mods.RA.Render;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
@@ -51,5 +51,10 @@ namespace OpenRA.Mods.RA.Activities
else else
NextActivity = activity; NextActivity = activity;
} }
public IEnumerable<float2> GetCurrentPath()
{
yield break;
}
} }
} }

View File

@@ -33,9 +33,11 @@ namespace OpenRA.Mods.RA.Activities
public override IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (IsCanceled) return NextActivity; if( t == 0 && IsCanceled ) return NextActivity;
if (!target.IsValid) return NextActivity; if (!target.IsValid) return NextActivity;
self.Trait<AttackLeap>().IsLeaping = true;
t += (1f / delay); t += (1f / delay);
self.CenterLocation = float2.Lerp(initialLocation, target.CenterLocation, t); self.CenterLocation = float2.Lerp(initialLocation, target.CenterLocation, t);
@@ -47,6 +49,7 @@ namespace OpenRA.Mods.RA.Activities
if (target.IsActor) if (target.IsActor)
target.Actor.Kill(self); target.Actor.Kill(self);
self.Trait<AttackLeap>().IsLeaping = false;
return NextActivity; return NextActivity;
} }

View File

@@ -12,11 +12,10 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class Wait : IActivity public class Wait : CancelableActivity
{ {
int remainingTicks; int remainingTicks;
bool interruptable = true; bool interruptable = true;
IActivity NextActivity { get; set; }
public Wait(int period) { remainingTicks = period; } public Wait(int period) { remainingTicks = period; }
public Wait(int period, bool interruptable) public Wait(int period, bool interruptable)
@@ -25,27 +24,17 @@ namespace OpenRA.Mods.RA.Activities
this.interruptable = interruptable; this.interruptable = interruptable;
} }
public virtual IActivity Tick(Actor self) public override IActivity Tick(Actor self)
{ {
if (remainingTicks-- == 0) return NextActivity; if (remainingTicks-- == 0) return NextActivity;
return this; return this;
} }
public void Cancel(Actor self) protected override bool OnCancel()
{ {
if (!interruptable) if( !interruptable ) return false;
return;
remainingTicks = 0; remainingTicks = 0;
NextActivity = null; return true;
}
public void Queue( IActivity activity )
{
if( NextActivity != null )
NextActivity.Queue( activity );
else
NextActivity = activity;
} }
} }
} }

View File

@@ -70,14 +70,6 @@ namespace OpenRA.Mods.RA
|| Info.RepairBuildings.Contains( a.Info.Name ); || Info.RepairBuildings.Contains( a.Info.Name );
} }
public virtual IEnumerable<float2> GetCurrentPath(Actor self)
{
var move = self.GetCurrentActivity() as Activities.Fly;
if (move == null) return new float2[] { };
return new float2[] { move.Pos };
}
public bool CanEnterCell(int2 location) { return true; } public bool CanEnterCell(int2 location) { return true; }
public float MovementSpeedForCell(Actor self, int2 cell) public float MovementSpeedForCell(Actor self, int2 cell)

View File

@@ -19,6 +19,8 @@ namespace OpenRA.Mods.RA
class AttackLeap : AttackBase class AttackLeap : AttackBase
{ {
internal bool IsLeaping;
public AttackLeap(Actor self) public AttackLeap(Actor self)
: base(self) {} : base(self) {}
@@ -27,7 +29,7 @@ namespace OpenRA.Mods.RA
base.Tick(self); base.Tick(self);
if (!target.IsValid) return; if (!target.IsValid) return;
if (self.GetCurrentActivity() is Leap) return; if (IsLeaping) return;
var weapon = self.Trait<AttackBase>().Weapons[0].Info; var weapon = self.Trait<AttackBase>().Weapons[0].Info;
if (weapon.Range * Game.CellSize * weapon.Range * Game.CellSize if (weapon.Range * Game.CellSize * weapon.Range * Game.CellSize

View File

@@ -171,13 +171,5 @@ namespace OpenRA.Mods.RA
return float2.FromAngle((float)self.World.SharedRandom.NextDouble() * 3.14f); return float2.FromAngle((float)self.World.SharedRandom.NextDouble() * 3.14f);
return (5 / d.LengthSquared) * d; return (5 / d.LengthSquared) * d;
} }
public override IEnumerable<float2> GetCurrentPath(Actor self)
{
var move = self.GetCurrentActivity() as Activities.HeliFly;
if (move == null) return new float2[] { };
return new float2[] { move.Dest };
}
} }
} }