From ca7a17ea06a56b072dab06725baddcc91579ad92 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Wed, 13 Apr 2016 21:01:56 +0200 Subject: [PATCH 1/5] Add facings support to SpriteEffect --- OpenRA.Game/Effects/SpriteEffect.cs | 4 ++-- OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs | 2 +- OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/OpenRA.Game/Effects/SpriteEffect.cs b/OpenRA.Game/Effects/SpriteEffect.cs index d3a8d00911..557390691e 100644 --- a/OpenRA.Game/Effects/SpriteEffect.cs +++ b/OpenRA.Game/Effects/SpriteEffect.cs @@ -23,14 +23,14 @@ namespace OpenRA.Effects readonly bool visibleThroughFog; readonly bool scaleSizeWithZoom; - public SpriteEffect(WPos pos, World world, string image, string sequence, string palette, bool visibleThroughFog = false, bool scaleSizeWithZoom = false) + public SpriteEffect(WPos pos, World world, string image, string sequence, string palette, int facing = 0, bool visibleThroughFog = false, bool scaleSizeWithZoom = false) { this.world = world; this.pos = pos; this.palette = palette; this.scaleSizeWithZoom = scaleSizeWithZoom; this.visibleThroughFog = visibleThroughFog; - anim = new Animation(world, image); + anim = new Animation(world, image, () => facing); anim.PlayThen(sequence, () => world.AddFrameEndTask(w => w.Remove(this))); } diff --git a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs index 386033902e..24717d2105 100644 --- a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs +++ b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs @@ -199,7 +199,7 @@ namespace OpenRA.Widgets else if (o.TargetLocation != CPos.Zero) { var pos = world.Map.CenterOfCell(cell); - world.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, world, "moveflsh", "idle", "moveflash", true, true))); + world.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, world, "moveflsh", "idle", "moveflash", 0, true, true))); flashed = true; } } diff --git a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs index 754b92971e..1670c3ade6 100644 --- a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs +++ b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs @@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Traits.Render if (Info.TerrainTypes.Contains(type) && !string.IsNullOrEmpty(Info.Image)) self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, self.World, Info.Image, - Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, Info.VisibleThroughFog))); + Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, 0, Info.VisibleThroughFog))); cachedPosition = self.CenterPosition; ticks = 0; From 866e0b3577eed79e1da16a2857212ae23bf18314 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Wed, 13 Apr 2016 21:27:31 +0200 Subject: [PATCH 2/5] Add facings support, start delay and offset controls to LeavesTrails --- .../Traits/Render/LeavesTrails.cs | 53 ++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs index 1670c3ade6..6d026e7f2c 100644 --- a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs +++ b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs @@ -49,41 +49,78 @@ namespace OpenRA.Mods.Common.Traits.Render [Desc("Delay between trail updates when moving.")] public readonly int MovingInterval = 0; + [Desc("Delay before first trail.")] + public readonly int StartDelay = 0; + + [Desc("Position relative to body.")] + public readonly WVec Offset = WVec.Zero; + + [Desc("Use opposite offset for every second spawned trail.")] + public readonly bool AlternateOffset = false; + public override object Create(ActorInitializer init) { return new LeavesTrails(init.Self, this); } } - public class LeavesTrails : UpgradableTrait, ITick + public class LeavesTrails : UpgradableTrait, ITick, INotifyCreated { + BodyOrientation body; + IFacing facing; + int cachedFacing; + int cachedInterval; + public LeavesTrails(Actor self, LeavesTrailsInfo info) - : base(info) { } + : base(info) + { + cachedInterval = Info.StartDelay; + } + + public void Created(Actor self) + { + body = self.Trait(); + facing = self.TraitOrDefault(); + cachedFacing = facing != null ? facing.Facing : 0; + } WPos cachedPosition; int ticks; + bool evenNumber; + bool wasStationary; + bool isMoving; public void Tick(Actor self) { if (IsTraitDisabled) return; - var isMoving = self.CenterPosition != cachedPosition; + wasStationary = !isMoving; + isMoving = self.CenterPosition != cachedPosition; if ((isMoving && !Info.TrailWhileMoving) || (!isMoving && !Info.TrailWhileStationary)) return; - var interval = isMoving ? Info.MovingInterval : Info.StationaryInterval; - if (++ticks >= interval) + if (isMoving && wasStationary) + cachedInterval = Info.StartDelay; + + if (++ticks >= cachedInterval) { var cachedCell = self.World.Map.CellContaining(cachedPosition); var type = self.World.Map.GetTerrainInfo(cachedCell).Type; - var pos = Info.Type == TrailType.CenterPosition ? cachedPosition : - self.World.Map.CenterOfCell(cachedCell); + var offset = Info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation)); + var pos = Info.Type == TrailType.CenterPosition ? cachedPosition + body.LocalToWorld(Info.AlternateOffset && evenNumber ? -offset : offset) + : self.World.Map.CenterOfCell(cachedCell); if (Info.TerrainTypes.Contains(type) && !string.IsNullOrEmpty(Info.Image)) self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, self.World, Info.Image, - Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, 0, Info.VisibleThroughFog))); + Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, cachedFacing, Info.VisibleThroughFog))); cachedPosition = self.CenterPosition; + cachedFacing = facing != null ? facing.Facing : 0; ticks = 0; + + if (!evenNumber) + evenNumber ^= true; + + cachedInterval = isMoving && !wasStationary ? Info.MovingInterval : Info.StationaryInterval; } } From 04d075ca9f55af41893ab16c112cad3bfbcfce9c Mon Sep 17 00:00:00 2001 From: reaperrr Date: Wed, 13 Apr 2016 21:42:53 +0200 Subject: [PATCH 3/5] Add control over whether trails should spawn with or without delay --- .../Traits/Render/LeavesTrails.cs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs index 6d026e7f2c..c6d03734b1 100644 --- a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs +++ b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs @@ -58,6 +58,9 @@ namespace OpenRA.Mods.Common.Traits.Render [Desc("Use opposite offset for every second spawned trail.")] public readonly bool AlternateOffset = false; + [Desc("Should the trail spawn at the last position or at current position?")] + public readonly bool SpawnAtLastPosition = true; + public override object Create(ActorInitializer init) { return new LeavesTrails(init.Self, this); } } @@ -74,14 +77,15 @@ namespace OpenRA.Mods.Common.Traits.Render cachedInterval = Info.StartDelay; } + WPos cachedPosition; public void Created(Actor self) { body = self.Trait(); facing = self.TraitOrDefault(); cachedFacing = facing != null ? facing.Facing : 0; + cachedPosition = self.CenterPosition; } - WPos cachedPosition; int ticks; bool evenNumber; bool wasStationary; @@ -102,16 +106,21 @@ namespace OpenRA.Mods.Common.Traits.Render if (++ticks >= cachedInterval) { - var cachedCell = self.World.Map.CellContaining(cachedPosition); - var type = self.World.Map.GetTerrainInfo(cachedCell).Type; + var spawnCell = Info.SpawnAtLastPosition ? self.World.Map.CellContaining(cachedPosition) : self.World.Map.CellContaining(self.CenterPosition); + var type = self.World.Map.GetTerrainInfo(spawnCell).Type; - var offset = Info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation)); - var pos = Info.Type == TrailType.CenterPosition ? cachedPosition + body.LocalToWorld(Info.AlternateOffset && evenNumber ? -offset : offset) - : self.World.Map.CenterOfCell(cachedCell); + var offsetRotation = Info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation)); + var spawnOffset = body.LocalToWorld(Info.AlternateOffset && evenNumber ? -offsetRotation : offsetRotation); + var spawnPosition = Info.SpawnAtLastPosition ? cachedPosition : self.CenterPosition; + + var pos = Info.Type == TrailType.CenterPosition ? spawnPosition + spawnOffset : + self.World.Map.CenterOfCell(spawnCell); + + var spawnFacing = Info.SpawnAtLastPosition ? cachedFacing : (facing != null ? facing.Facing : 0); if (Info.TerrainTypes.Contains(type) && !string.IsNullOrEmpty(Info.Image)) self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, self.World, Info.Image, - Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, cachedFacing, Info.VisibleThroughFog))); + Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, spawnFacing, Info.VisibleThroughFog))); cachedPosition = self.CenterPosition; cachedFacing = facing != null ? facing.Facing : 0; From 62aabffed961a65499cd735fb8d811627037935a Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 14 Apr 2016 23:55:57 +0200 Subject: [PATCH 4/5] Make LeavesTrails loop through a list of offsets Instead of the unflexible 'AlternateOffset' approach. --- .../Traits/Render/LeavesTrails.cs | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs index c6d03734b1..16613ee989 100644 --- a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs +++ b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs @@ -52,13 +52,10 @@ namespace OpenRA.Mods.Common.Traits.Render [Desc("Delay before first trail.")] public readonly int StartDelay = 0; - [Desc("Position relative to body.")] - public readonly WVec Offset = WVec.Zero; + [Desc("Trail spawn positions relative to actor position. (forward, right, up) triples")] + public readonly WVec[] Offsets = { WVec.Zero }; - [Desc("Use opposite offset for every second spawned trail.")] - public readonly bool AlternateOffset = false; - - [Desc("Should the trail spawn at the last position or at current position?")] + [Desc("Should the trail spawn relative to last position or current position?")] public readonly bool SpawnAtLastPosition = true; public override object Create(ActorInitializer init) { return new LeavesTrails(init.Self, this); } @@ -87,7 +84,7 @@ namespace OpenRA.Mods.Common.Traits.Render } int ticks; - bool evenNumber; + int offset; bool wasStationary; bool isMoving; @@ -109,11 +106,13 @@ namespace OpenRA.Mods.Common.Traits.Render var spawnCell = Info.SpawnAtLastPosition ? self.World.Map.CellContaining(cachedPosition) : self.World.Map.CellContaining(self.CenterPosition); var type = self.World.Map.GetTerrainInfo(spawnCell).Type; - var offsetRotation = Info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation)); - var spawnOffset = body.LocalToWorld(Info.AlternateOffset && evenNumber ? -offsetRotation : offsetRotation); + if (++offset >= Info.Offsets.Length) + offset = 0; + + var offsetRotation = Info.Offsets[offset].Rotate(body.QuantizeOrientation(self, self.Orientation)); var spawnPosition = Info.SpawnAtLastPosition ? cachedPosition : self.CenterPosition; - var pos = Info.Type == TrailType.CenterPosition ? spawnPosition + spawnOffset : + var pos = Info.Type == TrailType.CenterPosition ? spawnPosition + body.LocalToWorld(offsetRotation) : self.World.Map.CenterOfCell(spawnCell); var spawnFacing = Info.SpawnAtLastPosition ? cachedFacing : (facing != null ? facing.Facing : 0); @@ -126,9 +125,6 @@ namespace OpenRA.Mods.Common.Traits.Render cachedFacing = facing != null ? facing.Facing : 0; ticks = 0; - if (!evenNumber) - evenNumber ^= true; - cachedInterval = isMoving && !wasStationary ? Info.MovingInterval : Info.StationaryInterval; } } From dbd7606667ea59e6274b45ede6ec19659bf173d1 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Fri, 20 May 2016 23:24:33 +0200 Subject: [PATCH 5/5] Move SpriteEffect facing parameter to the end To avoid potential incompatibilities with downstream projects. --- OpenRA.Game/Effects/SpriteEffect.cs | 2 +- OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs | 2 +- OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OpenRA.Game/Effects/SpriteEffect.cs b/OpenRA.Game/Effects/SpriteEffect.cs index 557390691e..1976de8012 100644 --- a/OpenRA.Game/Effects/SpriteEffect.cs +++ b/OpenRA.Game/Effects/SpriteEffect.cs @@ -23,7 +23,7 @@ namespace OpenRA.Effects readonly bool visibleThroughFog; readonly bool scaleSizeWithZoom; - public SpriteEffect(WPos pos, World world, string image, string sequence, string palette, int facing = 0, bool visibleThroughFog = false, bool scaleSizeWithZoom = false) + public SpriteEffect(WPos pos, World world, string image, string sequence, string palette, bool visibleThroughFog = false, bool scaleSizeWithZoom = false, int facing = 0) { this.world = world; this.pos = pos; diff --git a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs index 24717d2105..386033902e 100644 --- a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs +++ b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs @@ -199,7 +199,7 @@ namespace OpenRA.Widgets else if (o.TargetLocation != CPos.Zero) { var pos = world.Map.CenterOfCell(cell); - world.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, world, "moveflsh", "idle", "moveflash", 0, true, true))); + world.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, world, "moveflsh", "idle", "moveflash", true, true))); flashed = true; } } diff --git a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs index 16613ee989..f8f85ca5eb 100644 --- a/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs +++ b/OpenRA.Mods.Common/Traits/Render/LeavesTrails.cs @@ -119,7 +119,7 @@ namespace OpenRA.Mods.Common.Traits.Render if (Info.TerrainTypes.Contains(type) && !string.IsNullOrEmpty(Info.Image)) self.World.AddFrameEndTask(w => w.Add(new SpriteEffect(pos, self.World, Info.Image, - Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, spawnFacing, Info.VisibleThroughFog))); + Info.Sequences.Random(Game.CosmeticRandom), Info.Palette, Info.VisibleThroughFog, false, spawnFacing))); cachedPosition = self.CenterPosition; cachedFacing = facing != null ? facing.Facing : 0;