Introduce Target.Type property.

This commit is contained in:
Paul Chote
2013-08-07 22:51:48 +12:00
parent fa517b8787
commit 4f3c9aa0af
18 changed files with 56 additions and 39 deletions

View File

@@ -14,18 +14,19 @@ using System.Linq;
namespace OpenRA.Traits
{
public enum TargetType { Invalid, Actor, Terrain }
public struct Target
{
public static readonly Target[] NoTargets = {};
public static readonly Target None = new Target();
public static readonly Target[] None = {};
public static readonly Target Invalid = new Target { type = TargetType.Invalid };
TargetType type;
Actor actor;
WPos pos;
bool valid;
int generation;
public static Target FromPos(WPos p) { return new Target { pos = p, valid = true }; }
public static Target FromCell(CPos c) { return new Target { pos = c.CenterPosition, valid = true }; }
public static Target FromPos(WPos p) { return new Target { pos = p, type = TargetType.Terrain }; }
public static Target FromCell(CPos c) { return new Target { pos = c.CenterPosition, type = TargetType.Terrain }; }
public static Target FromOrder(Order o)
{
return o.TargetActor != null
@@ -38,23 +39,39 @@ namespace OpenRA.Traits
return new Target
{
actor = a,
valid = (a != null),
type = a != null ? TargetType.Actor : TargetType.Invalid,
generation = a.Generation,
};
}
public bool IsValid { get { return valid && (actor == null || (actor.IsInWorld && !actor.IsDead() && actor.Generation == generation)); } }
public Actor Actor { get { return IsActor ? actor : null; } }
public bool IsValid { get { return Type != TargetType.Invalid; } }
public Actor Actor { get { return actor; } }
// TODO: This should return true even if the actor is destroyed
public bool IsActor { get { return actor != null && !actor.Destroyed; } }
public TargetType Type
{
get
{
if (type == TargetType.Actor)
{
// Actor is no longer in the world
if (!actor.IsInWorld || actor.IsDead())
return TargetType.Invalid;
// Actor generation has changed (teleported or captured)
if (actor.Generation != generation)
return TargetType.Invalid;
}
return type;
}
}
// Representative position - see Positions for the full set of targetable positions.
public WPos CenterPosition
{
get
{
if (!IsValid)
if (Type == TargetType.Invalid)
throw new InvalidOperationException("Attempting to query the position of an invalid Target");
return actor != null ? actor.CenterPosition : pos;
@@ -67,7 +84,7 @@ namespace OpenRA.Traits
{
get
{
if (!IsValid)
if (Type == TargetType.Invalid)
return NoPositions;
if (actor == null)
@@ -83,7 +100,7 @@ namespace OpenRA.Traits
public bool IsInRange(WPos origin, WRange range)
{
if (!IsValid)
if (Type == TargetType.Invalid)
return false;
// Target ranges are calculated in 2D, so ignore height differences