From 8daf861088000583c8c02a801cb1eba77d2e3b47 Mon Sep 17 00:00:00 2001 From: TheEvilPenguin Date: Thu, 12 Jun 2025 12:04:33 +1000 Subject: [PATCH] Add location of cars within the trainset the user is in Slight tidy up --- CommunityMapLocation/Api/ApiServer.cs | 2 +- .../Api/Location/CarLocationResponse.cs | 42 +++++++++++++++++++ .../Api/Location/LocationEndpoint.cs | 23 +++++++--- .../Api/Location/LocationResponse.cs | 19 +++++++-- .../CommunityMapLocation.csproj | 1 + CommunityMapLocation/Config/Settings.cs | 1 + CommunityMapLocation/Info.json | 2 +- 7 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 CommunityMapLocation/Api/Location/CarLocationResponse.cs diff --git a/CommunityMapLocation/Api/ApiServer.cs b/CommunityMapLocation/Api/ApiServer.cs index 154138b..6079596 100644 --- a/CommunityMapLocation/Api/ApiServer.cs +++ b/CommunityMapLocation/Api/ApiServer.cs @@ -20,7 +20,7 @@ namespace CommunityMapLocation.Api serializerOptions = new JsonSerializerOptions() { ReferenceHandler = ReferenceHandler.IgnoreCycles, - IncludeFields = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; diff --git a/CommunityMapLocation/Api/Location/CarLocationResponse.cs b/CommunityMapLocation/Api/Location/CarLocationResponse.cs new file mode 100644 index 0000000..e23236c --- /dev/null +++ b/CommunityMapLocation/Api/Location/CarLocationResponse.cs @@ -0,0 +1,42 @@ +using DV.OriginShift; +using UnityEngine; + +namespace CommunityMapLocation.Api.Location +{ + public class CarLocationResponse + { + public CarLocationResponse(TrainCar trainCar) + { + IsLoco = trainCar.IsLoco; + Name = trainCar.name; + Id = trainCar.ID; + CarGuid = trainCar.CarGUID; + + // Probable not needed. Commented to remove additional dll dependency + //HandbrakePosition = trainCar.brakeSystem.handbrakePosition; + + // TrainCar.Bounds does not seem to represent the actual bounds of the car. + // Approximate the car length as the distance between the couplers + Length = (trainCar.FrontCouplerAnchor.AbsolutePosition() - trainCar.RearCouplerAnchor.AbsolutePosition()).magnitude; + + var transform = trainCar.transform.AbsolutePosition(); + X = transform.x; + Y = transform.y; + Z = transform.z; + + Rotation = trainCar.transform.rotation.eulerAngles.y * (Mathf.PI / 180); + } + + public string Id { get; } + public string CarGuid { get; } + public string Name { get; } + public bool IsLoco { get; } + //public float HandbrakePosition { get; } + + public float X { get; } + public float Y { get; } + public float Z { get; } + public float Length { get; } + public float Rotation { get; } + } +} diff --git a/CommunityMapLocation/Api/Location/LocationEndpoint.cs b/CommunityMapLocation/Api/Location/LocationEndpoint.cs index 808f0ef..de7e32d 100644 --- a/CommunityMapLocation/Api/Location/LocationEndpoint.cs +++ b/CommunityMapLocation/Api/Location/LocationEndpoint.cs @@ -1,5 +1,6 @@ using DV.HUD; -using DV.OriginShift; +using System; +using System.Linq; using System.Net; using System.Net.Http; using UnityEngine; @@ -15,26 +16,36 @@ namespace CommunityMapLocation.Api.Location public override LocationResponse Handle(UnityModManager.ModEntry.ModLogger logger, HttpListenerRequest request) { - float angle; + Transform rotationTransform; + var reverse = false; // If the player is in a loco, we want to get the heading of the loco instead of the play if (PlayerManager.Car?.IsLoco == true) { - angle = PlayerManager.Car.transform.rotation.eulerAngles.y; + rotationTransform = PlayerManager.Car.transform; // If the loco is in reverse, rotate the direction 180 degrees to match the direction of travel if (PlayerManager.Car.interior.GetComponentInChildren().TryGetControl(InteriorControlsManager.ControlType.Reverser, out var control) && control.controlImplBase.Value <= 0.25) { - angle += 180; + reverse = true; } } else { - angle = PlayerManager.PlayerCamera.transform.rotation.eulerAngles.y; + rotationTransform = PlayerManager.PlayerCamera.transform; } - return new LocationResponse(PlayerManager.PlayerTransform.AbsolutePosition(), angle * Mathf.PI / 180); + var cars = Array.Empty(); + if (PlayerManager.Car != null) + { + cars = PlayerManager.Car.trainset.cars.Select(c => new CarLocationResponse(c)).ToArray(); + } + + return new LocationResponse(PlayerManager.PlayerTransform, reverse) + { + Cars = cars + }; } } } diff --git a/CommunityMapLocation/Api/Location/LocationResponse.cs b/CommunityMapLocation/Api/Location/LocationResponse.cs index 34b8163..12385b0 100644 --- a/CommunityMapLocation/Api/Location/LocationResponse.cs +++ b/CommunityMapLocation/Api/Location/LocationResponse.cs @@ -1,21 +1,34 @@ -using UnityEngine; +using DV.OriginShift; +using System.Collections.Generic; +using UnityEngine; namespace CommunityMapLocation.Api.Location { public class LocationResponse { - public LocationResponse(Vector3 position, float rotation) + public LocationResponse(Transform playerTransform, bool reverse) { + var position = playerTransform.AbsolutePosition(); + X = position.x; Y = position.y; Z = position.z; - Rotation = rotation; + var angle = playerTransform.rotation.eulerAngles.y; + + if (reverse) + { + angle += 180; + } + + Rotation = angle * (Mathf.PI / 180); } public float X { get; } public float Y { get; } public float Z { get; } public float Rotation { get; } + + public IEnumerable Cars { get; set; } } } diff --git a/CommunityMapLocation/CommunityMapLocation.csproj b/CommunityMapLocation/CommunityMapLocation.csproj index 5cd654f..10eaba7 100644 --- a/CommunityMapLocation/CommunityMapLocation.csproj +++ b/CommunityMapLocation/CommunityMapLocation.csproj @@ -99,6 +99,7 @@ + diff --git a/CommunityMapLocation/Config/Settings.cs b/CommunityMapLocation/Config/Settings.cs index 48ea3c6..9bec364 100644 --- a/CommunityMapLocation/Config/Settings.cs +++ b/CommunityMapLocation/Config/Settings.cs @@ -41,6 +41,7 @@ namespace CommunityMapLocation.Config public event PropertyChangedEventHandler PropertyChanged; // Todo: store and hold back property changes until Settings is saved to prevent rapidly reconfiguring server on each keypress? + // Not yet used anywhere, so could revert to using standard auto-properties private void Set(ref T field, T value, [CallerMemberName] string propertyName = null) { if (!EqualityComparer.Default.Equals(field, value)) diff --git a/CommunityMapLocation/Info.json b/CommunityMapLocation/Info.json index 2082a65..517f69b 100644 --- a/CommunityMapLocation/Info.json +++ b/CommunityMapLocation/Info.json @@ -1,7 +1,7 @@ { "Id":"CommunityMapLocation", "DisplayName":"Derail Valley Community Map Location", - "Author":"TheEvilPenguin", + "Author":"BenjaminGreene", "Version":"0.0.1", "ManagerVersion":"0.31.1.0", "EntryMethod":"CommunityMapLocation.Main.Load"