Move MuzzleFlash definition onto Armament.

This commit is contained in:
Paul Chote
2014-03-17 21:29:36 +13:00
parent 11d4567b5d
commit d7d00fffef
2 changed files with 33 additions and 38 deletions

View File

@@ -44,6 +44,12 @@ namespace OpenRA.Mods.RA
[Desc("Recoil recovery per-frame")] [Desc("Recoil recovery per-frame")]
public readonly WRange RecoilRecovery = new WRange(9); public readonly WRange RecoilRecovery = new WRange(9);
[Desc("Muzzle flash sequence to render")]
public readonly string MuzzleSequence = null;
[Desc("Use multiple muzzle images if non-zero")]
public readonly int MuzzleSplitFacings = 0;
public object Create(ActorInitializer init) { return new Armament(init.self, this); } public object Create(ActorInitializer init) { return new Armament(init.self, this); }
} }

View File

@@ -19,67 +19,56 @@ namespace OpenRA.Mods.RA.Render
{ {
class WithMuzzleFlashInfo : ITraitInfo, Requires<RenderSpritesInfo>, Requires<AttackBaseInfo>, Requires<ArmamentInfo> class WithMuzzleFlashInfo : ITraitInfo, Requires<RenderSpritesInfo>, Requires<AttackBaseInfo>, Requires<ArmamentInfo>
{ {
[Desc("Sequence name to use")] public object Create(ActorInitializer init) { return new WithMuzzleFlash(init.self); }
public readonly string Sequence = "muzzle";
[Desc("Armament name")]
public readonly string Armament = "primary";
[Desc("Are the muzzle facings split into multiple shps?")]
public readonly bool SplitFacings = false;
[Desc("Number of separate facing images that are defined in the sequences.")]
public readonly int FacingCount = 8;
public object Create(ActorInitializer init) { return new WithMuzzleFlash(init.self, this); }
} }
class WithMuzzleFlash : INotifyAttack, IRender, ITick class WithMuzzleFlash : INotifyAttack, IRender, ITick
{ {
readonly WithMuzzleFlashInfo info;
Dictionary<Barrel, bool> visible = new Dictionary<Barrel, bool>(); Dictionary<Barrel, bool> visible = new Dictionary<Barrel, bool>();
Dictionary<Barrel, AnimationWithOffset> anims = new Dictionary<Barrel, AnimationWithOffset>(); Dictionary<Barrel, AnimationWithOffset> anims = new Dictionary<Barrel, AnimationWithOffset>();
Func<int> getFacing; Func<int> getFacing;
public WithMuzzleFlash(Actor self, WithMuzzleFlashInfo info) public WithMuzzleFlash(Actor self)
{ {
this.info = info;
var render = self.Trait<RenderSprites>(); var render = self.Trait<RenderSprites>();
var facing = self.TraitOrDefault<IFacing>(); var facing = self.TraitOrDefault<IFacing>();
var arm = self.TraitsImplementing<Armament>() foreach (var arm in self.TraitsImplementing<Armament>())
.Single(a => a.Info.Name == info.Armament);
foreach (var b in arm.Barrels)
{ {
var barrel = b; // Skip armaments that don't define muzzles
var turreted = self.TraitsImplementing<Turreted>() if (arm.Info.MuzzleSequence == null)
.FirstOrDefault(t => t.Name == arm.Info.Turret); continue;
getFacing = turreted != null ? () => turreted.turretFacing : foreach (var b in arm.Barrels)
facing != null ? (Func<int>)(() => facing.Facing) : () => 0; {
var barrel = b;
var turreted = self.TraitsImplementing<Turreted>()
.FirstOrDefault(t => t.Name == arm.Info.Turret);
var muzzleFlash = new Animation(render.GetImage(self), getFacing); getFacing = turreted != null ? () => turreted.turretFacing :
visible.Add(barrel, false); facing != null ? (Func<int>)(() => facing.Facing) : () => 0;
anims.Add(barrel,
new AnimationWithOffset(muzzleFlash, var muzzleFlash = new Animation(render.GetImage(self), getFacing);
() => arm.MuzzleOffset(self, barrel), visible.Add(barrel, false);
() => !visible[barrel], anims.Add(barrel,
p => WithTurret.ZOffsetFromCenter(self, p, 2))); new AnimationWithOffset(muzzleFlash,
() => arm.MuzzleOffset(self, barrel),
() => !visible[barrel],
p => WithTurret.ZOffsetFromCenter(self, p, 2)));
}
} }
} }
public void Attacking(Actor self, Target target, Armament a, Barrel barrel) public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
{ {
if (a.Info.Name != info.Armament) var sequence = a.Info.MuzzleSequence;
if (sequence == null)
return; return;
if (a.Info.MuzzleSplitFacings > 0)
sequence += Traits.Util.QuantizeFacing(getFacing(), a.Info.MuzzleSplitFacings).ToString();
visible[barrel] = true; visible[barrel] = true;
var sequence = info.Sequence;
if (info.SplitFacings)
sequence += Traits.Util.QuantizeFacing(getFacing(), info.FacingCount).ToString();
anims[barrel].Animation.PlayThen(sequence, () => visible[barrel] = false); anims[barrel].Animation.PlayThen(sequence, () => visible[barrel] = false);
} }