Merge pull request #11428 from reaperrr/attack-delay

Allow attack anims/overlays to start before weapons fire
This commit is contained in:
abcdefg30
2016-07-03 18:58:07 +02:00
committed by GitHub
16 changed files with 193 additions and 23 deletions

View File

@@ -28,6 +28,12 @@ namespace OpenRA.Mods.Common.Traits.Render
[Desc("Shown while reloading.")]
[SequenceReference(null, true)] public readonly string ReloadPrefix = null;
[Desc("Delay in ticks before animation starts, either relative to attack preparation or attack.")]
public readonly int Delay = 0;
[Desc("Should the animation be delayed relative to preparation or actual attack?")]
public readonly AttackDelayType DelayRelativeTo = AttackDelayType.Preparation;
public object Create(ActorInitializer init) { return new WithAttackAnimation(init, this); }
}
@@ -38,6 +44,8 @@ namespace OpenRA.Mods.Common.Traits.Render
readonly Armament armament;
readonly WithSpriteBody wsb;
int tick;
public WithAttackAnimation(ActorInitializer init, WithAttackAnimationInfo info)
{
this.info = info;
@@ -47,14 +55,39 @@ namespace OpenRA.Mods.Common.Traits.Render
wsb = init.Self.Trait<WithSpriteBody>();
}
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
void PlayAttackAnimation(Actor self)
{
if (!string.IsNullOrEmpty(info.AttackSequence))
wsb.PlayCustomAnimation(self, info.AttackSequence, () => wsb.CancelCustomAnimation(self));
}
public void Tick(Actor self)
void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
if (info.DelayRelativeTo == AttackDelayType.Attack)
{
if (info.Delay > 0)
tick = info.Delay;
else
PlayAttackAnimation(self);
}
}
void INotifyAttack.PreparingAttack(Actor self, Target target, Armament a, Barrel barrel)
{
if (info.DelayRelativeTo == AttackDelayType.Preparation)
{
if (info.Delay > 0)
tick = info.Delay;
else
PlayAttackAnimation(self);
}
}
void ITick.Tick(Actor self)
{
if (info.Delay > 0 && --tick == 0)
PlayAttackAnimation(self);
if (string.IsNullOrEmpty(info.AimSequence) && string.IsNullOrEmpty(info.ReloadPrefix))
return;

View File

@@ -0,0 +1,94 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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 OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
[Desc("Rendered together with an attack.")]
public class WithAttackOverlayInfo : ITraitInfo, Requires<RenderSpritesInfo>
{
[FieldLoader.Require]
[Desc("Sequence name to use")]
[SequenceReference] public readonly string Sequence = null;
[Desc("Custom palette name")]
[PaletteReference("IsPlayerPalette")] public readonly string Palette = null;
[Desc("Custom palette is a player palette BaseName")]
public readonly bool IsPlayerPalette = false;
[Desc("Delay in ticks before overlay starts, either relative to attack preparation or attack.")]
public readonly int Delay = 0;
[Desc("Should the overlay be delayed relative to preparation or actual attack?")]
public readonly AttackDelayType DelayRelativeTo = AttackDelayType.Preparation;
public object Create(ActorInitializer init) { return new WithAttackOverlay(init, this); }
}
public class WithAttackOverlay : INotifyAttack, ITick
{
readonly Animation overlay;
readonly RenderSprites renderSprites;
readonly WithAttackOverlayInfo info;
bool attacking;
int tick;
public WithAttackOverlay(ActorInitializer init, WithAttackOverlayInfo info)
{
this.info = info;
renderSprites = init.Self.Trait<RenderSprites>();
overlay = new Animation(init.World, renderSprites.GetImage(init.Self));
renderSprites.Add(new AnimationWithOffset(overlay, null, () => !attacking),
info.Palette, info.IsPlayerPalette);
}
void PlayOverlay(Actor self)
{
attacking = true;
overlay.PlayThen(info.Sequence, () => attacking = false);
}
void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
if (info.DelayRelativeTo == AttackDelayType.Attack)
{
if (info.Delay > 0)
tick = info.Delay;
else
PlayOverlay(self);
}
}
void INotifyAttack.PreparingAttack(Actor self, Target target, Armament a, Barrel barrel)
{
if (info.DelayRelativeTo == AttackDelayType.Preparation)
{
if (info.Delay > 0)
tick = info.Delay;
else
PlayOverlay(self);
}
}
void ITick.Tick(Actor self)
{
if (info.Delay > 0 && --tick == 0)
PlayOverlay(self);
}
}
}

View File

@@ -105,11 +105,13 @@ namespace OpenRA.Mods.Common.Traits.Render
}
}
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
void INotifyAttack.PreparingAttack(Actor self, Target target, Armament a, Barrel barrel)
{
Attacking(self, target);
}
void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel) { }
public virtual void Tick(Actor self)
{
if (rsm != null)

View File

@@ -75,7 +75,7 @@ namespace OpenRA.Mods.Common.Traits.Render
}
}
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
if (a == null)
return;
@@ -94,6 +94,8 @@ namespace OpenRA.Mods.Common.Traits.Render
anims[barrel].Animation.PlayThen(sequence, () => visible[barrel] = false);
}
void INotifyAttack.PreparingAttack(Actor self, Target target, Armament a, Barrel barrel) { }
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{
foreach (var arm in armaments)