Rework the addition and removal of building influence

This commit is contained in:
abcdefg30
2017-08-08 15:32:08 +02:00
committed by Matthias Mailänder
parent 93f385e9fc
commit 5cfb5aaf2d
2 changed files with 23 additions and 22 deletions

View File

@@ -241,13 +241,16 @@ namespace OpenRA.Mods.Common.Traits
}
}
public class Building : IOccupySpace, ITargetableCells, INotifySold, INotifyTransform, ISync, INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld
public class Building : IOccupySpace, ITargetableCells, INotifySold, INotifyTransform, ISync, INotifyCreated,
INotifyAddedToWorld, INotifyRemovedFromWorld
{
public readonly bool SkipMakeAnimation;
public readonly BuildingInfo Info;
public bool BuildComplete { get; private set; }
[Sync] readonly CPos topLeft;
readonly Actor self;
public readonly bool SkipMakeAnimation;
readonly BuildingInfluence influence;
Pair<CPos, SubCell>[] occupiedCells;
Pair<CPos, SubCell>[] targetableCells;
@@ -274,6 +277,7 @@ namespace OpenRA.Mods.Common.Traits
self = init.Self;
topLeft = init.Get<LocationInit, CPos>();
Info = info;
influence = self.World.WorldActor.Trait<BuildingInfluence>();
occupiedCells = Info.UnpathableTiles(TopLeft)
.Select(c => Pair.New(c, SubCell.FullCell)).ToArray();
@@ -305,6 +309,8 @@ namespace OpenRA.Mods.Common.Traits
if (!self.Bounds.Size.IsEmpty)
self.World.ScreenMap.Add(self);
influence.AddInfluence(self, Info.Tiles(self.Location));
}
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
@@ -314,6 +320,8 @@ namespace OpenRA.Mods.Common.Traits
if (!self.Bounds.Size.IsEmpty)
self.World.ScreenMap.Remove(self);
influence.RemoveInfluence(self, Info.Tiles(self.Location));
}
public void NotifyBuildingComplete(Actor self)

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -29,28 +30,20 @@ namespace OpenRA.Mods.Common.Traits
map = world.Map;
influence = new CellLayer<Actor>(map);
}
world.ActorAdded += a =>
internal void AddInfluence(Actor a, IEnumerable<CPos> tiles)
{
var b = a.Info.TraitInfoOrDefault<BuildingInfo>();
if (b == null)
return;
foreach (var u in b.Tiles(a.Location))
foreach (var u in tiles)
if (influence.Contains(u) && influence[u] == null)
influence[u] = a;
};
}
world.ActorRemoved += a =>
internal void RemoveInfluence(Actor a, IEnumerable<CPos> tiles)
{
var b = a.Info.TraitInfoOrDefault<BuildingInfo>();
if (b == null)
return;
foreach (var u in b.Tiles(a.Location))
foreach (var u in tiles)
if (influence.Contains(u) && influence[u] == a)
influence[u] = null;
};
}
public Actor GetBuildingAt(CPos cell)