Skip to content

Commit

Permalink
Add INotifyClientMoving interface
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun committed Aug 2, 2023
1 parent f6a2cd3 commit f26b598
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
10 changes: 5 additions & 5 deletions OpenRA.Mods.Common/Activities/HarvestResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class HarvestResource : Activity
readonly BodyOrientation body;
readonly IMove move;
readonly CPos targetCell;
readonly INotifyHarvesterAction[] notifyHarvesterActions;
readonly INotifyHarvestAction[] notifyHarvestActions;

public HarvestResource(Actor self, CPos targetCell)
{
Expand All @@ -39,7 +39,7 @@ public HarvestResource(Actor self, CPos targetCell)
claimLayer = self.World.WorldActor.Trait<ResourceClaimLayer>();
resourceLayer = self.World.WorldActor.Trait<IResourceLayer>();
this.targetCell = targetCell;
notifyHarvesterActions = self.TraitsImplementing<INotifyHarvesterAction>().ToArray();
notifyHarvestActions = self.TraitsImplementing<INotifyHarvestAction>().ToArray();
}

protected override void OnFirstRun(Actor self)
Expand All @@ -61,7 +61,7 @@ public override bool Tick(Actor self)
// Move towards the target cell
if (self.Location != targetCell)
{
foreach (var n in notifyHarvesterActions)
foreach (var n in notifyHarvestActions)
n.MovingToResources(self, targetCell);

QueueChild(move.MoveTo(targetCell, 0));
Expand Down Expand Up @@ -89,7 +89,7 @@ public override bool Tick(Actor self)

harv.AcceptResource(self, resource.Type);

foreach (var t in notifyHarvesterActions)
foreach (var t in notifyHarvestActions)
t.Harvested(self, resource.Type);

QueueChild(new Wait(harvInfo.BaleLoadDelay));
Expand All @@ -103,7 +103,7 @@ protected override void OnLastRun(Actor self)

public override void Cancel(Actor self, bool keepQueue = false)
{
foreach (var n in notifyHarvesterActions)
foreach (var n in notifyHarvestActions)
n.MovementCancelled(self);

base.Cancel(self, keepQueue);
Expand Down
12 changes: 6 additions & 6 deletions OpenRA.Mods.Common/Activities/MoveToDock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class MoveToDock : Activity
readonly DockClientManager dockClient;
Actor dockHostActor;
IDockHost dockHost;
readonly INotifyHarvesterAction[] notifyHarvesterActions;
readonly INotifyDockClientMoving[] notifyDockClientMoving;

public MoveToDock(Actor self, Actor dockHostActor = null, IDockHost dockHost = null)
{
dockClient = self.Trait<DockClientManager>();
this.dockHostActor = dockHostActor;
this.dockHost = dockHost;
notifyHarvesterActions = self.TraitsImplementing<INotifyHarvesterAction>().ToArray();
notifyDockClientMoving = self.TraitsImplementing<INotifyDockClientMoving>().ToArray();
}

public override bool Tick(Actor self)
Expand Down Expand Up @@ -64,8 +64,8 @@ public override bool Tick(Actor self)
{
if (dockHost.QueueMoveActivity(this, dockHostActor, self, dockClient))
{
foreach (var n in notifyHarvesterActions)
n.MovingToRefinery(self, dockHostActor);
foreach (var ndcm in notifyDockClientMoving)
ndcm.MovingToDock(self, dockHostActor, dockHost);

return false;
}
Expand All @@ -84,8 +84,8 @@ public override bool Tick(Actor self)
public override void Cancel(Actor self, bool keepQueue = false)
{
dockClient.UnreserveHost();
foreach (var n in notifyHarvesterActions)
n.MovementCancelled(self);
foreach (var ndcm in notifyDockClientMoving)
ndcm.MovementCancelled(self);

base.Cancel(self, keepQueue);
}
Expand Down
19 changes: 12 additions & 7 deletions OpenRA.Mods.Common/Traits/CarryableHarvester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CarryableHarvesterInfo : TraitInfo
public override object Create(ActorInitializer init) { return new CarryableHarvester(); }
}

public class CarryableHarvester : INotifyCreated, INotifyHarvesterAction
public class CarryableHarvester : INotifyCreated, INotifyHarvestAction, INotifyDockClientMoving
{
ICallForTransport[] transports;

Expand All @@ -28,25 +28,30 @@ void INotifyCreated.Created(Actor self)
transports = self.TraitsImplementing<ICallForTransport>().ToArray();
}

void INotifyHarvesterAction.MovingToResources(Actor self, CPos targetCell)
void INotifyHarvestAction.MovingToResources(Actor self, CPos targetCell)
{
foreach (var t in transports)
t.RequestTransport(self, targetCell);
}

void INotifyHarvesterAction.MovingToRefinery(Actor self, Actor refineryActor)
void INotifyHarvestAction.MovementCancelled(Actor self)
{
var dock = refineryActor.Trait<IDockHost>();
foreach (var t in transports)
t.RequestTransport(self, self.World.Map.CellContaining(dock.DockPosition));
t.MovementCancelled(self);
}

void INotifyDockClientMoving.MovingToDock(Actor self, Actor hostActor, IDockHost host)
{
foreach (var t in transports)
t.RequestTransport(self, self.World.Map.CellContaining(host.DockPosition));
}

void INotifyHarvesterAction.MovementCancelled(Actor self)
void INotifyDockClientMoving.MovementCancelled(Actor self)
{
foreach (var t in transports)
t.MovementCancelled(self);
}

void INotifyHarvesterAction.Harvested(Actor self, string resourceType) { }
void INotifyHarvestAction.Harvested(Actor self, string resourceType) { }
}
}
9 changes: 4 additions & 5 deletions OpenRA.Mods.Common/Traits/Render/WithHarvestAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class WithHarvestAnimationInfo : TraitInfo, Requires<WithSpriteBodyInfo>,
public override object Create(ActorInitializer init) { return new WithHarvestAnimation(init, this); }
}

public class WithHarvestAnimation : INotifyHarvesterAction
public class WithHarvestAnimation : INotifyHarvestAction
{
public readonly WithHarvestAnimationInfo Info;
readonly WithSpriteBody wsb;
Expand All @@ -37,15 +37,14 @@ public WithHarvestAnimation(ActorInitializer init, WithHarvestAnimationInfo info
wsb = init.Self.TraitsImplementing<WithSpriteBody>().Single(w => w.Info.Name == Info.Body);
}

void INotifyHarvesterAction.Harvested(Actor self, string resourceType)
void INotifyHarvestAction.Harvested(Actor self, string resourceType)
{
var sequence = wsb.NormalizeSequence(self, Info.HarvestSequence);
if (wsb.DefaultAnimation.HasSequence(sequence) && wsb.DefaultAnimation.CurrentSequence.Name != sequence)
wsb.PlayCustomAnimation(self, sequence);
}

void INotifyHarvesterAction.MovingToResources(Actor self, CPos targetCell) { }
void INotifyHarvesterAction.MovingToRefinery(Actor self, Actor refineryActor) { }
void INotifyHarvesterAction.MovementCancelled(Actor self) { }
void INotifyHarvestAction.MovingToResources(Actor self, CPos targetCell) { }
void INotifyHarvestAction.MovementCancelled(Actor self) { }
}
}
9 changes: 4 additions & 5 deletions OpenRA.Mods.Common/Traits/Render/WithHarvestOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sealed class WithHarvestOverlayInfo : TraitInfo, Requires<RenderSpritesInfo>, Re
public override object Create(ActorInitializer init) { return new WithHarvestOverlay(init.Self, this); }
}

sealed class WithHarvestOverlay : INotifyHarvesterAction
sealed class WithHarvestOverlay : INotifyHarvestAction
{
readonly WithHarvestOverlayInfo info;
readonly Animation anim;
Expand All @@ -54,7 +54,7 @@ public WithHarvestOverlay(Actor self, WithHarvestOverlayInfo info)
p => ZOffsetFromCenter(self, p, 0)), info.Palette);
}

void INotifyHarvesterAction.Harvested(Actor self, string resourceType)
void INotifyHarvestAction.Harvested(Actor self, string resourceType)
{
if (visible)
return;
Expand All @@ -63,9 +63,8 @@ void INotifyHarvesterAction.Harvested(Actor self, string resourceType)
anim.PlayThen(info.Sequence, () => visible = false);
}

void INotifyHarvesterAction.MovingToResources(Actor self, CPos targetCell) { }
void INotifyHarvesterAction.MovingToRefinery(Actor self, Actor targetRefinery) { }
void INotifyHarvesterAction.MovementCancelled(Actor self) { }
void INotifyHarvestAction.MovingToResources(Actor self, CPos targetCell) { }
void INotifyHarvestAction.MovementCancelled(Actor self) { }

public static int ZOffsetFromCenter(Actor self, WPos pos, int offset)
{
Expand Down
12 changes: 9 additions & 3 deletions OpenRA.Mods.Common/TraitsInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public interface INotifyDockHost { void Docked(Actor self, Actor client); void U
[RequireExplicitImplementation]
public interface INotifyDockClient { void Docked(Actor self, Actor host); void Undocked(Actor self, Actor host); }

[RequireExplicitImplementation]
public interface INotifyDockClientMoving
{
void MovingToDock(Actor self, Actor hostActor, IDockHost host);
void MovementCancelled(Actor self);
}

[RequireExplicitImplementation]
public interface INotifyResourceAccepted { void OnResourceAccepted(Actor self, Actor refinery, string resourceType, int count, int value); }
public interface INotifyParachute { void OnParachute(Actor self); void OnLanded(Actor self); }
Expand Down Expand Up @@ -202,12 +209,11 @@ public interface INotifyEnteredCargo { void OnEnteredCargo(Actor self, Actor car
[RequireExplicitImplementation]
public interface INotifyExitedCargo { void OnExitedCargo(Actor self, Actor cargo); }

public interface INotifyHarvesterAction
public interface INotifyHarvestAction
{
void Harvested(Actor self, string resourceType);
void MovingToResources(Actor self, CPos targetCell);
void MovingToRefinery(Actor self, Actor refineryActor);
void MovementCancelled(Actor self);
void Harvested(Actor self, string resourceType);
}

public interface IDockClientInfo : ITraitInfoInterface { }
Expand Down

0 comments on commit f26b598

Please sign in to comment.