Move unwanted AttackBase logic to the individual Armaments.

This commit is contained in:
Paul Chote
2014-03-17 11:14:09 +13:00
parent b31240ccc5
commit 0ca7ee280f
7 changed files with 59 additions and 55 deletions

View File

@@ -47,13 +47,15 @@ namespace OpenRA.Mods.RA
public object Create(ActorInitializer init) { return new Armament(init.self, this); } public object Create(ActorInitializer init) { return new Armament(init.self, this); }
} }
public class Armament : ITick public class Armament : ITick, IExplodeModifier
{ {
public readonly ArmamentInfo Info; public readonly ArmamentInfo Info;
public readonly WeaponInfo Weapon; public readonly WeaponInfo Weapon;
public readonly Barrel[] Barrels; public readonly Barrel[] Barrels;
Lazy<Turreted> Turret; Lazy<Turreted> Turret;
Lazy<IBodyOrientation> Coords; Lazy<IBodyOrientation> Coords;
Lazy<LimitedAmmo> limitedAmmo;
List<Pair<int, Action>> delayedActions = new List<Pair<int, Action>>();
public WRange Recoil; public WRange Recoil;
public int FireDelay { get; private set; } public int FireDelay { get; private set; }
@@ -66,6 +68,7 @@ namespace OpenRA.Mods.RA
// We can't resolve these until runtime // We can't resolve these until runtime
Turret = Lazy.New(() => self.TraitsImplementing<Turreted>().FirstOrDefault(t => t.Name == info.Turret)); Turret = Lazy.New(() => self.TraitsImplementing<Turreted>().FirstOrDefault(t => t.Name == info.Turret));
Coords = Lazy.New(() => self.Trait<IBodyOrientation>()); Coords = Lazy.New(() => self.Trait<IBodyOrientation>());
limitedAmmo = Lazy.New(() => self.TraitOrDefault<LimitedAmmo>());
Weapon = Rules.Weapons[info.Weapon.ToLowerInvariant()]; Weapon = Rules.Weapons[info.Weapon.ToLowerInvariant()];
Burst = Weapon.Burst; Burst = Weapon.Burst;
@@ -94,17 +97,34 @@ namespace OpenRA.Mods.RA
if (FireDelay > 0) if (FireDelay > 0)
--FireDelay; --FireDelay;
Recoil = new WRange(Math.Max(0, Recoil.Range - Info.RecoilRecovery.Range)); Recoil = new WRange(Math.Max(0, Recoil.Range - Info.RecoilRecovery.Range));
for (var i = 0; i < delayedActions.Count; i++)
{
var x = delayedActions[i];
if (--x.First <= 0)
x.Second();
delayedActions[i] = x;
}
delayedActions.RemoveAll(a => a.First <= 0);
}
void ScheduleDelayedAction(int t, Action a)
{
if (t > 0)
delayedActions.Add(Pair.New(t, a));
else
a();
} }
// Note: facing is only used by the legacy positioning code // Note: facing is only used by the legacy positioning code
// The world coordinate model uses Actor.Orientation // The world coordinate model uses Actor.Orientation
public void CheckFire(Actor self, AttackBase attack, IFacing facing, Target target) public void CheckFire(Actor self, IFacing facing, Target target)
{ {
if (FireDelay > 0) if (FireDelay > 0)
return; return;
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>(); if (limitedAmmo.Value != null && !limitedAmmo.Value.HasAmmo())
if (limitedAmmo != null && !limitedAmmo.HasAmmo())
return; return;
if (!target.IsInRange(self.CenterPosition, Weapon.Range)) if (!target.IsInRange(self.CenterPosition, Weapon.Range))
@@ -134,7 +154,7 @@ namespace OpenRA.Mods.RA
GuidedTarget = target GuidedTarget = target
}; };
attack.ScheduleDelayedAction(Info.FireDelay, () => ScheduleDelayedAction(Info.FireDelay, () =>
{ {
if (args.Weapon.Projectile != null) if (args.Weapon.Projectile != null)
{ {
@@ -162,6 +182,7 @@ namespace OpenRA.Mods.RA
} }
public bool IsReloading { get { return FireDelay > 0; } } public bool IsReloading { get { return FireDelay > 0; } }
public bool ShouldExplode(Actor self) { return !IsReloading; }
public WVec MuzzleOffset(Actor self, Barrel b) public WVec MuzzleOffset(Actor self, Barrel b)
{ {

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.RA
public abstract object Create(ActorInitializer init); public abstract object Create(ActorInitializer init);
} }
public abstract class AttackBase : IIssueOrder, IResolveOrder, ITick, IExplodeModifier, IOrderVoice, ISync public abstract class AttackBase : IIssueOrder, IResolveOrder, IOrderVoice, ISync
{ {
[Sync] public bool IsAttacking { get; internal set; } [Sync] public bool IsAttacking { get; internal set; }
@@ -35,7 +35,6 @@ namespace OpenRA.Mods.RA
protected Lazy<IFacing> facing; protected Lazy<IFacing> facing;
Lazy<IEnumerable<Armament>> armaments; Lazy<IEnumerable<Armament>> armaments;
protected IEnumerable<Armament> Armaments { get { return armaments.Value; } } protected IEnumerable<Armament> Armaments { get { return armaments.Value; } }
List<Pair<int, Action>> delayedActions = new List<Pair<int, Action>>();
public AttackBase(Actor self, AttackBaseInfo info) public AttackBase(Actor self, AttackBaseInfo info)
{ {
@@ -63,38 +62,13 @@ namespace OpenRA.Mods.RA
return true; return true;
} }
public bool ShouldExplode(Actor self) { return !IsReloading(); }
public bool IsReloading() { return Armaments.Any(a => a.IsReloading); }
public virtual void Tick(Actor self)
{
for (var i = 0; i < delayedActions.Count; i++)
{
var x = delayedActions[i];
if (--x.First <= 0)
x.Second();
delayedActions[i] = x;
}
delayedActions.RemoveAll(a => a.First <= 0);
}
internal void ScheduleDelayedAction(int t, Action a)
{
if (t > 0)
delayedActions.Add(Pair.New(t, a));
else
a();
}
public virtual void DoAttack(Actor self, Target target) public virtual void DoAttack(Actor self, Target target)
{ {
if (!CanAttack(self, target)) if (!CanAttack(self, target))
return; return;
foreach (var a in Armaments) foreach (var a in Armaments)
a.CheckFire(self, this, facing.Value, target); a.CheckFire(self, facing.Value, target);
} }
public IEnumerable<IOrderTargeter> Orders public IEnumerable<IOrderTargeter> Orders

View File

@@ -41,12 +41,10 @@ namespace OpenRA.Mods.RA
charges = aci.MaxCharges; charges = aci.MaxCharges;
} }
public override void Tick(Actor self) public void Tick(Actor self)
{ {
if (--timeToRecharge <= 0) if (--timeToRecharge <= 0)
charges = aci.MaxCharges; charges = aci.MaxCharges;
base.Tick(self);
} }
public void Attacking(Actor self, Target target, Armament a, Barrel barrel) public void Attacking(Actor self, Target target, Armament a, Barrel barrel)

View File

@@ -36,7 +36,7 @@ namespace OpenRA.Mods.RA
var facing = self.TraitOrDefault<IFacing>(); var facing = self.TraitOrDefault<IFacing>();
foreach (var a in Armaments) foreach (var a in Armaments)
a.CheckFire(self, this, facing, target); a.CheckFire(self, facing, target);
if (target.Actor != null) if (target.Actor != null)
target.Actor.ChangeOwner(self.Owner); target.Actor.ChangeOwner(self.Owner);

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA
public override object Create(ActorInitializer init) { return new AttackTurreted(init.self, this); } public override object Create(ActorInitializer init) { return new AttackTurreted(init.self, this); }
} }
class AttackTurreted : AttackBase, INotifyBuildComplete, ISync class AttackTurreted : AttackBase, ITick, INotifyBuildComplete, ISync
{ {
public Target Target { get; protected set; } public Target Target { get; protected set; }
protected IEnumerable<Turreted> turrets; protected IEnumerable<Turreted> turrets;
@@ -53,9 +53,8 @@ namespace OpenRA.Mods.RA
return base.CanAttack(self, target); return base.CanAttack(self, target);
} }
public override void Tick(Actor self) public void Tick(Actor self)
{ {
base.Tick(self);
DoAttack(self, Target); DoAttack(self, Target);
IsAttacking = Target.IsValidFor(self); IsAttacking = Target.IsValidFor(self);
} }

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.RA
public override object Create(ActorInitializer init) { return new AttackBomber(init.self, this); } public override object Create(ActorInitializer init) { return new AttackBomber(init.self, this); }
} }
class AttackBomber : AttackBase, ISync, INotifyRemovedFromWorld class AttackBomber : AttackBase, ITick, ISync, INotifyRemovedFromWorld
{ {
AttackBomberInfo info; AttackBomberInfo info;
[Sync] Target target; [Sync] Target target;
@@ -43,10 +43,8 @@ namespace OpenRA.Mods.RA
this.info = info; this.info = info;
} }
public override void Tick(Actor self) public void Tick(Actor self)
{ {
base.Tick(self);
var cp = self.CenterPosition; var cp = self.CenterPosition;
var bombTarget = Target.FromPos(cp - new WVec(0, 0, cp.Z)); var bombTarget = Target.FromPos(cp - new WVec(0, 0, cp.Z));
var wasInAttackRange = inAttackRange; var wasInAttackRange = inAttackRange;
@@ -59,7 +57,7 @@ namespace OpenRA.Mods.RA
continue; continue;
inAttackRange = true; inAttackRange = true;
a.CheckFire(self, this, facing.Value, bombTarget); a.CheckFire(self, facing.Value, bombTarget);
} }
// Guns only fire when approaching the target // Guns only fire when approaching the target
@@ -74,7 +72,7 @@ namespace OpenRA.Mods.RA
var t = Target.FromPos(cp - new WVec(0, a.Weapon.Range.Range / 2, cp.Z).Rotate(WRot.FromFacing(f))); var t = Target.FromPos(cp - new WVec(0, a.Weapon.Range.Range / 2, cp.Z).Rotate(WRot.FromFacing(f)));
inAttackRange = true; inAttackRange = true;
a.CheckFire(self, this, facing.Value, t); a.CheckFire(self, facing.Value, t);
} }
} }

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * 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 * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -8,25 +8,39 @@
*/ */
#endregion #endregion
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render namespace OpenRA.Mods.RA.Render
{ {
class RenderUnitReloadInfo : RenderUnitInfo class RenderUnitReloadInfo : RenderUnitInfo, Requires<ArmamentInfo>, Requires<AttackBaseInfo>
{ {
public override object Create(ActorInitializer init) { return new RenderUnitReload(init.self); } [Desc("Armament name")]
public readonly string Armament = "primary";
public override object Create(ActorInitializer init) { return new RenderUnitReload(init.self, this); }
} }
class RenderUnitReload : RenderUnit class RenderUnitReload : RenderUnit
{ {
public RenderUnitReload(Actor self) readonly AttackBase attack;
: base(self) { } readonly Armament armament;
public RenderUnitReload(Actor self, RenderUnitReloadInfo info)
: base(self)
{
attack = self.Trait<AttackBase>();
armament = self.TraitsImplementing<Armament>()
.Single(a => a.Info.Name == info.Armament);
}
public override void Tick(Actor self) public override void Tick(Actor self)
{ {
var attack = self.TraitOrDefault<AttackBase>(); var sequence = (armament.IsReloading ? "empty-" : "") + (attack.IsAttacking ? "aim" : "idle");
if (sequence != anim.CurrentSequence.Name)
anim.ReplaceAnim(sequence);
if (attack != null)
anim.ReplaceAnim((attack.IsReloading() ? "empty-" : "")
+ (attack.IsAttacking ? "aim" : "idle"));
base.Tick(self); base.Tick(self);
} }
} }