Files
OpenRA/OpenRA.Game/Traits/Player/FrozenActorLayer.cs
RoosterDragon a512d9ad0a Sped up shroud rendering.
- Only update shroud within the visible screen area, rather than the whole map. This improves performance on larger maps significantly when scrolling around since large portions of the shroud do not need to be updated.
- Provide methods in Shroud to return delegates to check for explored/visibility for tiles within a certain region. This allows it to return more efficient delegates whenever the region is within the map bounds, or shroud/fog is disabled. In the typical case where the region is in bounds and shroud/fog is enabled, the fast check is almost twice as fast as the slow check.
- Use the Shroud delegate functions in shroud rendering, frozen actors, minimap rendering and resource layer areas to provide a speedup since these areas of code can often take advantage of the fact they perform checks within the map boundary.
- Cache current element in CellRegionEnumerator to prevent repeated work if the element is accessed more than once.
- Decrease the size of elements in some arrays in hopes of reducing memory needs and improving cache hits.
2014-07-07 17:34:31 +01:00

157 lines
3.6 KiB
C#
Executable File

#region Copyright & License Information
/*
* Copyright 2007-2014 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.Drawing;
using System.Linq;
using OpenRA.Graphics;
namespace OpenRA.Traits
{
public class FrozenActorLayerInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new FrozenActorLayer(init.self); }
}
public class FrozenActor
{
public readonly CPos[] Footprint;
public readonly CellRegion FootprintRegion;
public readonly WPos CenterPosition;
public readonly Rectangle Bounds;
readonly Actor actor;
public IRenderable[] Renderables { private get; set; }
public Player Owner;
public string TooltipName;
public Player TooltipOwner;
public int HP;
public DamageState DamageState;
public bool Visible;
public FrozenActor(Actor self, CPos[] footprint, CellRegion footprintRegion)
{
actor = self;
Footprint = footprint;
FootprintRegion = footprintRegion;
CenterPosition = self.CenterPosition;
Bounds = self.Bounds.Value;
}
public uint ID { get { return actor.ActorID; } }
public bool IsValid { get { return Owner != null && HasRenderables; } }
public ActorInfo Info { get { return actor.Info; } }
public Actor Actor { get { return !actor.IsDead() ? actor : null; } }
int flashTicks;
public void Tick(World world, Shroud shroud)
{
Visible = !Footprint.Any(shroud.IsVisibleTest(FootprintRegion));
if (flashTicks > 0)
flashTicks--;
}
public void Flash()
{
flashTicks = 5;
}
public IEnumerable<IRenderable> Render(WorldRenderer wr)
{
if (Renderables == null)
return SpriteRenderable.None;
if (flashTicks > 0 && flashTicks % 2 == 0)
{
var highlight = wr.Palette("highlight");
return Renderables.Concat(Renderables.Where(r => !r.IsDecoration)
.Select(r => r.WithPalette(highlight)));
}
return Renderables;
}
public bool HasRenderables { get { return Renderables != null && Renderables.Any(); } }
public override string ToString()
{
return "{0} {1}{2}".F(Info.Name, ID, IsValid ? "" : " (invalid)");
}
}
public class FrozenActorLayer : IRender, ITick, ISync
{
[Sync] public int VisibilityHash;
[Sync] public int FrozenHash;
readonly World world;
readonly Player owner;
Dictionary<uint, FrozenActor> frozen;
public FrozenActorLayer(Actor self)
{
world = self.World;
owner = self.Owner;
frozen = new Dictionary<uint, FrozenActor>();
}
public void Add(FrozenActor fa)
{
frozen.Add(fa.ID, fa);
world.ScreenMap.Add(owner, fa);
}
public void Tick(Actor self)
{
var remove = new List<uint>();
VisibilityHash = 0;
FrozenHash = 0;
foreach (var kv in frozen)
{
FrozenHash += (int)kv.Key;
kv.Value.Tick(self.World, self.Owner.Shroud);
if (kv.Value.Visible)
VisibilityHash += (int)kv.Key;
if (!kv.Value.Visible && kv.Value.Actor == null)
remove.Add(kv.Key);
}
foreach (var r in remove)
{
world.ScreenMap.Remove(owner, frozen[r]);
frozen.Remove(r);
}
}
public virtual IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{
return world.ScreenMap.FrozenActorsInBox(owner, wr.Viewport.TopLeft, wr.Viewport.BottomRight)
.Where(f => f.Visible)
.SelectMany(ff => ff.Render(wr));
}
public FrozenActor FromID(uint id)
{
FrozenActor ret;
if (!frozen.TryGetValue(id, out ret))
return null;
return ret;
}
}
}