refactor Targetable into Targetable{Unit,Building} and ITargetable
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
41
OpenRA.Mods.RA/TargetableUnit.cs
Executable file
41
OpenRA.Mods.RA/TargetableUnit.cs
Executable 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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user