Introduce a new CarryableHarvester trait

This commit is contained in:
abcdefg30
2018-02-22 00:39:57 +01:00
committed by Paul Chote
parent 89e3b62f61
commit 7e20bdd7ea
8 changed files with 84 additions and 55 deletions

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new AutoCarryable(init.Self, this); }
}
public class AutoCarryable : Carryable, INotifyHarvesterAction, ICallForTransport
public class AutoCarryable : Carryable, ICallForTransport
{
readonly AutoCarryableInfo info;
Activity afterLandActivity;
@@ -38,21 +38,6 @@ namespace OpenRA.Mods.Common.Traits
public WDist MinimumDistance { get { return info.MinDistance; } }
void INotifyHarvesterAction.MovingToResources(Actor self, CPos targetCell, Activity next) { RequestTransport(self, targetCell, next); }
void INotifyHarvesterAction.MovingToRefinery(Actor self, Actor refineryActor, Activity next)
{
var iao = refineryActor.Trait<IAcceptResources>();
RequestTransport(self, refineryActor.Location + iao.DeliveryOffset, next);
}
void INotifyHarvesterAction.MovementCancelled(Actor self) { MovementCancelled(self); }
// We do not handle Harvested notification
void INotifyHarvesterAction.Harvested(Actor self, ResourceType resource) { }
void INotifyHarvesterAction.Docked() { }
void INotifyHarvesterAction.Undocked() { }
// No longer want to be carried
void ICallForTransport.MovementCancelled(Actor self) { MovementCancelled(self); }
void ICallForTransport.RequestTransport(Actor self, CPos destination, Activity afterLandActivity) { RequestTransport(self, destination, afterLandActivity); }

View File

@@ -0,0 +1,57 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class CarryableHarvesterInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new CarryableHarvester(); }
}
public class CarryableHarvester : INotifyCreated, INotifyHarvesterAction
{
ICallForTransport[] transports;
void INotifyCreated.Created(Actor self)
{
transports = self.TraitsImplementing<ICallForTransport>().ToArray();
}
void INotifyHarvesterAction.MovingToResources(Actor self, CPos targetCell, Activity next)
{
foreach (var t in transports)
t.RequestTransport(self, targetCell, next);
}
void INotifyHarvesterAction.MovingToRefinery(Actor self, Actor refineryActor, Activity next)
{
var iao = refineryActor.Trait<IAcceptResources>();
var location = refineryActor.Location + iao.DeliveryOffset;
foreach (var t in transports)
t.RequestTransport(self, location, next);
}
void INotifyHarvesterAction.MovementCancelled(Actor self)
{
foreach (var t in transports)
t.MovementCancelled(self);
}
void INotifyHarvesterAction.Harvested(Actor self, ResourceType resource) { }
void INotifyHarvesterAction.Docked() { }
void INotifyHarvesterAction.Undocked() { }
}
}

View File

@@ -83,9 +83,9 @@ namespace OpenRA.Mods.Common.Traits
readonly Mobile mobile;
readonly ResourceLayer resLayer;
readonly ResourceClaimLayer claimLayer;
Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
INotifyHarvesterAction[] notify;
readonly Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
bool idleSmart = true;
INotifyHarvesterAction[] notifyHarvesterAction;
int idleDuration;
[Sync] public bool LastSearchFailed;
@@ -119,7 +119,7 @@ namespace OpenRA.Mods.Common.Traits
void INotifyCreated.Created(Actor self)
{
notify = self.TraitsImplementing<INotifyHarvesterAction>().ToArray();
notifyHarvesterAction = self.TraitsImplementing<INotifyHarvesterAction>().ToArray();
// Note: This is queued in a FrameEndTask because otherwise the activity is dropped/overridden while moving out of a factory.
if (Info.SearchOnCreation)
@@ -233,11 +233,8 @@ namespace OpenRA.Mods.Common.Traits
var unblockCell = LastHarvestedCell ?? (deliveryLoc + Info.UnblockCell);
var moveTo = mobile.NearestMoveableCell(unblockCell, 1, 5);
// TODO: The harvest-deliver-return sequence is a horrible mess of duplicated code and edge-cases
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
var findResources = new FindResources(self);
foreach (var n in notify)
n.MovingToResources(self, moveTo, findResources);
// FindResources takes care of calling INotifyHarvesterAction
self.QueueActivity(new FindResources(self));
self.QueueActivity(mobile.MoveTo(moveTo, 1));
self.SetTargetLine(Target.FromCell(self.World, moveTo), Color.Gray, false);
@@ -381,12 +378,10 @@ namespace OpenRA.Mods.Common.Traits
loc = self.Location;
}
var findResources = new FindResources(self);
self.QueueActivity(findResources);
self.SetTargetLine(Target.FromCell(self.World, loc.Value), Color.Red);
foreach (var n in notify)
n.MovingToResources(self, loc.Value, findResources);
// FindResources takes care of calling INotifyHarvesterAction
self.QueueActivity(new FindResources(self));
LastOrderLocation = loc;
@@ -414,16 +409,14 @@ namespace OpenRA.Mods.Common.Traits
self.SetTargetLine(order.Target, Color.Green);
self.CancelActivity();
self.QueueActivity(new DeliverResources(self));
var deliver = new DeliverResources(self);
self.QueueActivity(deliver);
foreach (var n in notify)
n.MovingToRefinery(self, targetActor, deliver);
foreach (var n in notifyHarvesterAction)
n.MovingToRefinery(self, targetActor, new DeliverResources(self));
}
else if (order.OrderString == "Stop" || order.OrderString == "Move")
{
foreach (var n in notify)
foreach (var n in notifyHarvesterAction)
n.MovementCancelled(self);
// Turn off idle smarts to obey the stop/move:

View File

@@ -50,7 +50,7 @@ namespace OpenRA.Mods.Common.Traits.Render
p => ZOffsetFromCenter(self, p, 0)), info.Palette);
}
public void Harvested(Actor self, ResourceType resource)
void INotifyHarvesterAction.Harvested(Actor self, ResourceType resource)
{
if (visible)
return;
@@ -59,11 +59,11 @@ namespace OpenRA.Mods.Common.Traits.Render
anim.PlayThen(info.Sequence, () => visible = false);
}
public void MovingToResources(Actor self, CPos targetCell, Activity next) { }
public void MovingToRefinery(Actor self, Actor targetRefinery, Activity next) { }
public void MovementCancelled(Actor self) { }
public void Docked() { }
public void Undocked() { }
void INotifyHarvesterAction.MovingToResources(Actor self, CPos targetCell, Activity next) { }
void INotifyHarvesterAction.MovingToRefinery(Actor self, Actor targetRefinery, Activity next) { }
void INotifyHarvesterAction.MovementCancelled(Actor self) { }
void INotifyHarvesterAction.Docked() { }
void INotifyHarvesterAction.Undocked() { }
public static int ZOffsetFromCenter(Actor self, WPos pos, int offset)
{