attack if in range of any cell of a building

This commit is contained in:
Bob
2010-11-03 21:06:55 +13:00
committed by Chris Forbes
parent c3fc7b98f3
commit 50b1ba3acc
8 changed files with 56 additions and 7 deletions

View File

@@ -11,6 +11,7 @@
using System.Drawing;
using OpenRA.Graphics;
using System.Linq;
using System.Collections.Generic;
namespace OpenRA.Traits
{
@@ -32,5 +33,10 @@ namespace OpenRA.Traits
{
get { return Info.TargetTypes;}
}
public virtual IEnumerable<int2> TargetableSquares( Actor self )
{
yield return self.Location;
}
}
}

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Activities
var mobile = self.Trait<Mobile>();
var targetCell = Util.CellContaining(Target.CenterLocation);
if ((targetCell - self.Location).LengthSquared >= Range * Range)
if (!Combat.IsInRange( self.CenterLocation, Range, Util.CenterOfCell(targetCell)))
return Util.SequenceActivities( mobile.MoveTo( Target, Range ), this );
var desiredFacing = Util.GetFacing((targetCell - self.Location).ToFloat2(), 0);

View File

@@ -32,8 +32,7 @@ namespace OpenRA.Mods.RA
if (IsLeaping) return;
var weapon = self.Trait<AttackBase>().Weapons[0].Info;
if (weapon.Range * Game.CellSize * weapon.Range * Game.CellSize
< (target.CenterLocation - self.CenterLocation).LengthSquared) return;
if( !Combat.IsInRange( self.CenterLocation, weapon.Range, target.Actor ) ) return;
self.CancelActivity();
self.QueueActivity(new Leap(self, target));

View File

@@ -219,5 +219,20 @@ namespace OpenRA.Mods.RA
return Util.RotateVectorByFacing(barrel.Position, turretFacing, .7f);
}
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 ) )
if( ( attackOrigin - cell * Game.CellSize ).LengthSquared < rsq )
return true;
return false;
}
public static bool IsInRange( float2 attackOrigin, float range, float2 targetLocation )
{
var rsq = range * range * Game.CellSize * Game.CellSize;
return ( attackOrigin - targetLocation ).LengthSquared < rsq;
}
}
}

View File

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

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class TargetableBuildingInfo : TargetableInfo, ITraitPrerequisite<BuildingInfo>
{
public override object Create( ActorInitializer init ) { return new TargetableBuilding( this ); }
}
class TargetableBuilding : Targetable
{
public TargetableBuilding( TargetableBuildingInfo info )
: base( info )
{
}
public override IEnumerable<int2> TargetableSquares( Actor self )
{
return self.Trait<Building>().OccupiedCells();
}
}
}

View File

@@ -102,8 +102,10 @@ namespace OpenRA.Mods.RA
if (limitedAmmo != null && !limitedAmmo.HasAmmo())
return;
if (Info.Range * Info.Range * Game.CellSize * Game.CellSize
< (target.CenterLocation - self.CenterLocation).LengthSquared) return;
if( target.IsActor && !Combat.IsInRange( self.CenterLocation, Info.Range, target.Actor ) )
return;
else if( !target.IsActor && !Combat.IsInRange( self.CenterLocation, Info.Range, target.CenterLocation ) )
return;
if (Info.MinRange * Info.MinRange * Game.CellSize * Game.CellSize >
(target.CenterLocation - self.CenterLocation).LengthSquared) return;

View File

@@ -125,7 +125,7 @@
AppearsOnRadar:
Selectable:
Priority: 3
Targetable:
TargetableBuilding:
TargetTypes: Ground
Building:
Dimensions: 1,1