Add support for granting a condition while the make animation plays.
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
using OpenRA.Activities;
|
using OpenRA.Activities;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
|
||||||
@@ -20,36 +21,79 @@ namespace OpenRA.Mods.Common.Traits.Render
|
|||||||
[Desc("Sequence name to use")]
|
[Desc("Sequence name to use")]
|
||||||
[SequenceReference] public readonly string Sequence = "make";
|
[SequenceReference] public readonly string Sequence = "make";
|
||||||
|
|
||||||
|
[GrantedConditionReference]
|
||||||
|
[Desc("The condition to grant to self while the make animation is playing.")]
|
||||||
|
public readonly string Condition = null;
|
||||||
|
|
||||||
public object Create(ActorInitializer init) { return new WithMakeAnimation(init, this); }
|
public object Create(ActorInitializer init) { return new WithMakeAnimation(init, this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WithMakeAnimation
|
public class WithMakeAnimation : INotifyCreated
|
||||||
{
|
{
|
||||||
readonly WithMakeAnimationInfo info;
|
readonly WithMakeAnimationInfo info;
|
||||||
readonly WithSpriteBody wsb;
|
readonly WithSpriteBody wsb;
|
||||||
|
|
||||||
|
ConditionManager conditionManager;
|
||||||
|
int token = ConditionManager.InvalidConditionToken;
|
||||||
|
|
||||||
public WithMakeAnimation(ActorInitializer init, WithMakeAnimationInfo info)
|
public WithMakeAnimation(ActorInitializer init, WithMakeAnimationInfo info)
|
||||||
{
|
{
|
||||||
this.info = info;
|
this.info = info;
|
||||||
var self = init.Self;
|
var self = init.Self;
|
||||||
wsb = self.Trait<WithSpriteBody>();
|
wsb = self.Trait<WithSpriteBody>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void INotifyCreated.Created(Actor self)
|
||||||
|
{
|
||||||
|
conditionManager = self.TraitOrDefault<ConditionManager>();
|
||||||
var building = self.TraitOrDefault<Building>();
|
var building = self.TraitOrDefault<Building>();
|
||||||
if (building != null && !building.SkipMakeAnimation)
|
if (building != null && !building.SkipMakeAnimation)
|
||||||
|
Forward(self, () => building.NotifyBuildingComplete(self));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Forward(Actor self, Action onComplete)
|
||||||
|
{
|
||||||
|
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken)
|
||||||
|
token = conditionManager.GrantCondition(self, info.Condition);
|
||||||
|
|
||||||
|
wsb.PlayCustomAnimation(self, info.Sequence, () =>
|
||||||
{
|
{
|
||||||
wsb.PlayCustomAnimation(self, info.Sequence, () =>
|
if (token != ConditionManager.InvalidConditionToken)
|
||||||
{
|
token = conditionManager.RevokeCondition(self, token);
|
||||||
building.NotifyBuildingComplete(self);
|
|
||||||
});
|
// TODO: Rewrite this to use a trait notification for save game support
|
||||||
}
|
onComplete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reverse(Actor self, Action onComplete)
|
||||||
|
{
|
||||||
|
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition) && token == ConditionManager.InvalidConditionToken)
|
||||||
|
token = conditionManager.GrantCondition(self, info.Condition);
|
||||||
|
|
||||||
|
wsb.PlayCustomAnimationBackwards(self, info.Sequence, () =>
|
||||||
|
{
|
||||||
|
if (token != ConditionManager.InvalidConditionToken)
|
||||||
|
token = conditionManager.RevokeCondition(self, token);
|
||||||
|
|
||||||
|
// TODO: Rewrite this to use a trait notification for save game support
|
||||||
|
onComplete();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Reverse(Actor self, Activity activity, bool queued = true)
|
public void Reverse(Actor self, Activity activity, bool queued = true)
|
||||||
{
|
{
|
||||||
wsb.PlayCustomAnimationBackwards(self, info.Sequence, () =>
|
Reverse(self, () =>
|
||||||
{
|
{
|
||||||
// avoids visual glitches as we wait for the actor to get destroyed
|
// HACK: The actor remains alive and active for one tick before the followup activity
|
||||||
|
// (sell/transform/etc) runs. This causes visual glitches that we attempt to minimize
|
||||||
|
// by forcing the animation to frame 0 and regranting the make condition.
|
||||||
|
// These workarounds will break the actor if the followup activity doesn't dispose it!
|
||||||
wsb.DefaultAnimation.PlayFetchIndex(info.Sequence, () => 0);
|
wsb.DefaultAnimation.PlayFetchIndex(info.Sequence, () => 0);
|
||||||
|
|
||||||
|
if (conditionManager != null && !string.IsNullOrEmpty(info.Condition))
|
||||||
|
token = conditionManager.GrantCondition(self, info.Condition);
|
||||||
|
|
||||||
self.QueueActivity(queued, activity);
|
self.QueueActivity(queued, activity);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user