Remove BibLayer and use sequences / RenderSprites instead.

This commit is contained in:
Paul Chote
2013-08-10 19:32:40 +12:00
parent 810a52af36
commit b02fd0d002
14 changed files with 249 additions and 203 deletions

68
OpenRA.Mods.RA/Buildings/Bib.cs Executable file
View File

@@ -0,0 +1,68 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Buildings
{
public class BibInfo : ITraitInfo, Requires<BuildingInfo>, Requires<RenderSpritesInfo>
{
public readonly string Sequence = "bib";
public readonly string Palette = "terrain";
public object Create(ActorInitializer init) { return new Bib(init.self, this); }
}
public class Bib : IRender
{
readonly BibInfo info;
readonly List<AnimationWithOffset> tiles;
public Bib(Actor self, BibInfo info)
{
this.info = info;
var rs = self.Trait<RenderSprites>();
var building = self.Info.Traits.Get<BuildingInfo>();
var width = building.Dimensions.X;
var bibOffset = building.Dimensions.Y - 1;
var centerOffset = FootprintUtils.CenterOffset(building);
tiles = new List<AnimationWithOffset>();
for (var i = 0; i < 2*width; i++)
{
var index = i;
var anim = new Animation(rs.GetImage(self));
anim.PlayFetchIndex(info.Sequence, () => index);
anim.IsDecoration = true;
// Z-order is one set to the top of the footprint
var offset = new CVec(i % width, i / width + bibOffset).ToWVec() - centerOffset;
tiles.Add(new AnimationWithOffset(anim, () => offset, null, -(offset.Y + centerOffset.Y + 512)));
}
}
bool paletteInitialized;
PaletteReference palette;
public virtual IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{
if (!paletteInitialized)
{
palette = wr.Palette(info.Palette);
paletteInitialized = true;
}
return tiles.SelectMany(t => t.Render(self, wr, palette, 1f));
}
}
}

View File

@@ -1,154 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Buildings
{
class BibLayerInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new BibLayer(init.self); }
}
struct CachedBib
{
public Dictionary<CPos, Sprite> Tiles;
public IEnumerable<CPos> Footprint;
public bool Visible;
public bool Immediate;
}
class BibLayer : IRenderOverlay, ITickRender
{
World world;
Dictionary<Actor, CachedBib> visible;
Dictionary<Actor, CachedBib> dirty;
Cache<string, Sprite[]> sprites;
public BibLayer(Actor self)
{
world = self.World;
visible = new Dictionary<Actor, CachedBib>();
dirty = new Dictionary<Actor, CachedBib>();
sprites = new Cache<string, Sprite[]>(x => Game.modData.SpriteLoader.LoadAllSprites(x));
}
public void Update(Actor a, CachedBib bib)
{
dirty[a] = bib;
}
public Sprite[] LoadSprites(string bibType)
{
return sprites[bibType];
}
public void TickRender(WorldRenderer wr, Actor self)
{
var remove = new List<Actor>();
foreach (var kv in dirty)
{
if (kv.Value.Immediate || kv.Value.Footprint.Any(c => !self.World.FogObscures(c)))
{
if (kv.Value.Visible)
visible[kv.Key] = kv.Value;
else
visible.Remove(kv.Key);
remove.Add(kv.Key);
}
}
foreach (var r in remove)
dirty.Remove(r);
}
public void Render(WorldRenderer wr)
{
var pal = wr.Palette("terrain");
var cliprect = Game.viewport.WorldBounds(world);
foreach (var bib in visible.Values)
{
foreach (var kv in bib.Tiles)
{
if (!cliprect.Contains(kv.Key.X, kv.Key.Y))
continue;
if (world.ShroudObscures(kv.Key))
continue;
kv.Value.DrawAt(wr.ScreenPxPosition(kv.Key.CenterPosition) - 0.5f * kv.Value.size, pal);
}
}
}
}
public class BibInfo : ITraitInfo, Requires<BuildingInfo>
{
public readonly string Sprite = "bib3";
public object Create(ActorInitializer init) { return new Bib(init.self, this); }
}
public class Bib : INotifyAddedToWorld, INotifyRemovedFromWorld
{
readonly BibInfo info;
readonly BibLayer bibLayer;
bool firstAdd;
public Bib(Actor self, BibInfo info)
{
this.info = info;
bibLayer = self.World.WorldActor.Trait<BibLayer>();
firstAdd = true;
}
void DoBib(Actor self, bool add)
{
var buildingInfo = self.Info.Traits.Get<BuildingInfo>();
var size = buildingInfo.Dimensions.X;
var bibOffset = buildingInfo.Dimensions.Y - 1;
var sprites = bibLayer.LoadSprites(info.Sprite);
if (sprites.Length != 2*size)
throw new InvalidOperationException("{0} is an invalid bib for a {1}-wide building".F(info.Sprite, size));
var immediate = !self.HasTrait<FrozenUnderFog>() ||
(firstAdd && self.Info.Traits.GetOrDefault<FrozenUnderFogInfo>().StartsRevealed);
var dirty = new CachedBib()
{
Footprint = FootprintUtils.Tiles(self),
Tiles = new Dictionary<CPos, Sprite>(),
Visible = add,
Immediate = immediate
};
for (var i = 0; i < 2 * size; i++)
{
var cell = self.Location + new CVec(i % size, i / size + bibOffset);
dirty.Tiles.Add(cell, sprites[i]);
}
firstAdd = false;
bibLayer.Update(self, dirty);
}
public void AddedToWorld(Actor self) { DoBib(self, true); }
public void RemovedFromWorld(Actor self) { DoBib(self, false); }
}
}

View File

@@ -154,7 +154,6 @@
<Compile Include="BridgeLayer.cs" />
<Compile Include="Buildable.cs" />
<Compile Include="BuildingCaptureNotification.cs" />
<Compile Include="Buildings\BibLayer.cs" />
<Compile Include="Buildings\Building.cs" />
<Compile Include="Buildings\BuildingInfluence.cs" />
<Compile Include="Buildings\CanPowerDown.cs" />
@@ -462,6 +461,7 @@
<Compile Include="Effects\FrozenActorProxy.cs" />
<Compile Include="Widgets\Logic\WorldTooltipLogic.cs" />
<Compile Include="TeslaZapRenderable.cs" />
<Compile Include="Buildings\Bib.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">