Extract HarvestResource into own file and cache trait lookups in

constructor
This commit is contained in:
reaperrr
2015-07-09 19:40:40 +02:00
parent c8661ca2f9
commit 8fdeb04569
3 changed files with 79 additions and 51 deletions

View File

@@ -133,7 +133,7 @@ namespace OpenRA.Mods.Common.Activities
foreach (var n in notify) foreach (var n in notify)
n.MovingToResources(self, path[0], next); 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 // Diagonal distance heuristic
@@ -150,54 +150,4 @@ namespace OpenRA.Mods.Common.Activities
yield return Target.FromCell(self.World, self.Location); yield return Target.FromCell(self.World, self.Location);
} }
} }
public class HarvestResource : Activity
{
public override Activity Tick(Actor self)
{
var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
if (IsCanceled)
{
if (territory != null)
territory.UnclaimByActor(self);
return NextActivity;
}
var harv = self.Trait<Harvester>();
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
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<IFacing>().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<ResourceLayer>();
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<INotifyHarvesterAction>())
t.Harvested(self, resource);
return Util.SequenceActivities(new Wait(harvInfo.LoadTicksPerBale), this);
}
}
} }

View File

@@ -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<Harvester>();
harvInfo = self.Info.Traits.Get<HarvesterInfo>();
facing = self.Trait<IFacing>();
territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
}
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<INotifyHarvesterAction>())
t.Harvested(self, resource);
return Util.SequenceActivities(new Wait(harvInfo.LoadTicksPerBale), this);
}
}
}

View File

@@ -112,6 +112,7 @@
<Compile Include="Activities\EnterTransport.cs" /> <Compile Include="Activities\EnterTransport.cs" />
<Compile Include="Activities\ExternalCaptureActor.cs" /> <Compile Include="Activities\ExternalCaptureActor.cs" />
<Compile Include="Activities\FindResources.cs" /> <Compile Include="Activities\FindResources.cs" />
<Compile Include="Activities\HarvestResource.cs" />
<Compile Include="Activities\HarvesterDockSequence.cs" /> <Compile Include="Activities\HarvesterDockSequence.cs" />
<Compile Include="Activities\Heal.cs" /> <Compile Include="Activities\Heal.cs" />
<Compile Include="Activities\Hunt.cs" /> <Compile Include="Activities\Hunt.cs" />