Pull the make animation crap out of Transforms

This commit is contained in:
Paul Chote
2011-04-10 10:53:04 +12:00
parent 43f81501a8
commit e4d8680bd5
4 changed files with 78 additions and 41 deletions

View File

@@ -0,0 +1,56 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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.FileFormats;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities
{
class MakeAnimation : CancelableActivity
{
readonly bool Reversed;
readonly RenderBuilding rb;
public MakeAnimation(Actor self) : this(self, false) {}
public MakeAnimation(Actor self, bool reversed)
{
Reversed = reversed;
rb = self.Trait<RenderBuilding>();
}
bool complete = false;
bool started = false;
public override IActivity Tick( Actor self )
{
if (IsCanceled) return NextActivity;
if (!started)
{
started = true;
if (Reversed)
{
foreach (var s in self.Info.Traits.Get<BuildingInfo>().SellSounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterLocation);
// PlayCustomAnim is required to stop a frame of the normal state after the anim completes
rb.PlayCustomAnimBackwards(self, "make", () => {rb.PlayCustomAnim(self, "make"); complete = true;});
}
else
rb.PlayCustomAnimThen(self, "make", () => complete = true);
}
return complete ? NextActivity : this;
}
// Not actually cancellable
protected override bool OnCancel( Actor self ) { return false; }
}
}

View File

@@ -18,69 +18,45 @@ namespace OpenRA.Mods.RA.Activities
{ {
class Transform : CancelableActivity class Transform : CancelableActivity
{ {
string actor = null; public readonly string ToActor = null;
int2 offset; public int2 Offset = new int2(0,0);
string[] sounds = null; public int Facing = 96;
int facing; public string[] Sounds = {};
RenderBuilding rb; public Transform(Actor self, string toActor)
public Transform(Actor self, string toActor, int2 offset, int facing, string[] sounds)
{ {
this.actor = toActor; this.ToActor = toActor;
this.offset = offset;
this.sounds = sounds;
this.facing = facing;
rb = self.TraitOrDefault<RenderBuilding>();
} }
void DoTransform(Actor self) public override IActivity Tick( Actor self )
{ {
// Hack: repeat the first frame of the make anim instead if (IsCanceled) return NextActivity;
// of flashing the full structure for a frame
if (rb != null)
rb.PlayCustomAnim(self, "make");
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>
{ {
var selected = w.Selection.Contains(self); var selected = w.Selection.Contains(self);
self.Destroy(); self.Destroy();
foreach (var s in sounds) foreach (var s in Sounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterLocation); Sound.PlayToPlayer(self.Owner, s, self.CenterLocation);
var init = new TypeDictionary var init = new TypeDictionary
{ {
new LocationInit( self.Location + offset ), new LocationInit( self.Location + Offset ),
new OwnerInit( self.Owner ), new OwnerInit( self.Owner ),
new FacingInit( facing ), new FacingInit( Facing ),
}; };
if (self.HasTrait<Health>()) var health = self.TraitOrDefault<Health>();
init.Add( new HealthInit( self.Trait<Health>().HPFraction )); // TODO: fix potential desync from HPFraction
if (health != null)
init.Add( new HealthInit( health.HPFraction ));
var a = w.CreateActor( actor, init ); var a = w.CreateActor( ToActor, init );
if (selected) if (selected)
w.Selection.Add(w, a); w.Selection.Add(w, a);
}); });
}
bool started = false;
public override IActivity Tick( Actor self )
{
if (IsCanceled) return NextActivity;
if (started) return this;
if (rb == null)
DoTransform(self);
else
{
rb.PlayCustomAnimBackwards(self, "make", () => DoTransform(self));
foreach (var s in self.Info.Traits.Get<BuildingInfo>().SellSounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterLocation);
started = true;
}
return this; return this;
} }
} }

View File

@@ -341,6 +341,7 @@
<Compile Include="Render\WithShadow.cs" /> <Compile Include="Render\WithShadow.cs" />
<Compile Include="AnnounceOnBuild.cs" /> <Compile Include="AnnounceOnBuild.cs" />
<Compile Include="Capturable.cs" /> <Compile Include="Capturable.cs" />
<Compile Include="Activities\MakeAnimation.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -13,6 +13,7 @@ using OpenRA.Mods.RA.Activities;
using OpenRA.Mods.RA.Buildings; using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.RA.Orders; using OpenRA.Mods.RA.Orders;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Mods.RA.Render;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
@@ -79,7 +80,10 @@ namespace OpenRA.Mods.RA
if (self.HasTrait<IFacing>()) if (self.HasTrait<IFacing>())
self.QueueActivity(new Turn(Info.Facing)); self.QueueActivity(new Turn(Info.Facing));
self.QueueActivity(new Transform(self, Info.IntoActor, Info.Offset, Info.Facing, Info.TransformSounds)); if (self.HasTrait<RenderBuilding>() && self.Info.Traits.Get<RenderBuildingInfo>().HasMakeAnimation)
self.QueueActivity(new MakeAnimation(self, true));
self.QueueActivity(new Transform(self, Info.IntoActor) {Offset = Info.Offset, Facing = Info.Facing, Sounds = Info.TransformSounds});
} }
} }
} }