Skip to content

Commit

Permalink
Add waypoint serialization/deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Jan 21, 2024
1 parent 1bc7bf4 commit 0c99215
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
49 changes: 49 additions & 0 deletions src/SpaceWarp.Game/API/Game/Waypoints/SerializedWaypoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace SpaceWarp.API.Game.Waypoints;

/// <summary>
/// This contains the serialized information for a waypoint, used for saving/loading waypoints
/// </summary>
[Serializable]
[method: JsonConstructor]
[PublicAPI]
public class SerializedWaypoint(string name, string bodyName, double latitude, double longitude, double altitude, WaypointState state)
{
/// <summary>
/// The name of the waypoint
/// </summary>
public string Name => name;

/// <summary>
/// The body the waypoint is on
/// </summary>
public string BodyName => bodyName;

/// <summary>
/// The latitude of the waypoint
/// </summary>
public double Latitude => latitude;

/// <summary>
/// The longitude of the waypoint
/// </summary>
public double Longitude => longitude;

/// <summary>
/// The altitude of the waypoint
/// </summary>
public double Altitude => altitude;

/// <summary>
/// The current state of the waypoint
/// </summary>
public WaypointState State => state;

/// <summary>
/// Deserializes the waypoint, creating an actual waypoint from it
/// </summary>
/// <returns>A newly created waypoint from the serialized waypoint's parameters</returns>
public virtual Waypoint Deserialize() => new(latitude, longitude, altitude, bodyName, name, state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,15 @@
using KSP.Sim.impl;
using Newtonsoft.Json;

namespace SpaceWarp.API.Game;
namespace SpaceWarp.API.Game.Waypoints;

/// <summary>
/// A handle for a waypoint in the flight/map view for KSP2
/// </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
}

/// <summary>
/// This contains the serialized information for a waypoint, used for saving/loading waypoints
/// </summary>
[Serializable]
[method: JsonConstructor]
public struct SerializedWaypoint(string name, string bodyName, double latitude, double longitude, double altitude, WaypointState state)
{
/// <summary>
/// Deserializes the waypoint, creating an actual waypoint from it
/// </summary>
/// <returns>A newly created waypoint from the serialized waypoint's parameters</returns>
public Waypoint Deserialize() => new(latitude, longitude, altitude, bodyName, name, state);
}


private SimulationObjectModel _waypointObject;

Expand Down Expand Up @@ -255,6 +227,6 @@ public void Rename([CanBeNull] string name = null)
/// Serializes the waypoint to be saved in save data
/// </summary>
/// <returns>The waypoint as a serialized record</returns>
public SerializedWaypoint Serialize() =>
public virtual SerializedWaypoint Serialize() =>
new(Name, BodyName, Latitude, Longitude, AltitudeFromRadius, State);
}
17 changes: 17 additions & 0 deletions src/SpaceWarp.Game/API/Game/Waypoints/WaypointState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SpaceWarp.API.Game.Waypoints;

/// <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
}

0 comments on commit 0c99215

Please sign in to comment.