Rework paradrop logic to be more robust.

This commit is contained in:
Paul Chote
2019-05-26 22:19:46 +00:00
committed by reaperrr
parent 829b8cd2e1
commit e2b27328bd
5 changed files with 94 additions and 91 deletions

View File

@@ -18,72 +18,45 @@ namespace OpenRA.Mods.Common.Activities
public class Parachute : Activity
{
readonly IPositionable pos;
readonly ParachutableInfo para;
readonly WVec fallVector;
readonly Actor ignore;
WPos dropPosition;
WPos currentPosition;
bool triggered = false;
int groundLevel;
public Parachute(Actor self, WPos dropPosition, Actor ignoreActor = null)
public Parachute(Actor self, Actor ignoreActor = null)
{
pos = self.TraitOrDefault<IPositionable>();
ignore = ignoreActor;
// Parachutable trait is a prerequisite for running this activity
para = self.Info.TraitInfo<ParachutableInfo>();
fallVector = new WVec(0, 0, para.FallRate);
this.dropPosition = dropPosition;
fallVector = new WVec(0, 0, self.Info.TraitInfo<ParachutableInfo>().FallRate);
IsInterruptible = false;
}
Activity FirstTick(Actor self)
protected override void OnFirstRun(Actor self)
{
triggered = true;
groundLevel = self.World.Map.CenterOfCell(self.Location).Z;
foreach (var np in self.TraitsImplementing<INotifyParachute>())
np.OnParachute(self);
// Place the actor and retrieve its visual position (CenterPosition)
pos.SetPosition(self, dropPosition);
currentPosition = self.CenterPosition;
return this;
}
Activity LastTick(Actor self)
{
var dat = self.World.Map.DistanceAboveTerrain(currentPosition);
pos.SetPosition(self, currentPosition - new WVec(WDist.Zero, WDist.Zero, dat));
foreach (var np in self.TraitsImplementing<INotifyParachute>())
np.OnLanded(self, ignore);
return NextActivity;
}
public override Activity Tick(Actor self)
{
// If this is the first tick
if (!triggered)
return FirstTick(self);
var nextPosition = self.CenterPosition - fallVector;
if (nextPosition.Z < groundLevel)
return NextActivity;
currentPosition -= fallVector;
// If the unit has landed, this will be the last tick
if (self.World.Map.DistanceAboveTerrain(currentPosition).Length <= 0)
return LastTick(self);
pos.SetVisualPosition(self, currentPosition);
pos.SetVisualPosition(self, nextPosition);
return this;
}
// Only the last queued activity (given order) is kept
public override void Queue(Actor self, Activity activity)
protected override void OnLastRun(Actor self)
{
NextActivity = activity;
var centerPosition = self.CenterPosition;
pos.SetPosition(self, centerPosition - new WVec(0, 0, groundLevel - centerPosition.Z));
foreach (var np in self.TraitsImplementing<INotifyParachute>())
np.OnLanded(self, ignore);
}
}
}