Add FrozenActor target type.

This commit is contained in:
Paul Chote
2013-08-08 14:34:49 +12:00
parent c0cb52a5ca
commit 40a9caddc7
5 changed files with 59 additions and 28 deletions

View File

@@ -149,11 +149,27 @@ namespace OpenRA.GameRules
return true; return true;
} }
public bool IsValidAgainst(FrozenActor a)
{
var targetable = a.Info.Traits.GetOrDefault<ITargetableInfo>();
if (targetable == null || !ValidTargets.Intersect(targetable.GetTargetTypes()).Any())
return false;
if (Warheads.All(w => w.EffectivenessAgainst(a.Info) <= 0))
return false;
return true;
}
public bool IsValidAgainst(Target target, World world) public bool IsValidAgainst(Target target, World world)
{ {
if (target.Type == TargetType.Actor) if (target.Type == TargetType.Actor)
return IsValidAgainst(target.Actor); return IsValidAgainst(target.Actor);
if (target.Type == TargetType.FrozenActor)
return IsValidAgainst(target.FrozenActor);
if (target.Type == TargetType.Terrain) if (target.Type == TargetType.Terrain)
{ {
var cell = target.CenterPosition.ToCPos(); var cell = target.CenterPosition.ToCPos();

View File

@@ -14,7 +14,7 @@ using System.Linq;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
public enum TargetType { Invalid, Actor, Terrain } public enum TargetType { Invalid, Actor, Terrain, FrozenActor }
public struct Target public struct Target
{ {
public static readonly Target[] None = {}; public static readonly Target[] None = {};
@@ -22,6 +22,7 @@ namespace OpenRA.Traits
TargetType type; TargetType type;
Actor actor; Actor actor;
FrozenActor frozen;
WPos pos; WPos pos;
int generation; int generation;
@@ -44,8 +45,11 @@ namespace OpenRA.Traits
}; };
} }
public static Target FromFrozenActor(FrozenActor a) { return new Target { frozen = a, type = TargetType.FrozenActor }; }
public bool IsValid { get { return Type != TargetType.Invalid; } } public bool IsValid { get { return Type != TargetType.Invalid; } }
public Actor Actor { get { return actor; } } public Actor Actor { get { return actor; } }
public FrozenActor FrozenActor { get { return frozen; } }
public TargetType Type public TargetType Type
{ {
@@ -71,10 +75,18 @@ namespace OpenRA.Traits
{ {
get get
{ {
if (Type == TargetType.Invalid) switch (Type)
{
case TargetType.Actor:
return actor.CenterPosition;
case TargetType.FrozenActor:
return frozen.CenterPosition;
case TargetType.Terrain:
return pos;
default:
case TargetType.Invalid:
throw new InvalidOperationException("Attempting to query the position of an invalid Target"); throw new InvalidOperationException("Attempting to query the position of an invalid Target");
}
return actor != null ? actor.CenterPosition : pos;
} }
} }
@@ -84,17 +96,23 @@ namespace OpenRA.Traits
{ {
get get
{ {
if (Type == TargetType.Invalid) switch (Type)
return NoPositions; {
case TargetType.Actor:
if (actor == null)
return new []{pos};
var targetable = actor.TraitOrDefault<ITargetable>(); var targetable = actor.TraitOrDefault<ITargetable>();
if (targetable == null) if (targetable == null)
return new []{actor.CenterPosition}; return new [] { actor.CenterPosition };
return targetable.TargetablePositions(actor); var positions = targetable.TargetablePositions(actor);
return positions.Any() ? positions : new [] { actor.CenterPosition };
case TargetType.FrozenActor:
return new [] { frozen.CenterPosition };
case TargetType.Terrain:
return new [] { pos };
default:
case TargetType.Invalid:
return NoPositions;
}
} }
} }

View File

@@ -134,13 +134,7 @@ namespace OpenRA.Traits
public static IEnumerable<CPos> AdjacentCells(Target target) public static IEnumerable<CPos> AdjacentCells(Target target)
{ {
var cells = target.Type == TargetType.Actor var cells = target.Positions.Select(p => p.ToCPos()).Distinct();
? target.Actor.OccupiesSpace.OccupiedCells().Select(c => c.First).ToArray()
: new CPos[] { };
if (cells.Length == 0)
cells = new CPos[] { target.CenterPosition.ToCPos() };
return Util.ExpandFootprint(cells, true); return Util.ExpandFootprint(cells, true);
} }
} }

View File

@@ -53,10 +53,11 @@ namespace OpenRA.Mods.RA.Activities
if (IsCanceled) if (IsCanceled)
return NextActivity; return NextActivity;
if (!Target.IsValid) var type = Target.Type;
if (type != TargetType.Actor && type != TargetType.Terrain)
return NextActivity; return NextActivity;
if (!self.Owner.HasFogVisibility() && Target.Actor != null && Target.Actor.HasTrait<Mobile>() && !self.Owner.Shroud.IsTargetable(Target.Actor)) if (type == TargetType.Actor && !self.Owner.HasFogVisibility() && Target.Actor.HasTrait<Mobile>() && !self.Owner.Shroud.IsTargetable(Target.Actor))
return NextActivity; return NextActivity;
if (targetable != null && !targetable.TargetableBy(Target.Actor, self)) if (targetable != null && !targetable.TargetableBy(Target.Actor, self))

View File

@@ -216,14 +216,16 @@ namespace OpenRA.Mods.RA
public bool CanTarget(Actor self, Target target, List<Actor> othersAtTarget, TargetModifiers modifiers, ref string cursor) public bool CanTarget(Actor self, Target target, List<Actor> othersAtTarget, TargetModifiers modifiers, ref string cursor)
{ {
if (target.Type == TargetType.Actor) switch (target.Type)
{
case TargetType.Actor:
return CanTargetActor(self, target.Actor, modifiers, ref cursor); return CanTargetActor(self, target.Actor, modifiers, ref cursor);
case TargetType.Terrain:
if (target.Type == TargetType.Terrain)
return CanTargetLocation(self, target.CenterPosition.ToCPos(), othersAtTarget, modifiers, ref cursor); return CanTargetLocation(self, target.CenterPosition.ToCPos(), othersAtTarget, modifiers, ref cursor);
default:
return false; return false;
} }
}
public bool IsQueued { get; protected set; } public bool IsQueued { get; protected set; }
} }