Move world state tracking to Creates/RevealsShroud.
This commit is contained in:
@@ -19,7 +19,7 @@ namespace OpenRA.Traits
|
||||
public object Create(ActorInitializer init) { return new CreatesShroud(init.Self, this); }
|
||||
}
|
||||
|
||||
public class CreatesShroud : ITick, ISync
|
||||
public class CreatesShroud : ITick, ISync, INotifyAddedToWorld, INotifyRemovedFromWorld
|
||||
{
|
||||
readonly CreatesShroudInfo info;
|
||||
readonly bool lobbyShroudFogDisabled;
|
||||
@@ -42,10 +42,29 @@ namespace OpenRA.Traits
|
||||
{
|
||||
cachedLocation = self.Location;
|
||||
cachedDisabled = disabled;
|
||||
Shroud.UpdateShroudGeneration(self.World.Players.Select(p => p.Shroud), self);
|
||||
|
||||
CPos[] shrouded = null;
|
||||
foreach (var p in self.World.Players)
|
||||
{
|
||||
p.Shroud.RemoveShroudGeneration(self);
|
||||
p.Shroud.AddShroudGeneration(self, ref shrouded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddedToWorld(Actor self)
|
||||
{
|
||||
CPos[] shrouded = null;
|
||||
foreach (var p in self.World.Players)
|
||||
p.Shroud.AddShroudGeneration(self, ref shrouded);
|
||||
}
|
||||
|
||||
public void RemovedFromWorld(Actor self)
|
||||
{
|
||||
foreach (var p in self.World.Players)
|
||||
p.Shroud.RemoveShroudGeneration(self);
|
||||
}
|
||||
|
||||
public WRange Range { get { return cachedDisabled ? WRange.Zero : info.Range; } }
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ namespace OpenRA.Traits
|
||||
public object Create(ActorInitializer init) { return new RevealsShroud(init.Self, this); }
|
||||
}
|
||||
|
||||
public class RevealsShroud : ITick, ISync
|
||||
public class RevealsShroud : ITick, ISync, INotifyAddedToWorld, INotifyRemovedFromWorld
|
||||
{
|
||||
readonly RevealsShroudInfo info;
|
||||
readonly bool lobbyShroudFogDisabled;
|
||||
@@ -39,10 +39,29 @@ namespace OpenRA.Traits
|
||||
if (cachedLocation != self.Location)
|
||||
{
|
||||
cachedLocation = self.Location;
|
||||
Shroud.UpdateVisibility(self.World.Players.Select(p => p.Shroud), self);
|
||||
|
||||
CPos[] visible = null;
|
||||
foreach (var p in self.World.Players)
|
||||
{
|
||||
p.Shroud.RemoveVisibility(self);
|
||||
p.Shroud.AddVisibility(self, ref visible);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddedToWorld(Actor self)
|
||||
{
|
||||
CPos[] visible = null;
|
||||
foreach (var p in self.World.Players)
|
||||
p.Shroud.AddVisibility(self, ref visible);
|
||||
}
|
||||
|
||||
public void RemovedFromWorld(Actor self)
|
||||
{
|
||||
foreach (var p in self.World.Players)
|
||||
p.Shroud.RemoveVisibility(self);
|
||||
}
|
||||
|
||||
public WRange Range { get { return info.Range; } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,12 +54,6 @@ namespace OpenRA.Traits
|
||||
generatedShroudCount = new CellLayer<short>(map);
|
||||
explored = new CellLayer<bool>(map);
|
||||
|
||||
self.World.ActorAdded += a => { CPos[] visible = null; AddVisibility(a, ref visible); };
|
||||
self.World.ActorRemoved += RemoveVisibility;
|
||||
|
||||
self.World.ActorAdded += a => { CPos[] shrouded = null; AddShroudGeneration(a, ref shrouded); };
|
||||
self.World.ActorRemoved += RemoveShroudGeneration;
|
||||
|
||||
shroudEdgeTest = map.Contains;
|
||||
isExploredTest = IsExploredCore;
|
||||
isVisibleTest = IsVisibleCore;
|
||||
@@ -78,20 +72,6 @@ namespace OpenRA.Traits
|
||||
Hash += 1;
|
||||
}
|
||||
|
||||
public static void UpdateVisibility(IEnumerable<Shroud> shrouds, Actor actor)
|
||||
{
|
||||
CPos[] visbility = null;
|
||||
foreach (var shroud in shrouds)
|
||||
shroud.UpdateVisibility(actor, ref visbility);
|
||||
}
|
||||
|
||||
public static void UpdateShroudGeneration(IEnumerable<Shroud> shrouds, Actor actor)
|
||||
{
|
||||
CPos[] shrouded = null;
|
||||
foreach (var shroud in shrouds)
|
||||
shroud.UpdateShroudGeneration(actor, ref shrouded);
|
||||
}
|
||||
|
||||
static CPos[] FindVisibleTiles(Actor actor, WRange range)
|
||||
{
|
||||
return GetVisOrigins(actor).SelectMany(o => FindVisibleTiles(actor.World, o, range)).Distinct().ToArray();
|
||||
@@ -109,7 +89,7 @@ namespace OpenRA.Traits
|
||||
yield return cell;
|
||||
}
|
||||
|
||||
void AddVisibility(Actor a, ref CPos[] visible)
|
||||
public void AddVisibility(Actor a, ref CPos[] visible)
|
||||
{
|
||||
var rs = a.TraitOrDefault<RevealsShroud>();
|
||||
if (rs == null || !a.Owner.IsAlliedWith(self.Owner) || rs.Range == WRange.Zero)
|
||||
@@ -137,7 +117,7 @@ namespace OpenRA.Traits
|
||||
Invalidate(visible);
|
||||
}
|
||||
|
||||
void RemoveVisibility(Actor a)
|
||||
public void RemoveVisibility(Actor a)
|
||||
{
|
||||
CPos[] visible;
|
||||
if (!visibility.TryGetValue(a, out visible))
|
||||
@@ -154,17 +134,7 @@ namespace OpenRA.Traits
|
||||
Invalidate(visible);
|
||||
}
|
||||
|
||||
void UpdateVisibility(Actor a, ref CPos[] visible)
|
||||
{
|
||||
// Actors outside the world don't have any vis
|
||||
if (!a.IsInWorld)
|
||||
return;
|
||||
|
||||
RemoveVisibility(a);
|
||||
AddVisibility(a, ref visible);
|
||||
}
|
||||
|
||||
void AddShroudGeneration(Actor a, ref CPos[] shrouded)
|
||||
public void AddShroudGeneration(Actor a, ref CPos[] shrouded)
|
||||
{
|
||||
var cs = a.TraitOrDefault<CreatesShroud>();
|
||||
if (cs == null || a.Owner.IsAlliedWith(self.Owner) || cs.Range == WRange.Zero)
|
||||
@@ -183,7 +153,7 @@ namespace OpenRA.Traits
|
||||
Invalidate(shrouded);
|
||||
}
|
||||
|
||||
void RemoveShroudGeneration(Actor a)
|
||||
public void RemoveShroudGeneration(Actor a)
|
||||
{
|
||||
CPos[] shrouded;
|
||||
if (!generation.TryGetValue(a, out shrouded))
|
||||
@@ -196,12 +166,6 @@ namespace OpenRA.Traits
|
||||
Invalidate(shrouded);
|
||||
}
|
||||
|
||||
void UpdateShroudGeneration(Actor a, ref CPos[] shrouded)
|
||||
{
|
||||
RemoveShroudGeneration(a);
|
||||
AddShroudGeneration(a, ref shrouded);
|
||||
}
|
||||
|
||||
public void UpdatePlayerStance(World w, Player player, Stance oldStance, Stance newStance)
|
||||
{
|
||||
if (oldStance == newStance)
|
||||
@@ -210,9 +174,14 @@ namespace OpenRA.Traits
|
||||
foreach (var a in w.Actors.Where(a => a.Owner == player))
|
||||
{
|
||||
CPos[] visible = null;
|
||||
UpdateVisibility(a, ref visible);
|
||||
CPos[] shrouded = null;
|
||||
UpdateShroudGeneration(a, ref shrouded);
|
||||
foreach (var p in self.World.Players)
|
||||
{
|
||||
p.Shroud.RemoveVisibility(self);
|
||||
p.Shroud.AddVisibility(self, ref visible);
|
||||
p.Shroud.RemoveShroudGeneration(self);
|
||||
p.Shroud.AddShroudGeneration(self, ref shrouded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user