Separated resource rendering into another trait
This commit is contained in:
@@ -24,64 +24,38 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public virtual object Create(ActorInitializer init) { return new ResourceLayer(init.Self); }
|
||||
}
|
||||
|
||||
public class ResourceLayer : IRenderOverlay, IWorldLoaded, ITickRender, INotifyActorDisposing
|
||||
public class ResourceLayer : IWorldLoaded
|
||||
{
|
||||
static readonly CellContents EmptyCell = default(CellContents);
|
||||
|
||||
readonly World world;
|
||||
readonly BuildingInfluence buildingInfluence;
|
||||
readonly HashSet<CPos> dirty = new HashSet<CPos>();
|
||||
readonly Dictionary<PaletteReference, TerrainSpriteLayer> spriteLayers = new Dictionary<PaletteReference, TerrainSpriteLayer>();
|
||||
|
||||
protected readonly CellLayer<CellContents> Content;
|
||||
protected readonly CellLayer<CellContents> RenderContent;
|
||||
|
||||
public bool IsResourceLayerEmpty { get { return resCells < 1; } }
|
||||
|
||||
bool disposed;
|
||||
int resCells;
|
||||
|
||||
public event Action<CPos, ResourceType> CellChanged;
|
||||
|
||||
public ResourceLayer(Actor self)
|
||||
{
|
||||
world = self.World;
|
||||
buildingInfluence = self.Trait<BuildingInfluence>();
|
||||
|
||||
Content = new CellLayer<CellContents>(world.Map);
|
||||
RenderContent = new CellLayer<CellContents>(world.Map);
|
||||
|
||||
RenderContent.CellEntryChanged += UpdateSpriteLayers;
|
||||
}
|
||||
|
||||
void UpdateSpriteLayers(CPos cell)
|
||||
{
|
||||
var resource = RenderContent[cell];
|
||||
foreach (var kv in spriteLayers)
|
||||
{
|
||||
// resource.Type is meaningless (and may be null) if resource.Sprite is null
|
||||
if (resource.Sprite != null && resource.Type.Palette == kv.Key)
|
||||
kv.Value.Update(cell, resource.Sprite);
|
||||
else
|
||||
kv.Value.Update(cell, null);
|
||||
}
|
||||
}
|
||||
|
||||
void IRenderOverlay.Render(WorldRenderer wr)
|
||||
{
|
||||
foreach (var kv in spriteLayers.Values)
|
||||
kv.Draw(wr.Viewport);
|
||||
}
|
||||
|
||||
int GetAdjacentCellsWith(ResourceType t, CPos cell)
|
||||
{
|
||||
var sum = 0;
|
||||
for (var u = -1; u < 2; u++)
|
||||
var directions = CVec.Directions;
|
||||
for (var i = 0; i < directions.Length; i++)
|
||||
{
|
||||
for (var v = -1; v < 2; v++)
|
||||
{
|
||||
var c = cell + new CVec(u, v);
|
||||
if (Content.Contains(c) && Content[c].Type == t)
|
||||
++sum;
|
||||
}
|
||||
var c = cell + directions[i];
|
||||
if (Content.Contains(c) && Content[c].Type == t)
|
||||
++sum;
|
||||
}
|
||||
|
||||
return sum;
|
||||
@@ -92,27 +66,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
var resources = w.WorldActor.TraitsImplementing<ResourceType>()
|
||||
.ToDictionary(r => r.Info.ResourceType, r => r);
|
||||
|
||||
// Build the sprite layer dictionary for rendering resources
|
||||
// All resources that have the same palette must also share a sheet and blend mode
|
||||
foreach (var r in resources)
|
||||
{
|
||||
var layer = spriteLayers.GetOrAdd(r.Value.Palette, pal =>
|
||||
{
|
||||
var first = r.Value.Variants.First().Value.First();
|
||||
return new TerrainSpriteLayer(w, wr, first.Sheet, first.BlendMode, pal, wr.World.Type != WorldType.Editor);
|
||||
});
|
||||
|
||||
// Validate that sprites are compatible with this layer
|
||||
var sheet = layer.Sheet;
|
||||
if (r.Value.Variants.Any(kv => kv.Value.Any(s => s.Sheet != sheet)))
|
||||
throw new InvalidDataException("Resource sprites span multiple sheets. Try loading their sequences earlier.");
|
||||
|
||||
var blendMode = layer.BlendMode;
|
||||
if (r.Value.Variants.Any(kv => kv.Value.Any(s => s.BlendMode != blendMode)))
|
||||
throw new InvalidDataException("Resource sprites specify different blend modes. "
|
||||
+ "Try using different palettes for resource types that use different blend modes.");
|
||||
}
|
||||
|
||||
foreach (var cell in w.Map.AllCells)
|
||||
{
|
||||
ResourceType t;
|
||||
@@ -127,61 +80,21 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
foreach (var cell in w.Map.AllCells)
|
||||
{
|
||||
var type = Content[cell].Type;
|
||||
var type = GetResourceType(cell);
|
||||
if (type != null)
|
||||
{
|
||||
// Set initial density based on the number of neighboring resources
|
||||
// Adjacent includes the current cell, so is always >= 1
|
||||
var adjacent = GetAdjacentCellsWith(type, cell);
|
||||
var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9);
|
||||
var temp = Content[cell];
|
||||
var temp = GetResource(cell);
|
||||
temp.Density = Math.Max(density, 1);
|
||||
|
||||
// Initialize the RenderContent with the initial map state
|
||||
// because the shroud may not be enabled.
|
||||
RenderContent[cell] = Content[cell] = temp;
|
||||
UpdateRenderedSprite(cell);
|
||||
Content[cell] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void UpdateRenderedSprite(CPos cell)
|
||||
{
|
||||
var t = RenderContent[cell];
|
||||
if (t.Density > 0)
|
||||
{
|
||||
var sprites = t.Type.Variants[t.Variant];
|
||||
var frame = int2.Lerp(0, sprites.Length - 1, t.Density, t.Type.Info.MaxDensity);
|
||||
t.Sprite = sprites[frame];
|
||||
}
|
||||
else
|
||||
t.Sprite = null;
|
||||
|
||||
RenderContent[cell] = t;
|
||||
}
|
||||
|
||||
protected virtual string ChooseRandomVariant(ResourceType t)
|
||||
{
|
||||
return t.Variants.Keys.Random(Game.CosmeticRandom);
|
||||
}
|
||||
|
||||
void ITickRender.TickRender(WorldRenderer wr, Actor self)
|
||||
{
|
||||
var remove = new List<CPos>();
|
||||
foreach (var c in dirty)
|
||||
{
|
||||
if (!self.World.FogObscures(c))
|
||||
{
|
||||
RenderContent[c] = Content[c];
|
||||
UpdateRenderedSprite(c);
|
||||
remove.Add(c);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var r in remove)
|
||||
dirty.Remove(r);
|
||||
}
|
||||
|
||||
public bool AllowResourceAt(ResourceType rt, CPos cell)
|
||||
{
|
||||
if (!world.Map.Contains(cell))
|
||||
@@ -212,7 +125,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (!world.Map.Contains(cell))
|
||||
return false;
|
||||
|
||||
var currentResourceType = GetResource(cell);
|
||||
var currentResourceType = GetResourceType(cell);
|
||||
return (currentResourceType == newResourceType && !IsFull(cell))
|
||||
|| (currentResourceType == null && AllowResourceAt(newResourceType, cell));
|
||||
}
|
||||
@@ -224,8 +137,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
return new CellContents
|
||||
{
|
||||
Type = t,
|
||||
Variant = ChooseRandomVariant(t),
|
||||
Type = t
|
||||
};
|
||||
}
|
||||
|
||||
@@ -241,12 +153,14 @@ namespace OpenRA.Mods.Common.Traits
|
||||
cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n);
|
||||
Content[p] = cell;
|
||||
|
||||
dirty.Add(p);
|
||||
if (CellChanged != null)
|
||||
CellChanged(p, cell.Type);
|
||||
}
|
||||
|
||||
public bool IsFull(CPos cell)
|
||||
{
|
||||
return Content[cell].Density == Content[cell].Type.Info.MaxDensity;
|
||||
var cellContents = Content[cell];
|
||||
return cellContents.Density == cellContents.Type.Info.MaxDensity;
|
||||
}
|
||||
|
||||
public ResourceType Harvest(CPos cell)
|
||||
@@ -264,7 +178,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
else
|
||||
Content[cell] = c;
|
||||
|
||||
dirty.Add(cell);
|
||||
if (CellChanged != null)
|
||||
CellChanged(cell, c.Type);
|
||||
|
||||
return c.Type;
|
||||
}
|
||||
@@ -272,7 +187,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public void Destroy(CPos cell)
|
||||
{
|
||||
// Don't break other users of CustomTerrain if there are no resources
|
||||
if (Content[cell].Type == null)
|
||||
var c = Content[cell];
|
||||
if (c.Type == null)
|
||||
return;
|
||||
|
||||
--resCells;
|
||||
@@ -281,11 +197,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
Content[cell] = EmptyCell;
|
||||
world.Map.CustomTerrain[cell] = byte.MaxValue;
|
||||
|
||||
dirty.Add(cell);
|
||||
if (CellChanged != null)
|
||||
CellChanged(cell, c.Type);
|
||||
}
|
||||
|
||||
public ResourceType GetResource(CPos cell) { return Content[cell].Type; }
|
||||
public ResourceType GetRenderedResource(CPos cell) { return RenderContent[cell].Type; }
|
||||
public CellContents GetResource(CPos cell) { return Content[cell]; }
|
||||
public ResourceType GetResourceType(CPos cell) { return Content[cell].Type; }
|
||||
|
||||
public int GetResourceDensity(CPos cell) { return Content[cell].Density; }
|
||||
public int GetMaxResourceDensity(CPos cell)
|
||||
{
|
||||
@@ -295,26 +213,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return Content[cell].Type.Info.MaxDensity;
|
||||
}
|
||||
|
||||
void INotifyActorDisposing.Disposing(Actor self)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
foreach (var kv in spriteLayers.Values)
|
||||
kv.Dispose();
|
||||
|
||||
RenderContent.CellEntryChanged -= UpdateSpriteLayers;
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
public struct CellContents
|
||||
{
|
||||
public static readonly CellContents Empty = default(CellContents);
|
||||
public ResourceType Type;
|
||||
public int Density;
|
||||
public string Variant;
|
||||
public Sprite Sprite;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user