some accessors

This commit is contained in:
Chris Forbes
2010-01-11 21:59:32 +13:00
parent e7a99541e5
commit 10a5b40e16
5 changed files with 34 additions and 15 deletions

View File

@@ -78,9 +78,9 @@ namespace OpenRa.Game
{ {
var info = self.Info.Traits.WithInterface<AttackBaseInfo>().First(); var info = self.Info.Traits.WithInterface<AttackBaseInfo>().First();
if (info.PrimaryWeapon != null && if (info.PrimaryWeapon != null &&
WeaponValidForTarget(Rules.WeaponInfo[info.PrimaryWeapon], target)) return true; WeaponValidForTarget(self.GetPrimaryWeapon(), target)) return true;
if (info.SecondaryWeapon != null && if (info.SecondaryWeapon != null &&
WeaponValidForTarget(Rules.WeaponInfo[info.SecondaryWeapon], target)) return true; WeaponValidForTarget(self.GetSecondaryWeapon(), target)) return true;
return false; return false;
} }

View File

@@ -2,6 +2,8 @@
using System.Windows.Forms; using System.Windows.Forms;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRa.Game.GameRules;
using OpenRa.Game.Traits;
namespace OpenRa.Game namespace OpenRa.Game
{ {
@@ -27,5 +29,27 @@ namespace OpenRa.Game
{ {
return xs.Aggregate(1f, (a, x) => a * x); return xs.Aggregate(1f, (a, x) => a * x);
} }
public static WeaponInfo GetPrimaryWeapon(this Actor self)
{
var info = self.Info.Traits.WithInterface<AttackBaseInfo>().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<AttackBaseInfo>().FirstOrDefault();
if (info == null) return null;
var weapon = info.SecondaryWeapon;
if (weapon == null) return null;
return Rules.WeaponInfo[weapon];
}
} }
} }

View File

@@ -30,7 +30,7 @@ namespace OpenRa.Game.Traits.Activities
return this; return this;
} }
var range = Rules.WeaponInfo[ self.Info.Traits.WithInterface<AttackBaseInfo>().First().PrimaryWeapon ].Range - 1; var range = self.GetPrimaryWeapon().Range - 1;
var dist = target.CenterLocation - self.CenterLocation; var dist = target.CenterLocation - self.CenterLocation;
var desiredFacing = Util.GetFacing(dist, unit.Facing); var desiredFacing = Util.GetFacing(dist, unit.Facing);

View File

@@ -38,10 +38,8 @@ namespace OpenRa.Game.Traits
public AttackBase(Actor self) public AttackBase(Actor self)
{ {
var info = self.Info.Traits.WithInterface<AttackBaseInfo>().First(); var primaryWeapon = self.GetPrimaryWeapon();
var secondaryWeapon = self.GetSecondaryWeapon();
var primaryWeapon = info.PrimaryWeapon != null ? Rules.WeaponInfo[info.PrimaryWeapon] : null;
var secondaryWeapon = info.SecondaryWeapon != null ? Rules.WeaponInfo[info.SecondaryWeapon] : null;
primaryBurst = primaryWeapon != null ? primaryWeapon.Burst : 1; primaryBurst = primaryWeapon != null ? primaryWeapon.Burst : 1;
secondaryBurst = secondaryWeapon != null ? secondaryWeapon.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 (mi.Button == MouseButton.Left || underCursor == null) return null;
if (self == underCursor) return null; if (self == underCursor) return null;
var info = self.Info.Traits.WithInterface<AttackBaseInfo>().First(); var isHeal = self.GetPrimaryWeapon().Damage < 0;
var isHeal = Rules.WeaponInfo[info.PrimaryWeapon].Damage < 0;
if (((underCursor.Owner == self.Owner) ^ isHeal) if (((underCursor.Owner == self.Owner) ^ isHeal)
&& !mi.Modifiers.HasModifier( Modifiers.Ctrl )) return null; && !mi.Modifiers.HasModifier( Modifiers.Ctrl )) return null;
@@ -207,13 +204,12 @@ namespace OpenRa.Game.Traits
protected virtual void QueueAttack(Actor self, Order order) protected virtual void QueueAttack(Actor self, Order order)
{ {
var info = self.Info.Traits.WithInterface<AttackBaseInfo>().First();
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */ 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 */ /* 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, self.QueueActivity(new Activities.Attack(order.TargetActor,
Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance))); Math.Max(0, (int)weapon.Range - RangeTolerance)));
} }
} }
} }

View File

@@ -146,9 +146,8 @@ namespace OpenRa.Game.Traits
public static float GetMaximumRange(Actor self) public static float GetMaximumRange(Actor self)
{ {
var info = self.Info.Traits.WithInterface<AttackBaseInfo>().First(); var info = self.Info.Traits.WithInterface<AttackBaseInfo>().First();
return new[] { info.PrimaryWeapon, info.SecondaryWeapon } return new[] { self.GetPrimaryWeapon(), self.GetSecondaryWeapon() }
.Where(w => w != null) .Where(w => w != null).Max(w => w.Range);
.Max(w => Rules.WeaponInfo[w].Range);
} }
} }
} }