From 8c7f77d2c76b170080ce42f41f880c7dcf825cbd Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 9 Dec 2013 23:22:01 +1300 Subject: [PATCH] Convert SmudgeLayer to sequences. --- OpenRA.Game/Graphics/SequenceProvider.cs | 9 ++++ OpenRA.Mods.RA/World/SmudgeLayer.cs | 61 +++++++++++++++++------- mods/cnc/rules/system.yaml | 6 +-- mods/cnc/sequences/misc.yaml | 32 ++++++++++++- mods/d2k/rules/system.yaml | 6 +-- mods/d2k/sequences/misc.yaml | 12 +++++ mods/ra/rules/system.yaml | 6 +-- mods/ra/sequences/misc.yaml | 32 ++++++++++++- 8 files changed, 133 insertions(+), 31 deletions(-) diff --git a/OpenRA.Game/Graphics/SequenceProvider.cs b/OpenRA.Game/Graphics/SequenceProvider.cs index ea148f316c..02e2fe8cd5 100644 --- a/OpenRA.Game/Graphics/SequenceProvider.cs +++ b/OpenRA.Game/Graphics/SequenceProvider.cs @@ -68,5 +68,14 @@ namespace OpenRA.Graphics return units[unit].ContainsKey(seq); } + + public static IEnumerable Sequences(string unit) + { + if (!units.ContainsKey(unit)) + throw new InvalidOperationException( + "Unit `{0}` does not have all sequences defined.".F(unit)); + + return units[unit].Keys; + } } } diff --git a/OpenRA.Mods.RA/World/SmudgeLayer.cs b/OpenRA.Mods.RA/World/SmudgeLayer.cs index 47ac2709fc..49ef8f9551 100644 --- a/OpenRA.Mods.RA/World/SmudgeLayer.cs +++ b/OpenRA.Mods.RA/World/SmudgeLayer.cs @@ -21,36 +21,61 @@ namespace OpenRA.Mods.RA public class SmudgeLayerInfo : ITraitInfo { public readonly string Type = "Scorch"; - public readonly string[] Types = { "sc1", "sc2", "sc3", "sc4", "sc5", "sc6" }; - public readonly int[] Depths = { 1, 1, 1, 1, 1, 1 }; + public readonly string Sequence = "scorch"; + public readonly int SmokePercentage = 25; public readonly string SmokeType = "smoke_m"; + public object Create(ActorInitializer init) { return new SmudgeLayer(this); } } public class SmudgeLayer : IRenderOverlay, IWorldLoaded, ITickRender { + struct Smudge + { + public string Type; + public int Depth; + public Sprite Sprite; + } + public SmudgeLayerInfo Info; - Dictionary> tiles; - Dictionary> dirty; - Sprite[][] smudgeSprites; + Dictionary tiles; + Dictionary dirty; + Dictionary smudges; World world; public SmudgeLayer(SmudgeLayerInfo info) { this.Info = info; - smudgeSprites = Info.Types.Select(x => Game.modData.SpriteLoader.LoadAllSprites(x)).ToArray(); } public void WorldLoaded(World w, WorldRenderer wr) { world = w; - tiles = new Dictionary>(); - dirty = new Dictionary>(); + tiles = new Dictionary(); + dirty = new Dictionary(); + smudges = new Dictionary(); + + var types = SequenceProvider.Sequences(Info.Sequence); + foreach (var t in types) + { + var seq = SequenceProvider.GetSequence(Info.Sequence, t); + var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x)); + smudges.Add(t, sprites); + } // Add map smudges - foreach (var s in w.Map.Smudges.Value.Where(s => Info.Types.Contains(s.Type))) - tiles.Add((CPos)s.Location, new TileReference((byte)(Array.IndexOf(Info.Types, s.Type) + 1), (byte)s.Depth)); + foreach (var s in w.Map.Smudges.Value.Where(s => smudges.Keys.Contains(s.Type))) + { + var smudge = new Smudge + { + Type = s.Type, + Depth = s.Depth, + Sprite = smudges[s.Type][s.Depth] + }; + + tiles.Add((CPos)s.Location, smudge); + } } public void AddSmudge(CPos loc) @@ -61,16 +86,19 @@ namespace OpenRA.Mods.RA if (!dirty.ContainsKey(loc) && !tiles.ContainsKey(loc)) { // No smudge; create a new one - var st = (byte)(1 + world.SharedRandom.Next(Info.Types.Length - 1)); - dirty[loc] = new TileReference(st, (byte)0); + var st = smudges.Keys.Random(world.SharedRandom); + dirty[loc] = new Smudge { Type = st, Depth = 0, Sprite = smudges[st][0] }; } else { // Existing smudge; make it deeper var tile = dirty.ContainsKey(loc) ? dirty[loc] : tiles[loc]; - var depth = Info.Depths[tile.Type - 1]; - if (tile.Index < depth - 1) - tile.Index++; + var maxDepth = smudges[tile.Type].Length; + if (tile.Depth < maxDepth - 1) + { + tile.Depth++; + tile.Sprite = smudges[tile.Type][tile.Depth]; + } dirty[loc] = tile; } @@ -105,8 +133,7 @@ namespace OpenRA.Mods.RA if (world.ShroudObscures(kv.Key)) continue; - var tile = smudgeSprites[kv.Value.Type - 1][kv.Value.Index]; - new SpriteRenderable(tile, kv.Key.CenterPosition, + new SpriteRenderable(kv.Value.Sprite, kv.Key.CenterPosition, WVec.Zero, -511, pal, 1f, true).Render(wr); } } diff --git a/mods/cnc/rules/system.yaml b/mods/cnc/rules/system.yaml index ca3a0f8a1a..0f9110f9ac 100644 --- a/mods/cnc/rules/system.yaml +++ b/mods/cnc/rules/system.yaml @@ -313,13 +313,11 @@ World: AllowUnderActors: false SmudgeLayer@SCORCH: Type:Scorch + Sequence: scorches SmokePercentage:50 - Types:sc1,sc2,sc3,sc4,sc5,sc6 - Depths:1,1,1,1,1,1 SmudgeLayer@CRATER: Type:Crater - Types:cr1,cr2,cr3,cr4,cr5,cr6 - Depths:5,5,5,5,5,5 + Sequence: craters PathfinderDebugOverlay: SpawnMapActors: MPStartLocations: diff --git a/mods/cnc/sequences/misc.yaml b/mods/cnc/sequences/misc.yaml index ad893668bd..809742e8d5 100644 --- a/mods/cnc/sequences/misc.yaml +++ b/mods/cnc/sequences/misc.yaml @@ -374,4 +374,34 @@ shroud: Length: 12 typed: shadow Start: 36 - Length: 12 \ No newline at end of file + Length: 12 + +# Note: The order of smudges and craters determines +# the index that is mapped to them in maps +scorches: + sc1: sc1 + Length: * + sc2: sc2 + Length: * + sc3: sc3 + Length: * + sc4: sc4 + Length: * + sc5: sc5 + Length: * + sc6: sc6 + Length: * + +craters: + cr1: cr1 + Length: * + cr2: cr2 + Length: * + cr3: cr3 + Length: * + cr4: cr4 + Length: * + cr5: cr5 + Length: * + cr6: cr6 + Length: * diff --git a/mods/d2k/rules/system.yaml b/mods/d2k/rules/system.yaml index 1079401928..a9fc3dd967 100644 --- a/mods/d2k/rules/system.yaml +++ b/mods/d2k/rules/system.yaml @@ -463,13 +463,11 @@ World: AllowUnderActors: false SmudgeLayer@Rock: Type: RockCrater - Types: rockcrater1, rockcrater2 - Depths: 15, 15 + Sequence: rockcraters SmokePercentage: 0 SmudgeLayer@Sand: Type: SandCrater - Types: sandcrater1, sandcrater2 - Depths: 15, 15 + Sequence: sandcraters SmokePercentage: 0 SpawnMapActors: CreateMPPlayers: diff --git a/mods/d2k/sequences/misc.yaml b/mods/d2k/sequences/misc.yaml index 533476230d..91f1b156ca 100644 --- a/mods/d2k/sequences/misc.yaml +++ b/mods/d2k/sequences/misc.yaml @@ -333,3 +333,15 @@ shroud: Length: 14 Offset: -16,-16 BlendMode: Multiply + +rockcraters: + rockcrater1: rockcrater1 + Length: * + rockcrater2: rockcrater2 + Length: * + +sandcraters: + sandcrater1: sandcrater1 + Length: * + sandcrater2: sandcrater2 + Length: * diff --git a/mods/ra/rules/system.yaml b/mods/ra/rules/system.yaml index d125c58b59..d45c03dd77 100644 --- a/mods/ra/rules/system.yaml +++ b/mods/ra/rules/system.yaml @@ -655,13 +655,11 @@ World: TerrainType: Gems SmudgeLayer@SCORCH: Type:Scorch + Sequence: scorches SmokePercentage:50 - Types:sc1,sc2,sc3,sc4,sc5,sc6 - Depths:1,1,1,1,1,1 SmudgeLayer@CRATER: Type:Crater - Types:cr1,cr2,cr3,cr4,cr5,cr6 - Depths:5,5,5,5,5,5 + Sequence: craters PathfinderDebugOverlay: SpawnMapActors: CreateMPPlayers: diff --git a/mods/ra/sequences/misc.yaml b/mods/ra/sequences/misc.yaml index 5db38a16db..e1255f9ac6 100644 --- a/mods/ra/sequences/misc.yaml +++ b/mods/ra/sequences/misc.yaml @@ -490,4 +490,34 @@ resources: shroud: shroud: shadow - Length: * \ No newline at end of file + Length: * + +# Note: The order of smudges and craters determines +# the index that is mapped to them in maps +scorches: + sc1: sc1 + Length: * + sc2: sc2 + Length: * + sc3: sc3 + Length: * + sc4: sc4 + Length: * + sc5: sc5 + Length: * + sc6: sc6 + Length: * + +craters: + cr1: cr1 + Length: * + cr2: cr2 + Length: * + cr3: cr3 + Length: * + cr4: cr4 + Length: * + cr5: cr5 + Length: * + cr6: cr6 + Length: *