Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#79 Show screen where a player is next to its nickname. #244

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/MultiplayerMod/Game/UI/Tools/Events/InterfaceToolEvents.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using HarmonyLib;
using MultiplayerMod.ModRuntime.Context;
using UnityEngine;
Expand All @@ -8,12 +9,28 @@ namespace MultiplayerMod.Game.UI.Tools.Events;
[HarmonyPatch(typeof(InterfaceTool))]
public static class InterfaceToolEvents {

public static event Action<Vector2>? MouseMoved;
public static event Action<Vector2, string?>? MouseMoved;
polycone marked this conversation as resolved.
Show resolved Hide resolved

// ReSharper disable once InconsistentNaming, UnusedMember.Local
[HarmonyPrefix]
[HarmonyPatch(nameof(InterfaceTool.OnMouseMove))]
[RequireExecutionLevel(ExecutionLevel.Game)]
private static void OnMouseMove(Vector3 cursor_pos) => MouseMoved?.Invoke(new Vector2(cursor_pos.x, cursor_pos.y));
private static void OnMouseMove(Vector3 cursor_pos) {
var kScreens = KScreenManager.Instance.screenStack.Where(screen => screen.mouseOver).ToList();
MouseMoved?.Invoke(
new Vector2(cursor_pos.x, cursor_pos.y),
GetScreenName(kScreens.FirstOrDefault())
);
}

private static string? GetScreenName(KScreen? screen) {
switch (screen) {
case TableScreen tableScreen:
return tableScreen.title.ToLower();
case RootMenu rootMenu:
return rootMenu.detailsScreen.displayName;
default:
return screen?.displayName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ public class UpdatePlayerCursorPosition : MultiplayerCommand {

private PlayerIdentity playerId;
private Vector2 position;
private string? screenName;

public UpdatePlayerCursorPosition(PlayerIdentity playerId, Vector2 position) {
public UpdatePlayerCursorPosition(PlayerIdentity playerId, Vector2 position, string? screenName) {
this.playerId = playerId;
this.position = position;
this.screenName = screenName;
}

public override void Execute(MultiplayerCommandContext context) {
var player = context.Multiplayer.Players[playerId];
context.EventDispatcher.Dispatch(new PlayerCursorPositionUpdatedEvent(player, position));
context.EventDispatcher.Dispatch(new PlayerCursorPositionUpdatedEvent(player, position, screenName));
}

}
6 changes: 4 additions & 2 deletions src/MultiplayerMod/Multiplayer/Components/CursorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public Vector3 Position {
}
}

public string CursorText { get; set; } = null!;
public string PlayerName { get; set; } = null!;

public string? ScreenName { get; set; }

public void Trace(Vector2 position) {
previous = current;
Expand Down Expand Up @@ -84,7 +86,7 @@ private void Update() {
return;

gameObject.transform.position = camera.WorldToScreenPoint(GetCurrentPosition());
textComponent.text = CursorText;
textComponent.text = PlayerName + (ScreenName == null ? "" : $" ({ScreenName})");
}

private Vector2 GetCurrentPosition() {
Expand Down
14 changes: 8 additions & 6 deletions src/MultiplayerMod/Multiplayer/Components/CursorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ private void OnPlayerLeft(PlayerLeftEvent @event) {
private void OnDisable() => subscriptions.Cancel();

private void OnCursorUpdated(PlayerCursorPositionUpdatedEvent updatedEvent) {
if (!cursors.TryGetValue(updatedEvent.Player, out var cursor)) {
cursor = gameObject.AddComponent<CursorComponent>();
cursor.Position = updatedEvent.Position;
cursor.CursorText = updatedEvent.Player.Profile.PlayerName;
cursors[updatedEvent.Player] = cursor;
if (!cursors.TryGetValue(updatedEvent.Player, out var cursorComponent)) {
cursorComponent = gameObject.AddComponent<CursorComponent>();
cursorComponent.Position = updatedEvent.Position;
cursorComponent.PlayerName = updatedEvent.Player.Profile.PlayerName;
cursorComponent.ScreenName = updatedEvent.ScreenName;
cursors[updatedEvent.Player] = cursorComponent;
return;
}
cursor.Trace(updatedEvent.Position);
cursorComponent.ScreenName = updatedEvent.ScreenName;
cursorComponent.Trace(updatedEvent.Position);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private void BindSpeedControl() {
private void BindMouse() {
// TODO: Cursor update may be ignored if MouseMoved isn't triggered after the rate period.
// TODO: Will be changed later (probably with current / last sent positions check).
InterfaceToolEvents.MouseMoved += position => throttle10Hz.Run<UpdatePlayerCursorPosition>(
() => client.Send(new UpdatePlayerCursorPosition(multiplayer.Players.Current.Id, position))
InterfaceToolEvents.MouseMoved += (position, screenName) => throttle10Hz.Run<UpdatePlayerCursorPosition>(
() => client.Send(new UpdatePlayerCursorPosition(multiplayer.Players.Current.Id, position, screenName))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

namespace MultiplayerMod.Multiplayer.Players.Events;

public record PlayerCursorPositionUpdatedEvent(MultiplayerPlayer Player, Vector2 Position) : IDispatchableEvent;
public record PlayerCursorPositionUpdatedEvent
(MultiplayerPlayer Player, Vector2 Position, string? ScreenName) : IDispatchableEvent;