Skip to content

Commit

Permalink
Add player tracking in NavigateChatLink
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Mar 9, 2024
1 parent 18e9f84 commit 5673272
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
53 changes: 53 additions & 0 deletions NebulaPatcher/Patches/Dynamic/GameCamera_Patch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#region

using HarmonyLib;
using NebulaModel.DataStructures.Chat;
using NebulaWorld;
using NebulaWorld.GameStates;
using NebulaWorld.MonoBehaviours.Local.Chat;

#endregion

Expand All @@ -17,4 +20,54 @@ public static bool Logic_Prefix()
// prevent NRE while doing a reconnect as a client issued through the chat command
return !(GameStatesManager.DuringReconnect && GameMain.mainPlayer == null);
}

[HarmonyPostfix]
[HarmonyPatch(nameof(GameCamera.Logic))]
public static void Logic_Postfix(GameCamera __instance)
{
if (!Multiplayer.IsActive || GameStatesManager.DuringReconnect) return;

var observingPlayerId = Multiplayer.Session.Gizmos.ObservingPlayerId;
if (observingPlayerId == 0) return;

if (VFInput.escape)
{
Multiplayer.Session.Gizmos.ObservingPlayerId = 0;
ChatManager.Instance.SendChatMessage(
"Exit tracking mode".Translate(),
ChatMessageType.CommandOutputMessage);
return;
}

if (observingPlayerId > 0)
{
TrackingPlayer(__instance, observingPlayerId);
}
}

private static void TrackingPlayer(GameCamera cam, ushort playerId)
{
using (Multiplayer.Session.World.GetRemotePlayersModels(out var models))
{
if (!models.TryGetValue(playerId, out var model))
{
ChatManager.Instance.SendChatMessage(
string.Format("Can't find player {0}".Translate(), playerId),
ChatMessageType.CommandOutputMessage);
Multiplayer.Session.Gizmos.ObservingPlayerId = 0;
return;
}
var planetId = model.PlayerInstance.planetId;
if (planetId > 0 && planetId != GameMain.mainPlayer.planetId)
{
ChatManager.Instance.SendChatMessage(
string.Format("Player {0} is on a different planet".Translate(), playerId),
ChatMessageType.CommandOutputMessage);
Multiplayer.Session.Gizmos.ObservingPlayerId = 0;
return;
}
cam.rtsTarget.position = model.PlayerTransform.position;
cam.rtsTarget.eulerAngles = Maths.SphericalRotation(cam.rtsTarget.position, 0f).eulerAngles;
}
}
}
16 changes: 13 additions & 3 deletions NebulaWorld/Chat/ChatLinks/NavigateChatLinkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public void OnClick(string data)

public void OnRightClick(string data)
{
var substrings = data.Split(SplitSeparator);
switch (substrings.Length)
{
case 2: // PlayerId
if (!ushort.TryParse(substrings[0], out var playerId)) return;
Multiplayer.Session.Gizmos.ObservingPlayerId = playerId;
ChatManager.Instance.SendChatMessage("Start tracking to player (ESC to exit)".Translate(),
ChatMessageType.CommandOutputMessage);
break;
}
}

public void OnHover(string data, ChatLinkTrigger trigger, ref MonoBehaviour tipObject)
Expand Down Expand Up @@ -65,7 +75,7 @@ private static void UpdateTip(ChatLinkTrigger trigger, ref MonoBehaviour tipObje
if (buttonTip == null)
{
buttonTip = UIButtonTip.Create(false, "Navigate".Translate(),
"Click to create a navigate line to the target.".Translate(), 2, offset, 0, rect, "", "");
"Left click to create a navigate line to the target.\nRight click to track the target.".Translate(), 2, offset, 0, rect, "", "");
if (tipObject != null)
{
Object.Destroy(tipObject.gameObject);
Expand All @@ -77,13 +87,13 @@ private static void UpdateTip(ChatLinkTrigger trigger, ref MonoBehaviour tipObje
if (!buttonTip.gameObject.activeSelf)
{
buttonTip.gameObject.SetActive(true);
buttonTip.SetTip(false, "Navigate".Translate(), "Click to create a navigate line to the target.".Translate(), 2,
buttonTip.SetTip(false, "Navigate".Translate(), "Left click to create a navigate line to the target.\nRight click to track the target.".Translate(), 2,
offset, 0, rect, "", "");
}

if (buttonTip.isActiveAndEnabled && !buttonTip.titleComp.text.Equals("Navigate"))
{
buttonTip.SetTip(false, "Navigate".Translate(), "Click to create a navigate line to the target.".Translate(), 2,
buttonTip.SetTip(false, "Navigate".Translate(), "Left click to create a navigate line to the target.\nRight click to track the target.".Translate(), 2,
offset, 0, rect, "", "");
}
}
Expand Down
2 changes: 2 additions & 0 deletions NebulaWorld/Player/GizmoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace NebulaWorld.Player;

public class GizmoManager : IDisposable
{
public ushort ObservingPlayerId { get; set; }

private ushort indicatorPlayerId;
private int indicatorPlanetId;
private Vector3 indicatorPos;
Expand Down

0 comments on commit 5673272

Please sign in to comment.