Skip to content

Commit

Permalink
Unhardcoded SpriteSequence properties
Browse files Browse the repository at this point in the history
To prepare them for documentation generation.
Also added descriptions to SpriteSequence implementations and their properties.
Also made a few code style fixes.
  • Loading branch information
penev92 authored and pchote committed Jun 26, 2022
1 parent d1f7fb8 commit c3c5dbf
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 85 deletions.
12 changes: 8 additions & 4 deletions OpenRA.Mods.Cnc/Graphics/ClassicSpriteSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@ public override ISpriteSequence CreateSequence(ModData modData, string tileSet,
}
}

[Desc("A sprite sequence that has the oddities that come with first-generation Westwood titles.")]
public class ClassicSpriteSequence : DefaultSpriteSequence
{
readonly bool useClassicFacings;
// This needs to be a public property for the documentation generation to work.
[Desc("Incorporate a compensation factor due to the distortion caused by 3D-Studio " +
"when it tried to render 45% angles which was used by Westwood Studios at that time.")]
public bool UseClassicFacings { get; }

public ClassicSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
: base(modData, tileSet, cache, loader, sequence, animation, info)
{
var d = info.ToDictionary();
useClassicFacings = LoadField(d, "UseClassicFacings", false);
UseClassicFacings = LoadField(d, nameof(UseClassicFacings), UseClassicFacings);

if (useClassicFacings && Facings != 32)
if (UseClassicFacings && Facings != 32)
throw new InvalidOperationException(
$"{info.Nodes[0].Location}: Sequence {sequence}.{animation}: UseClassicFacings is only valid for 32 facings");
}

protected override int GetFacingFrameOffset(WAngle facing)
{
return useClassicFacings ? Util.ClassicIndexFacing(facing, Facings) : Common.Util.IndexFacing(facing, Facings);
return UseClassicFacings ? Util.ClassicIndexFacing(facing, Facings) : Common.Util.IndexFacing(facing, Facings);
}
}
}
31 changes: 24 additions & 7 deletions OpenRA.Mods.Cnc/Graphics/ClassicTilesetSpecificSpriteSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,32 @@ public override ISpriteSequence CreateSequence(ModData modData, string tileSet,
}
}

[Desc("A sprite sequence that can have tileset-specific variants and has the oddities " +
"that come with first-generation Westwood titles.")]
public class ClassicTilesetSpecificSpriteSequence : ClassicSpriteSequence
{
// These need to be public properties for the documentation generation to work.
[Desc("Dictionary of <string: string> with tileset name to override -> tileset name to use instead.")]
public static Dictionary<string, string> TilesetOverrides => null;

[Desc("Use `TilesetCodes` as defined in `mod.yaml` to add a letter as a second character " +
"into the sprite filename like the Westwood 2.5D titles did for tileset-specific variants.")]
public static bool UseTilesetCode => false;

[Desc("Append a tileset-specific extension to the file name " +
"- either as defined in `mod.yaml`'s `TilesetExtensions` (if `UseTilesetExtension` is used) " +
"or the default hardcoded one for this sequence type (.shp).")]
public static bool AddExtension => true;

[Desc("Whether `mod.yaml`'s `TilesetExtensions` should be used with the sequence's file name.")]
public static bool UseTilesetExtension { get; private set; }

public ClassicTilesetSpecificSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
: base(modData, tileSet, cache, loader, sequence, animation, info) { }

string ResolveTilesetId(string tileSet, Dictionary<string, MiniYaml> d)
static string ResolveTilesetId(string tileSet, Dictionary<string, MiniYaml> d)
{
if (d.TryGetValue("TilesetOverrides", out var yaml))
if (d.TryGetValue(nameof(TilesetOverrides), out var yaml))
{
var tsNode = yaml.Nodes.FirstOrDefault(n => n.Key == tileSet);
if (tsNode != null)
Expand All @@ -64,17 +82,16 @@ protected override string GetSpriteSrc(ModData modData, string tileSet, string s

var spriteName = sprite ?? sequence;

if (LoadField(d, "UseTilesetCode", false))
if (LoadField(d, nameof(UseTilesetCode), UseTilesetCode))
{
if (loader.TilesetCodes.TryGetValue(ResolveTilesetId(tileSet, d), out var code))
spriteName = spriteName.Substring(0, 1) + code + spriteName.Substring(2, spriteName.Length - 2);
}

if (LoadField(d, "AddExtension", true))
if (LoadField(d, nameof(AddExtension), AddExtension))
{
var useTilesetExtension = LoadField(d, "UseTilesetExtension", false);

if (useTilesetExtension && loader.TilesetExtensions.TryGetValue(ResolveTilesetId(tileSet, d), out var tilesetExtension))
UseTilesetExtension = LoadField(d, nameof(UseTilesetExtension), UseTilesetExtension);
if (UseTilesetExtension && loader.TilesetExtensions.TryGetValue(ResolveTilesetId(tileSet, d), out var tilesetExtension))
return spriteName + tilesetExtension;

return spriteName + loader.DefaultSpriteExtension;
Expand Down
Loading

0 comments on commit c3c5dbf

Please sign in to comment.