Refactor McvDeploy into a generic 'transform into another actor' trait

This commit is contained in:
Paul Chote
2010-02-06 01:06:38 +13:00
parent a08e062d96
commit 5b702b76cf
9 changed files with 142 additions and 75 deletions

View File

@@ -130,7 +130,7 @@
<Compile Include="Traits\Activities\HeliReturn.cs" />
<Compile Include="Traits\Activities\IActivity.cs" />
<Compile Include="Traits\Activities\DeliverOre.cs" />
<Compile Include="Traits\Activities\DeployMcv.cs" />
<Compile Include="Traits\Activities\TransformIntoActor.cs" />
<Compile Include="Actor.cs" />
<Compile Include="Effects\Bullet.cs" />
<Compile Include="Controller.cs" />
@@ -236,7 +236,7 @@
<Compile Include="Traits\ProductionSurround.cs" />
<Compile Include="Traits\RenderBuildingCharge.cs" />
<Compile Include="Traits\RenderInfantry.cs" />
<Compile Include="Traits\McvDeploy.cs" />
<Compile Include="Traits\TransformsOnDeploy.cs" />
<Compile Include="Traits\Mobile.cs" />
<Compile Include="Traits\Production.cs" />
<Compile Include="Traits\RallyPoint.cs" />

View File

@@ -67,12 +67,17 @@ namespace OpenRa.Orders
return Cursor.Move;
else
return Cursor.MoveBlocked;
case "DeployMcv":
var factBuildingInfo = Rules.Info["fact"].Traits.Get<BuildingInfo>();
if (a.World.CanPlaceBuilding("fact", factBuildingInfo, a.Location - new int2(1, 1), a))
return Cursor.Deploy;
else
return Cursor.DeployBlocked;
case "DeployTransform":
var depInfo = a.Info.Traits.Get<TransformsOnDeployInfo>();
var transInfo = Rules.Info[depInfo.TransformsInto];
if (transInfo.Traits.Contains<BuildingInfo>())
{
var bi = transInfo.Traits.Get<BuildingInfo>();
if (!a.World.CanPlaceBuilding(depInfo.TransformsInto, bi, a.Location + new int2(depInfo.Offset[0], depInfo.Offset[1]), a))
return Cursor.DeployBlocked;
}
return Cursor.Deploy;
case "Deploy": return Cursor.Deploy;
case "Enter": return Cursor.Enter;
case "EnterTransport": return Cursor.Enter;

View File

@@ -1,28 +0,0 @@
using System;
namespace OpenRa.Traits.Activities
{
class DeployMcv : IActivity
{
public IActivity NextActivity { get; set; }
public IActivity Tick( Actor self )
{
self.World.AddFrameEndTask( _ =>
{
self.Health = 0;
self.World.Remove( self );
Sound.PlayToPlayer(self.Owner, "placbldg.aud");
Sound.PlayToPlayer(self.Owner, "build5.aud");
self.World.CreateActor( "fact", self.Location - new int2( 1, 1 ), self.Owner );
} );
return this;
}
public void Cancel( Actor self )
{
// Cancel can't happen between this being moved to the head of the list, and it being Ticked.
throw new InvalidOperationException( "DeployMcvAction: Cancel() should never occur." );
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
namespace OpenRa.Traits.Activities
{
class TransformIntoActor : IActivity
{
string actor = null;
int2 offset;
string[] sounds = null;
bool transferPercentage;
bool isCanceled;
public TransformIntoActor(string actor, int2 offset, bool transferHealthPercentage, string[] sounds)
{
this.actor = actor;
this.offset = offset;
this.sounds = sounds;
this.transferPercentage = transferHealthPercentage;
}
public IActivity NextActivity { get; set; }
public IActivity Tick( Actor self )
{
if (isCanceled) return NextActivity;
self.World.AddFrameEndTask( _ =>
{
var oldHP = self.GetMaxHP();
var newHP = Rules.Info[actor].Traits.Get<OwnedActorInfo>().HP;
var newHealth = (transferPercentage) ? (int)((float)self.Health/oldHP*newHP) : Math.Min(self.Health, newHP);
self.Health = 0;
self.World.Remove( self );
foreach (var s in sounds)
Sound.PlayToPlayer(self.Owner, s);
var a = self.World.CreateActor( actor, self.Location + offset, self.Owner );
a.Health = newHealth;
} );
return this;
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
}
}

View File

@@ -1,37 +0,0 @@
using OpenRa.GameRules;
using OpenRa.Traits.Activities;
namespace OpenRa.Traits
{
class McvDeployInfo : ITraitInfo
{
public object Create(Actor self) { return new McvDeploy(self); }
}
class McvDeploy : IIssueOrder, IResolveOrder
{
public McvDeploy(Actor self) { }
public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor)
{
if (mi.Button == MouseButton.Right && self == underCursor)
return new Order("DeployMcv", self);
return null;
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "DeployMcv" )
{
var factBuildingInfo = Rules.Info[ "fact" ].Traits.Get<BuildingInfo>();
if( self.World.CanPlaceBuilding( "fact", factBuildingInfo, self.Location - new int2( 1, 1 ), self ) )
{
self.CancelActivity();
self.QueueActivity( new Turn( 96 ) );
self.QueueActivity( new DeployMcv() );
}
}
}
}
}

View File

@@ -26,6 +26,8 @@ namespace OpenRa.Traits
Sound.PlayToPlayer(order.Player, "placbldg.aud");
Sound.PlayToPlayer(order.Player, "build5.aud");
// TODO: Prioritise the primary conyard if it exists
var fact = self.World.Queries
.OwnedBy[self.Owner]
.WithTrait<ConstructionYard>()

View File

@@ -0,0 +1,68 @@
using OpenRa.GameRules;
using OpenRa.Traits.Activities;
using System;
namespace OpenRa.Traits
{
class TransformsOnDeployInfo : ITraitInfo
{
public readonly string TransformsInto = null;
public readonly int[] Offset = null;
public readonly int[] DeployDirections = new int[] {96};
public readonly bool TransferHealthPercentage = true; // Set to false to transfer the absolute health
public readonly string[] TransformSounds = null;
public readonly string[] NoTransformSounds = null;
public object Create(Actor self) { return new TransformsOnDeploy(self); }
}
class TransformsOnDeploy : IIssueOrder, IResolveOrder
{
public TransformsOnDeploy(Actor self) { }
public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor)
{
if (mi.Button == MouseButton.Right && self == underCursor)
return new Order("DeployTransform", self);
return null;
}
public void ResolveOrder( Actor self, Order order )
{
if (order.OrderString == "DeployTransform")
{
var info = self.Info.Traits.Get<TransformsOnDeployInfo>();
var transInfo = Rules.Info[info.TransformsInto];
if (transInfo.Traits.Contains<BuildingInfo>())
{
var bi = transInfo.Traits.Get<BuildingInfo>();
if (!self.World.CanPlaceBuilding(info.TransformsInto, bi, self.Location + new int2(info.Offset[0], info.Offset[1]), self))
{
foreach (var s in info.NoTransformSounds)
Sound.PlayToPlayer(self.Owner, s);
return;
}
}
self.CancelActivity();
// Pick the closed deploy direction to turn to
if (self.traits.Contains<Unit>())
{
var unit = self.traits.Get<Unit>();
// TODO: Pick the closest deploy direction
var bestDir = info.DeployDirections[0];
self.QueueActivity(new Turn(bestDir));
}
self.QueueActivity(new TransformIntoActor(info.TransformsInto, new int2(info.Offset[0], info.Offset[1]), info.TransferHealthPercentage, info.TransformSounds));
}
}
}
}

View File

@@ -16,7 +16,12 @@ MCV:
Crewed: yes
Sight: 2
Speed: 12
McvDeploy:
TransformsOnDeploy:
TransformsInto: fact
Offset:-1,-1
DeployDirections: 96
TransformSounds: placbldg.aud, build5.aud
NoTransformSounds: nodeply1.aud
RenderUnit:
HARV:

View File

@@ -734,7 +734,12 @@ MCV:
Crewed: yes
Sight: 4
Speed: 6
McvDeploy:
TransformsOnDeploy:
TransformsInto: fact
Offset:-1,-1
DeployDirections: 96
TransformSounds: placbldg.aud, build5.aud
NoTransformSounds: nodeply1.aud
RenderUnit:
JEEP: