Implement IRenderInfantrySequenceModifier for ScaredyCat and TakeCover
This commit is contained in:
@@ -319,7 +319,6 @@
|
||||
<Compile Include="Render\RenderFlare.cs" />
|
||||
<Compile Include="Render\RenderHarvester.cs" />
|
||||
<Compile Include="Render\RenderInfantry.cs" />
|
||||
<Compile Include="Render\RenderInfantryPanic.cs" />
|
||||
<Compile Include="Render\RenderDisguise.cs" />
|
||||
<Compile Include="Render\RenderLandingCraft.cs" />
|
||||
<Compile Include="Render\RenderUnit.cs" />
|
||||
|
||||
@@ -12,12 +12,12 @@ using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA.Render
|
||||
{
|
||||
class RenderDisguiseInfo : RenderInfantryProneInfo, Requires<DisguiseInfo>
|
||||
class RenderDisguiseInfo : RenderInfantryInfo, Requires<DisguiseInfo>
|
||||
{
|
||||
public override object Create(ActorInitializer init) { return new RenderDisguise(init.self, this); }
|
||||
}
|
||||
|
||||
class RenderDisguise : RenderInfantryProne
|
||||
class RenderDisguise : RenderInfantry
|
||||
{
|
||||
RenderDisguiseInfo info;
|
||||
string intendedSprite;
|
||||
|
||||
@@ -50,48 +50,47 @@ namespace OpenRA.Mods.RA.Render
|
||||
|
||||
public class RenderInfantry : RenderSimple, INotifyAttack, INotifyKilled, INotifyIdle
|
||||
{
|
||||
public enum AnimationState
|
||||
{
|
||||
Idle,
|
||||
Attacking,
|
||||
Moving,
|
||||
Waiting,
|
||||
IdleAnimating
|
||||
}
|
||||
|
||||
IMove move;
|
||||
RenderInfantryInfo info;
|
||||
public bool IsMoving { get; set; }
|
||||
protected bool dirty = false;
|
||||
readonly RenderInfantryInfo info;
|
||||
readonly IMove move;
|
||||
bool dirty = false;
|
||||
string idleSequence;
|
||||
int idleDelay;
|
||||
AnimationState state;
|
||||
|
||||
protected virtual string NormalizeInfantrySequence(Actor self, string baseSequence)
|
||||
{
|
||||
return baseSequence;
|
||||
}
|
||||
|
||||
protected virtual bool AllowIdleAnimation(Actor self)
|
||||
{
|
||||
return info.IdleAnimations.Length > 0;
|
||||
}
|
||||
|
||||
public AnimationState State { get; private set; }
|
||||
IRenderInfantrySequenceModifier rsm;
|
||||
bool isModifyingSequence { get { return rsm != null && rsm.IsModifyingSequence; } }
|
||||
bool wasModifying;
|
||||
|
||||
public RenderInfantry(Actor self, RenderInfantryInfo info)
|
||||
: base(self, MakeFacingFunc(self))
|
||||
{
|
||||
this.info = info;
|
||||
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0);
|
||||
State = AnimationState.Waiting;
|
||||
state = AnimationState.Waiting;
|
||||
move = self.Trait<IMove>();
|
||||
rsm = self.TraitOrDefault<IRenderInfantrySequenceModifier>();
|
||||
}
|
||||
|
||||
protected virtual string NormalizeInfantrySequence(Actor self, string baseSequence)
|
||||
{
|
||||
var prefix = isModifyingSequence ? rsm.SequencePrefix : "";
|
||||
|
||||
if (DefaultAnimation.HasSequence(prefix + baseSequence))
|
||||
return prefix + baseSequence;
|
||||
else
|
||||
return baseSequence;
|
||||
}
|
||||
|
||||
protected virtual bool AllowIdleAnimation(Actor self)
|
||||
{
|
||||
return info.IdleAnimations.Length > 0 && !isModifyingSequence;
|
||||
}
|
||||
|
||||
public void Attacking(Actor self, Target target)
|
||||
{
|
||||
State = AnimationState.Attacking;
|
||||
state = AnimationState.Attacking;
|
||||
if (DefaultAnimation.HasSequence(NormalizeInfantrySequence(self, info.AttackAnimation)))
|
||||
DefaultAnimation.PlayThen(NormalizeInfantrySequence(self, info.AttackAnimation), () => State = AnimationState.Idle);
|
||||
DefaultAnimation.PlayThen(NormalizeInfantrySequence(self, info.AttackAnimation), () => state = AnimationState.Idle);
|
||||
}
|
||||
|
||||
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
|
||||
@@ -103,14 +102,22 @@ namespace OpenRA.Mods.RA.Render
|
||||
{
|
||||
base.Tick(self);
|
||||
|
||||
if ((State == AnimationState.Moving || dirty) && !move.IsMoving)
|
||||
if (rsm != null)
|
||||
{
|
||||
State = AnimationState.Waiting;
|
||||
if (wasModifying != rsm.IsModifyingSequence)
|
||||
dirty = true;
|
||||
|
||||
wasModifying = rsm.IsModifyingSequence;
|
||||
}
|
||||
|
||||
if ((state == AnimationState.Moving || dirty) && !move.IsMoving)
|
||||
{
|
||||
state = AnimationState.Waiting;
|
||||
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0);
|
||||
}
|
||||
else if ((State != AnimationState.Moving || dirty) && move.IsMoving)
|
||||
else if ((state != AnimationState.Moving || dirty) && move.IsMoving)
|
||||
{
|
||||
State = AnimationState.Moving;
|
||||
state = AnimationState.Moving;
|
||||
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.MoveAnimation));
|
||||
}
|
||||
|
||||
@@ -119,10 +126,10 @@ namespace OpenRA.Mods.RA.Render
|
||||
|
||||
public void TickIdle(Actor self)
|
||||
{
|
||||
if (State != AnimationState.Idle && State != AnimationState.IdleAnimating)
|
||||
if (state != AnimationState.Idle && state != AnimationState.IdleAnimating)
|
||||
{
|
||||
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0);
|
||||
State = AnimationState.Idle;
|
||||
state = AnimationState.Idle;
|
||||
|
||||
if (info.IdleAnimations.Length > 0)
|
||||
{
|
||||
@@ -134,11 +141,11 @@ namespace OpenRA.Mods.RA.Render
|
||||
{
|
||||
if (DefaultAnimation.HasSequence(idleSequence))
|
||||
{
|
||||
State = AnimationState.IdleAnimating;
|
||||
state = AnimationState.IdleAnimating;
|
||||
DefaultAnimation.PlayThen(idleSequence, () =>
|
||||
{
|
||||
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)));
|
||||
State = AnimationState.Waiting;
|
||||
state = AnimationState.Waiting;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -166,5 +173,14 @@ namespace OpenRA.Mods.RA.Render
|
||||
sequence, info.PlayerPalette + self.Owner.InternalName));
|
||||
});
|
||||
}
|
||||
|
||||
enum AnimationState
|
||||
{
|
||||
Idle,
|
||||
Attacking,
|
||||
Moving,
|
||||
Waiting,
|
||||
IdleAnimating
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2014 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.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA.Render
|
||||
{
|
||||
class RenderInfantryPanicInfo : RenderInfantryInfo, Requires<ScaredyCatInfo>
|
||||
{
|
||||
public override object Create(ActorInitializer init) { return new RenderInfantryPanic(init.self, this); }
|
||||
}
|
||||
|
||||
class RenderInfantryPanic : RenderInfantry
|
||||
{
|
||||
readonly ScaredyCat sc;
|
||||
bool wasPanic;
|
||||
|
||||
public RenderInfantryPanic(Actor self, RenderInfantryPanicInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
sc = self.Trait<ScaredyCat>();
|
||||
}
|
||||
|
||||
protected override string NormalizeInfantrySequence(Actor self, string baseSequence)
|
||||
{
|
||||
var prefix = sc != null && sc.Panicking ? "panic-" : "";
|
||||
|
||||
if (DefaultAnimation.HasSequence(prefix + baseSequence))
|
||||
return prefix + baseSequence;
|
||||
else
|
||||
return baseSequence;
|
||||
}
|
||||
|
||||
protected override bool AllowIdleAnimation(Actor self)
|
||||
{
|
||||
return base.AllowIdleAnimation(self) && !sc.Panicking;
|
||||
}
|
||||
|
||||
public override void Tick (Actor self)
|
||||
{
|
||||
if (wasPanic != sc.Panicking)
|
||||
dirty = true;
|
||||
|
||||
wasPanic = sc.Panicking;
|
||||
base.Tick(self);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,48 +16,58 @@ namespace OpenRA.Mods.RA
|
||||
[Desc("Makes the unit automatically run around when taking damage.")]
|
||||
class ScaredyCatInfo : ITraitInfo
|
||||
{
|
||||
[Desc("How long (in ticks) the actor should panic for.")]
|
||||
public readonly int PanicLength = 25 * 10;
|
||||
|
||||
[Desc("Panic movement speed as a precentage of the normal speed.")]
|
||||
public readonly int PanicSpeedModifier = 200;
|
||||
|
||||
[Desc("Chance (out of 100) the unit has to enter panic mode when attacked.")]
|
||||
public readonly int AttackPanicChance = 20;
|
||||
|
||||
public object Create(ActorInitializer init) { return new ScaredyCat(init.self, this); }
|
||||
}
|
||||
|
||||
class ScaredyCat : ITick, INotifyIdle, INotifyDamage, INotifyAttack, ISpeedModifier, ISync
|
||||
class ScaredyCat : ITick, INotifyIdle, INotifyDamage, INotifyAttack, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
|
||||
{
|
||||
readonly ScaredyCatInfo Info;
|
||||
[Sync] readonly Actor Self;
|
||||
readonly ScaredyCatInfo info;
|
||||
[Sync] readonly Actor self;
|
||||
[Sync] int panicStartedTick;
|
||||
bool panicking { get { return panicStartedTick > 0; } }
|
||||
|
||||
[Sync] public int PanicStartedTick;
|
||||
[Sync] public bool Panicking { get { return PanicStartedTick > 0; } }
|
||||
public bool IsModifyingSequence { get { return panicking; } }
|
||||
public string SequencePrefix { get { return "panic-"; } }
|
||||
|
||||
public ScaredyCat(Actor self, ScaredyCatInfo info)
|
||||
{
|
||||
Self = self;
|
||||
Info = info;
|
||||
this.self = self;
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public void Panic()
|
||||
{
|
||||
if (!Panicking)
|
||||
Self.CancelActivity();
|
||||
PanicStartedTick = Self.World.WorldTick;
|
||||
if (!panicking)
|
||||
self.CancelActivity();
|
||||
|
||||
panicStartedTick = self.World.WorldTick;
|
||||
}
|
||||
|
||||
public void Tick(Actor self)
|
||||
{
|
||||
if (!Panicking) return;
|
||||
if (!panicking)
|
||||
return;
|
||||
|
||||
if (self.World.WorldTick >= PanicStartedTick + Info.PanicLength)
|
||||
if (self.World.WorldTick >= panicStartedTick + info.PanicLength)
|
||||
{
|
||||
self.CancelActivity();
|
||||
PanicStartedTick = 0;
|
||||
panicStartedTick = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void TickIdle(Actor self)
|
||||
{
|
||||
if (!Panicking) return;
|
||||
if (!panicking)
|
||||
return;
|
||||
|
||||
self.Trait<Mobile>().Nudge(self, self, true);
|
||||
}
|
||||
@@ -70,13 +80,13 @@ namespace OpenRA.Mods.RA
|
||||
|
||||
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
|
||||
{
|
||||
if (self.World.SharedRandom.Next(100 / Info.AttackPanicChance) == 0)
|
||||
if (self.World.SharedRandom.Next(100 / info.AttackPanicChance) == 0)
|
||||
Panic();
|
||||
}
|
||||
|
||||
public int GetSpeedModifier()
|
||||
{
|
||||
return Panicking ? Info.PanicSpeedModifier : 100;
|
||||
return panicking ? info.PanicSpeedModifier : 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,14 @@
|
||||
#endregion
|
||||
|
||||
using OpenRA.GameRules;
|
||||
using OpenRA.Mods.RA.Render;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA
|
||||
{
|
||||
[Desc("This actor goes prone in an attempt to reduce damage.")]
|
||||
[Desc("Make the unit go prone when under attack, in an attempt to reduce damage.")]
|
||||
public class TakeCoverInfo : TurretedInfo
|
||||
{
|
||||
[Desc("How long should we remain in the prone position?" +
|
||||
"Measured in game ticks. Default is 4 seconds.")]
|
||||
[Desc("How long (in ticks) the actor remains prone.")]
|
||||
public readonly int ProneTime = 100;
|
||||
|
||||
[Desc("Prone movement speed as a percentage of the normal speed.")]
|
||||
@@ -29,87 +27,48 @@ namespace OpenRA.Mods.RA
|
||||
public override object Create(ActorInitializer init) { return new TakeCover(init, this); }
|
||||
}
|
||||
|
||||
// Infantry prone behavior
|
||||
public class TakeCover : Turreted, ITick, INotifyDamage, IDamageModifier, ISpeedModifier, ISync
|
||||
public class TakeCover : Turreted, INotifyDamage, IDamageModifier, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
|
||||
{
|
||||
TakeCoverInfo Info;
|
||||
readonly TakeCoverInfo info;
|
||||
[Sync] int remainingProneTime = 0;
|
||||
bool isProne { get { return remainingProneTime > 0; } }
|
||||
|
||||
public bool IsModifyingSequence { get { return isProne; } }
|
||||
public string SequencePrefix { get { return "prone-"; } }
|
||||
|
||||
public TakeCover(ActorInitializer init, TakeCoverInfo info)
|
||||
: base(init, info)
|
||||
{
|
||||
Info = info;
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public bool IsProne { get { return remainingProneTime > 0; } }
|
||||
|
||||
public void Damaged(Actor self, AttackInfo e)
|
||||
{
|
||||
if (e.Damage > 0 && (e.Warhead == null || !e.Warhead.PreventProne)) /* Don't go prone when healed */
|
||||
{
|
||||
if (!IsProne)
|
||||
LocalOffset = Info.ProneOffset;
|
||||
if (!isProne)
|
||||
LocalOffset = info.ProneOffset;
|
||||
|
||||
remainingProneTime = Info.ProneTime;
|
||||
remainingProneTime = info.ProneTime;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Tick(Actor self)
|
||||
{
|
||||
base.Tick(self);
|
||||
if (IsProne && --remainingProneTime == 0)
|
||||
|
||||
if (isProne && --remainingProneTime == 0)
|
||||
LocalOffset = WVec.Zero;
|
||||
}
|
||||
|
||||
public int GetDamageModifier(Actor attacker, DamageWarhead warhead)
|
||||
{
|
||||
return IsProne && warhead != null ? warhead.ProneModifier : 100;
|
||||
return isProne && warhead != null ? warhead.ProneModifier : 100;
|
||||
}
|
||||
|
||||
public int GetSpeedModifier()
|
||||
{
|
||||
return IsProne ? Info.SpeedModifier : 100;
|
||||
}
|
||||
}
|
||||
|
||||
class RenderInfantryProneInfo : RenderInfantryInfo, Requires<TakeCoverInfo>
|
||||
{
|
||||
public override object Create(ActorInitializer init) { return new RenderInfantryProne(init.self, this); }
|
||||
}
|
||||
|
||||
class RenderInfantryProne : RenderInfantry
|
||||
{
|
||||
readonly TakeCover tc;
|
||||
bool wasProne;
|
||||
|
||||
public RenderInfantryProne(Actor self, RenderInfantryProneInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
tc = self.Trait<TakeCover>();
|
||||
}
|
||||
|
||||
protected override string NormalizeInfantrySequence(Actor self, string baseSequence)
|
||||
{
|
||||
var prefix = tc != null && tc.IsProne ? "prone-" : "";
|
||||
|
||||
if (DefaultAnimation.HasSequence(prefix + baseSequence))
|
||||
return prefix + baseSequence;
|
||||
else
|
||||
return baseSequence;
|
||||
}
|
||||
|
||||
protected override bool AllowIdleAnimation(Actor self)
|
||||
{
|
||||
return base.AllowIdleAnimation(self) && !tc.IsProne;
|
||||
}
|
||||
|
||||
public override void Tick(Actor self)
|
||||
{
|
||||
if (wasProne != tc.IsProne)
|
||||
dirty = true;
|
||||
|
||||
wasProne = tc.IsProne;
|
||||
base.Tick(self);
|
||||
return isProne ? info.SpeedModifier : 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user