Shows the size of the bolt closest to the crosshair (Screw FSM "BoltSize"). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
4.5 KiB
C#
116 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using HutongGames.PlayMaker;
|
|
using MSCO;
|
|
using UnityEngine;
|
|
|
|
[assembly: AssemblyTitle("Show Bolt Size")]
|
|
[assembly: AssemblyProduct("Show Bolt Size")]
|
|
[assembly: AssemblyDescription("Shows the size of the bolt you look at, so you pick the right wrench.")]
|
|
[assembly: AssemblyVersion("1.0.0.0")]
|
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
[assembly: AssemblyInformationalVersion("1.0.0")]
|
|
|
|
namespace ShowBoltSize
|
|
{
|
|
/// <summary>
|
|
/// Zeigt unter dem Fadenkreuz die Größe der Schraube, auf die man schaut ("10 mm"). Jede Schraube hat die FSM "Screw"
|
|
/// mit der Variable "BoltSize". Deren Kollider sind nur mit Werkzeug in der Hand aktiv - deshalb kein Raycast, sondern:
|
|
/// die Schraube, die am nächsten an der Blickrichtung liegt (bis 1,3 m). Nur Anzeige, ändert nichts am Spiel.
|
|
/// </summary>
|
|
public class ShowBoltSize : MscoMod
|
|
{
|
|
public override string ID { get { return "showboltsize"; } }
|
|
public override string Name { get { return "Show Bolt Size"; } }
|
|
public override string Version { get { return "1.0.0"; } }
|
|
public override string Author { get { return "MBM"; } }
|
|
|
|
const float MaxDistance = 1.3f;
|
|
const float BoltRadius = 0.025f; // so groß wirkt eine Schraube beim Zielen
|
|
const float ScanInterval = 2f;
|
|
|
|
sealed class Bolt
|
|
{
|
|
public Transform Transform;
|
|
public FsmFloat SizeF;
|
|
public FsmInt SizeI;
|
|
}
|
|
|
|
readonly List<Bolt> bolts = new List<Bolt>();
|
|
bool inGame;
|
|
float nextScan;
|
|
string label;
|
|
GUIStyle style;
|
|
|
|
public override void OnMenuLoad() { inGame = false; bolts.Clear(); label = null; }
|
|
public override void OnGameLoad() { inGame = true; nextScan = 0f; }
|
|
|
|
public override void Update()
|
|
{
|
|
label = null;
|
|
if (!inGame) return;
|
|
Camera cam = Camera.main;
|
|
if (cam == null) return;
|
|
if (Time.realtimeSinceStartup >= nextScan) Scan();
|
|
|
|
Transform eye = cam.transform;
|
|
Bolt best = null;
|
|
float bestAngle = float.MaxValue;
|
|
foreach (Bolt b in bolts)
|
|
{
|
|
if (b.Transform == null || !b.Transform.gameObject.activeInHierarchy) continue;
|
|
Vector3 to = b.Transform.position - eye.position;
|
|
float distance = to.magnitude;
|
|
if (distance > MaxDistance || distance < 0.05f) continue;
|
|
float angle = Vector3.Angle(eye.forward, to);
|
|
float limit = Mathf.Atan2(BoltRadius, distance) * Mathf.Rad2Deg + 0.5f;
|
|
if (angle < limit && angle < bestAngle)
|
|
{
|
|
bestAngle = angle;
|
|
best = b;
|
|
}
|
|
}
|
|
if (best != null) label = Describe(best);
|
|
}
|
|
|
|
void Scan()
|
|
{
|
|
nextScan = Time.realtimeSinceStartup + ScanInterval;
|
|
bolts.Clear();
|
|
foreach (PlayMakerFSM fsm in Object.FindObjectsOfType<PlayMakerFSM>())
|
|
{
|
|
if (fsm == null || fsm.FsmName != "Screw") continue;
|
|
FsmFloat f = fsm.FsmVariables.FindFsmFloat("BoltSize");
|
|
FsmInt i = fsm.FsmVariables.FindFsmInt("BoltSize");
|
|
if (f == null && i == null) continue;
|
|
bolts.Add(new Bolt { Transform = fsm.transform, SizeF = f, SizeI = i });
|
|
}
|
|
}
|
|
|
|
/// <summary>Größe in mm. Das Spiel speichert sie je nach Schraube als 10 oder als 1.0 (= 10 mm).</summary>
|
|
static string Describe(Bolt b)
|
|
{
|
|
float value = b.SizeF != null ? b.SizeF.Value : b.SizeI.Value;
|
|
if (value <= 0f) return null;
|
|
float mm = value < 2.5f ? value * 10f : value;
|
|
return Mathf.RoundToInt(mm) + " mm";
|
|
}
|
|
|
|
public override void OnGUI()
|
|
{
|
|
if (label == null) return;
|
|
if (style == null)
|
|
{
|
|
style = new GUIStyle(GUI.skin.label) { alignment = TextAnchor.MiddleCenter, fontSize = 16, fontStyle = FontStyle.Bold };
|
|
style.normal.textColor = Color.white;
|
|
}
|
|
var r = new Rect(Screen.width / 2f - 50f, Screen.height / 2f + 22f, 100f, 26f);
|
|
Color old = GUI.color;
|
|
GUI.color = new Color(0f, 0f, 0f, 0.6f);
|
|
GUI.DrawTexture(r, Texture2D.whiteTexture);
|
|
GUI.color = old;
|
|
GUI.Label(r, label, style);
|
|
}
|
|
}
|
|
}
|