Skip to content

Commit

Permalink
Add ability to create waypoint handle from preexisting waypoint, and …
Browse files Browse the repository at this point in the history
…to hide/show waypoints
  • Loading branch information
cheese3660 committed Jan 21, 2024
1 parent 8d9646c commit 214b2f0
Showing 1 changed file with 90 additions and 4 deletions.
94 changes: 90 additions & 4 deletions src/SpaceWarp.Game/API/Game/Waypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ namespace SpaceWarp.API.Game;
/// </summary>
[PublicAPI]
public class Waypoint {
/// <summary>
/// This contains the state for a waypoint
/// </summary>
public enum WaypointState
{
/// <summary>
/// The waypoint is shown in the flight/map view
/// </summary>
Visible,
/// <summary>
/// The waypoint is hidden in the flight/map view
/// </summary>
Hidden
}

private SimulationObjectModel _waypointObject;
/// <summary>
/// The current name of the waypoint
Expand All @@ -37,9 +52,62 @@ public class Waypoint {
/// </summary>
public double AltitudeFromRadius { get; private set; }

[CanBeNull] private string _hiddenRename;

private WaypointState _state = WaypointState.Visible;
/// <summary>
/// Set the state of the waypoint to either being hidden/shown
/// </summary>
/// <exception cref="Exception">Thrown when trying to set the state of a destroyed waypoint</exception>
public WaypointState State
{
get => _state;
set
{
if (_isDestroyed)
{
throw new Exception("Waypoint was already destroyed");
}
if (value == _state) return;
_state = value;
if (value == WaypointState.Hidden)
{
_hiddenRename = Name;
_waypointObject.Destroy();
}
else
{
var spaceSimulation = GameManager.Instance.Game.SpaceSimulation;
var celestialBodies = GameManager.Instance.Game.UniverseModel.GetAllCelestialBodies();
var body = celestialBodies.Find(c => c.Name == BodyName);
if (body == null)
throw new Exception($"Could not create waypoint as there is no body with the name of {BodyName}");
var waypointComponentDefinition = new WaypointComponentDefinition { Name = _hiddenRename };
_waypointObject = spaceSimulation.CreateWaypointSimObject(
waypointComponentDefinition, body, Latitude, Longitude, AltitudeFromRadius);
_hiddenRename = null;
}
}
}

private bool _isDestroyed;

private static long _nextID;

/// <summary>
/// Creates a waypoint handle from a preexisting waypoint
/// </summary>
/// <param name="preexistingWaypoint">The preexisting waypoint</param>
public Waypoint(WaypointComponent preexistingWaypoint)
{
_waypointObject = preexistingWaypoint.SimulationObject;
var body = _waypointObject.transform.parent.transform.objectModel.CelestialBody;
BodyName = body.Name;
body.GetLatLonAltFromRadius(_waypointObject.transform.Position, out var latitude, out var longitude, out var altitudeFromRadius);
Latitude = latitude;
Longitude = longitude;
AltitudeFromRadius = altitudeFromRadius;
}

/// <summary>
/// Create a new waypoint at the specified location
Expand Down Expand Up @@ -100,10 +168,11 @@ public void Move(double latitude, double longitude, double? altitudeFromRadius =
Latitude = latitude;
Longitude = longitude;
AltitudeFromRadius = altitudeFromRadius.Value;
if (_state != WaypointState.Visible) return;
var bodyFrame = body.transform.bodyFrame;
var relSurfacePosition = body.GetRelSurfacePosition(latitude,longitude,altitudeFromRadius.Value);
var relSurfacePosition = body.GetRelSurfacePosition(latitude, longitude, altitudeFromRadius.Value);
_waypointObject.transform.parent = bodyFrame;
_waypointObject.transform.Position = new Position(bodyFrame,relSurfacePosition);
_waypointObject.transform.Position = new Position(bodyFrame, relSurfacePosition);
}

/// <summary>
Expand All @@ -115,7 +184,24 @@ public void Rename([CanBeNull] string name = null) {
if (_isDestroyed) {
throw new Exception("Waypoint was already destroyed");
}
_waypointObject.Name = name ?? $"Waypoint-{_nextID++}";

if (State == WaypointState.Visible)
{
_waypointObject.Name = name ?? $"Waypoint-{_nextID++}";
}
else
{
_hiddenRename = _waypointObject.Name;
}
}



/// <summary>
/// Hides the waypoint
/// </summary>
public void Hide() => State = WaypointState.Hidden;
/// <summary>
/// Shows the waypoint
/// </summary>
public void Show() => State = WaypointState.Visible;
}

0 comments on commit 214b2f0

Please sign in to comment.