Convert SmudgeLayer to sequences.

This commit is contained in:
Paul Chote
2013-12-09 23:22:01 +13:00
parent f5123981fa
commit 8c7f77d2c7
8 changed files with 133 additions and 31 deletions

View File

@@ -68,5 +68,14 @@ namespace OpenRA.Graphics
return units[unit].ContainsKey(seq); return units[unit].ContainsKey(seq);
} }
public static IEnumerable<string> Sequences(string unit)
{
if (!units.ContainsKey(unit))
throw new InvalidOperationException(
"Unit `{0}` does not have all sequences defined.".F(unit));
return units[unit].Keys;
}
} }
} }

View File

@@ -21,36 +21,61 @@ namespace OpenRA.Mods.RA
public class SmudgeLayerInfo : ITraitInfo public class SmudgeLayerInfo : ITraitInfo
{ {
public readonly string Type = "Scorch"; public readonly string Type = "Scorch";
public readonly string[] Types = { "sc1", "sc2", "sc3", "sc4", "sc5", "sc6" }; public readonly string Sequence = "scorch";
public readonly int[] Depths = { 1, 1, 1, 1, 1, 1 };
public readonly int SmokePercentage = 25; public readonly int SmokePercentage = 25;
public readonly string SmokeType = "smoke_m"; public readonly string SmokeType = "smoke_m";
public object Create(ActorInitializer init) { return new SmudgeLayer(this); } public object Create(ActorInitializer init) { return new SmudgeLayer(this); }
} }
public class SmudgeLayer : IRenderOverlay, IWorldLoaded, ITickRender public class SmudgeLayer : IRenderOverlay, IWorldLoaded, ITickRender
{ {
struct Smudge
{
public string Type;
public int Depth;
public Sprite Sprite;
}
public SmudgeLayerInfo Info; public SmudgeLayerInfo Info;
Dictionary<CPos, TileReference<byte, byte>> tiles; Dictionary<CPos, Smudge> tiles;
Dictionary<CPos, TileReference<byte, byte>> dirty; Dictionary<CPos, Smudge> dirty;
Sprite[][] smudgeSprites; Dictionary<string, Sprite[]> smudges;
World world; World world;
public SmudgeLayer(SmudgeLayerInfo info) public SmudgeLayer(SmudgeLayerInfo info)
{ {
this.Info = info; this.Info = info;
smudgeSprites = Info.Types.Select(x => Game.modData.SpriteLoader.LoadAllSprites(x)).ToArray();
} }
public void WorldLoaded(World w, WorldRenderer wr) public void WorldLoaded(World w, WorldRenderer wr)
{ {
world = w; world = w;
tiles = new Dictionary<CPos, TileReference<byte, byte>>(); tiles = new Dictionary<CPos, Smudge>();
dirty = new Dictionary<CPos, TileReference<byte, byte>>(); dirty = new Dictionary<CPos, Smudge>();
smudges = new Dictionary<string, Sprite[]>();
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 // Add map smudges
foreach (var s in w.Map.Smudges.Value.Where(s => Info.Types.Contains(s.Type))) foreach (var s in w.Map.Smudges.Value.Where(s => smudges.Keys.Contains(s.Type)))
tiles.Add((CPos)s.Location, new TileReference<byte, byte>((byte)(Array.IndexOf(Info.Types, s.Type) + 1), (byte)s.Depth)); {
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) public void AddSmudge(CPos loc)
@@ -61,16 +86,19 @@ namespace OpenRA.Mods.RA
if (!dirty.ContainsKey(loc) && !tiles.ContainsKey(loc)) if (!dirty.ContainsKey(loc) && !tiles.ContainsKey(loc))
{ {
// No smudge; create a new one // No smudge; create a new one
var st = (byte)(1 + world.SharedRandom.Next(Info.Types.Length - 1)); var st = smudges.Keys.Random(world.SharedRandom);
dirty[loc] = new TileReference<byte, byte>(st, (byte)0); dirty[loc] = new Smudge { Type = st, Depth = 0, Sprite = smudges[st][0] };
} }
else else
{ {
// Existing smudge; make it deeper // Existing smudge; make it deeper
var tile = dirty.ContainsKey(loc) ? dirty[loc] : tiles[loc]; var tile = dirty.ContainsKey(loc) ? dirty[loc] : tiles[loc];
var depth = Info.Depths[tile.Type - 1]; var maxDepth = smudges[tile.Type].Length;
if (tile.Index < depth - 1) if (tile.Depth < maxDepth - 1)
tile.Index++; {
tile.Depth++;
tile.Sprite = smudges[tile.Type][tile.Depth];
}
dirty[loc] = tile; dirty[loc] = tile;
} }
@@ -105,8 +133,7 @@ namespace OpenRA.Mods.RA
if (world.ShroudObscures(kv.Key)) if (world.ShroudObscures(kv.Key))
continue; continue;
var tile = smudgeSprites[kv.Value.Type - 1][kv.Value.Index]; new SpriteRenderable(kv.Value.Sprite, kv.Key.CenterPosition,
new SpriteRenderable(tile, kv.Key.CenterPosition,
WVec.Zero, -511, pal, 1f, true).Render(wr); WVec.Zero, -511, pal, 1f, true).Render(wr);
} }
} }

View File

@@ -313,13 +313,11 @@ World:
AllowUnderActors: false AllowUnderActors: false
SmudgeLayer@SCORCH: SmudgeLayer@SCORCH:
Type:Scorch Type:Scorch
Sequence: scorches
SmokePercentage:50 SmokePercentage:50
Types:sc1,sc2,sc3,sc4,sc5,sc6
Depths:1,1,1,1,1,1
SmudgeLayer@CRATER: SmudgeLayer@CRATER:
Type:Crater Type:Crater
Types:cr1,cr2,cr3,cr4,cr5,cr6 Sequence: craters
Depths:5,5,5,5,5,5
PathfinderDebugOverlay: PathfinderDebugOverlay:
SpawnMapActors: SpawnMapActors:
MPStartLocations: MPStartLocations:

View File

@@ -374,4 +374,34 @@ shroud:
Length: 12 Length: 12
typed: shadow typed: shadow
Start: 36 Start: 36
Length: 12 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: *

View File

@@ -463,13 +463,11 @@ World:
AllowUnderActors: false AllowUnderActors: false
SmudgeLayer@Rock: SmudgeLayer@Rock:
Type: RockCrater Type: RockCrater
Types: rockcrater1, rockcrater2 Sequence: rockcraters
Depths: 15, 15
SmokePercentage: 0 SmokePercentage: 0
SmudgeLayer@Sand: SmudgeLayer@Sand:
Type: SandCrater Type: SandCrater
Types: sandcrater1, sandcrater2 Sequence: sandcraters
Depths: 15, 15
SmokePercentage: 0 SmokePercentage: 0
SpawnMapActors: SpawnMapActors:
CreateMPPlayers: CreateMPPlayers:

View File

@@ -333,3 +333,15 @@ shroud:
Length: 14 Length: 14
Offset: -16,-16 Offset: -16,-16
BlendMode: Multiply BlendMode: Multiply
rockcraters:
rockcrater1: rockcrater1
Length: *
rockcrater2: rockcrater2
Length: *
sandcraters:
sandcrater1: sandcrater1
Length: *
sandcrater2: sandcrater2
Length: *

View File

@@ -655,13 +655,11 @@ World:
TerrainType: Gems TerrainType: Gems
SmudgeLayer@SCORCH: SmudgeLayer@SCORCH:
Type:Scorch Type:Scorch
Sequence: scorches
SmokePercentage:50 SmokePercentage:50
Types:sc1,sc2,sc3,sc4,sc5,sc6
Depths:1,1,1,1,1,1
SmudgeLayer@CRATER: SmudgeLayer@CRATER:
Type:Crater Type:Crater
Types:cr1,cr2,cr3,cr4,cr5,cr6 Sequence: craters
Depths:5,5,5,5,5,5
PathfinderDebugOverlay: PathfinderDebugOverlay:
SpawnMapActors: SpawnMapActors:
CreateMPPlayers: CreateMPPlayers:

View File

@@ -490,4 +490,34 @@ resources:
shroud: shroud:
shroud: shadow shroud: shadow
Length: * 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: *