65 lines
1.7 KiB
C#
65 lines
1.7 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public sealed class PlayerHandController : NetworkBehaviour, IPlayerSystem
|
|
{
|
|
[SerializeField] private Transform itemSocket;
|
|
|
|
private bool isInitialized;
|
|
|
|
public Transform ItemSocket => itemSocket;
|
|
|
|
public void Initialize(PlayerFacade facade)
|
|
{
|
|
if (itemSocket == null)
|
|
{
|
|
Transform candidate = transform.Find("HandItemSocket");
|
|
if (candidate != null)
|
|
itemSocket = candidate;
|
|
}
|
|
|
|
isInitialized = itemSocket != null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isInitialized || !IsOwner || Keyboard.current == null)
|
|
return;
|
|
|
|
if (!NetworkHandItemInteractable.TryGetHeldItem(
|
|
OwnerClientId,
|
|
out NetworkHandItemInteractable heldItem))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
|
|
heldItem.RequestUse();
|
|
|
|
if (Keyboard.current.gKey.wasPressedThisFrame)
|
|
heldItem.RequestThrow();
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
if (!IsOwner ||
|
|
!NetworkHandItemInteractable.TryGetHeldItem(
|
|
OwnerClientId,
|
|
out NetworkHandItemInteractable heldItem))
|
|
{
|
|
return;
|
|
}
|
|
|
|
GUIStyle style = new(GUI.skin.label)
|
|
{
|
|
alignment = TextAnchor.MiddleCenter,
|
|
fontSize = 15,
|
|
normal = { textColor = Color.white }
|
|
};
|
|
|
|
Rect area = new(0f, Screen.height * 0.68f, Screen.width, 28f);
|
|
GUI.Label(area, $"{heldItem.ItemName} [LMB] Use [G] Throw", style);
|
|
}
|
|
}
|