Let all Positionable traits notify visual position changes

This commit is contained in:
abcdefg30
2020-01-15 14:31:46 +01:00
committed by Paul Chote
parent d0f44143c2
commit 15b2d6b9e0
3 changed files with 28 additions and 1 deletions

View File

@@ -60,6 +60,7 @@ namespace OpenRA.Mods.Cnc.Traits
readonly Actor self; readonly Actor self;
IEnumerable<int> speedModifiers; IEnumerable<int> speedModifiers;
INotifyVisualPositionChanged[] notifyVisualPositionChanged;
[Sync] [Sync]
public int Facing { get; set; } public int Facing { get; set; }
@@ -96,6 +97,7 @@ namespace OpenRA.Mods.Cnc.Traits
{ {
speedModifiers = self.TraitsImplementing<ISpeedModifier>().ToArray().Select(sm => sm.GetSpeedModifier()); speedModifiers = self.TraitsImplementing<ISpeedModifier>().ToArray().Select(sm => sm.GetSpeedModifier());
cachedLocation = self.Location; cachedLocation = self.Location;
notifyVisualPositionChanged = self.TraitsImplementing<INotifyVisualPositionChanged>().ToArray();
} }
void INotifyAddedToWorld.AddedToWorld(Actor self) void INotifyAddedToWorld.AddedToWorld(Actor self)
@@ -178,6 +180,11 @@ namespace OpenRA.Mods.Cnc.Traits
return; return;
self.World.UpdateMaps(self, this); self.World.UpdateMaps(self, this);
// This can be called from the constructor before notifyVisualPositionChanged is assigned.
if (notifyVisualPositionChanged != null)
foreach (var n in notifyVisualPositionChanged)
n.VisualPositionChanged(self, 0, 0);
} }
public Activity MoveTo(CPos cell, int nearEnough = 0, Actor ignoreActor = null, public Activity MoveTo(CPos cell, int nearEnough = 0, Actor ignoreActor = null,

View File

@@ -74,12 +74,13 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
public class Crate : ITick, IPositionable, ICrushable, ISync, public class Crate : ITick, IPositionable, ICrushable, ISync, INotifyCreated,
INotifyParachute, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyCrushed INotifyParachute, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyCrushed
{ {
readonly Actor self; readonly Actor self;
readonly CrateInfo info; readonly CrateInfo info;
bool collected; bool collected;
INotifyVisualPositionChanged[] notifyVisualPositionChanged;
[Sync] [Sync]
int ticks; int ticks;
@@ -96,6 +97,11 @@ namespace OpenRA.Mods.Common.Traits
SetPosition(self, init.Get<LocationInit, CPos>()); SetPosition(self, init.Get<LocationInit, CPos>());
} }
void INotifyCreated.Created(Actor self)
{
notifyVisualPositionChanged = self.TraitsImplementing<INotifyVisualPositionChanged>().ToArray();
}
void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses) { } void INotifyCrushed.WarnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses) { }
void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses) void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet<CrushClass> crushClasses)
@@ -197,6 +203,11 @@ namespace OpenRA.Mods.Common.Traits
{ {
CenterPosition = pos; CenterPosition = pos;
self.World.UpdateMaps(self, this); self.World.UpdateMaps(self, this);
// This can be called from the constructor before notifyVisualPositionChanged is assigned.
if (notifyVisualPositionChanged != null)
foreach (var n in notifyVisualPositionChanged)
n.VisualPositionChanged(self, 0, 0);
} }
// Sets only the location (Location) // Sets only the location (Location)

View File

@@ -60,6 +60,8 @@ namespace OpenRA.Mods.Common.Traits
readonly int dragSpeed; readonly int dragSpeed;
readonly WPos finalPosition; readonly WPos finalPosition;
INotifyVisualPositionChanged[] notifyVisualPositionChanged;
[Sync] [Sync]
public CPos TopLeft { get; private set; } public CPos TopLeft { get; private set; }
@@ -91,6 +93,8 @@ namespace OpenRA.Mods.Common.Traits
var distance = (finalPosition - CenterPosition).Length; var distance = (finalPosition - CenterPosition).Length;
if (dragSpeed > 0 && distance > 0) if (dragSpeed > 0 && distance > 0)
self.QueueActivity(new Drag(self, CenterPosition, finalPosition, distance / dragSpeed)); self.QueueActivity(new Drag(self, CenterPosition, finalPosition, distance / dragSpeed));
notifyVisualPositionChanged = self.TraitsImplementing<INotifyVisualPositionChanged>().ToArray();
} }
public bool CanExistInCell(CPos cell) public bool CanExistInCell(CPos cell)
@@ -130,6 +134,11 @@ namespace OpenRA.Mods.Common.Traits
{ {
CenterPosition = pos; CenterPosition = pos;
self.World.ScreenMap.AddOrUpdate(self); self.World.ScreenMap.AddOrUpdate(self);
// This can be called from the constructor before notifyVisualPositionChanged is assigned.
if (notifyVisualPositionChanged != null)
foreach (var n in notifyVisualPositionChanged)
n.VisualPositionChanged(self, 0, 0);
} }
public void SetPosition(Actor self, WPos pos) public void SetPosition(Actor self, WPos pos)