diff --git a/OpenRA.Mods.RA/Activities/MakeAnimation.cs b/OpenRA.Mods.RA/Activities/MakeAnimation.cs new file mode 100644 index 0000000000..c02b6ff3e8 --- /dev/null +++ b/OpenRA.Mods.RA/Activities/MakeAnimation.cs @@ -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(); + } + + 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().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; } + } +} diff --git a/OpenRA.Mods.RA/Activities/Transform.cs b/OpenRA.Mods.RA/Activities/Transform.cs index e749648351..f36a8987d9 100644 --- a/OpenRA.Mods.RA/Activities/Transform.cs +++ b/OpenRA.Mods.RA/Activities/Transform.cs @@ -18,69 +18,45 @@ namespace OpenRA.Mods.RA.Activities { class Transform : CancelableActivity { - string actor = null; - int2 offset; - string[] sounds = null; - int facing; + public readonly string ToActor = null; + public int2 Offset = new int2(0,0); + public int Facing = 96; + public string[] Sounds = {}; - RenderBuilding rb; - public Transform(Actor self, string toActor, int2 offset, int facing, string[] sounds) + public Transform(Actor self, string toActor) { - this.actor = toActor; - this.offset = offset; - this.sounds = sounds; - this.facing = facing; - rb = self.TraitOrDefault(); + this.ToActor = toActor; } - void DoTransform(Actor self) + public override IActivity Tick( Actor self ) { - // Hack: repeat the first frame of the make anim instead - // of flashing the full structure for a frame - if (rb != null) - rb.PlayCustomAnim(self, "make"); + if (IsCanceled) return NextActivity; self.World.AddFrameEndTask(w => { var selected = w.Selection.Contains(self); self.Destroy(); - foreach (var s in sounds) + foreach (var s in Sounds) Sound.PlayToPlayer(self.Owner, s, self.CenterLocation); var init = new TypeDictionary { - new LocationInit( self.Location + offset ), + new LocationInit( self.Location + Offset ), new OwnerInit( self.Owner ), - new FacingInit( facing ), + new FacingInit( Facing ), }; - if (self.HasTrait()) - init.Add( new HealthInit( self.Trait().HPFraction )); + var health = self.TraitOrDefault(); + // 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) 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().SellSounds) - Sound.PlayToPlayer(self.Owner, s, self.CenterLocation); - - started = true; - } return this; } } diff --git a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj index 32f2c844bc..5980a0a4c9 100644 --- a/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj +++ b/OpenRA.Mods.RA/OpenRA.Mods.RA.csproj @@ -341,6 +341,7 @@ + diff --git a/OpenRA.Mods.RA/Transforms.cs b/OpenRA.Mods.RA/Transforms.cs index 930c83ba8a..5a64e7a92e 100644 --- a/OpenRA.Mods.RA/Transforms.cs +++ b/OpenRA.Mods.RA/Transforms.cs @@ -13,6 +13,7 @@ using OpenRA.Mods.RA.Activities; using OpenRA.Mods.RA.Buildings; using OpenRA.Mods.RA.Orders; using OpenRA.Traits; +using OpenRA.Mods.RA.Render; namespace OpenRA.Mods.RA { @@ -79,7 +80,10 @@ namespace OpenRA.Mods.RA if (self.HasTrait()) self.QueueActivity(new Turn(Info.Facing)); - self.QueueActivity(new Transform(self, Info.IntoActor, Info.Offset, Info.Facing, Info.TransformSounds)); + if (self.HasTrait() && self.Info.Traits.Get().HasMakeAnimation) + self.QueueActivity(new MakeAnimation(self, true)); + + self.QueueActivity(new Transform(self, Info.IntoActor) {Offset = Info.Offset, Facing = Info.Facing, Sounds = Info.TransformSounds}); } } }