Generalize Fly activity Aircraft caching naming
To make a possible future merger (or inheritance or other code-sharing) of these activities easier.
This commit is contained in:
@@ -18,14 +18,14 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
{
|
{
|
||||||
public class Fly : Activity
|
public class Fly : Activity
|
||||||
{
|
{
|
||||||
readonly Aircraft plane;
|
readonly Aircraft aircraft;
|
||||||
readonly Target target;
|
readonly Target target;
|
||||||
readonly WDist maxRange;
|
readonly WDist maxRange;
|
||||||
readonly WDist minRange;
|
readonly WDist minRange;
|
||||||
|
|
||||||
public Fly(Actor self, Target t)
|
public Fly(Actor self, Target t)
|
||||||
{
|
{
|
||||||
plane = self.Trait<Aircraft>();
|
aircraft = self.Trait<Aircraft>();
|
||||||
target = t;
|
target = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,29 +36,29 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
this.minRange = minRange;
|
this.minRange = minRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void FlyToward(Actor self, Aircraft plane, int desiredFacing, WDist desiredAltitude)
|
public static void FlyToward(Actor self, Aircraft aircraft, int desiredFacing, WDist desiredAltitude)
|
||||||
{
|
{
|
||||||
desiredAltitude = new WDist(plane.CenterPosition.Z) + desiredAltitude - self.World.Map.DistanceAboveTerrain(plane.CenterPosition);
|
desiredAltitude = new WDist(aircraft.CenterPosition.Z) + desiredAltitude - self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition);
|
||||||
|
|
||||||
var move = plane.FlyStep(plane.Facing);
|
var move = aircraft.FlyStep(aircraft.Facing);
|
||||||
var altitude = plane.CenterPosition.Z;
|
var altitude = aircraft.CenterPosition.Z;
|
||||||
|
|
||||||
plane.Facing = Util.TickFacing(plane.Facing, desiredFacing, plane.TurnSpeed);
|
aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.TurnSpeed);
|
||||||
|
|
||||||
if (altitude != desiredAltitude.Length)
|
if (altitude != desiredAltitude.Length)
|
||||||
{
|
{
|
||||||
var delta = move.HorizontalLength * plane.Info.MaximumPitch.Tan() / 1024;
|
var delta = move.HorizontalLength * aircraft.Info.MaximumPitch.Tan() / 1024;
|
||||||
var dz = (desiredAltitude.Length - altitude).Clamp(-delta, delta);
|
var dz = (desiredAltitude.Length - altitude).Clamp(-delta, delta);
|
||||||
move += new WVec(0, 0, dz);
|
move += new WVec(0, 0, dz);
|
||||||
}
|
}
|
||||||
|
|
||||||
plane.SetPosition(self, plane.CenterPosition + move);
|
aircraft.SetPosition(self, aircraft.CenterPosition + move);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Activity Tick(Actor self)
|
public override Activity Tick(Actor self)
|
||||||
{
|
{
|
||||||
// Refuse to take off if it would land immediately again.
|
// Refuse to take off if it would land immediately again.
|
||||||
if (plane.ForceLanding)
|
if (aircraft.ForceLanding)
|
||||||
{
|
{
|
||||||
Cancel(self);
|
Cancel(self);
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
@@ -68,25 +68,25 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
return NextActivity;
|
return NextActivity;
|
||||||
|
|
||||||
// Inside the target annulus, so we're done
|
// Inside the target annulus, so we're done
|
||||||
var insideMaxRange = maxRange.Length > 0 && target.IsInRange(plane.CenterPosition, maxRange);
|
var insideMaxRange = maxRange.Length > 0 && target.IsInRange(aircraft.CenterPosition, maxRange);
|
||||||
var insideMinRange = minRange.Length > 0 && target.IsInRange(plane.CenterPosition, minRange);
|
var insideMinRange = minRange.Length > 0 && target.IsInRange(aircraft.CenterPosition, minRange);
|
||||||
if (insideMaxRange && !insideMinRange)
|
if (insideMaxRange && !insideMinRange)
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
|
|
||||||
var d = target.CenterPosition - self.CenterPosition;
|
var d = target.CenterPosition - self.CenterPosition;
|
||||||
|
|
||||||
// The next move would overshoot, so consider it close enough
|
// The next move would overshoot, so consider it close enough
|
||||||
var move = plane.FlyStep(plane.Facing);
|
var move = aircraft.FlyStep(aircraft.Facing);
|
||||||
if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
|
if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
|
|
||||||
// Don't turn until we've reached the cruise altitude
|
// Don't turn until we've reached the cruise altitude
|
||||||
var desiredFacing = d.Yaw.Facing;
|
var desiredFacing = d.Yaw.Facing;
|
||||||
var targetAltitude = plane.CenterPosition.Z + plane.Info.CruiseAltitude.Length - self.World.Map.DistanceAboveTerrain(plane.CenterPosition).Length;
|
var targetAltitude = aircraft.CenterPosition.Z + aircraft.Info.CruiseAltitude.Length - self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition).Length;
|
||||||
if (plane.CenterPosition.Z < targetAltitude)
|
if (aircraft.CenterPosition.Z < targetAltitude)
|
||||||
desiredFacing = plane.Facing;
|
desiredFacing = aircraft.Facing;
|
||||||
|
|
||||||
FlyToward(self, plane, desiredFacing, plane.Info.CruiseAltitude);
|
FlyToward(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
{
|
{
|
||||||
public class HeliFly : Activity
|
public class HeliFly : Activity
|
||||||
{
|
{
|
||||||
readonly Aircraft helicopter;
|
readonly Aircraft aircraft;
|
||||||
readonly Target target;
|
readonly Target target;
|
||||||
readonly WDist maxRange;
|
readonly WDist maxRange;
|
||||||
readonly WDist minRange;
|
readonly WDist minRange;
|
||||||
@@ -26,7 +26,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
|
|
||||||
public HeliFly(Actor self, Target t)
|
public HeliFly(Actor self, Target t)
|
||||||
{
|
{
|
||||||
helicopter = self.Trait<Aircraft>();
|
aircraft = self.Trait<Aircraft>();
|
||||||
target = t;
|
target = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,17 +37,17 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
this.minRange = minRange;
|
this.minRange = minRange;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool AdjustAltitude(Actor self, Aircraft helicopter, WDist targetAltitude)
|
public static bool AdjustAltitude(Actor self, Aircraft aircraft, WDist targetAltitude)
|
||||||
{
|
{
|
||||||
targetAltitude = new WDist(helicopter.CenterPosition.Z) + targetAltitude - self.World.Map.DistanceAboveTerrain(helicopter.CenterPosition);
|
targetAltitude = new WDist(aircraft.CenterPosition.Z) + targetAltitude - self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition);
|
||||||
|
|
||||||
var altitude = helicopter.CenterPosition.Z;
|
var altitude = aircraft.CenterPosition.Z;
|
||||||
if (altitude == targetAltitude.Length)
|
if (altitude == targetAltitude.Length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var delta = helicopter.Info.AltitudeVelocity.Length;
|
var delta = aircraft.Info.AltitudeVelocity.Length;
|
||||||
var dz = (targetAltitude.Length - altitude).Clamp(-delta, delta);
|
var dz = (targetAltitude.Length - altitude).Clamp(-delta, delta);
|
||||||
helicopter.SetPosition(self, helicopter.CenterPosition + new WVec(0, 0, dz));
|
aircraft.SetPosition(self, aircraft.CenterPosition + new WVec(0, 0, dz));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
public override Activity Tick(Actor self)
|
public override Activity Tick(Actor self)
|
||||||
{
|
{
|
||||||
// Refuse to take off if it would land immediately again.
|
// Refuse to take off if it would land immediately again.
|
||||||
if (helicopter.ForceLanding)
|
if (aircraft.ForceLanding)
|
||||||
{
|
{
|
||||||
Cancel(self);
|
Cancel(self);
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
@@ -64,43 +64,43 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
if (IsCanceled || !target.IsValidFor(self))
|
if (IsCanceled || !target.IsValidFor(self))
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
|
|
||||||
if (!playedSound && helicopter.Info.TakeoffSound != null && self.IsAtGroundLevel())
|
if (!playedSound && aircraft.Info.TakeoffSound != null && self.IsAtGroundLevel())
|
||||||
{
|
{
|
||||||
Game.Sound.Play(SoundType.World, helicopter.Info.TakeoffSound);
|
Game.Sound.Play(SoundType.World, aircraft.Info.TakeoffSound);
|
||||||
playedSound = true;
|
playedSound = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (AdjustAltitude(self, helicopter, helicopter.Info.CruiseAltitude))
|
if (AdjustAltitude(self, aircraft, aircraft.Info.CruiseAltitude))
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
var pos = target.CenterPosition;
|
var pos = target.CenterPosition;
|
||||||
|
|
||||||
// Rotate towards the target
|
// Rotate towards the target
|
||||||
var dist = pos - self.CenterPosition;
|
var dist = pos - self.CenterPosition;
|
||||||
var desiredFacing = dist.HorizontalLengthSquared != 0 ? dist.Yaw.Facing : helicopter.Facing;
|
var desiredFacing = dist.HorizontalLengthSquared != 0 ? dist.Yaw.Facing : aircraft.Facing;
|
||||||
helicopter.Facing = Util.TickFacing(helicopter.Facing, desiredFacing, helicopter.TurnSpeed);
|
aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.TurnSpeed);
|
||||||
var move = helicopter.FlyStep(desiredFacing);
|
var move = aircraft.FlyStep(desiredFacing);
|
||||||
|
|
||||||
// Inside the minimum range, so reverse
|
// Inside the minimum range, so reverse
|
||||||
if (minRange.Length > 0 && target.IsInRange(helicopter.CenterPosition, minRange))
|
if (minRange.Length > 0 && target.IsInRange(aircraft.CenterPosition, minRange))
|
||||||
{
|
{
|
||||||
helicopter.SetPosition(self, helicopter.CenterPosition - move);
|
aircraft.SetPosition(self, aircraft.CenterPosition - move);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inside the maximum range, so we're done
|
// Inside the maximum range, so we're done
|
||||||
if (maxRange.Length > 0 && target.IsInRange(helicopter.CenterPosition, maxRange))
|
if (maxRange.Length > 0 && target.IsInRange(aircraft.CenterPosition, maxRange))
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
|
|
||||||
// The next move would overshoot, so just set the final position
|
// The next move would overshoot, so just set the final position
|
||||||
if (dist.HorizontalLengthSquared < move.HorizontalLengthSquared)
|
if (dist.HorizontalLengthSquared < move.HorizontalLengthSquared)
|
||||||
{
|
{
|
||||||
var targetAltitude = helicopter.CenterPosition.Z + helicopter.Info.CruiseAltitude.Length - self.World.Map.DistanceAboveTerrain(helicopter.CenterPosition).Length;
|
var targetAltitude = aircraft.CenterPosition.Z + aircraft.Info.CruiseAltitude.Length - self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition).Length;
|
||||||
helicopter.SetPosition(self, pos + new WVec(0, 0, targetAltitude - pos.Z));
|
aircraft.SetPosition(self, pos + new WVec(0, 0, targetAltitude - pos.Z));
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
}
|
}
|
||||||
|
|
||||||
helicopter.SetPosition(self, helicopter.CenterPosition + move);
|
aircraft.SetPosition(self, aircraft.CenterPosition + move);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user