|
| 1 | +#region Copyright & License Information |
| 2 | +/* |
| 3 | + * Copyright (c) The OpenRA Developers and Contributors |
| 4 | + * This file is part of OpenRA, which is free software. It is made |
| 5 | + * available to you under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation, either version 3 of |
| 7 | + * the License, or (at your option) any later version. For more |
| 8 | + * information, see COPYING. |
| 9 | + */ |
| 10 | +#endregion |
| 11 | + |
| 12 | +using OpenRA.Mods.Common.Activities; |
| 13 | +using OpenRA.Mods.Common.Traits; |
| 14 | +using OpenRA.Primitives; |
| 15 | +using OpenRA.Traits; |
| 16 | + |
| 17 | +namespace OpenRA.Mods.CA.Activities |
| 18 | +{ |
| 19 | + public class MassRideTransport : Enter |
| 20 | + { |
| 21 | + readonly Passenger passenger; |
| 22 | + |
| 23 | + Actor enterActor; |
| 24 | + Cargo enterCargo; |
| 25 | + Aircraft enterAircraft; |
| 26 | + |
| 27 | + public MassRideTransport(Actor self, in Target target, Color? targetLineColor) |
| 28 | + : base(self, target, targetLineColor) |
| 29 | + { |
| 30 | + passenger = self.Trait<Passenger>(); |
| 31 | + } |
| 32 | + |
| 33 | + protected override bool TryStartEnter(Actor self, Actor targetActor) |
| 34 | + { |
| 35 | + enterActor = targetActor; |
| 36 | + enterCargo = targetActor.TraitOrDefault<Cargo>(); |
| 37 | + enterAircraft = targetActor.TraitOrDefault<Aircraft>(); |
| 38 | + |
| 39 | + // Make sure we can still enter the transport |
| 40 | + // (but not before, because this may stop the actor in the middle of nowhere) |
| 41 | + if (enterCargo == null || !passenger.Reserve(self, enterCargo)) |
| 42 | + { |
| 43 | + Cancel(self, true); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + if (enterAircraft != null && !enterAircraft.AtLandAltitude) |
| 48 | + return false; |
| 49 | + |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + protected override void OnEnterComplete(Actor self, Actor targetActor) |
| 54 | + { |
| 55 | + self.World.AddFrameEndTask(w => |
| 56 | + { |
| 57 | + if (self.IsDead) |
| 58 | + return; |
| 59 | + |
| 60 | + // Make sure the target hasn't changed while entering |
| 61 | + // OnEnterComplete is only called if targetActor is alive |
| 62 | + if (targetActor != enterActor) |
| 63 | + return; |
| 64 | + |
| 65 | + if (!enterCargo.CanLoad(self)) |
| 66 | + return; |
| 67 | + |
| 68 | + foreach (var inl in targetActor.TraitsImplementing<INotifyLoadCargo>()) |
| 69 | + inl.Loading(self); |
| 70 | + |
| 71 | + enterCargo.Load(enterActor, self); |
| 72 | + w.Remove(self); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + protected override void OnLastRun(Actor self) |
| 77 | + { |
| 78 | + passenger.Unreserve(self); |
| 79 | + } |
| 80 | + |
| 81 | + public override void Cancel(Actor self, bool keepQueue = false) |
| 82 | + { |
| 83 | + passenger.Unreserve(self); |
| 84 | + |
| 85 | + base.Cancel(self, keepQueue); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments