From 2f60039ca6c01de8e85f7ad20d8746bc76d572e8 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 31 Jan 2022 16:33:11 -0800 Subject: [PATCH] custom ship attach --- QSB/ShipSync/ShipCustomAttach.cs | 56 ++++++++++++++++++++++++++++++++ QSB/ShipSync/ShipManager.cs | 14 ++++++-- 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 QSB/ShipSync/ShipCustomAttach.cs diff --git a/QSB/ShipSync/ShipCustomAttach.cs b/QSB/ShipSync/ShipCustomAttach.cs new file mode 100644 index 00000000..80f01ff1 --- /dev/null +++ b/QSB/ShipSync/ShipCustomAttach.cs @@ -0,0 +1,56 @@ +using UnityEngine; + +namespace QSB.ShipSync +{ + public class ShipCustomAttach : MonoBehaviour + { + private const string ATTACH_TEXT = "Attach to ship"; + private const string DETACH_TEXT = "Detach from ship"; + private static readonly ScreenPrompt _prompt = new(InputLibrary.interactSecondary, ATTACH_TEXT + " "); + private PlayerAttachPoint _playerAttachPoint; + + private void Awake() + { + Locator.GetPromptManager().AddScreenPrompt(_prompt, PromptPosition.UpperRight); + + _playerAttachPoint = gameObject.AddComponent(); + _playerAttachPoint._lockPlayerTurning = false; + _playerAttachPoint._matchRotation = false; + _playerAttachPoint._centerCamera = false; + } + + private void OnDestroy() => + Locator.GetPromptManager().RemoveScreenPrompt(_prompt, PromptPosition.UpperRight); + + private void Update() + { + _prompt.SetVisibility(false); + if (!PlayerState.IsInsideShip()) + { + return; + } + + var attachedToUs = _playerAttachPoint.enabled; + if (PlayerState.IsAttached() && !attachedToUs) + { + return; + } + + _prompt.SetVisibility(true); + if (OWInput.IsNewlyPressed(InputLibrary.interactSecondary, InputMode.Character)) + { + if (!attachedToUs) + { + transform.position = Locator.GetPlayerTransform().position; + _playerAttachPoint.AttachPlayer(); + _prompt.SetText(DETACH_TEXT + " "); + } + else + { + _playerAttachPoint.DetachPlayer(); + _prompt.SetText(ATTACH_TEXT + " "); + } + } + } + } +} diff --git a/QSB/ShipSync/ShipManager.cs b/QSB/ShipSync/ShipManager.cs index 3a3cb5c2..a81da850 100644 --- a/QSB/ShipSync/ShipManager.cs +++ b/QSB/ShipSync/ShipManager.cs @@ -8,6 +8,7 @@ using QSB.Utility; using QSB.WorldSync; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Threading; using UnityEngine; @@ -24,6 +25,7 @@ namespace QSB.ShipSync public ShipTractorBeamSwitch ShipTractorBeam; public ShipCockpitController CockpitController; public ShipElectricalComponent ShipElectricalComponent; + private GameObject _shipCustomAttach; public uint CurrentFlyer { get => _currentFlyer; @@ -47,14 +49,14 @@ namespace QSB.ShipSync public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct) { - var shipTransform = GameObject.Find("Ship_Body"); - if (shipTransform == null) + var shipBody = Locator.GetShipBody(); + if (shipBody == null) { DebugLog.ToConsole($"Error - Couldn't find ship!", MessageType.Error); return; } - HatchController = shipTransform.GetComponentInChildren(); + HatchController = shipBody.GetComponentInChildren(); if (HatchController == null) { DebugLog.ToConsole($"Error - Couldn't find hatch controller!", MessageType.Error); @@ -93,8 +95,14 @@ namespace QSB.ShipSync QSBWorldSync.Init(); QSBWorldSync.Init(); + + _shipCustomAttach = new GameObject(nameof(ShipCustomAttach)); + _shipCustomAttach.transform.SetParent(shipBody.transform, false); + _shipCustomAttach.AddComponent(); } + public override void UnbuildWorldObjects() => Destroy(_shipCustomAttach); + public void AddPlayerToShip(PlayerInfo player) { DebugLog.DebugWrite($"{player.PlayerId} enter ship.");