add command intepreter and message scrolling

This commit is contained in:
_nebula 2023-07-09 21:19:52 +01:00
parent b1e38b6909
commit 4ab15e74e6
2 changed files with 113 additions and 1 deletions

View File

@ -9,6 +9,7 @@ using QSB.WorldSync;
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
@ -157,6 +158,8 @@ internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
_textChat.GetComponent<CanvasGroup>().alpha = 1;
}
ListStack<string> previousMessages = new(true);
private void Update()
{
if (!QSBWorldSync.AllObjectsReady || _playerList == null)
@ -168,12 +171,39 @@ internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
var inSuit = Locator.GetPlayerSuit().IsWearingHelmet();
if (OWInput.IsNewlyPressed(InputLibrary.enter, InputMode.Character) && !_writingMessage && inSuit && QSBCore.TextChatInput)
if ((OWInput.IsNewlyPressed(InputLibrary.enter, InputMode.Character) || (Keyboard.current[Key.Slash].wasPressedThisFrame && OWInput.IsInputMode(InputMode.Character)))
&& !_writingMessage && inSuit && QSBCore.TextChatInput)
{
OWInput.ChangeInputMode(InputMode.KeyboardInput);
_writingMessage = true;
_inputField.ActivateInputField();
_textChat.GetComponent<CanvasGroup>().alpha = 1;
if (Keyboard.current[Key.Slash].wasPressedThisFrame)
{
Delay.RunNextFrame(() => _inputField.text = "/");
}
}
if (Keyboard.current[Key.UpArrow].wasPressedThisFrame && _writingMessage)
{
var currentText = _inputField.text;
if (previousMessages.Contains(currentText))
{
var index = previousMessages.IndexOf(currentText);
if (index == 0)
{
return;
}
_inputField.text = previousMessages[index - 1];
}
else
{
_inputField.text = previousMessages.Last();
}
}
if (OWInput.IsNewlyPressed(InputLibrary.enter, InputMode.KeyboardInput) && _writingMessage)
@ -185,6 +215,14 @@ internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
var message = _inputField.text;
_inputField.text = "";
message = message.Replace("\n", "").Replace("\r", "");
previousMessages.Push(message);
if (QSBCore.DebugSettings.DebugMode && CommandInterpreter.InterpretCommand(message))
{
return;
}
message = $"{QSBPlayerManager.LocalPlayer.Name}: {message}";
new ChatMessage(message, Color.white).Send();
}

View File

@ -0,0 +1,74 @@
using QSB.HUD;
using QSB.Messaging;
using QSB.ShipSync;
using QSB.ShipSync.Messages;
using QSB.WorldSync;
using System.Linq;
using UnityEngine;
namespace QSB.Utility;
public class CommandInterpreter : MonoBehaviour, IAddComponentOnStart
{
public static bool InterpretCommand(string message)
{
if (message[0] != '/')
{
return false;
}
var commandParts = message.ToLower().Substring(1).Split(' ');
var command = commandParts[0];
switch (command)
{
case "ship":
ShipCommand(commandParts.Skip(1).ToArray());
break;
default:
MultiplayerHUDManager.Instance.WriteMessage($"Unknown command \"{command}\".", Color.red);
break;
}
return true;
}
public static void ShipCommand(string[] arguments)
{
var command = arguments[0];
switch (command)
{
case "explode":
MultiplayerHUDManager.Instance.WriteMessage($"Blowing up the ship.", Color.green);
var shipDamageController = Locator.GetShipTransform().GetComponentInChildren<ShipDamageController>();
shipDamageController.Explode();
break;
case "repair":
case "damage":
var damage = command == "damage";
switch (arguments[1])
{
case "headlight":
var headlight = QSBWorldSync.GetUnityObject<ShipHeadlightComponent>();
headlight.SetDamaged(damage);
break;
default:
break;
}
MultiplayerHUDManager.Instance.WriteMessage($"{(damage ? "Damaging" : "Repairing")} the {arguments[1]}.", Color.green);
break;
case "open-hatch":
QSBWorldSync.GetUnityObject<HatchController>().OpenHatch();
new HatchMessage(true).Send();
break;
case "close-hatch":
QSBWorldSync.GetUnityObject<HatchController>().CloseHatch();
new HatchMessage(false).Send();
break;
default:
MultiplayerHUDManager.Instance.WriteMessage($"Unknown ship command \"{command}\".", Color.red);
break;
}
}
}