Enable use of "custom" palettes per tile Templates

Keep the tileset's palette as default, defined on the tileset, but override it for any tile templates that may want to do so with a palette defined on the template.
This commit is contained in:
Pavel Penev
2015-12-20 15:50:01 +02:00
parent da4f04acfb
commit 0c51d73be9
2 changed files with 24 additions and 6 deletions

View File

@@ -9,25 +9,34 @@
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Graphics
{
sealed class TerrainRenderer : IDisposable
{
readonly TerrainSpriteLayer terrain;
const string TerrainPalette = "terrain";
readonly World world;
readonly Dictionary<string, TerrainSpriteLayer> spriteLayers = new Dictionary<string, TerrainSpriteLayer>();
readonly Theater theater;
readonly CellLayer<TerrainTile> mapTiles;
readonly CellLayer<byte> mapHeight;
public TerrainRenderer(World world, WorldRenderer wr)
{
this.world = world;
theater = wr.Theater;
mapTiles = world.Map.MapTiles.Value;
mapHeight = world.Map.MapHeight.Value;
terrain = new TerrainSpriteLayer(world, wr, theater.Sheet, BlendMode.Alpha,
wr.Palette("terrain"), wr.World.Type != WorldType.Editor);
foreach (var template in world.TileSet.Templates)
{
var palette = template.Value.Palette ?? TerrainPalette;
spriteLayers.GetOrAdd(palette, pal =>
new TerrainSpriteLayer(world, wr, theater.Sheet, BlendMode.Alpha, wr.Palette(palette), wr.World.Type != WorldType.Editor));
}
foreach (var cell in world.Map.AllCells)
UpdateCell(cell);
@@ -38,12 +47,18 @@ namespace OpenRA.Graphics
public void UpdateCell(CPos cell)
{
terrain.Update(cell, theater.TileSprite(mapTiles[cell]));
var tile = mapTiles[cell];
var palette = world.TileSet.Templates[tile.Type].Palette ?? TerrainPalette;
var sprite = theater.TileSprite(tile);
foreach (var kv in spriteLayers)
kv.Value.Update(cell, palette == kv.Key ? sprite : null);
}
public void Draw(WorldRenderer wr, Viewport viewport)
{
terrain.Draw(viewport);
foreach (var kv in spriteLayers.Values)
kv.Draw(wr.Viewport);
foreach (var r in wr.World.WorldActor.TraitsImplementing<IRenderOverlay>())
r.Render(wr);
}
@@ -52,7 +67,9 @@ namespace OpenRA.Graphics
{
mapTiles.CellEntryChanged -= UpdateCell;
mapHeight.CellEntryChanged -= UpdateCell;
terrain.Dispose();
foreach (var kv in spriteLayers.Values)
kv.Dispose();
}
}
}

View File

@@ -73,6 +73,7 @@ namespace OpenRA
public readonly int2 Size;
public readonly bool PickAny;
public readonly string Category;
public readonly string Palette;
readonly TerrainTileInfo[] tileInfo;