From 10a5b40e1652b4df1b0304a4be1e29617d249939 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Mon, 11 Jan 2010 21:59:32 +1300 Subject: [PATCH] some accessors --- OpenRa.Game/Combat.cs | 4 ++-- OpenRa.Game/Exts.cs | 24 +++++++++++++++++++++ OpenRa.Game/Traits/Activities/HeliAttack.cs | 2 +- OpenRa.Game/Traits/AttackBase.cs | 14 +++++------- OpenRa.Game/Traits/Util.cs | 5 ++--- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/OpenRa.Game/Combat.cs b/OpenRa.Game/Combat.cs index eaa282844b..241f6c9852 100644 --- a/OpenRa.Game/Combat.cs +++ b/OpenRa.Game/Combat.cs @@ -78,9 +78,9 @@ namespace OpenRa.Game { var info = self.Info.Traits.WithInterface().First(); if (info.PrimaryWeapon != null && - WeaponValidForTarget(Rules.WeaponInfo[info.PrimaryWeapon], target)) return true; + WeaponValidForTarget(self.GetPrimaryWeapon(), target)) return true; if (info.SecondaryWeapon != null && - WeaponValidForTarget(Rules.WeaponInfo[info.SecondaryWeapon], target)) return true; + WeaponValidForTarget(self.GetSecondaryWeapon(), target)) return true; return false; } diff --git a/OpenRa.Game/Exts.cs b/OpenRa.Game/Exts.cs index 3de1b767a5..ae908bedc1 100644 --- a/OpenRa.Game/Exts.cs +++ b/OpenRa.Game/Exts.cs @@ -2,6 +2,8 @@ using System.Windows.Forms; using System.Collections.Generic; using System.Linq; +using OpenRa.Game.GameRules; +using OpenRa.Game.Traits; namespace OpenRa.Game { @@ -27,5 +29,27 @@ namespace OpenRa.Game { return xs.Aggregate(1f, (a, x) => a * x); } + + public static WeaponInfo GetPrimaryWeapon(this Actor self) + { + var info = self.Info.Traits.WithInterface().FirstOrDefault(); + if (info == null) return null; + + var weapon = info.PrimaryWeapon; + if (weapon == null) return null; + + return Rules.WeaponInfo[weapon]; + } + + public static WeaponInfo GetSecondaryWeapon(this Actor self) + { + var info = self.Info.Traits.WithInterface().FirstOrDefault(); + if (info == null) return null; + + var weapon = info.SecondaryWeapon; + if (weapon == null) return null; + + return Rules.WeaponInfo[weapon]; + } } } diff --git a/OpenRa.Game/Traits/Activities/HeliAttack.cs b/OpenRa.Game/Traits/Activities/HeliAttack.cs index 42574e4a80..71b27ab22f 100644 --- a/OpenRa.Game/Traits/Activities/HeliAttack.cs +++ b/OpenRa.Game/Traits/Activities/HeliAttack.cs @@ -30,7 +30,7 @@ namespace OpenRa.Game.Traits.Activities return this; } - var range = Rules.WeaponInfo[ self.Info.Traits.WithInterface().First().PrimaryWeapon ].Range - 1; + var range = self.GetPrimaryWeapon().Range - 1; var dist = target.CenterLocation - self.CenterLocation; var desiredFacing = Util.GetFacing(dist, unit.Facing); diff --git a/OpenRa.Game/Traits/AttackBase.cs b/OpenRa.Game/Traits/AttackBase.cs index b989b967e4..725ef2f753 100644 --- a/OpenRa.Game/Traits/AttackBase.cs +++ b/OpenRa.Game/Traits/AttackBase.cs @@ -38,10 +38,8 @@ namespace OpenRa.Game.Traits public AttackBase(Actor self) { - var info = self.Info.Traits.WithInterface().First(); - - var primaryWeapon = info.PrimaryWeapon != null ? Rules.WeaponInfo[info.PrimaryWeapon] : null; - var secondaryWeapon = info.SecondaryWeapon != null ? Rules.WeaponInfo[info.SecondaryWeapon] : null; + var primaryWeapon = self.GetPrimaryWeapon(); + var secondaryWeapon = self.GetSecondaryWeapon(); primaryBurst = primaryWeapon != null ? primaryWeapon.Burst : 1; secondaryBurst = secondaryWeapon != null ? secondaryWeapon.Burst : 1; @@ -181,8 +179,7 @@ namespace OpenRa.Game.Traits if (mi.Button == MouseButton.Left || underCursor == null) return null; if (self == underCursor) return null; - var info = self.Info.Traits.WithInterface().First(); - var isHeal = Rules.WeaponInfo[info.PrimaryWeapon].Damage < 0; + var isHeal = self.GetPrimaryWeapon().Damage < 0; if (((underCursor.Owner == self.Owner) ^ isHeal) && !mi.Modifiers.HasModifier( Modifiers.Ctrl )) return null; @@ -207,13 +204,12 @@ namespace OpenRa.Game.Traits protected virtual void QueueAttack(Actor self, Order order) { - var info = self.Info.Traits.WithInterface().First(); const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */ /* todo: choose the appropriate weapon, when only one works against this target */ - var weapon = info.PrimaryWeapon ?? info.SecondaryWeapon; + var weapon = self.GetPrimaryWeapon() ?? self.GetSecondaryWeapon(); self.QueueActivity(new Activities.Attack(order.TargetActor, - Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance))); + Math.Max(0, (int)weapon.Range - RangeTolerance))); } } } diff --git a/OpenRa.Game/Traits/Util.cs b/OpenRa.Game/Traits/Util.cs index bef22a08b9..9b8345796c 100755 --- a/OpenRa.Game/Traits/Util.cs +++ b/OpenRa.Game/Traits/Util.cs @@ -146,9 +146,8 @@ namespace OpenRa.Game.Traits public static float GetMaximumRange(Actor self) { var info = self.Info.Traits.WithInterface().First(); - return new[] { info.PrimaryWeapon, info.SecondaryWeapon } - .Where(w => w != null) - .Max(w => Rules.WeaponInfo[w].Range); + return new[] { self.GetPrimaryWeapon(), self.GetSecondaryWeapon() } + .Where(w => w != null).Max(w => w.Range); } } }