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

View File

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