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="Traits\Player\DeveloperMode.cs" />
<Compile Include="Traits\RevealsShroud.cs" />
<Compile Include="Traits\Targetable.cs" />
<Compile Include="Traits\Health.cs" />
<Compile Include="Widgets\VqaPlayerWidget.cs" />
<Compile Include="Widgets\Delegates\VideoPlayerDelegate.cs" />
@@ -259,4 +258,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

View File

@@ -30,7 +30,7 @@ namespace OpenRA.Orders
else
{
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>()
@@ -106,7 +106,7 @@ namespace OpenRA.Orders
}
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)
.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 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)
{
var targetable = target.TraitOrDefault<Targetable>();
var targetable = target.TraitOrDefault<ITargetable>();
if (targetable == null || !weapon.ValidTargets.Intersect(targetable.TargetTypes).Any())
return false;
@@ -223,7 +223,7 @@ namespace OpenRA.Mods.RA
public static bool IsInRange( float2 attackOrigin, float range, Actor target )
{
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 )
return true;
return false;

View File

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

View File

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

View File

@@ -12,13 +12,13 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
public class TargetableCloakedInfo : TargetableInfo, ITraitPrerequisite<CloakInfo>
public class TargetableCloakedInfo : TargetableUnitInfo, ITraitPrerequisite<CloakInfo>
{
public readonly string[] CloakedTargetTypes = {};
public override object Create( ActorInitializer init ) { return new TargetableCloaked(init.self, this); }
}
public class TargetableCloaked : Targetable
public class TargetableCloaked : TargetableUnit<TargetableCloakedInfo>
{
Cloak Cloak;
public TargetableCloaked(Actor self, TargetableCloakedInfo info)
@@ -29,8 +29,8 @@ namespace OpenRA.Mods.RA
public override string[] TargetTypes
{
get { return (Cloak.Cloaked) ? ((TargetableCloakedInfo)Info).CloakedTargetTypes
: ((TargetableCloakedInfo)Info).TargetTypes;}
get { return (Cloak.Cloaked) ? info.CloakedTargetTypes
: 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
Selectable:
Voice: VehicleVoice
Targetable:
TargetableUnit:
TargetTypes: Ground
Buildable:
Queue: Vehicle
@@ -43,7 +43,7 @@
ROT: 5
Selectable:
Voice: VehicleVoice
Targetable:
TargetableUnit:
TargetTypes: Ground
Buildable:
Queue: Vehicle
@@ -63,7 +63,7 @@
^Helicopter:
AppearsOnRadar:
UseLocation: yes
Targetable:
TargetableUnit:
TargetTypes: Air
Selectable:
Voice: VehicleVoice
@@ -100,7 +100,7 @@
Beach: 80
Selectable:
Voice: GenericVoice
Targetable:
TargetableUnit:
TargetTypes: Ground
Buildable:
Queue: Infantry
@@ -146,7 +146,7 @@
UseLocation: yes
Selectable:
Voice: GenericVoice
Targetable:
TargetableUnit:
TargetTypes: Air
HiddenUnderFog:
GainsExperience:
@@ -163,7 +163,7 @@
Water: 100
Selectable:
Voice: GenericVoice
Targetable:
TargetableUnit:
TargetTypes: Ground, Water
HiddenUnderFog:
GainsExperience:
@@ -177,7 +177,7 @@
AppearsOnRadar:
Selectable:
Priority: 3
Targetable:
TargetableBuilding:
TargetTypes: Ground
Armor:
Type: Wood
@@ -242,7 +242,7 @@
DamagedSound: xplos.aud
DestroyedSound: xplobig4.aud
Adjacent: 7
Targetable:
TargetableBuilding:
TargetTypes: Ground
Wall:
CrushClasses: wall
@@ -293,7 +293,7 @@
^Bridge:
Tooltip:
Name: Bridge
Targetable:
TargetableBuilding:
TargetTypes: Ground, Water
BelowUnits:
Health:

View File

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

View File

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

View File

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

View File

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