Skip to content

Instantly share code, notes, and snippets.

@MustaphaTR
Created September 21, 2020 16:08
Show Gist options
  • Save MustaphaTR/f58c2703b098a26c742e3a1b2d10b3ff to your computer and use it in GitHub Desktop.
Save MustaphaTR/f58c2703b098a26c742e3a1b2d10b3ff to your computer and use it in GitHub Desktop.
#region Copyright & License Information
/*
* Copyright 2007-2020 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
[Desc("Replaces the default animation when actor resupplies a unit.")]
public class WithResupplyAnimationInfo : ConditionalTraitInfo, Requires<WithSpriteBodyInfo>
{
[SequenceReference]
[Desc("Sequence to use upon resupplying beginning.")]
public readonly string StartSequence = null;
[SequenceReference]
[Desc("Sequence name to play once during repair intervals or repeatedly if a start sequence is set.")]
public readonly string Sequence = "active";
[SequenceReference("Image")]
[Desc("Sequence to use after resupplying has finished.")]
public readonly string EndSequence = null;
[Desc("Which sprite body to play the animation on.")]
public readonly string Body = "body";
[Desc("Events leading to the animation getting played. Possible values currently are: Rearm, Repair.")]
public readonly ResupplyType PlayAnimationOn = ResupplyType.Rearm | ResupplyType.Repair;
public override object Create(ActorInitializer init) { return new WithResupplyAnimation(init.Self, this); }
}
public class WithResupplyAnimation : ConditionalTrait<WithResupplyAnimationInfo>, INotifyResupply, ITick
{
readonly WithSpriteBody wsb;
bool animPlaying;
bool repairing;
bool rearming;
public WithResupplyAnimation(Actor self, WithResupplyAnimationInfo info)
: base(info)
{
wsb = self.TraitsImplementing<WithSpriteBody>().Single(w => w.Info.Name == Info.Body);
}
void ITick.Tick(Actor self)
{
if (IsTraitDisabled)
return;
if (!animPlaying
&& ((repairing && Info.PlayAnimationOn.HasFlag(ResupplyType.Repair))
|| (rearming && Info.PlayAnimationOn.HasFlag(ResupplyType.Rearm))))
{
wsb.PlayCustomAnimationRepeating(self, Info.Sequence);
animPlaying = true;
}
else if (animPlaying
&& (!repairing || !Info.PlayAnimationOn.HasFlag(ResupplyType.Repair))
&& (!rearming || !Info.PlayAnimationOn.HasFlag(ResupplyType.Rearm)))
{
if (Info.EndSequence != null)
{
wsb.PlayCustomAnimation(self, Info.EndSequence, () => animPlaying = false);
}
else
{
wsb.CancelCustomAnimation(self);
animPlaying = false;
}
}
// If an actor died before finishing resupply, there will never be a ResupplyTick call
// with ResupplyType.None (as activities tick before ITick), so reset here every tick
// to prevent the animation from continuing after the resupplied actor died.
repairing = false;
rearming = false;
}
void INotifyResupply.BeforeResupply(Actor self, Actor target, ResupplyType types)
{
repairing = types.HasFlag(ResupplyType.Repair);
rearming = types.HasFlag(ResupplyType.Rearm);
if (Info.StartSequence != null)
{
animPlaying = true;
wsb.PlayCustomAnimation(self, Info.StartSequence,
() => wsb.PlayCustomAnimationRepeating(self, Info.Sequence));
}
}
void INotifyResupply.ResupplyTick(Actor self, Actor target, ResupplyType types)
{
repairing = types.HasFlag(ResupplyType.Repair);
rearming = types.HasFlag(ResupplyType.Rearm);
}
protected override void TraitDisabled(Actor self)
{
// Cancel immediately instead of waiting for the next tick
repairing = rearming = animPlaying = false;
wsb.CancelCustomAnimation(self);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment