Move ResourceLayer init into the constructor.

Stylecop rules force renaming content and render.
This commit is contained in:
Paul Chote
2015-07-05 13:41:46 +01:00
parent 53f00a7930
commit ae0d9c0e7d
2 changed files with 50 additions and 41 deletions

View File

@@ -17,19 +17,30 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")] [Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")]
public class ResourceLayerInfo : TraitInfo<ResourceLayer>, Requires<ResourceTypeInfo> { } public class ResourceLayerInfo : ITraitInfo, Requires<ResourceTypeInfo>, Requires<BuildingInfluenceInfo>
{
public virtual object Create(ActorInitializer init) { return new ResourceLayer(init.Self); }
}
public class ResourceLayer : IRenderOverlay, IWorldLoaded, ITickRender public class ResourceLayer : IRenderOverlay, IWorldLoaded, ITickRender
{ {
static readonly CellContents EmptyCell = new CellContents(); static readonly CellContents EmptyCell = new CellContents();
World world; readonly World world;
readonly BuildingInfluence buildingInfluence;
readonly List<CPos> dirty = new List<CPos>();
BuildingInfluence buildingInfluence; protected readonly CellLayer<CellContents> Content;
protected readonly CellLayer<CellContents> RenderContent;
protected CellLayer<CellContents> content; public ResourceLayer(Actor self)
protected CellLayer<CellContents> render; {
List<CPos> dirty; world = self.World;
buildingInfluence = world.WorldActor.Trait<BuildingInfluence>();
Content = new CellLayer<CellContents>(world.Map);
RenderContent = new CellLayer<CellContents>(world.Map);
}
public void Render(WorldRenderer wr) public void Render(WorldRenderer wr)
{ {
@@ -39,7 +50,7 @@ namespace OpenRA.Mods.Common.Traits
if (shroudObscured(uv)) if (shroudObscured(uv))
continue; continue;
var c = render[uv]; var c = RenderContent[uv];
if (c.Sprite != null) if (c.Sprite != null)
new SpriteRenderable(c.Sprite, wr.World.Map.CenterOfCell(uv.ToCPos(world.Map)), new SpriteRenderable(c.Sprite, wr.World.Map.CenterOfCell(uv.ToCPos(world.Map)),
WVec.Zero, -511, c.Type.Palette, 1f, true).Render(wr); // TODO ZOffset is ignored WVec.Zero, -511, c.Type.Palette, 1f, true).Render(wr); // TODO ZOffset is ignored
@@ -54,7 +65,7 @@ namespace OpenRA.Mods.Common.Traits
for (var v = -1; v < 2; v++) for (var v = -1; v < 2; v++)
{ {
var c = cell + new CVec(u, v); var c = cell + new CVec(u, v);
if (content.Contains(c) && content[c].Type == t) if (Content.Contains(c) && Content[c].Type == t)
++sum; ++sum;
} }
} }
@@ -64,14 +75,6 @@ namespace OpenRA.Mods.Common.Traits
public void WorldLoaded(World w, WorldRenderer wr) public void WorldLoaded(World w, WorldRenderer wr)
{ {
this.world = w;
buildingInfluence = world.WorldActor.Trait<BuildingInfluence>();
content = new CellLayer<CellContents>(w.Map);
render = new CellLayer<CellContents>(w.Map);
dirty = new List<CPos>();
var resources = w.WorldActor.TraitsImplementing<ResourceType>() var resources = w.WorldActor.TraitsImplementing<ResourceType>()
.ToDictionary(r => r.Info.ResourceType, r => r); .ToDictionary(r => r.Info.ResourceType, r => r);
@@ -84,22 +87,22 @@ namespace OpenRA.Mods.Common.Traits
if (!AllowResourceAt(t, cell)) if (!AllowResourceAt(t, cell))
continue; continue;
content[cell] = CreateResourceCell(t, cell); Content[cell] = CreateResourceCell(t, cell);
} }
// Set initial density based on the number of neighboring resources // Set initial density based on the number of neighboring resources
foreach (var cell in w.Map.AllCells) foreach (var cell in w.Map.AllCells)
{ {
var type = content[cell].Type; var type = Content[cell].Type;
if (type != null) if (type != null)
{ {
// Adjacent includes the current cell, so is always >= 1 // Adjacent includes the current cell, so is always >= 1
var adjacent = GetAdjacentCellsWith(type, cell); var adjacent = GetAdjacentCellsWith(type, cell);
var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9); var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9);
var temp = content[cell]; var temp = Content[cell];
temp.Density = Math.Max(density, 1); temp.Density = Math.Max(density, 1);
render[cell] = content[cell] = temp; RenderContent[cell] = Content[cell] = temp;
UpdateRenderedSprite(cell); UpdateRenderedSprite(cell);
} }
} }
@@ -107,7 +110,7 @@ namespace OpenRA.Mods.Common.Traits
protected virtual void UpdateRenderedSprite(CPos cell) protected virtual void UpdateRenderedSprite(CPos cell)
{ {
var t = render[cell]; var t = RenderContent[cell];
if (t.Density > 0) if (t.Density > 0)
{ {
var sprites = t.Type.Variants[t.Variant]; var sprites = t.Type.Variants[t.Variant];
@@ -117,7 +120,7 @@ namespace OpenRA.Mods.Common.Traits
else else
t.Sprite = null; t.Sprite = null;
render[cell] = t; RenderContent[cell] = t;
} }
protected virtual string ChooseRandomVariant(ResourceType t) protected virtual string ChooseRandomVariant(ResourceType t)
@@ -132,7 +135,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (!self.World.FogObscures(c)) if (!self.World.FogObscures(c))
{ {
render[c] = content[c]; RenderContent[c] = Content[c];
UpdateRenderedSprite(c); UpdateRenderedSprite(c);
remove.Add(c); remove.Add(c);
} }
@@ -187,7 +190,7 @@ namespace OpenRA.Mods.Common.Traits
public void AddResource(ResourceType t, CPos p, int n) public void AddResource(ResourceType t, CPos p, int n)
{ {
var cell = content[p]; var cell = Content[p];
if (cell.Type == null) if (cell.Type == null)
cell = CreateResourceCell(t, p); cell = CreateResourceCell(t, p);
@@ -195,7 +198,7 @@ namespace OpenRA.Mods.Common.Traits
return; return;
cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n); cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n);
content[p] = cell; Content[p] = cell;
if (!dirty.Contains(p)) if (!dirty.Contains(p))
dirty.Add(p); dirty.Add(p);
@@ -203,22 +206,22 @@ namespace OpenRA.Mods.Common.Traits
public bool IsFull(CPos cell) public bool IsFull(CPos cell)
{ {
return content[cell].Density == content[cell].Type.Info.MaxDensity; return Content[cell].Density == Content[cell].Type.Info.MaxDensity;
} }
public ResourceType Harvest(CPos cell) public ResourceType Harvest(CPos cell)
{ {
var c = content[cell]; var c = Content[cell];
if (c.Type == null) if (c.Type == null)
return null; return null;
if (--c.Density < 0) if (--c.Density < 0)
{ {
content[cell] = EmptyCell; Content[cell] = EmptyCell;
world.Map.CustomTerrain[cell] = byte.MaxValue; world.Map.CustomTerrain[cell] = byte.MaxValue;
} }
else else
content[cell] = c; Content[cell] = c;
if (!dirty.Contains(cell)) if (!dirty.Contains(cell))
dirty.Add(cell); dirty.Add(cell);
@@ -229,26 +232,26 @@ namespace OpenRA.Mods.Common.Traits
public void Destroy(CPos cell) public void Destroy(CPos cell)
{ {
// Don't break other users of CustomTerrain if there are no resources // Don't break other users of CustomTerrain if there are no resources
if (content[cell].Type == null) if (Content[cell].Type == null)
return; return;
// Clear cell // Clear cell
content[cell] = EmptyCell; Content[cell] = EmptyCell;
world.Map.CustomTerrain[cell] = byte.MaxValue; world.Map.CustomTerrain[cell] = byte.MaxValue;
if (!dirty.Contains(cell)) if (!dirty.Contains(cell))
dirty.Add(cell); dirty.Add(cell);
} }
public ResourceType GetResource(CPos cell) { return content[cell].Type; } public ResourceType GetResource(CPos cell) { return Content[cell].Type; }
public ResourceType GetRenderedResource(CPos cell) { return render[cell].Type; } public ResourceType GetRenderedResource(CPos cell) { return RenderContent[cell].Type; }
public int GetResourceDensity(CPos cell) { return content[cell].Density; } public int GetResourceDensity(CPos cell) { return Content[cell].Density; }
public int GetMaxResourceDensity(CPos cell) public int GetMaxResourceDensity(CPos cell)
{ {
if (content[cell].Type == null) if (Content[cell].Type == null)
return 0; return 0;
return content[cell].Type.Info.MaxDensity; return Content[cell].Type.Info.MaxDensity;
} }
public struct CellContents public struct CellContents

View File

@@ -17,7 +17,10 @@ using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits namespace OpenRA.Mods.D2k.Traits
{ {
[Desc("Used to render spice with round borders.")] [Desc("Used to render spice with round borders.")]
public class D2kResourceLayerInfo : TraitInfo<D2kResourceLayer> { } public class D2kResourceLayerInfo : ResourceLayerInfo
{
public override object Create(ActorInitializer init) { return new D2kResourceLayer(init.Self); }
}
public class D2kResourceLayer : ResourceLayer public class D2kResourceLayer : ResourceLayer
{ {
@@ -96,9 +99,12 @@ namespace OpenRA.Mods.D2k.Traits
{ ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 49 }, { ClearSides.Bottom | ClearSides.TopLeft | ClearSides.BottomLeft | ClearSides.BottomRight, 49 },
}; };
public D2kResourceLayer(Actor self)
: base(self) { }
bool CellContains(CPos c, ResourceType t) bool CellContains(CPos c, ResourceType t)
{ {
return render.Contains(c) && render[c].Type == t; return RenderContent.Contains(c) && RenderContent[c].Type == t;
} }
ClearSides FindClearSides(ResourceType t, CPos p) ClearSides FindClearSides(ResourceType t, CPos p)
@@ -133,10 +139,10 @@ namespace OpenRA.Mods.D2k.Traits
void UpdateRenderedTileInner(CPos p) void UpdateRenderedTileInner(CPos p)
{ {
if (!render.Contains(p)) if (!RenderContent.Contains(p))
return; return;
var t = render[p]; var t = RenderContent[p];
if (t.Density > 0) if (t.Density > 0)
{ {
var clear = FindClearSides(t.Type, p); var clear = FindClearSides(t.Type, p);
@@ -156,7 +162,7 @@ namespace OpenRA.Mods.D2k.Traits
else else
t.Sprite = null; t.Sprite = null;
render[p] = t; RenderContent[p] = t;
} }
protected override void UpdateRenderedSprite(CPos p) protected override void UpdateRenderedSprite(CPos p)