Add location of cars within the trainset the user is in

Slight tidy up
This commit is contained in:
TheEvilPenguin committed 2025-06-12 12:04:33 +10:00
1 parent 55dfd09550
commit 8daf861088
7 files changed
+79 -11

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@ namespace CommunityMapLocation.Api
serializerOptions = new JsonSerializerOptions() serializerOptions = new JsonSerializerOptions()
{ {
ReferenceHandler = ReferenceHandler.IgnoreCycles, ReferenceHandler = ReferenceHandler.IgnoreCycles,
IncludeFields = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true WriteIndented = true
}; };
@@ -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; }
}
}
@@ -1,5 +1,6 @@
using DV.HUD; using DV.HUD;
using DV.OriginShift; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using UnityEngine; using UnityEngine;
@@ -15,26 +16,36 @@ namespace CommunityMapLocation.Api.Location
public override LocationResponse Handle(UnityModManager.ModEntry.ModLogger logger, HttpListenerRequest request) 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 the player is in a loco, we want to get the heading of the loco instead of the play
if (PlayerManager.Car?.IsLoco == true) 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 the loco is in reverse, rotate the direction 180 degrees to match the direction of travel
if (PlayerManager.Car.interior.GetComponentInChildren<InteriorControlsManager>().TryGetControl(InteriorControlsManager.ControlType.Reverser, out var control) && if (PlayerManager.Car.interior.GetComponentInChildren<InteriorControlsManager>().TryGetControl(InteriorControlsManager.ControlType.Reverser, out var control) &&
control.controlImplBase.Value <= 0.25) control.controlImplBase.Value <= 0.25)
{ {
angle += 180; reverse = true;
} }
} }
else 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<CarLocationResponse>();
if (PlayerManager.Car != null)
{
cars = PlayerManager.Car.trainset.cars.Select(c => new CarLocationResponse(c)).ToArray();
}
return new LocationResponse(PlayerManager.PlayerTransform, reverse)
{
Cars = cars
};
} }
} }
} }
@@ -1,21 +1,34 @@
using UnityEngine; using DV.OriginShift;
using System.Collections.Generic;
using UnityEngine;
namespace CommunityMapLocation.Api.Location namespace CommunityMapLocation.Api.Location
{ {
public class LocationResponse public class LocationResponse
{ {
public LocationResponse(Vector3 position, float rotation) public LocationResponse(Transform playerTransform, bool reverse)
{ {
var position = playerTransform.AbsolutePosition();
X = position.x; X = position.x;
Y = position.y; Y = position.y;
Z = position.z; 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 X { get; }
public float Y { get; } public float Y { get; }
public float Z { get; } public float Z { get; }
public float Rotation { get; } public float Rotation { get; }
public IEnumerable<CarLocationResponse> Cars { get; set; }
} }
} }
@@ -99,6 +99,7 @@
<Compile Include="Api\ApiServer.cs" /> <Compile Include="Api\ApiServer.cs" />
<Compile Include="Api\Endpoint.cs" /> <Compile Include="Api\Endpoint.cs" />
<Compile Include="Api\IEndpoint.cs" /> <Compile Include="Api\IEndpoint.cs" />
<Compile Include="Api\Location\CarLocationResponse.cs" />
<Compile Include="Api\Location\LocationEndpoint.cs" /> <Compile Include="Api\Location\LocationEndpoint.cs" />
<Compile Include="Api\Location\LocationResponse.cs" /> <Compile Include="Api\Location\LocationResponse.cs" />
<Compile Include="Config\RequestSourceType.cs" /> <Compile Include="Config\RequestSourceType.cs" />
+1
View File
@@ -41,6 +41,7 @@ namespace CommunityMapLocation.Config
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
// Todo: store and hold back property changes until Settings is saved to prevent rapidly reconfiguring server on each keypress? // 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<T>(ref T field, T value, [CallerMemberName] string propertyName = null) private void Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{ {
if (!EqualityComparer<T>.Default.Equals(field, value)) if (!EqualityComparer<T>.Default.Equals(field, value))
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"Id":"CommunityMapLocation", "Id":"CommunityMapLocation",
"DisplayName":"Derail Valley Community Map Location", "DisplayName":"Derail Valley Community Map Location",
"Author":"TheEvilPenguin", "Author":"BenjaminGreene",
"Version":"0.0.1", "Version":"0.0.1",
"ManagerVersion":"0.31.1.0", "ManagerVersion":"0.31.1.0",
"EntryMethod":"CommunityMapLocation.Main.Load" "EntryMethod":"CommunityMapLocation.Main.Load"