NotifyBlocker extension method for Actor

This commit is contained in:
atimoschenkow
2015-01-10 12:01:29 +01:00
parent ae03fa9af5
commit cefd554a5e
4 changed files with 25 additions and 23 deletions

View File

@@ -202,16 +202,6 @@ namespace OpenRA.Mods.Common.Activities
throw new InvalidOperationException("(Move) Sanity check failed");
}
static void NotifyBlocker(Actor self, CPos nextCell)
{
foreach (var blocker in self.World.ActorMap.GetUnitsAt(nextCell))
{
// Notify the blocker that he's blocking our move:
foreach (var moveBlocked in blocker.TraitsImplementing<INotifyBlockingMove>())
moveBlocked.OnNotifyBlockingMove(blocker, self);
}
}
Pair<CPos, SubCell>? PopPath(Actor self, Mobile mobile)
{
if (path.Count == 0)
@@ -233,7 +223,7 @@ namespace OpenRA.Mods.Common.Activities
// See if they will move
if (!hasNotifiedBlocker)
{
NotifyBlocker(self, nextCell);
self.NotifyBlocker(nextCell);
hasNotifiedBlocker = true;
}

View File

@@ -68,11 +68,7 @@ namespace OpenRA.Mods.Common.Activities
var exitSubCell = ChooseExitSubCell(actor);
if (exitSubCell == null)
{
foreach (var blocker in BlockedExitCells(actor).SelectMany(p => self.World.ActorMap.GetUnitsAt(p)))
{
foreach (var nbm in blocker.TraitsImplementing<INotifyBlockingMove>())
nbm.OnNotifyBlockingMove(blocker, self);
}
self.NotifyBlocker(BlockedExitCells(actor));
return Util.SequenceActivities(new Wait(10), this);
}

View File

@@ -8,7 +8,9 @@
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
@@ -77,5 +79,24 @@ namespace OpenRA.Mods.Common
return Target.Invalid;
}
public static void NotifyBlocker(this Actor self, IEnumerable<Actor> blockers)
{
foreach (var blocker in blockers)
{
foreach (var moveBlocked in blocker.TraitsImplementing<INotifyBlockingMove>())
moveBlocked.OnNotifyBlockingMove(blocker, self);
}
}
public static void NotifyBlocker(this Actor self, CPos position)
{
NotifyBlocker(self, self.World.ActorMap.GetUnitsAt(position));
}
public static void NotifyBlocker(this Actor self, IEnumerable<CPos> positions)
{
NotifyBlocker(self, positions.SelectMany(p => self.World.ActorMap.GetUnitsAt(p)));
}
}
}

View File

@@ -12,9 +12,9 @@ using System;
using System.Drawing;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.RA.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;
@@ -119,12 +119,7 @@ namespace OpenRA.Mods.RA.Traits
{
var mobileInfo = producee.Traits.GetOrDefault<MobileInfo>();
foreach (var blocker in self.World.ActorMap.GetUnitsAt(self.Location + s.ExitCell))
{
// Notify the blocker that he's blocking our move:
foreach (var moveBlocked in blocker.TraitsImplementing<INotifyBlockingMove>())
moveBlocked.OnNotifyBlockingMove(blocker, self);
}
self.NotifyBlocker(self.Location + s.ExitCell);
return mobileInfo == null ||
mobileInfo.CanEnterCell(self.World, self, self.Location + s.ExitCell, self);