diff --git a/OpenRA.Mods.Common/Activities/FindResources.cs b/OpenRA.Mods.Common/Activities/FindResources.cs index 30ca37a8b1..efe54b8db4 100644 --- a/OpenRA.Mods.Common/Activities/FindResources.cs +++ b/OpenRA.Mods.Common/Activities/FindResources.cs @@ -133,7 +133,7 @@ namespace OpenRA.Mods.Common.Activities foreach (var n in notify) n.MovingToResources(self, path[0], next); - return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), next); + return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(self), next); } // Diagonal distance heuristic @@ -150,54 +150,4 @@ namespace OpenRA.Mods.Common.Activities yield return Target.FromCell(self.World, self.Location); } } - - public class HarvestResource : Activity - { - public override Activity Tick(Actor self) - { - var territory = self.World.WorldActor.TraitOrDefault(); - if (IsCanceled) - { - if (territory != null) - territory.UnclaimByActor(self); - return NextActivity; - } - - var harv = self.Trait(); - var harvInfo = self.Info.Traits.Get(); - harv.LastHarvestedCell = self.Location; - - if (harv.IsFull) - { - if (territory != null) - territory.UnclaimByActor(self); - return NextActivity; - } - - // Turn to one of the harvestable facings - if (harvInfo.HarvestFacings != 0) - { - var facing = self.Trait().Facing; - var desired = Util.QuantizeFacing(facing, harvInfo.HarvestFacings) * (256 / harvInfo.HarvestFacings); - if (desired != facing) - return Util.SequenceActivities(new Turn(self, desired), this); - } - - var resLayer = self.World.WorldActor.Trait(); - var resource = resLayer.Harvest(self.Location); - if (resource == null) - { - if (territory != null) - territory.UnclaimByActor(self); - return NextActivity; - } - - harv.AcceptResource(resource); - - foreach (var t in self.TraitsImplementing()) - t.Harvested(self, resource); - - return Util.SequenceActivities(new Wait(harvInfo.LoadTicksPerBale), this); - } - } } diff --git a/OpenRA.Mods.Common/Activities/HarvestResource.cs b/OpenRA.Mods.Common/Activities/HarvestResource.cs new file mode 100644 index 0000000000..71bed722f3 --- /dev/null +++ b/OpenRA.Mods.Common/Activities/HarvestResource.cs @@ -0,0 +1,77 @@ +#region Copyright & License Information +/* + * Copyright 2007-2015 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. For more information, + * see COPYING. + */ +#endregion + +using OpenRA.Activities; +using OpenRA.Mods.Common.Traits; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Activities +{ + public class HarvestResource : Activity + { + readonly Harvester harv; + readonly HarvesterInfo harvInfo; + readonly IFacing facing; + readonly ResourceClaimLayer territory; + readonly ResourceLayer resLayer; + + public HarvestResource(Actor self) + { + harv = self.Trait(); + harvInfo = self.Info.Traits.Get(); + facing = self.Trait(); + territory = self.World.WorldActor.TraitOrDefault(); + resLayer = self.World.WorldActor.Trait(); + } + + public override Activity Tick(Actor self) + { + if (IsCanceled) + { + if (territory != null) + territory.UnclaimByActor(self); + return NextActivity; + } + + harv.LastHarvestedCell = self.Location; + + if (harv.IsFull) + { + if (territory != null) + territory.UnclaimByActor(self); + return NextActivity; + } + + // Turn to one of the harvestable facings + if (harvInfo.HarvestFacings != 0) + { + var current = facing.Facing; + var desired = Util.QuantizeFacing(current, harvInfo.HarvestFacings) * (256 / harvInfo.HarvestFacings); + if (desired != current) + return Util.SequenceActivities(new Turn(self, desired), this); + } + + var resource = resLayer.Harvest(self.Location); + if (resource == null) + { + if (territory != null) + territory.UnclaimByActor(self); + return NextActivity; + } + + harv.AcceptResource(resource); + + foreach (var t in self.TraitsImplementing()) + t.Harvested(self, resource); + + return Util.SequenceActivities(new Wait(harvInfo.LoadTicksPerBale), this); + } + } +} diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index f2feebe28c..f2be7133e8 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -112,6 +112,7 @@ +