Move carryall code to Mods.Common.
This commit is contained in:
138
OpenRA.Mods.Common/Activities/DeliverUnit.cs
Normal file
138
OpenRA.Mods.Common/Activities/DeliverUnit.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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 OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class DeliverUnit : Activity
|
||||
{
|
||||
readonly Actor self;
|
||||
readonly Actor cargo;
|
||||
readonly IMove movement;
|
||||
readonly Carryable carryable;
|
||||
readonly Carryall carryall;
|
||||
readonly Aircraft aircraft;
|
||||
readonly IPositionable positionable;
|
||||
readonly IFacing cargoFacing;
|
||||
readonly IFacing selfFacing;
|
||||
|
||||
enum State { Transport, Land, Release }
|
||||
|
||||
State state;
|
||||
|
||||
public DeliverUnit(Actor self)
|
||||
{
|
||||
carryall = self.Trait<Carryall>();
|
||||
this.self = self;
|
||||
cargo = carryall.Carrying;
|
||||
movement = self.Trait<IMove>();
|
||||
carryable = cargo.Trait<Carryable>();
|
||||
aircraft = self.Trait<Aircraft>();
|
||||
positionable = cargo.Trait<IPositionable>();
|
||||
cargoFacing = cargo.Trait<IFacing>();
|
||||
selfFacing = self.Trait<IFacing>();
|
||||
state = State.Transport;
|
||||
}
|
||||
|
||||
// Find a suitable location to drop our carryable
|
||||
CPos GetLocationToDrop(CPos requestedPosition)
|
||||
{
|
||||
if (positionable.CanEnterCell(requestedPosition))
|
||||
return requestedPosition;
|
||||
|
||||
var candidateCells = Util.AdjacentCells(self.World, Target.FromCell(self.World, requestedPosition));
|
||||
|
||||
// TODO: This will behave badly if there is no suitable drop point nearby
|
||||
do
|
||||
{
|
||||
foreach (var c in candidateCells)
|
||||
if (positionable.CanEnterCell(c))
|
||||
return c;
|
||||
|
||||
// Expanding dropable cells search area
|
||||
// TODO: This also includes all of the cells we have just checked
|
||||
candidateCells = Util.ExpandFootprint(candidateCells, true);
|
||||
} while (true);
|
||||
}
|
||||
|
||||
// Check if we can drop the unit at our current location.
|
||||
bool CanDropHere()
|
||||
{
|
||||
return positionable.CanEnterCell(self.Location);
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (cargo.IsDead || !carryall.IsBusy)
|
||||
{
|
||||
carryall.UnreserveCarryable();
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case State.Transport:
|
||||
var targetl = GetLocationToDrop(carryable.Destination);
|
||||
state = State.Land;
|
||||
return ActivityUtils.SequenceActivities(movement.MoveTo(targetl, 0), this);
|
||||
|
||||
case State.Land:
|
||||
if (!CanDropHere())
|
||||
{
|
||||
state = State.Transport;
|
||||
return this;
|
||||
}
|
||||
|
||||
if (HeliFly.AdjustAltitude(self, aircraft, aircraft.Info.LandAltitude))
|
||||
return this;
|
||||
state = State.Release;
|
||||
return ActivityUtils.SequenceActivities(new Wait(15), this);
|
||||
|
||||
case State.Release:
|
||||
if (!CanDropHere())
|
||||
{
|
||||
state = State.Transport;
|
||||
return this;
|
||||
}
|
||||
|
||||
Release();
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
void Release()
|
||||
{
|
||||
positionable.SetPosition(cargo, self.Location, SubCell.FullCell);
|
||||
cargoFacing.Facing = selfFacing.Facing;
|
||||
|
||||
// Put back into world
|
||||
self.World.AddFrameEndTask(w =>
|
||||
{
|
||||
cargo.World.Add(cargo);
|
||||
carryall.UnreserveCarryable();
|
||||
});
|
||||
|
||||
// Unlock carryable
|
||||
carryall.CarryableReleased();
|
||||
carryable.Dropped();
|
||||
}
|
||||
|
||||
public override void Cancel(Actor self)
|
||||
{
|
||||
// TODO: Drop the unit at the nearest available cell
|
||||
}
|
||||
}
|
||||
}
|
||||
105
OpenRA.Mods.Common/Activities/PickupUnit.cs
Normal file
105
OpenRA.Mods.Common/Activities/PickupUnit.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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 OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class PickupUnit : Activity
|
||||
{
|
||||
readonly Actor cargo;
|
||||
readonly IMove movement;
|
||||
readonly Carryable carryable;
|
||||
readonly Carryall carryall;
|
||||
readonly Aircraft aircraft;
|
||||
readonly IFacing cargoFacing;
|
||||
readonly IFacing selfFacing;
|
||||
|
||||
enum State { Intercept, LockCarryable, MoveToCarryable, Turn, Pickup, TakeOff }
|
||||
|
||||
State state;
|
||||
|
||||
public PickupUnit(Actor self, Actor cargo)
|
||||
{
|
||||
this.cargo = cargo;
|
||||
carryable = cargo.Trait<Carryable>();
|
||||
cargoFacing = cargo.Trait<IFacing>();
|
||||
movement = self.Trait<IMove>();
|
||||
carryall = self.Trait<Carryall>();
|
||||
aircraft = self.Trait<Aircraft>();
|
||||
selfFacing = self.Trait<IFacing>();
|
||||
state = State.Intercept;
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (cargo.IsDead || !carryall.IsBusy)
|
||||
{
|
||||
carryall.UnreserveCarryable();
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case State.Intercept:
|
||||
state = State.LockCarryable;
|
||||
return ActivityUtils.SequenceActivities(movement.MoveWithinRange(Target.FromActor(cargo), WDist.FromCells(4)), this);
|
||||
|
||||
case State.LockCarryable:
|
||||
// Last check
|
||||
if (carryable.StandbyForPickup(self))
|
||||
{
|
||||
state = State.MoveToCarryable;
|
||||
return this;
|
||||
}
|
||||
|
||||
// We got cancelled
|
||||
carryall.UnreserveCarryable();
|
||||
return NextActivity;
|
||||
|
||||
case State.MoveToCarryable: // We arrived, move on top
|
||||
if (self.Location == cargo.Location)
|
||||
{
|
||||
state = State.Turn;
|
||||
return this;
|
||||
}
|
||||
|
||||
return ActivityUtils.SequenceActivities(movement.MoveTo(cargo.Location, 0), this);
|
||||
|
||||
case State.Turn: // Align facing and Land
|
||||
if (selfFacing.Facing != cargoFacing.Facing)
|
||||
return ActivityUtils.SequenceActivities(new Turn(self, cargoFacing.Facing), this);
|
||||
state = State.Pickup;
|
||||
return ActivityUtils.SequenceActivities(new HeliLand(self, false), new Wait(10), this);
|
||||
|
||||
case State.Pickup:
|
||||
// Remove our carryable from world
|
||||
self.World.AddFrameEndTask(w => cargo.World.Remove(cargo));
|
||||
carryall.AttachCarryable(cargo);
|
||||
state = State.TakeOff;
|
||||
return this;
|
||||
case State.TakeOff:
|
||||
if (HeliFly.AdjustAltitude(self, aircraft, aircraft.Info.CruiseAltitude))
|
||||
return this;
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
public override void Cancel(Actor self)
|
||||
{
|
||||
// TODO: Drop the unit at the nearest available cell
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -758,6 +758,11 @@
|
||||
<Compile Include="FileFormats\MSCabCompression.cs" />
|
||||
<Compile Include="UtilityCommands\OutputActorMiniYamlCommand.cs" />
|
||||
<Compile Include="Traits\World\ScriptLobbyDropdown.cs" />
|
||||
<Compile Include="Activities\DeliverUnit.cs" />
|
||||
<Compile Include="Activities\PickupUnit.cs" />
|
||||
<Compile Include="Traits\Carryable.cs" />
|
||||
<Compile Include="Traits\Carryall.cs" />
|
||||
<Compile Include="Traits\Buildings\FreeActorWithDelivery.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
|
||||
101
OpenRA.Mods.Common/Traits/Buildings/FreeActorWithDelivery.cs
Normal file
101
OpenRA.Mods.Common/Traits/Buildings/FreeActorWithDelivery.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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 OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Player receives a unit for free once the building is placed.",
|
||||
"If you want more than one unit to be delivered, copy this section and assign IDs like FreeActorWithDelivery@2, ...")]
|
||||
public class FreeActorWithDeliveryInfo : FreeActorInfo
|
||||
{
|
||||
[ActorReference, FieldLoader.Require]
|
||||
[Desc("Name of the delivering actor. This actor must have the `Carryall` trait")]
|
||||
public readonly string DeliveringActor = null;
|
||||
|
||||
[Desc("Cell coordinates for spawning the delivering actor. If left blank, the closest edge cell will be chosen.")]
|
||||
public readonly CPos SpawnLocation = CPos.Zero;
|
||||
|
||||
[Desc("Offset relative to the top-left cell of the building.")]
|
||||
public readonly CVec DeliveryOffset = CVec.Zero;
|
||||
|
||||
public override object Create(ActorInitializer init) { return new FreeActorWithDelivery(init, this); }
|
||||
}
|
||||
|
||||
public class FreeActorWithDelivery
|
||||
{
|
||||
public readonly FreeActorWithDeliveryInfo Info;
|
||||
|
||||
readonly Actor self;
|
||||
|
||||
public FreeActorWithDelivery(ActorInitializer init, FreeActorWithDeliveryInfo info)
|
||||
{
|
||||
self = init.Self;
|
||||
Info = info;
|
||||
|
||||
DoDelivery(self.Location + info.DeliveryOffset, info.Actor, info.DeliveringActor);
|
||||
}
|
||||
|
||||
public void DoDelivery(CPos location, string actorName, string carrierActorName)
|
||||
{
|
||||
Actor cargo;
|
||||
Actor carrier;
|
||||
|
||||
CreateActors(actorName, carrierActorName, out cargo, out carrier);
|
||||
|
||||
var carryable = cargo.Trait<Carryable>();
|
||||
carryable.Destination = location;
|
||||
carryable.Reserve(carrier);
|
||||
|
||||
carrier.Trait<Carryall>().AttachCarryable(cargo);
|
||||
|
||||
carrier.QueueActivity(new DeliverUnit(carrier));
|
||||
carrier.QueueActivity(new HeliFly(carrier, Target.FromCell(self.World, self.World.Map.ChooseRandomEdgeCell(self.World.SharedRandom))));
|
||||
carrier.QueueActivity(new RemoveSelf());
|
||||
|
||||
self.World.AddFrameEndTask(w => self.World.Add(carrier));
|
||||
}
|
||||
|
||||
void CreateActors(string actorName, string deliveringActorName, out Actor cargo, out Actor carrier)
|
||||
{
|
||||
// Get a carryall spawn location
|
||||
var location = Info.SpawnLocation;
|
||||
if (location == CPos.Zero)
|
||||
location = self.World.Map.ChooseClosestEdgeCell(self.Location);
|
||||
|
||||
var spawn = self.World.Map.CenterOfCell(location);
|
||||
|
||||
var initialFacing = self.World.Map.FacingBetween(location, self.Location, 0);
|
||||
|
||||
// If aircraft, spawn at cruise altitude
|
||||
var aircraftInfo = self.World.Map.Rules.Actors[deliveringActorName.ToLower()].TraitInfoOrDefault<AircraftInfo>();
|
||||
if (aircraftInfo != null)
|
||||
spawn += new WVec(0, 0, aircraftInfo.CruiseAltitude.Length);
|
||||
|
||||
// Create delivery actor
|
||||
carrier = self.World.CreateActor(false, deliveringActorName, new TypeDictionary
|
||||
{
|
||||
new LocationInit(location),
|
||||
new CenterPositionInit(spawn),
|
||||
new OwnerInit(self.Owner),
|
||||
new FacingInit(initialFacing)
|
||||
});
|
||||
|
||||
// Create delivered actor
|
||||
cargo = self.World.CreateActor(false, actorName, new TypeDictionary
|
||||
{
|
||||
new OwnerInit(self.Owner),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
200
OpenRA.Mods.Common/Traits/Carryable.cs
Normal file
200
OpenRA.Mods.Common/Traits/Carryable.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Can be carried by units with the trait `Carryall`.")]
|
||||
public class CarryableInfo : ITraitInfo, Requires<UpgradeManagerInfo>
|
||||
{
|
||||
[Desc("Required distance away from destination before requesting a pickup. Default is 6 cells.")]
|
||||
public readonly WDist MinDistance = WDist.FromCells(6);
|
||||
|
||||
[UpgradeGrantedReference]
|
||||
[Desc("The upgrades to grant to self while waiting or being carried.")]
|
||||
public readonly string[] CarryableUpgrades = { };
|
||||
|
||||
public object Create(ActorInitializer init) { return new Carryable(init.Self, this); }
|
||||
}
|
||||
|
||||
public class Carryable : INotifyHarvesterAction, ICallForTransport
|
||||
{
|
||||
readonly CarryableInfo info;
|
||||
readonly Actor self;
|
||||
readonly UpgradeManager upgradeManager;
|
||||
|
||||
public bool Reserved { get; private set; }
|
||||
|
||||
// If we're locked there isn't much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
|
||||
bool locked;
|
||||
|
||||
void Unlock()
|
||||
{
|
||||
if (!locked)
|
||||
return;
|
||||
|
||||
locked = false;
|
||||
foreach (var u in info.CarryableUpgrades)
|
||||
upgradeManager.RevokeUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
void Lock()
|
||||
{
|
||||
if (locked)
|
||||
return;
|
||||
|
||||
locked = true;
|
||||
foreach (var u in info.CarryableUpgrades)
|
||||
upgradeManager.GrantUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
public bool WantsTransport { get; set; }
|
||||
public CPos Destination;
|
||||
Activity afterLandActivity;
|
||||
|
||||
public Carryable(Actor self, CarryableInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
this.self = self;
|
||||
upgradeManager = self.Trait<UpgradeManager>();
|
||||
|
||||
locked = false;
|
||||
Reserved = false;
|
||||
WantsTransport = false;
|
||||
}
|
||||
|
||||
public void MovingToResources(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
|
||||
public void MovingToRefinery(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
|
||||
|
||||
public WDist MinimumDistance { get { return info.MinDistance; } }
|
||||
|
||||
public void RequestTransport(CPos destination, Activity afterLandActivity)
|
||||
{
|
||||
var destPos = self.World.Map.CenterOfCell(destination);
|
||||
if (destination == CPos.Zero || (self.CenterPosition - destPos).LengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
WantsTransport = false; // Be sure to cancel any pending transports
|
||||
return;
|
||||
}
|
||||
|
||||
Destination = destination;
|
||||
this.afterLandActivity = afterLandActivity;
|
||||
WantsTransport = true;
|
||||
|
||||
if (locked || Reserved)
|
||||
return;
|
||||
|
||||
// Inform all idle carriers
|
||||
var carriers = self.World.ActorsWithTrait<Carryall>()
|
||||
.Where(c => !c.Trait.IsBusy && !c.Actor.IsDead && c.Actor.Owner == self.Owner && c.Actor.IsInWorld)
|
||||
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
|
||||
|
||||
// Is any carrier able to transport the actor?
|
||||
// Any will return once it finds a carrier that returns true.
|
||||
carriers.Any(carrier => carrier.Trait.RequestTransportNotify(self));
|
||||
}
|
||||
|
||||
// No longer want to be carried
|
||||
public void MovementCancelled(Actor self)
|
||||
{
|
||||
if (locked)
|
||||
return;
|
||||
|
||||
WantsTransport = false;
|
||||
afterLandActivity = null;
|
||||
|
||||
// TODO: We could implement something like a carrier.Trait<Carryall>().CancelTransportNotify(self) and call it here
|
||||
}
|
||||
|
||||
// We do not handle Harvested notification
|
||||
public void Harvested(Actor self, ResourceType resource) { }
|
||||
public void Docked() { }
|
||||
public void Undocked() { }
|
||||
|
||||
public Actor GetClosestIdleCarrier()
|
||||
{
|
||||
// Find carriers
|
||||
var carriers = self.World.ActorsHavingTrait<Carryall>(c => !c.IsBusy)
|
||||
.Where(a => a.Owner == self.Owner && a.IsInWorld);
|
||||
|
||||
return carriers.ClosestTo(self);
|
||||
}
|
||||
|
||||
// This gets called by carrier after we touched down
|
||||
public void Dropped()
|
||||
{
|
||||
WantsTransport = false;
|
||||
Unlock();
|
||||
|
||||
if (afterLandActivity != null)
|
||||
{
|
||||
// HACK: Harvesters need special treatment to avoid getting stuck on resource fields,
|
||||
// so if a Harvester's afterLandActivity is not DeliverResources, queue a new FindResources activity
|
||||
var findResources = self.Info.HasTraitInfo<HarvesterInfo>() && !(afterLandActivity is DeliverResources);
|
||||
if (findResources)
|
||||
self.QueueActivity(new FindResources(self));
|
||||
else
|
||||
self.QueueActivity(false, afterLandActivity);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Reserve(Actor carrier)
|
||||
{
|
||||
if (Reserved)
|
||||
return false;
|
||||
|
||||
var destPos = self.World.Map.CenterOfCell(Destination);
|
||||
if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
MovementCancelled(self);
|
||||
return false;
|
||||
}
|
||||
|
||||
Reserved = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void UnReserve(Actor carrier)
|
||||
{
|
||||
Reserved = false;
|
||||
Unlock();
|
||||
}
|
||||
|
||||
// Prepare for transport pickup
|
||||
public bool StandbyForPickup(Actor carrier)
|
||||
{
|
||||
if (Destination == CPos.Zero)
|
||||
return false;
|
||||
|
||||
if (locked || !WantsTransport)
|
||||
return false;
|
||||
|
||||
// Last chance to change our mind...
|
||||
var destPos = self.World.Map.CenterOfCell(Destination);
|
||||
if ((self.CenterPosition - destPos).LengthSquared < info.MinDistance.LengthSquared)
|
||||
{
|
||||
MovementCancelled(self);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Cancel our activities
|
||||
self.CancelActivity();
|
||||
Lock();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
216
OpenRA.Mods.Common/Traits/Carryall.cs
Normal file
216
OpenRA.Mods.Common/Traits/Carryall.cs
Normal file
@@ -0,0 +1,216 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2016 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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Mods.Common.Traits.Render;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries.")]
|
||||
public class CarryallInfo : ITraitInfo, Requires<BodyOrientationInfo>
|
||||
{
|
||||
[Desc("Set to false when the carryall should not automatically get new jobs.")]
|
||||
public readonly bool Automatic = true;
|
||||
|
||||
public object Create(ActorInitializer init) { return new Carryall(init.Self, this); }
|
||||
}
|
||||
|
||||
public class Carryall : INotifyBecomingIdle, INotifyKilled, ISync, IRender, INotifyActorDisposing
|
||||
{
|
||||
readonly Actor self;
|
||||
readonly WDist carryHeight;
|
||||
readonly CarryallInfo info;
|
||||
|
||||
// The actor we are currently carrying.
|
||||
[Sync] public Actor Carrying { get; internal set; }
|
||||
public bool IsCarrying { get; internal set; }
|
||||
|
||||
// TODO: Use ActorPreviews so that this can support actors with multiple sprites
|
||||
Animation anim;
|
||||
|
||||
public bool IsBusy { get; internal set; }
|
||||
|
||||
public Carryall(Actor self, CarryallInfo info)
|
||||
{
|
||||
this.self = self;
|
||||
this.info = info;
|
||||
|
||||
IsBusy = false;
|
||||
IsCarrying = false;
|
||||
|
||||
var helicopter = self.Info.TraitInfoOrDefault<AircraftInfo>();
|
||||
carryHeight = helicopter != null ? helicopter.LandAltitude : WDist.Zero;
|
||||
}
|
||||
|
||||
public void OnBecomingIdle(Actor self)
|
||||
{
|
||||
if (info.Automatic)
|
||||
FindCarryableForTransport();
|
||||
|
||||
if (!IsBusy)
|
||||
self.QueueActivity(new HeliFlyCircle(self));
|
||||
}
|
||||
|
||||
// A carryable notifying us that he'd like to be carried
|
||||
public bool RequestTransportNotify(Actor carryable)
|
||||
{
|
||||
if (IsBusy || !info.Automatic)
|
||||
return false;
|
||||
|
||||
if (ReserveCarryable(carryable))
|
||||
{
|
||||
self.QueueActivity(false, new PickupUnit(self, carryable));
|
||||
self.QueueActivity(true, new DeliverUnit(self));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void FindCarryableForTransport()
|
||||
{
|
||||
if (!self.IsInWorld)
|
||||
return;
|
||||
|
||||
// Get all carryables who want transport
|
||||
var carryables = self.World.ActorsWithTrait<Carryable>()
|
||||
.Where(p =>
|
||||
{
|
||||
var actor = p.Actor;
|
||||
if (actor == null)
|
||||
return false;
|
||||
|
||||
if (actor.Owner != self.Owner)
|
||||
return false;
|
||||
|
||||
if (actor.IsDead)
|
||||
return false;
|
||||
|
||||
var trait = p.Trait;
|
||||
if (trait.Reserved)
|
||||
return false;
|
||||
|
||||
if (!trait.WantsTransport)
|
||||
return false;
|
||||
|
||||
if (actor.IsIdle)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
})
|
||||
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
|
||||
|
||||
foreach (var p in carryables)
|
||||
{
|
||||
// Check if its actually me who's the best candidate
|
||||
if (p.Trait.GetClosestIdleCarrier() == self && ReserveCarryable(p.Actor))
|
||||
{
|
||||
self.QueueActivity(false, new PickupUnit(self, p.Actor));
|
||||
self.QueueActivity(true, new DeliverUnit(self));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reserve the carryable so its ours exclusively
|
||||
public bool ReserveCarryable(Actor carryable)
|
||||
{
|
||||
if (Carrying != null)
|
||||
return false;
|
||||
|
||||
if (carryable.Trait<Carryable>().Reserve(self))
|
||||
{
|
||||
Carrying = carryable;
|
||||
IsBusy = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Unreserve the carryable
|
||||
public void UnreserveCarryable()
|
||||
{
|
||||
if (Carrying != null)
|
||||
{
|
||||
if (Carrying.IsInWorld && !Carrying.IsDead)
|
||||
Carrying.Trait<Carryable>().UnReserve(self);
|
||||
|
||||
Carrying = null;
|
||||
}
|
||||
|
||||
CarryableReleased();
|
||||
}
|
||||
|
||||
// INotifyKilled
|
||||
public void Killed(Actor self, AttackInfo e)
|
||||
{
|
||||
if (Carrying != null)
|
||||
{
|
||||
if (IsCarrying && Carrying.IsInWorld && !Carrying.IsDead)
|
||||
Carrying.Kill(e.Attacker);
|
||||
|
||||
Carrying = null;
|
||||
}
|
||||
|
||||
UnreserveCarryable();
|
||||
}
|
||||
|
||||
public void Disposing(Actor self)
|
||||
{
|
||||
if (Carrying != null && IsCarrying)
|
||||
{
|
||||
Carrying.Dispose();
|
||||
Carrying = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Called when carryable is inside.
|
||||
public void AttachCarryable(Actor carryable)
|
||||
{
|
||||
IsBusy = true;
|
||||
IsCarrying = true;
|
||||
Carrying = carryable;
|
||||
|
||||
// Create a new animation for our carryable unit
|
||||
var rs = carryable.Trait<RenderSprites>();
|
||||
anim = new Animation(self.World, rs.GetImage(carryable), RenderSprites.MakeFacingFunc(self));
|
||||
anim.PlayRepeating("idle");
|
||||
anim.IsDecoration = true;
|
||||
}
|
||||
|
||||
// Called when released
|
||||
public void CarryableReleased()
|
||||
{
|
||||
IsBusy = false;
|
||||
IsCarrying = false;
|
||||
anim = null;
|
||||
}
|
||||
|
||||
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
|
||||
{
|
||||
// Render the carryable below us TODO: Implement RenderSprites trait
|
||||
if (anim != null && !self.World.FogObscures(self))
|
||||
{
|
||||
anim.Tick();
|
||||
var renderables = anim.Render(self.CenterPosition + new WVec(0, 0, -carryHeight.Length),
|
||||
wr.Palette("player" + Carrying.Owner.InternalName));
|
||||
|
||||
foreach (var rr in renderables)
|
||||
yield return rr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user