Merge pull request #2917 from pchote/turret-cleanup

Remove Turret and PVecFloat cruft.
This commit is contained in:
Chris Forbes
2013-04-01 23:06:15 -07:00
41 changed files with 238 additions and 359 deletions

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA
var anim = new Animation("fire", () => 0);
anim.PlayRepeating(Info.Anim);
rs.anims.Add("fire",
new AnimationWithOffset(anim, () => new float2(0, -3), null));
new AnimationWithOffset(anim, wr => new float2(0, -3), null));
}
public void Tick(Actor self)

View File

@@ -128,7 +128,10 @@ namespace OpenRA.Mods.RA.Effects
}
if (Trail != null)
Trail.Tick((PPos)highPos.ToInt2());
{
var alt = (Info.High || Info.Angle > 0) ? GetAltitude() : 0;
Trail.Tick(new PPos((int)pos.X, (int)pos.Y).ToWPos((int)alt));
}
}
if (!Info.High) // check for hitting a wall
@@ -175,7 +178,7 @@ namespace OpenRA.Mods.RA.Effects
}
if (Trail != null)
Trail.Render(Args.firedBy);
Trail.Render(wr, Args.firedBy);
}
void Explode( World world )

View File

@@ -16,9 +16,10 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class ContrailInfo : ITraitInfo
class ContrailInfo : ITraitInfo, Requires<LocalCoordinatesModelInfo>
{
public readonly int[] ContrailOffset = {0, 0};
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
public readonly int TrailLength = 25;
public readonly Color Color = Color.White;
@@ -29,31 +30,31 @@ namespace OpenRA.Mods.RA
class Contrail : ITick, IPostRender
{
Turret contrailTurret = null;
ContrailInfo info;
ContrailHistory history;
IFacing facing;
IMove move;
ILocalCoordinatesModel coords;
public Contrail(Actor self, ContrailInfo info)
{
contrailTurret = new Turret(info.ContrailOffset);
this.info = info;
history = new ContrailHistory(info.TrailLength,
info.UsePlayerColor ? ContrailHistory.ChooseColor(self) : info.Color);
facing = self.Trait<IFacing>();
move = self.Trait<IMove>();
coords = self.Trait<ILocalCoordinatesModel>();
}
public void Tick(Actor self)
{
history.Tick(self.CenterLocation - new PVecInt(0, move.Altitude) - (PVecInt)contrailTurret.PxPosition(self, facing).ToInt2());
var local = info.Offset.Rotate(coords.QuantizeOrientation(self, self.Orientation));
history.Tick(self.CenterPosition + coords.LocalToWorld(local));
}
public void RenderAfterWorld(WorldRenderer wr, Actor self) { history.Render(self); }
public void RenderAfterWorld(WorldRenderer wr, Actor self) { history.Render(wr, self); }
}
class ContrailHistory
{
List<PPos> positions = new List<PPos>();
List<WPos> positions = new List<WPos>();
readonly int TrailLength;
readonly Color Color;
readonly int StartSkip;
@@ -74,27 +75,28 @@ namespace OpenRA.Mods.RA
this.StartSkip = startSkip;
}
public void Tick(PPos currentPos)
public void Tick(WPos currentPos)
{
positions.Add(currentPos);
if (positions.Count >= TrailLength)
positions.RemoveAt(0);
}
public void Render(Actor self)
public void Render(WorldRenderer wr, Actor self)
{
Color trailStart = Color;
Color trailEnd = Color.FromArgb(trailStart.A - 255 / TrailLength, trailStart.R, trailStart.G, trailStart.B);
for (int i = positions.Count - 1 - StartSkip; i >= 4; --i)
{
var conPos = PPos.Average(positions[i], positions[i-1], positions[i-2], positions[i-3]);
var nextPos = PPos.Average(positions[i-1], positions[i-2], positions[i-3], positions[i-4]);
// World positions
var conPos = WPos.Average(positions[i], positions[i-1], positions[i-2], positions[i-3]);
var nextPos = WPos.Average(positions[i-1], positions[i-2], positions[i-3], positions[i-4]);
if (self.World.RenderedShroud.IsVisible(conPos.ToCPos()) ||
self.World.RenderedShroud.IsVisible(nextPos.ToCPos()))
if (self.World.RenderedShroud.IsVisible(new CPos(conPos)) ||
self.World.RenderedShroud.IsVisible(new CPos(nextPos)))
{
Game.Renderer.WorldLineRenderer.DrawLine(conPos.ToFloat2(), nextPos.ToFloat2(), trailStart, trailEnd);
Game.Renderer.WorldLineRenderer.DrawLine(wr.ScreenPosition(conPos), wr.ScreenPosition(nextPos), trailStart, trailEnd);
trailStart = trailEnd;
trailEnd = Color.FromArgb(trailStart.A - 255 / positions.Count, trailStart.R, trailStart.G, trailStart.B);

View File

@@ -145,7 +145,7 @@ namespace OpenRA.Mods.RA.Effects
}
if (Trail != null)
Trail.Tick(PxPosition - new PVecInt(0, Altitude));
Trail.Tick(PxPosition.ToWPos(Altitude));
}
void Explode(World world)
@@ -163,7 +163,7 @@ namespace OpenRA.Mods.RA.Effects
wr.Palette(Args.weapon.Underwater ? "shadow" : "effect"), PxPosition.Y);
if (Trail != null)
Trail.Render(Args.firedBy);
Trail.Render(wr, Args.firedBy);
}
}
}

View File

@@ -317,7 +317,6 @@
<Compile Include="Render\RenderSpy.cs" />
<Compile Include="Render\RenderUnit.cs" />
<Compile Include="Render\RenderUnitReload.cs" />
<Compile Include="Render\RenderUnitSpinner.cs" />
<Compile Include="Render\RenderUnitTurreted.cs" />
<Compile Include="Render\WithBuildingExplosion.cs" />
<Compile Include="Render\WithMuzzleFlash.cs" />
@@ -423,6 +422,7 @@
<Compile Include="Armament.cs" />
<Compile Include="DebugMuzzlePositions.cs" />
<Compile Include="Buildings\BaseProvider.cs" />
<Compile Include="Render\WithSpinner.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA.Render
anim.Play("turret");
anims.Add("turret_{0}".F(i++), new AnimationWithOffset(anim,
() => PPos.FromWPosHackZ(WPos.Zero + t.Position(self)).ToFloat2(), null));
wr => wr.ScreenPxOffset(t.Position(self)), null));
}
}
}

View File

@@ -1,41 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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 COPYING.
*/
#endregion
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render
{
class RenderUnitSpinnerInfo : RenderUnitInfo
{
public readonly int[] Offset = { 0, 0 };
public override object Create(ActorInitializer init) { return new RenderUnitSpinner(init.self); }
}
class RenderUnitSpinner : RenderUnit
{
public RenderUnitSpinner(Actor self)
: base(self)
{
var info = self.Info.Traits.Get<RenderUnitSpinnerInfo>();
var spinnerAnim = new Animation(GetImage(self));
var facing = self.Trait<IFacing>();
spinnerAnim.PlayRepeating("spinner");
var turret = new Turret(info.Offset);
anims.Add("spinner", new AnimationWithOffset(
spinnerAnim,
() => turret.PxPosition(self, facing).ToFloat2(),
null ) { ZOffset = 1 } );
}
}
}

View File

@@ -37,11 +37,11 @@ namespace OpenRA.Mods.RA.Render
anim.Play("turret");
anims.Add("turret_{0}".F(i++), new AnimationWithOffset(anim,
() => TurretPosition(self, turret, facing), null));
wr => TurretPosition(self, wr, turret, facing), null));
}
}
float2 TurretPosition(Actor self, Turreted t, IFacing facing)
float2 TurretPosition(Actor self, WorldRenderer wr, Turreted t, IFacing facing)
{
var recoil = self.TraitsImplementing<Armament>()
.Where(w => w.Info.Turret == t.Name)
@@ -50,9 +50,9 @@ namespace OpenRA.Mods.RA.Render
var localOffset = new WVec(-recoil, WRange.Zero, WRange.Zero);
var bodyOrientation = QuantizeOrientation(self, self.Orientation);
var turretOrientation = QuantizeOrientation(self, t.LocalOrientation(self));
var worldPos = WPos.Zero + t.Position(self) + LocalToWorld(localOffset.Rotate(turretOrientation).Rotate(bodyOrientation));
var worldPos = t.Position(self) + LocalToWorld(localOffset.Rotate(turretOrientation).Rotate(bodyOrientation));
return PPos.FromWPosHackZ(worldPos).ToFloat2();
return wr.ScreenPxOffset(worldPos);
}
}
}

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Mods.RA.Render
muzzleFlashes.Add("muzzle{0}".F(muzzleFlashes.Count), new AnimationWithOffset(
muzzleFlash,
() => PPos.FromWPosHackZ(WPos.Zero + a.MuzzleOffset(self, barrel)).ToFloat2(),
wr => wr.ScreenPxOffset(a.MuzzleOffset(self, barrel)),
() => !isShowing));
}
}
@@ -63,7 +63,7 @@ namespace OpenRA.Mods.RA.Render
{
foreach (var a in muzzleFlashes.Values)
if (a.DisableFunc == null || !a.DisableFunc())
yield return a.Image(self, wr.Palette("effect"));
yield return a.Image(self, wr, wr.Palette("effect"));
}
public void Tick(Actor self)

View File

@@ -8,6 +8,7 @@
*/
#endregion
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Traits;
@@ -15,8 +16,10 @@ namespace OpenRA.Mods.RA.Render
{
public class WithRotorInfo : ITraitInfo, Requires<RenderSimpleInfo>
{
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
public readonly string Id = "rotor";
public readonly int[] Offset = { 0, 0 };
public object Create(ActorInitializer init) { return new WithRotor(init.self, this); }
}
@@ -26,14 +29,12 @@ namespace OpenRA.Mods.RA.Render
public WithRotor(Actor self, WithRotorInfo info)
{
var rs = self.Trait<RenderSimple>();
var facing = self.Trait<IFacing>();
rotorAnim = new Animation(rs.GetImage(self));
rotorAnim.PlayRepeating("rotor");
var turret = new Turret(info.Offset);
rs.anims.Add(info.Id, new AnimationWithOffset(
rotorAnim,
() => turret.PxPosition(self, facing).ToFloat2(),
wr => wr.ScreenPxOffset(rs.LocalToWorld(info.Offset.Rotate(rs.QuantizeOrientation(self, self.Orientation)))),
null ) { ZOffset = 1 } );
}

View File

@@ -0,0 +1,39 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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 COPYING.
*/
#endregion
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render
{
class WithSpinnerInfo : ITraitInfo, Requires<RenderSimpleInfo>
{
public readonly string Name = "spinner";
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
public object Create(ActorInitializer init) { return new WithSpinner(init.self, this); }
}
class WithSpinner
{
public WithSpinner(Actor self, WithSpinnerInfo info)
{
var rs = self.Trait<RenderSimple>();
var spinner = new Animation(rs.GetImage(self));
spinner.PlayRepeating("spinner");
rs.anims.Add(info.Name, new AnimationWithOffset(
spinner,
wr => wr.ScreenPxOffset(rs.LocalToWorld(info.Offset.Rotate(rs.QuantizeOrientation(self, self.Orientation)))),
null ) { ZOffset = 1 } );
}
}
}

View File

@@ -8,14 +8,16 @@
*/
#endregion
using OpenRA.FileFormats;
using OpenRA.Mods.RA.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SmokeTrailWhenDamagedInfo : ITraitInfo
class SmokeTrailWhenDamagedInfo : ITraitInfo, Requires<LocalCoordinatesModelInfo>
{
public readonly int[] Offset = { 0, 0 };
[Desc("Position relative to body")]
public readonly WVec Offset = WVec.Zero;
public readonly int Interval = 3;
public object Create(ActorInitializer init) { return new SmokeTrailWhenDamaged(init.self, this); }
@@ -23,34 +25,30 @@ namespace OpenRA.Mods.RA
class SmokeTrailWhenDamaged : ITick
{
Turret smokeTurret;
PPos position;
int interval;
ILocalCoordinatesModel coords;
SmokeTrailWhenDamagedInfo info;
int ticks;
public SmokeTrailWhenDamaged(Actor self, SmokeTrailWhenDamagedInfo info)
{
smokeTurret = new Turret(info.Offset);
interval = info.Interval;
this.info = info;
coords = self.Trait<ILocalCoordinatesModel>();
}
public void Tick(Actor self)
{
if (--ticks <= 0)
{
var move = self.Trait<IMove>();
if (move.Altitude > 0 && self.GetDamageState() >= DamageState.Heavy)
var position = self.CenterPosition;
if (position.Z > 0 && self.GetDamageState() >= DamageState.Heavy &&
self.World.RenderedShroud.IsVisible(new CPos(position)))
{
var facing = self.Trait<IFacing>();
var altitude = new PVecInt(0, move.Altitude);
position = (self.CenterLocation - (PVecInt)smokeTurret.PxPosition(self, facing).ToInt2());
if (self.World.RenderedShroud.IsVisible(position.ToCPos()))
self.World.AddFrameEndTask(
w => w.Add(new Smoke(w, position - altitude, "smokey")));
var offset = info.Offset.Rotate(coords.QuantizeOrientation(self, self.Orientation));
var pos = PPos.FromWPosHackZ(position + coords.LocalToWorld(offset));
self.World.AddFrameEndTask(w => w.Add(new Smoke(w, pos, "smokey")));
}
ticks = interval;
ticks = info.Interval;
}
}
}

View File

@@ -8,19 +8,30 @@
*/
#endregion
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class ThrowsParticleInfo : ITraitInfo, Requires<RenderUnitInfo>
class ThrowsParticleInfo : ITraitInfo, Requires<RenderSimpleInfo>
{
public readonly string Anim = null;
public readonly int[] Offset = new[] { 0, 0, 0, 0 };
public readonly int[] Spread = new[] { 0, 0 };
public readonly float Speed = 20;
public readonly string AnimKey = null;
[Desc("Initial position relative to body")]
public readonly WVec Offset = WVec.Zero;
[Desc("Maximum distance to throw the particle")]
public readonly WRange ThrowRange = new WRange(768);
[Desc("Maximum height to throw the particle")]
public readonly WRange ThrowHeight = new WRange(256);
[Desc("Number of ticks to animate")]
public readonly int Length = 15;
[Desc("Maximum rotation rate")]
public readonly float ROT = 15;
public object Create(ActorInitializer init) { return new ThrowsParticle(init, this); }
@@ -28,51 +39,56 @@ namespace OpenRA.Mods.RA
class ThrowsParticle : ITick
{
float2 pos;
float alt;
ThrowsParticleInfo info;
WVec pos;
WVec initialPos;
WVec finalPos;
int tick = 0;
float2 v;
float va;
float facing;
float dfacing;
const float gravity = 1.3f;
float rotation;
public ThrowsParticle(ActorInitializer init, ThrowsParticleInfo info)
{
this.info = info;
var self = init.self;
var ifacing = self.Trait<IFacing>();
var ru = self.Trait<RenderUnit>();
var rs = self.Trait<RenderSimple>();
alt = 0;
facing = Turreted.GetInitialTurretFacing( init, 0 );
pos = new Turret(info.Offset).PxPosition(self, ifacing).ToFloat2();
// TODO: Carry orientation over from the parent instead of just facing
var bodyFacing = init.Contains<FacingInit>() ? init.Get<FacingInit,int>() : 0;
facing = Turreted.GetInitialTurretFacing(init, 0);
v = Game.CosmeticRandom.Gauss2D(1) * info.Spread.RelOffset();
dfacing = Game.CosmeticRandom.Gauss1D(2) * info.ROT;
va = info.Speed;
// Calculate final position
var throwRotation = WRot.FromFacing(Game.CosmeticRandom.Next(1024));
var throwOffset = new WVec((int)(Game.CosmeticRandom.Gauss1D(1)*info.ThrowRange.Range), 0, 0).Rotate(throwRotation);
var anim = new Animation(ru.GetImage(self), () => (int)facing);
initialPos = pos = info.Offset.Rotate(rs.QuantizeOrientation(self, WRot.FromFacing(bodyFacing)));
finalPos = initialPos + throwOffset;
// Facing rotation
rotation = Game.CosmeticRandom.Gauss1D(2) * info.ROT;
var anim = new Animation(rs.GetImage(self), () => (int)facing);
anim.PlayRepeating(info.Anim);
ru.anims.Add(info.AnimKey, new AnimationWithOffset(
anim, () => pos - new float2(0, alt), null));
rs.anims.Add(info.Anim, new AnimationWithOffset(anim, wr => wr.ScreenPxOffset(pos), null));
}
public void Tick(Actor self)
{
va -= gravity;
alt += va;
if (tick == info.Length)
return;
tick++;
if (alt < 0) alt = 0;
else
{
pos += v;
v = .9f * v;
// Lerp position horizontally and height along a sinusoid using a cubic ease
var t = (tick*tick*tick / (info.Length*info.Length) - 3*tick*tick / info.Length + 3*tick);
var tp = WVec.Lerp(initialPos, finalPos, t, info.Length);
var th = new WAngle(512*(info.Length - t) / info.Length).Sin()*info.ThrowHeight.Range / 1024;
pos = new WVec(tp.X, tp.Y, th);
facing += dfacing;
dfacing *= .9f;
}
// Spin the particle
facing += rotation;
rotation *= .9f;
}
}
}

View File

@@ -95,31 +95,4 @@ namespace OpenRA.Mods.RA
return WRot.FromYaw(WAngle.FromFacing(turretFacing) - self.Orientation.Yaw);
}
}
// TODO: Remove this
public class Turret
{
public PVecInt UnitSpacePosition; // where, in the unit's local space.
public PVecInt ScreenSpacePosition; // screen-space hack to make things line up good.
public Turret(int[] offset)
{
ScreenSpacePosition = (PVecInt) offset.AbsOffset().ToInt2();
UnitSpacePosition = (PVecInt) offset.RelOffset().ToInt2();
}
public PVecFloat PxPosition(Actor self, IFacing facing)
{
// Things that don't have a rotating base don't need the turrets repositioned
if (facing == null) return ScreenSpacePosition;
var ru = self.TraitOrDefault<RenderUnit>();
var numDirs = (ru != null) ? ru.anim.CurrentSequence.Facings : 8;
var bodyFacing = facing.Facing;
var quantizedFacing = Util.QuantizeFacing(bodyFacing, numDirs) * (256 / numDirs);
return (PVecFloat)Util.RotateVectorByFacing(UnitSpacePosition.ToFloat2(), quantizedFacing, .7f)
+ (PVecFloat)ScreenSpacePosition.ToFloat2();
}
}
}