refactor Targetable into Targetable{Unit,Building} and ITargetable

This commit is contained in:
Bob
2010-11-03 22:28:53 +13:00
committed by Chris Forbes
parent 98dec6dc8e
commit 39b09780f6
15 changed files with 106 additions and 84 deletions

View File

@@ -192,7 +192,6 @@
<Compile Include="Widgets\ViewportScrollControllerWidget.cs" /> <Compile Include="Widgets\ViewportScrollControllerWidget.cs" />
<Compile Include="Traits\Player\DeveloperMode.cs" /> <Compile Include="Traits\Player\DeveloperMode.cs" />
<Compile Include="Traits\RevealsShroud.cs" /> <Compile Include="Traits\RevealsShroud.cs" />
<Compile Include="Traits\Targetable.cs" />
<Compile Include="Traits\Health.cs" /> <Compile Include="Traits\Health.cs" />
<Compile Include="Widgets\VqaPlayerWidget.cs" /> <Compile Include="Widgets\VqaPlayerWidget.cs" />
<Compile Include="Widgets\Delegates\VideoPlayerDelegate.cs" /> <Compile Include="Widgets\Delegates\VideoPlayerDelegate.cs" />

View File

@@ -30,7 +30,7 @@ namespace OpenRA.Orders
else else
{ {
var underCursor = world.FindUnitsAtMouse(mi.Location) var underCursor = world.FindUnitsAtMouse(mi.Location)
.Where(a => a.Info.Traits.Contains<TargetableInfo>()) .Where(a => a.HasTrait<ITargetable>())
.OrderByDescending( .OrderByDescending(
a => a =>
a.Info.Traits.Contains<SelectableInfo>() a.Info.Traits.Contains<SelectableInfo>()
@@ -106,7 +106,7 @@ namespace OpenRA.Orders
} }
var underCursor = world.FindUnitsAtMouse(mi.Location) var underCursor = world.FindUnitsAtMouse(mi.Location)
.Where(a => a.Info.Traits.Contains<TargetableInfo>()) .Where(a => a.HasTrait<ITargetable>())
.OrderByDescending(a => a.Info.Traits.Contains<SelectableInfo>() ? a.Info.Traits.Get<SelectableInfo>().Priority : int.MinValue) .OrderByDescending(a => a.Info.Traits.Contains<SelectableInfo>() ? a.Info.Traits.Get<SelectableInfo>().Priority : int.MinValue)
.FirstOrDefault(); .FirstOrDefault();

View File

@@ -1,42 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System.Drawing;
using OpenRA.Graphics;
using System.Linq;
using System.Collections.Generic;
namespace OpenRA.Traits
{
public class TargetableInfo : ITraitInfo
{
public readonly string[] TargetTypes = {};
public virtual object Create( ActorInitializer init ) { return new Targetable(this); }
}
public class Targetable
{
protected TargetableInfo Info;
public Targetable(TargetableInfo info)
{
Info = info;
}
public virtual string[] TargetTypes
{
get { return Info.TargetTypes;}
}
public virtual IEnumerable<int2> TargetableSquares( Actor self )
{
yield return self.Location;
}
}
}

View File

@@ -237,4 +237,10 @@ namespace OpenRA.Traits
public Actor Actor { get { return IsActor ? actor : null; } } public Actor Actor { get { return IsActor ? actor : null; } }
public bool IsActor { get { return actor != null && !actor.Destroyed; } } public bool IsActor { get { return actor != null && !actor.Destroyed; } }
} }
public interface ITargetable
{
string[] TargetTypes { get; }
IEnumerable<int2> TargetableSquares( Actor self );
}
} }

View File

@@ -163,7 +163,7 @@ namespace OpenRA.Mods.RA
public static bool WeaponValidForTarget(WeaponInfo weapon, Actor target) public static bool WeaponValidForTarget(WeaponInfo weapon, Actor target)
{ {
var targetable = target.TraitOrDefault<Targetable>(); var targetable = target.TraitOrDefault<ITargetable>();
if (targetable == null || !weapon.ValidTargets.Intersect(targetable.TargetTypes).Any()) if (targetable == null || !weapon.ValidTargets.Intersect(targetable.TargetTypes).Any())
return false; return false;
@@ -223,7 +223,7 @@ namespace OpenRA.Mods.RA
public static bool IsInRange( float2 attackOrigin, float range, Actor target ) public static bool IsInRange( float2 attackOrigin, float range, Actor target )
{ {
var rsq = range * range * Game.CellSize * Game.CellSize; var rsq = range * range * Game.CellSize * Game.CellSize;
foreach( var cell in target.Trait<Targetable>().TargetableSquares( target ) ) foreach( var cell in target.Trait<ITargetable>().TargetableSquares( target ) )
if( ( attackOrigin - cell * Game.CellSize ).LengthSquared < rsq ) if( ( attackOrigin - cell * Game.CellSize ).LengthSquared < rsq )
return true; return true;
return false; return false;

View File

@@ -222,6 +222,7 @@
<Compile Include="TakeCover.cs" /> <Compile Include="TakeCover.cs" />
<Compile Include="TargetableAircraft.cs" /> <Compile Include="TargetableAircraft.cs" />
<Compile Include="TargetableBuilding.cs" /> <Compile Include="TargetableBuilding.cs" />
<Compile Include="TargetableUnit.cs" />
<Compile Include="TeslaInstantKills.cs" /> <Compile Include="TeslaInstantKills.cs" />
<Compile Include="Crates\ResetRadarCrateAction.cs" /> <Compile Include="Crates\ResetRadarCrateAction.cs" />
<Compile Include="ThrowsParticles.cs" /> <Compile Include="ThrowsParticles.cs" />

View File

@@ -15,13 +15,13 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
public class TargetableAircraftInfo : TargetableInfo, ITraitPrerequisite<AircraftInfo> public class TargetableAircraftInfo : TargetableUnitInfo, ITraitPrerequisite<AircraftInfo>
{ {
public readonly string[] GroundedTargetTypes = { }; public readonly string[] GroundedTargetTypes = { };
public override object Create(ActorInitializer init) { return new TargetableAircraft(init.self, this); } public override object Create(ActorInitializer init) { return new TargetableAircraft(init.self, this); }
} }
public class TargetableAircraft : Targetable public class TargetableAircraft : TargetableUnit<TargetableAircraftInfo>
{ {
Aircraft Aircraft; Aircraft Aircraft;
public TargetableAircraft(Actor self, TargetableAircraftInfo info) public TargetableAircraft(Actor self, TargetableAircraftInfo info)
@@ -32,8 +32,8 @@ namespace OpenRA.Mods.RA
public override string[] TargetTypes public override string[] TargetTypes
{ {
get { return (Aircraft.Altitude > 0) ? ((TargetableAircraftInfo)Info).TargetTypes get { return (Aircraft.Altitude > 0) ? info.TargetTypes
: ((TargetableAircraftInfo)Info).GroundedTargetTypes; } : info.GroundedTargetTypes; }
} }
} }
} }

View File

@@ -1,4 +1,14 @@
using System; #region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -6,19 +16,23 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
class TargetableBuildingInfo : TargetableInfo, ITraitPrerequisite<BuildingInfo> class TargetableBuildingInfo : ITraitInfo, ITraitPrerequisite<BuildingInfo>
{ {
public override object Create( ActorInitializer init ) { return new TargetableBuilding( this ); } public readonly string[] TargetTypes = { };
public object Create( ActorInitializer init ) { return new TargetableBuilding( this ); }
} }
class TargetableBuilding : Targetable class TargetableBuilding : ITargetable
{ {
readonly TargetableBuildingInfo info;
public TargetableBuilding( TargetableBuildingInfo info ) public TargetableBuilding( TargetableBuildingInfo info )
: base( info )
{ {
this.info = info;
} }
public override IEnumerable<int2> TargetableSquares( Actor self ) public string[] TargetTypes { get { return info.TargetTypes; } }
public IEnumerable<int2> TargetableSquares( Actor self )
{ {
return self.Trait<Building>().OccupiedCells(); return self.Trait<Building>().OccupiedCells();
} }

View File

@@ -12,13 +12,13 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
public class TargetableCloakedInfo : TargetableInfo, ITraitPrerequisite<CloakInfo> public class TargetableCloakedInfo : TargetableUnitInfo, ITraitPrerequisite<CloakInfo>
{ {
public readonly string[] CloakedTargetTypes = {}; public readonly string[] CloakedTargetTypes = {};
public override object Create( ActorInitializer init ) { return new TargetableCloaked(init.self, this); } public override object Create( ActorInitializer init ) { return new TargetableCloaked(init.self, this); }
} }
public class TargetableCloaked : Targetable public class TargetableCloaked : TargetableUnit<TargetableCloakedInfo>
{ {
Cloak Cloak; Cloak Cloak;
public TargetableCloaked(Actor self, TargetableCloakedInfo info) public TargetableCloaked(Actor self, TargetableCloakedInfo info)
@@ -29,8 +29,8 @@ namespace OpenRA.Mods.RA
public override string[] TargetTypes public override string[] TargetTypes
{ {
get { return (Cloak.Cloaked) ? ((TargetableCloakedInfo)Info).CloakedTargetTypes get { return (Cloak.Cloaked) ? info.CloakedTargetTypes
: ((TargetableCloakedInfo)Info).TargetTypes;} : info.TargetTypes;}
} }
} }
} }

View File

@@ -0,0 +1,41 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* 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
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class TargetableUnitInfo : ITraitInfo
{
public readonly string[] TargetTypes = { };
public virtual object Create( ActorInitializer init ) { return new TargetableUnit<TargetableUnitInfo>( this ); }
}
public class TargetableUnit<Info> : ITargetable
where Info : TargetableUnitInfo
{
protected readonly Info info;
public TargetableUnit( Info info )
{
this.info = info;
}
public virtual string[] TargetTypes { get { return info.TargetTypes; } }
public virtual IEnumerable<int2> TargetableSquares( Actor self )
{
yield return Util.CellContaining( self.CenterLocation );
}
}
}

View File

@@ -12,7 +12,7 @@
ROT: 5 ROT: 5
Selectable: Selectable:
Voice: VehicleVoice Voice: VehicleVoice
Targetable: TargetableUnit:
TargetTypes: Ground TargetTypes: Ground
Buildable: Buildable:
Queue: Vehicle Queue: Vehicle
@@ -43,7 +43,7 @@
ROT: 5 ROT: 5
Selectable: Selectable:
Voice: VehicleVoice Voice: VehicleVoice
Targetable: TargetableUnit:
TargetTypes: Ground TargetTypes: Ground
Buildable: Buildable:
Queue: Vehicle Queue: Vehicle
@@ -63,7 +63,7 @@
^Helicopter: ^Helicopter:
AppearsOnRadar: AppearsOnRadar:
UseLocation: yes UseLocation: yes
Targetable: TargetableUnit:
TargetTypes: Air TargetTypes: Air
Selectable: Selectable:
Voice: VehicleVoice Voice: VehicleVoice
@@ -100,7 +100,7 @@
Beach: 80 Beach: 80
Selectable: Selectable:
Voice: GenericVoice Voice: GenericVoice
Targetable: TargetableUnit:
TargetTypes: Ground TargetTypes: Ground
Buildable: Buildable:
Queue: Infantry Queue: Infantry
@@ -146,7 +146,7 @@
UseLocation: yes UseLocation: yes
Selectable: Selectable:
Voice: GenericVoice Voice: GenericVoice
Targetable: TargetableUnit:
TargetTypes: Air TargetTypes: Air
HiddenUnderFog: HiddenUnderFog:
GainsExperience: GainsExperience:
@@ -163,7 +163,7 @@
Water: 100 Water: 100
Selectable: Selectable:
Voice: GenericVoice Voice: GenericVoice
Targetable: TargetableUnit:
TargetTypes: Ground, Water TargetTypes: Ground, Water
HiddenUnderFog: HiddenUnderFog:
GainsExperience: GainsExperience:
@@ -177,7 +177,7 @@
AppearsOnRadar: AppearsOnRadar:
Selectable: Selectable:
Priority: 3 Priority: 3
Targetable: TargetableBuilding:
TargetTypes: Ground TargetTypes: Ground
Armor: Armor:
Type: Wood Type: Wood
@@ -242,7 +242,7 @@
DamagedSound: xplos.aud DamagedSound: xplos.aud
DestroyedSound: xplobig4.aud DestroyedSound: xplobig4.aud
Adjacent: 7 Adjacent: 7
Targetable: TargetableBuilding:
TargetTypes: Ground TargetTypes: Ground
Wall: Wall:
CrushClasses: wall CrushClasses: wall
@@ -293,7 +293,7 @@
^Bridge: ^Bridge:
Tooltip: Tooltip:
Name: Bridge Name: Bridge
Targetable: TargetableBuilding:
TargetTypes: Ground, Water TargetTypes: Ground, Water
BelowUnits: BelowUnits:
Health: Health:

View File

@@ -446,6 +446,9 @@ STNK:
PrimaryWeapon: 227mm PrimaryWeapon: 227mm
RenderUnit: RenderUnit:
AutoTarget: AutoTarget:
-TargetableUnit:
TargetableCloaked:
CloakedTargetTypes: cloaked
TRAN: TRAN:
Inherits: ^Helicopter Inherits: ^Helicopter
@@ -565,7 +568,7 @@ C17:
Passengers: 10 Passengers: 10
Invulnerable: Invulnerable:
-Selectable: -Selectable:
-Targetable: -TargetableUnit:
-GainsExperience: -GainsExperience:
FlyAwayOnIdle: FlyAwayOnIdle:

View File

@@ -11,7 +11,7 @@
ROT: 5 ROT: 5
Selectable: Selectable:
Voice: VehicleVoice Voice: VehicleVoice
Targetable: TargetableUnit:
TargetTypes: Ground TargetTypes: Ground
Repairable: Repairable:
Chronoshiftable: Chronoshiftable:
@@ -39,7 +39,7 @@
ROT: 5 ROT: 5
Selectable: Selectable:
Voice: VehicleVoice Voice: VehicleVoice
Targetable: TargetableUnit:
TargetTypes: Ground TargetTypes: Ground
Repairable: Repairable:
Chronoshiftable: Chronoshiftable:
@@ -71,7 +71,7 @@
Beach: 80 Beach: 80
Selectable: Selectable:
Voice: GenericVoice Voice: GenericVoice
Targetable: TargetableUnit:
TargetTypes: Ground TargetTypes: Ground
RenderInfantry: RenderInfantry:
AutoTarget: AutoTarget:
@@ -94,7 +94,7 @@
Water: 100 Water: 100
Selectable: Selectable:
Voice: ShipVoice Voice: ShipVoice
Targetable: TargetableUnit:
TargetTypes: Ground, Water TargetTypes: Ground, Water
DetectCloaked: DetectCloaked:
Range: 3 Range: 3
@@ -157,7 +157,7 @@
Range: 8 Range: 8
Selectable: Selectable:
Priority: 1 Priority: 1
Targetable: TargetableBuilding:
TargetTypes: Ground TargetTypes: Ground
RenderBuildingWall: RenderBuildingWall:
HasMakeAnimation: false HasMakeAnimation: false
@@ -208,7 +208,7 @@
Tooltip: Tooltip:
Name: Bridge Name: Bridge
BelowUnits: BelowUnits:
Targetable: TargetableBuilding:
TargetTypes: Ground, Water TargetTypes: Ground, Water
Building: Building:
Footprint: ____ ____ Footprint: ____ ____

View File

@@ -68,7 +68,7 @@ SPEN:
Prerequisites: @Power Plant Prerequisites: @Power Plant
Owner: soviet Owner: soviet
Hotkey: s Hotkey: s
Targetable: TargetableBuilding:
TargetTypes: Ground, Water TargetTypes: Ground, Water
Building: Building:
Power: -30 Power: -30
@@ -123,7 +123,7 @@ SYRD:
Tooltip: Tooltip:
Name: Shipyard Name: Shipyard
Description: Produces and repairs ships Description: Produces and repairs ships
Targetable: TargetableBuilding:
TargetTypes: Ground, Water TargetTypes: Ground, Water
Building: Building:
Power: -30 Power: -30
@@ -964,7 +964,7 @@ SYRF:
# Description: Fake Shipyard # Description: Fake Shipyard
# LongDesc: Looks like a Shipyard # LongDesc: Looks like a Shipyard
# Hotkey: z # Hotkey: z
Targetable: TargetableBuilding:
TargetTypes: Ground, Water TargetTypes: Ground, Water
Building: Building:
Power: -2 Power: -2
@@ -985,7 +985,7 @@ SYRF:
SPEF: SPEF:
Inherits: ^Building Inherits: ^Building
Targetable: TargetableBuilding:
TargetTypes: Ground, Water TargetTypes: Ground, Water
Building: Building:
Power: -2 Power: -2

View File

@@ -480,7 +480,7 @@ SS:
Speed: 5 Speed: 5
RevealsShroud: RevealsShroud:
Range: 6 Range: 6
-Targetable: -TargetableUnit:
TargetableCloaked: TargetableCloaked:
TargetTypes: Ground, Water TargetTypes: Ground, Water
CloakedTargetTypes: Underwater CloakedTargetTypes: Underwater
@@ -523,7 +523,7 @@ MSUB:
RevealsShroud: RevealsShroud:
Range: 6 Range: 6
RenderUnit: RenderUnit:
-Targetable: -TargetableUnit:
TargetableCloaked: TargetableCloaked:
TargetTypes: Ground, Water TargetTypes: Ground, Water
CloakedTargetTypes: Underwater CloakedTargetTypes: Underwater