From 7c21459b48b157ec61b05c155c9cdf55ac360024 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 31 Mar 2013 19:18:30 +1300 Subject: [PATCH] Convert contrails to world coords. --- OpenRA.FileFormats/WPos.cs | 22 +++++++++++++ OpenRA.Game/CPos.cs | 1 + OpenRA.Game/Traits/Render/RenderSimple.cs | 2 +- OpenRA.Game/Traits/TraitsInterfaces.cs | 1 + OpenRA.Mods.RA/Effects/Bullet.cs | 7 +++-- OpenRA.Mods.RA/Effects/Contrail.cs | 38 ++++++++++++----------- OpenRA.Mods.RA/Effects/Missile.cs | 4 +-- mods/ra/rules/aircraft.yaml | 20 ++++++------ 8 files changed, 63 insertions(+), 32 deletions(-) diff --git a/OpenRA.FileFormats/WPos.cs b/OpenRA.FileFormats/WPos.cs index 6b0c0ec7bd..7800ef8508 100644 --- a/OpenRA.FileFormats/WPos.cs +++ b/OpenRA.FileFormats/WPos.cs @@ -34,6 +34,28 @@ namespace OpenRA public static bool operator ==(WPos me, WPos other) { return (me.X == other.X && me.Y == other.Y && me.Z == other.Z); } public static bool operator !=(WPos me, WPos other) { return !(me == other); } + public static WPos Average(params WPos[] list) + { + if (list == null || list.Length == 0) + return WPos.Zero; + + var x = 0; + var y = 0; + var z = 0; + foreach(var pos in list) + { + x += pos.X; + y += pos.Y; + z += pos.Z; + } + + x /= list.Length; + y /= list.Length; + z /= list.Length; + + return new WPos(x,y,z); + } + public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode(); } public override bool Equals(object obj) diff --git a/OpenRA.Game/CPos.cs b/OpenRA.Game/CPos.cs index 2743ca021a..832aab7998 100644 --- a/OpenRA.Game/CPos.cs +++ b/OpenRA.Game/CPos.cs @@ -21,6 +21,7 @@ namespace OpenRA public readonly int X, Y; public CPos(int x, int y) { X = x; Y = y; } + public CPos(WPos a) { X = a.X / 1024; Y = a.Y / 1024; } public static readonly CPos Zero = new CPos(0, 0); diff --git a/OpenRA.Game/Traits/Render/RenderSimple.cs b/OpenRA.Game/Traits/Render/RenderSimple.cs index 6b8b900204..e6c5eca69d 100755 --- a/OpenRA.Game/Traits/Render/RenderSimple.cs +++ b/OpenRA.Game/Traits/Render/RenderSimple.cs @@ -16,7 +16,7 @@ using OpenRA.FileFormats; namespace OpenRA.Traits { - public class RenderSimpleInfo : ITraitInfo + public class RenderSimpleInfo : ITraitInfo, LocalCoordinatesModelInfo { [Desc("Defaults to the actor name.")] public readonly string Image = null; diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 6550938016..42b75205ab 100755 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -216,6 +216,7 @@ namespace OpenRA.Traits WVec LocalToWorld(WVec vec); WRot QuantizeOrientation(Actor self, WRot orientation); } + public interface LocalCoordinatesModelInfo {} public interface ITargetable { diff --git a/OpenRA.Mods.RA/Effects/Bullet.cs b/OpenRA.Mods.RA/Effects/Bullet.cs index 0aa706e911..5939ed008a 100755 --- a/OpenRA.Mods.RA/Effects/Bullet.cs +++ b/OpenRA.Mods.RA/Effects/Bullet.cs @@ -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 ) diff --git a/OpenRA.Mods.RA/Effects/Contrail.cs b/OpenRA.Mods.RA/Effects/Contrail.cs index 046d5d5ffc..4c0b3ffb4a 100755 --- a/OpenRA.Mods.RA/Effects/Contrail.cs +++ b/OpenRA.Mods.RA/Effects/Contrail.cs @@ -16,9 +16,10 @@ using OpenRA.Traits; namespace OpenRA.Mods.RA { - class ContrailInfo : ITraitInfo + class ContrailInfo : ITraitInfo, Requires { - 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(); - move = self.Trait(); + + coords = self.Trait(); } 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 positions = new List(); + List positions = new List(); 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); diff --git a/OpenRA.Mods.RA/Effects/Missile.cs b/OpenRA.Mods.RA/Effects/Missile.cs index 06047a09f9..f04ec2cecd 100755 --- a/OpenRA.Mods.RA/Effects/Missile.cs +++ b/OpenRA.Mods.RA/Effects/Missile.cs @@ -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); } } } diff --git a/mods/ra/rules/aircraft.yaml b/mods/ra/rules/aircraft.yaml index 9c43313241..d823b06bcb 100644 --- a/mods/ra/rules/aircraft.yaml +++ b/mods/ra/rules/aircraft.yaml @@ -20,9 +20,9 @@ BADR: Tooltip: Name: Badger Contrail@1: - ContrailOffset: 11, -11 + Offset: -469,469,0 Contrail@2: - ContrailOffset: -11, -11 + Offset: -469,-469,0 FallsToEarth: Spins: no Moves: yes @@ -59,9 +59,9 @@ BADR.bomber: Tooltip: Name: Badger Contrail@1: - ContrailOffset: 11, -11 + Offset: 469,469,0 Contrail@2: - ContrailOffset: -11, -11 + Offset: 469,-469,0 FallsToEarth: Spins: no Moves: yes @@ -107,6 +107,7 @@ MIG: Speed: 20 RearmBuildings: afld RenderUnit: + CameraPitch: 99 WithShadow: LimitedAmmo: Ammo: 8 @@ -115,9 +116,9 @@ MIG: Selectable: Bounds: 44,40,0,0 Contrail@1: - ContrailOffset: 16,-14 + Offset: -598,-683,0 Contrail@2: - ContrailOffset: -16,-14 + Offset: -598,683,0 FallsToEarth: Spins: no Moves: yes @@ -160,6 +161,7 @@ YAK: ROT: 5 Speed: 16 RenderUnit: + CameraPitch: 99 WithShadow: LimitedAmmo: Ammo: 18 @@ -169,7 +171,7 @@ YAK: ReturnOnIdle: WithMuzzleFlash: Contrail: - ContrailOffset: 0, -20 + Offset: -853,0,0 FallsToEarth: Spins: no Moves: yes @@ -345,9 +347,9 @@ U2: -Selectable: -GainsExperience: Contrail@1: - ContrailOffset: 16, -17 + Offset: -725,683,0 Contrail@2: - ContrailOffset: -16, -17 + Offset: -725,-683,0 FallsToEarth: Spins: no Moves: yes