move Combat, AttackBase, and associates into mod

This commit is contained in:
Bob
2010-07-08 16:32:25 +12:00
parent bd74b29ea3
commit 281d013c3b
20 changed files with 95 additions and 91 deletions

View File

@@ -24,7 +24,7 @@ using OpenRA.Traits;
namespace OpenRA.Effects
{
class FlashTarget : IEffect
public class FlashTarget : IEffect
{
Actor target;
int remainingTicks = 4;

View File

@@ -21,9 +21,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
using OpenRA.Support;
using OpenRA.Traits;
namespace OpenRA
{
@@ -45,28 +44,6 @@ namespace OpenRA
return xs.Aggregate(1f, (a, x) => a * x);
}
public static WeaponInfo GetPrimaryWeapon(this Actor self)
{
var info = self.Info.Traits.GetOrDefault<AttackBaseInfo>();
if (info == null) return null;
var weapon = info.PrimaryWeapon;
if (weapon == null) return null;
return Rules.Weapons[weapon.ToLowerInvariant()];
}
public static WeaponInfo GetSecondaryWeapon(this Actor self)
{
var info = self.Info.Traits.GetOrDefault<AttackBaseInfo>();
if (info == null) return null;
var weapon = info.SecondaryWeapon;
if (weapon == null) return null;
return Rules.Weapons[weapon.ToLowerInvariant()];
}
public static int GetMaxHP(this Actor self)
{
var oai = self.Info.Traits.GetOrDefault<OwnedActorInfo>();

View File

@@ -89,7 +89,6 @@
<Compile Include="Traits\Modifiers\HiddenUnderFog.cs" />
<Compile Include="Traits\Render\RenderBuilding.cs" />
<Compile Include="Traits\Render\RenderBuildingTurreted.cs" />
<Compile Include="Traits\Render\RenderUnitTurreted.cs" />
<Compile Include="Traits\World\Shroud.cs" />
<Compile Include="Widgets\Delegates\ConnectionDialogsDelegate.cs" />
<Compile Include="Widgets\Delegates\CreateServerMenuDelegate.cs" />
@@ -102,7 +101,6 @@
<Compile Include="Widgets\MapPreviewWidget.cs" />
<Compile Include="Widgets\PostGameWidget.cs" />
<Compile Include="Widgets\WidgetUtils.cs" />
<Compile Include="Combat.cs" />
<Compile Include="Effects\DelayedAction.cs" />
<Compile Include="Effects\FlashTarget.cs" />
<Compile Include="Effects\MoveFlash.cs" />
@@ -136,7 +134,6 @@
<Compile Include="Traits\World\SpatialBins.cs" />
<Compile Include="Traits\World\ChoosePaletteOnSelect.cs" />
<Compile Include="Traits\World\Country.cs" />
<Compile Include="Traits\Activities\Attack.cs" />
<Compile Include="Traits\Activities\TransformIntoActor.cs" />
<Compile Include="Actor.cs" />
<Compile Include="Controller.cs" />
@@ -175,7 +172,6 @@
<Compile Include="Graphics\TerrainRenderer.cs" />
<Compile Include="Traits\Activities\Move.cs" />
<Compile Include="Traits\Activities\Turn.cs" />
<Compile Include="Traits\AttackBase.cs" />
<Compile Include="Traits\Buildable.cs" />
<Compile Include="Traits\Building.cs" />
<Compile Include="Traits\World\BuildingInfluence.cs" />
@@ -235,7 +231,6 @@
<Compile Include="Traits\SharesCell.cs" />
<Compile Include="Traits\World\AircraftInfluence.cs" />
<Compile Include="Traits\World\HazardLayer.cs" />
<Compile Include="Traits\Hazardous.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -52,9 +52,9 @@ namespace OpenRA.Orders
world.WorldRenderer.DrawSelectionBox(a, Color.White, true);
if (a.Owner == world.LocalPlayer)
{
if (a.traits.Contains<RenderRangeCircle>())
world.WorldRenderer.DrawRangeCircle(Color.FromArgb(128, Color.Yellow),
a.CenterLocation, (int)a.GetPrimaryWeapon().Range);
//if (a.traits.Contains<RenderRangeCircle>())
// world.WorldRenderer.DrawRangeCircle(Color.FromArgb(128, Color.Yellow),
// a.CenterLocation, (int)a.GetPrimaryWeapon().Range);
if (a.traits.Contains<DetectCloaked>())
world.WorldRenderer.DrawRangeCircle(Color.FromArgb(128, Color.LimeGreen),

View File

@@ -21,7 +21,6 @@
using System;
using System.Drawing;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Graphics;
namespace OpenRA.Traits
@@ -82,7 +81,7 @@ namespace OpenRA.Traits
return a / step;
}
static float2 RotateVectorByFacing(float2 v, int facing, float ecc)
public static float2 RotateVectorByFacing(float2 v, int facing, float ecc)
{
var angle = (facing / 256f) * (2 * (float)Math.PI);
var sinAngle = (float)Math.Sin(angle);
@@ -93,17 +92,6 @@ namespace OpenRA.Traits
ecc * (cosAngle * v.Y - sinAngle * v.X));
}
static float2 GetRecoil(Actor self, float recoil)
{
var abInfo = self.Info.Traits.GetOrDefault<AttackBaseInfo>();
if (abInfo == null || abInfo.Recoil == 0) return float2.Zero;
var rut = self.traits.GetOrDefault<RenderUnitTurreted>();
if (rut == null) return float2.Zero;
var facing = self.traits.Get<Turreted>().turretFacing;
return RotateVectorByFacing(new float2(0, recoil * self.Info.Traits.Get<AttackBaseInfo>().Recoil), facing, .7f);
}
public static float2 CenterOfCell(int2 loc)
{
return new float2(12, 12) + Game.CellSize * (float2)loc;
@@ -114,19 +102,6 @@ namespace OpenRA.Traits
return 0.5f * (CenterOfCell(from) + CenterOfCell(to));
}
public static float2 GetTurretPosition(Actor self, Unit unit, int[] offset, float recoil)
{
if( unit == null ) return offset.AbsOffset(); /* things that don't have a rotating base don't need the turrets repositioned */
var ru = self.traits.GetOrDefault<RenderUnit>();
var numDirs = (ru != null) ? ru.anim.CurrentSequence.Facings : 8;
var bodyFacing = unit.Facing;
var quantizedFacing = QuantizeFacing(bodyFacing, numDirs) * (256 / numDirs);
return (RotateVectorByFacing(offset.RelOffset(), quantizedFacing, .7f) + GetRecoil(self, recoil))
+ offset.AbsOffset();
}
public static int2 AsInt2(this int[] xs) { return new int2(xs[0], xs[1]); }
public static float2 RelOffset(this int[] offset) { return new float2(offset[0], offset[1]); }
public static float2 AbsOffset(this int[] offset) { return new float2(offset.ElementAtOrDefault(2), offset.ElementAtOrDefault(3)); }
@@ -144,12 +119,6 @@ namespace OpenRA.Traits
(next, a) => { a.NextActivity = next; return a; });
}
public static float GetMaximumRange(Actor self)
{
return new[] { self.GetPrimaryWeapon(), self.GetSecondaryWeapon() }
.Where(w => w != null).Max(w => w.Range);
}
public static Color ArrayToColor(int[] x) { return Color.FromArgb(x[0], x[1], x[2]); }
public static int2 CellContaining(float2 pos) { return (1f / Game.CellSize * pos).ToInt2(); }

View File

@@ -26,7 +26,7 @@ using OpenRA.FileFormats;
namespace OpenRA.Traits
{
class SmudgeLayerInfo : ITraitInfo
public class SmudgeLayerInfo : ITraitInfo
{
public readonly string Type = "Scorch";
public readonly string[] Types = {"sc1", "sc2", "sc3", "sc4", "sc5", "sc6"};
@@ -34,7 +34,7 @@ namespace OpenRA.Traits
public object Create(ActorInitializer init) { return new SmudgeLayer(this); }
}
class SmudgeLayer: IRenderOverlay, ILoadWorldHook
public class SmudgeLayer: IRenderOverlay, ILoadWorldHook
{
public SmudgeLayerInfo Info;
SpriteRenderer spriteRenderer;

View File

@@ -18,10 +18,10 @@
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Effects;
using OpenRA.Graphics;
using OpenRA.Mods.RA;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Effects

View File

@@ -20,6 +20,7 @@
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.RA;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc

View File

@@ -18,7 +18,10 @@
*/
#endregion
namespace OpenRA.Traits.Activities
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA.Activities
{
/* non-turreted attack */
public class Attack : IActivity

View File

@@ -24,8 +24,9 @@ using System.Linq;
using OpenRA.Effects;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Traits
namespace OpenRA.Mods.RA
{
public class AttackBaseInfo : ITraitInfo
{
@@ -178,7 +179,7 @@ namespace OpenRA.Traits
firedBy = self,
target = target,
src = self.CenterLocation.ToInt2() + Util.GetTurretPosition(self, unit, fireOffset, 0f).ToInt2(),
src = self.CenterLocation.ToInt2() + Combat.GetTurretPosition(self, unit, fireOffset, 0f).ToInt2(),
srcAltitude = unit != null ? unit.Altitude : 0,
dest = target.CenterLocation.ToInt2(),
destAltitude = destUnit != null ? destUnit.Altitude : 0,

View File

@@ -19,8 +19,8 @@
#endregion
using System.Linq;
using OpenRA.Mods.RA.Activities;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA
{
@@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA
bool NeedsNewTarget(Actor self)
{
var attack = self.traits.Get<AttackBase>();
var range = Util.GetMaximumRange(self);
var range = Combat.GetMaximumRange(self);
if (attack.target == null)
return true; // he's dead.
@@ -55,7 +55,7 @@ namespace OpenRA.Mods.RA
public void Tick(Actor self)
{
var range = Util.GetMaximumRange(self);
var range = Combat.GetMaximumRange(self);
if (NeedsNewTarget(self))
AttackTarget(self, ChooseTarget(self, range));

View File

@@ -48,7 +48,7 @@ namespace OpenRA.Mods.RA
if (--nextScanTime <= 0)
{
var attack = self.traits.Get<AttackBase>();
var range = Util.GetMaximumRange(self);
var range = Combat.GetMaximumRange(self);
if (attack.target == null ||
(attack.target.Location - self.Location).LengthSquared > range * range)

55
OpenRA.Game/Combat.cs → OpenRA.Mods.RA/Combat.cs Normal file → Executable file
View File

@@ -23,9 +23,8 @@ using System.Linq;
using OpenRA.Effects;
using OpenRA.GameRules;
using OpenRA.Traits;
using OpenRA.FileFormats;
namespace OpenRA
namespace OpenRA.Mods.RA
{
public static class Combat /* some utility bits that are shared between various things */
{
@@ -194,5 +193,57 @@ namespace OpenRA
return false;
}
public static float GetMaximumRange(Actor self)
{
return new[] { self.GetPrimaryWeapon(), self.GetSecondaryWeapon() }
.Where(w => w != null).Max(w => w.Range);
}
public static WeaponInfo GetPrimaryWeapon(this Actor self)
{
var info = self.Info.Traits.GetOrDefault<AttackBaseInfo>();
if (info == null) return null;
var weapon = info.PrimaryWeapon;
if (weapon == null) return null;
return Rules.Weapons[weapon.ToLowerInvariant()];
}
public static WeaponInfo GetSecondaryWeapon(this Actor self)
{
var info = self.Info.Traits.GetOrDefault<AttackBaseInfo>();
if (info == null) return null;
var weapon = info.SecondaryWeapon;
if (weapon == null) return null;
return Rules.Weapons[weapon.ToLowerInvariant()];
}
static float2 GetRecoil(Actor self, float recoil)
{
var abInfo = self.Info.Traits.GetOrDefault<AttackBaseInfo>();
if (abInfo == null || abInfo.Recoil == 0) return float2.Zero;
var rut = self.traits.GetOrDefault<RenderUnitTurreted>();
if (rut == null) return float2.Zero;
var facing = self.traits.Get<Turreted>().turretFacing;
return Util.RotateVectorByFacing(new float2(0, recoil * self.Info.Traits.Get<AttackBaseInfo>().Recoil), facing, .7f);
}
public static float2 GetTurretPosition(Actor self, Unit unit, int[] offset, float recoil)
{
if( unit == null ) return offset.AbsOffset(); /* things that don't have a rotating base don't need the turrets repositioned */
var ru = self.traits.GetOrDefault<RenderUnit>();
var numDirs = (ru != null) ? ru.anim.CurrentSequence.Facings : 8;
var bodyFacing = unit.Facing;
var quantizedFacing = Util.QuantizeFacing(bodyFacing, numDirs) * (256 / numDirs);
return (Util.RotateVectorByFacing(offset.RelOffset(), quantizedFacing, .7f) + GetRecoil(self, recoil))
+ offset.AbsOffset();
}
}
}

View File

@@ -18,10 +18,11 @@
*/
#endregion
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Traits
namespace OpenRA.Mods.RA
{
class AntiAirInfo : ITraitInfo
{

View File

@@ -46,6 +46,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\Attack.cs" />
<Compile Include="Activities\CallFunc.cs" />
<Compile Include="Activities\CaptureBuilding.cs" />
<Compile Include="Activities\DeliverOre.cs" />
@@ -70,14 +71,18 @@
<Compile Include="Activities\UndeployMcv.cs" />
<Compile Include="Activities\UnloadCargo.cs" />
<Compile Include="Activities\Wait.cs" />
<Compile Include="AttackBase.cs" />
<Compile Include="Combat.cs" />
<Compile Include="ConstructionYard.cs" />
<Compile Include="Effects\Bullet.cs" />
<Compile Include="Effects\GravityBomb.cs" />
<Compile Include="Effects\LaserZap.cs" />
<Compile Include="Effects\Missile.cs" />
<Compile Include="Effects\TeslaZap.cs" />
<Compile Include="Hazardous.cs" />
<Compile Include="Player\ActorGroupProxy.cs" />
<Compile Include="Aircraft.cs" />
<Compile Include="RenderUnitTurreted.cs" />
<Compile Include="SupportPowers\AirstrikePower.cs" />
<Compile Include="AttackFrontal.cs" />
<Compile Include="AttackHeli.cs" />

View File

@@ -18,8 +18,8 @@
*/
#endregion
using OpenRA.Mods.RA.Activities;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA
{

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA
rotorAnim.PlayRepeating("rotor");
anims.Add( "rotor_1", new AnimationWithOffset(
rotorAnim,
() => Traits.Util.GetTurretPosition( self, unit, info.PrimaryOffset, 0 ),
() => Combat.GetTurretPosition( self, unit, info.PrimaryOffset, 0 ),
null ) { ZOffset = 1 } );
if (info.SecondaryOffset == null) return;
@@ -54,7 +54,7 @@ namespace OpenRA.Mods.RA
secondRotorAnim.PlayRepeating( "rotor2" );
anims.Add( "rotor_2", new AnimationWithOffset(
secondRotorAnim,
() => Traits.Util.GetTurretPosition(self, unit, info.SecondaryOffset, 0),
() => Combat.GetTurretPosition(self, unit, info.SecondaryOffset, 0),
null) { ZOffset = 1 });
}

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA
spinnerAnim.PlayRepeating( "spinner" );
anims.Add( "spinner", new AnimationWithOffset(
spinnerAnim,
() => Traits.Util.GetTurretPosition( self, unit, info.Offset, 0 ),
() => Combat.GetTurretPosition( self, unit, info.Offset, 0 ),
null ) { ZOffset = 1 } );
}
}

View File

@@ -19,8 +19,9 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Traits
namespace OpenRA.Mods.RA
{
class RenderUnitTurretedInfo : RenderUnitInfo
{
@@ -43,13 +44,13 @@ namespace OpenRA.Traits
if( attackInfo.PrimaryOffset != null )
anims.Add("turret_1", new AnimationWithOffset(
turretAnim,
() => Util.GetTurretPosition(self, unit, attackInfo.PrimaryOffset, attack.primaryRecoil),
() => Combat.GetTurretPosition(self, unit, attackInfo.PrimaryOffset, attack.primaryRecoil),
null) { ZOffset = 1 });
if (attackInfo.SecondaryOffset != null)
anims.Add("turret_2", new AnimationWithOffset(
turretAnim,
() => Util.GetTurretPosition(self, unit, attackInfo.SecondaryOffset, attack.secondaryRecoil),
() => Combat.GetTurretPosition(self, unit, attackInfo.SecondaryOffset, attack.secondaryRecoil),
null) { ZOffset = 1 });
if( attackInfo.MuzzleFlash )
@@ -59,7 +60,7 @@ namespace OpenRA.Traits
() => (int)( attack.primaryRecoil * 5.9f ) ); /* hack: recoil can be 1.0f, but don't overflow into next anim */
anims.Add( "muzzle_flash", new AnimationWithOffset(
muzzleFlash,
() => Util.GetTurretPosition(self, unit, attackInfo.PrimaryOffset, attack.primaryRecoil),
() => Combat.GetTurretPosition(self, unit, attackInfo.PrimaryOffset, attack.primaryRecoil),
() => attack.primaryRecoil <= 0 ) );
}
}

View File

@@ -57,7 +57,7 @@ namespace OpenRA.Mods.RA
if (info != null)
{
alt = 0;
pos = Traits.Util.GetTurretPosition(self, self.traits.Get<Unit>(), info.Offset, 0);
pos = Combat.GetTurretPosition(self, self.traits.Get<Unit>(), info.Offset, 0);
var ru = self.traits.Get<RenderUnit>();
v = Game.CosmeticRandom.Gauss2D(1) * info.Spread.RelOffset();