Started make greybox for main location
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public sealed class FirstPersonCameraController : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
[SerializeField, Range(1f, 89f)] private float verticalLookLimit = 80f;
|
||||
|
||||
private Camera playerCamera;
|
||||
private AudioListener audioListener;
|
||||
private MeshRenderer playerRenderer;
|
||||
private float pitch;
|
||||
private bool isInitialized;
|
||||
|
||||
public void Initialize(PlayerFacade facade)
|
||||
{
|
||||
playerCamera = GetComponentInChildren<Camera>(true);
|
||||
audioListener = playerCamera != null ? playerCamera.GetComponent<AudioListener>() : null;
|
||||
playerRenderer = GetComponentInChildren<MeshRenderer>();
|
||||
isInitialized = playerCamera != null;
|
||||
|
||||
if (!isInitialized)
|
||||
return;
|
||||
|
||||
bool isLocalPlayer = IsOwner;
|
||||
playerCamera.gameObject.SetActive(isLocalPlayer);
|
||||
|
||||
if (!isLocalPlayer)
|
||||
return;
|
||||
|
||||
if (playerRenderer != null)
|
||||
playerRenderer.enabled = false;
|
||||
|
||||
foreach (Camera camera in FindObjectsByType<Camera>(FindObjectsSortMode.None))
|
||||
{
|
||||
if (camera != playerCamera)
|
||||
camera.enabled = false;
|
||||
}
|
||||
|
||||
if (audioListener != null)
|
||||
audioListener.enabled = true;
|
||||
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
public void ApplyPitch(float pitchDelta)
|
||||
{
|
||||
if (!isInitialized || !IsOwner)
|
||||
return;
|
||||
|
||||
pitch = Mathf.Clamp(pitch - pitchDelta, -verticalLookLimit, verticalLookLimit);
|
||||
playerCamera.transform.localRotation = Quaternion.Euler(pitch, 0f, 0f);
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
if (IsOwner)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40a194304b53aea488f1573da427fbd4
|
||||
@@ -0,0 +1,59 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public sealed class PlayerController : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
[Header("Movement")]
|
||||
[SerializeField, Min(0.1f)] private float walkSpeed = 4.5f;
|
||||
[SerializeField, Min(0.1f)] private float sprintSpeed = 7f;
|
||||
|
||||
[Header("Vertical movement")]
|
||||
[SerializeField, Min(0.1f)] private float jumpHeight = 1.4f;
|
||||
[SerializeField, Min(0.1f)] private float gravity = 20f;
|
||||
|
||||
private CharacterController characterController;
|
||||
private Vector3 verticalVelocity;
|
||||
private bool isInitialized;
|
||||
|
||||
public void Initialize(PlayerFacade facade)
|
||||
{
|
||||
characterController = GetComponent<CharacterController>();
|
||||
isInitialized = characterController != null;
|
||||
}
|
||||
|
||||
public void SubmitMovement(Vector2 moveInput, bool wantsToSprint, bool wantsToJump)
|
||||
{
|
||||
if (!isInitialized || !IsOwner)
|
||||
return;
|
||||
|
||||
SimulateMovement(moveInput, wantsToSprint, wantsToJump);
|
||||
}
|
||||
|
||||
public void SubmitYaw(float yawDelta)
|
||||
{
|
||||
if (!isInitialized || !IsOwner)
|
||||
return;
|
||||
|
||||
transform.Rotate(0f, yawDelta, 0f, Space.Self);
|
||||
}
|
||||
|
||||
private void SimulateMovement(Vector2 moveInput, bool wantsToSprint, bool wantsToJump)
|
||||
{
|
||||
if (moveInput.sqrMagnitude > 1f)
|
||||
moveInput.Normalize();
|
||||
|
||||
if (characterController.isGrounded && verticalVelocity.y < 0f)
|
||||
verticalVelocity.y = -2f;
|
||||
|
||||
if (wantsToJump && characterController.isGrounded)
|
||||
verticalVelocity.y = Mathf.Sqrt(jumpHeight * 2f * gravity);
|
||||
|
||||
verticalVelocity.y -= gravity * Time.deltaTime;
|
||||
|
||||
float speed = wantsToSprint ? sprintSpeed : walkSpeed;
|
||||
Vector3 horizontalDirection = transform.right * moveInput.x + transform.forward * moveInput.y;
|
||||
characterController.Move((horizontalDirection * speed + verticalVelocity) * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2df317ead1e2c824f8544bef0a049887
|
||||
@@ -0,0 +1,74 @@
|
||||
using Unity.Collections;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public sealed class PlayerIdentity : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
private readonly NetworkVariable<FixedString64Bytes> displayName = new(
|
||||
new FixedString64Bytes("Worker"),
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Owner);
|
||||
|
||||
private TextMesh nameLabel;
|
||||
private bool isInitialized;
|
||||
|
||||
public string DisplayName => displayName.Value.ToString();
|
||||
|
||||
public void Initialize(PlayerFacade facade)
|
||||
{
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
displayName.OnValueChanged += HandleNameChanged;
|
||||
CreateNameLabel();
|
||||
HandleNameChanged(default, displayName.Value);
|
||||
|
||||
if (IsOwner)
|
||||
{
|
||||
string localName = NetworkSessionController.Instance != null
|
||||
? NetworkSessionController.Instance.LocalPlayerName
|
||||
: "Worker";
|
||||
displayName.Value = new FixedString64Bytes(localName);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
displayName.OnValueChanged -= HandleNameChanged;
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!isInitialized || nameLabel == null || Camera.main == null)
|
||||
return;
|
||||
|
||||
Vector3 cameraPosition = Camera.main.transform.position;
|
||||
nameLabel.transform.rotation = Quaternion.LookRotation(nameLabel.transform.position - cameraPosition);
|
||||
}
|
||||
|
||||
private void CreateNameLabel()
|
||||
{
|
||||
if (nameLabel != null)
|
||||
return;
|
||||
|
||||
var labelObject = new GameObject("PlayerNameLabel");
|
||||
labelObject.transform.SetParent(transform, false);
|
||||
labelObject.transform.localPosition = new Vector3(0f, 2.25f, 0f);
|
||||
labelObject.transform.localScale = Vector3.one * 0.12f;
|
||||
|
||||
nameLabel = labelObject.AddComponent<TextMesh>();
|
||||
nameLabel.anchor = TextAnchor.MiddleCenter;
|
||||
nameLabel.alignment = TextAlignment.Center;
|
||||
nameLabel.fontSize = 48;
|
||||
nameLabel.characterSize = 0.1f;
|
||||
nameLabel.color = Color.white;
|
||||
}
|
||||
|
||||
private void HandleNameChanged(FixedString64Bytes previousValue, FixedString64Bytes newValue)
|
||||
{
|
||||
if (nameLabel != null)
|
||||
nameLabel.text = newValue.ToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77d253756ae661e4a9ba738e9a4d8d86
|
||||
@@ -0,0 +1,42 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
[RequireComponent(typeof(PlayerController), typeof(FirstPersonCameraController))]
|
||||
public sealed class PlayerInputController : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
[SerializeField, Min(0.001f)] private float mouseSensitivity = 0.08f;
|
||||
|
||||
private PlayerController playerController;
|
||||
private FirstPersonCameraController cameraController;
|
||||
private bool isInitialized;
|
||||
|
||||
public void Initialize(PlayerFacade facade)
|
||||
{
|
||||
playerController = GetComponent<PlayerController>();
|
||||
cameraController = GetComponent<FirstPersonCameraController>();
|
||||
isInitialized = playerController != null && cameraController != null;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!isInitialized || !IsOwner || Keyboard.current == null)
|
||||
return;
|
||||
|
||||
Vector2 moveInput = new(
|
||||
(Keyboard.current.dKey.isPressed ? 1f : 0f) - (Keyboard.current.aKey.isPressed ? 1f : 0f),
|
||||
(Keyboard.current.wKey.isPressed ? 1f : 0f) - (Keyboard.current.sKey.isPressed ? 1f : 0f));
|
||||
|
||||
playerController.SubmitMovement(
|
||||
moveInput,
|
||||
Keyboard.current.leftShiftKey.isPressed,
|
||||
Keyboard.current.spaceKey.wasPressedThisFrame);
|
||||
|
||||
if (Mouse.current == null)
|
||||
return;
|
||||
|
||||
Vector2 mouseDelta = Mouse.current.delta.ReadValue() * mouseSensitivity;
|
||||
playerController.SubmitYaw(mouseDelta.x);
|
||||
cameraController.ApplyPitch(mouseDelta.y);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b45058d0c34be841ad35cce7370cbb7
|
||||
@@ -0,0 +1,70 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public sealed class PlayerInteractionController : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
[SerializeField, Min(0.1f)] private float interactionDistance = 3f;
|
||||
[SerializeField] private LayerMask interactionMask = ~0;
|
||||
|
||||
private Camera playerCamera;
|
||||
private IInteractable focusedInteractable;
|
||||
private bool isInitialized;
|
||||
|
||||
public void Initialize(PlayerFacade facade)
|
||||
{
|
||||
playerCamera = GetComponentInChildren<Camera>(true);
|
||||
isInitialized = playerCamera != null;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!isInitialized || !IsOwner || Keyboard.current == null)
|
||||
return;
|
||||
|
||||
focusedInteractable = FindFocusedInteractable();
|
||||
|
||||
if (Keyboard.current.eKey.wasPressedThisFrame &&
|
||||
focusedInteractable != null &&
|
||||
focusedInteractable.IsInteractionAvailable)
|
||||
{
|
||||
focusedInteractable.RequestInteraction();
|
||||
}
|
||||
}
|
||||
|
||||
private IInteractable FindFocusedInteractable()
|
||||
{
|
||||
Ray ray = new(playerCamera.transform.position, playerCamera.transform.forward);
|
||||
if (!Physics.Raycast(ray, out RaycastHit hit, interactionDistance, interactionMask, QueryTriggerInteraction.Collide))
|
||||
return null;
|
||||
|
||||
foreach (MonoBehaviour component in hit.collider.GetComponentsInParent<MonoBehaviour>())
|
||||
{
|
||||
if (component is IInteractable interactable)
|
||||
return interactable;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (!IsOwner || focusedInteractable == null)
|
||||
return;
|
||||
|
||||
GUIStyle style = new(GUI.skin.label)
|
||||
{
|
||||
alignment = TextAnchor.MiddleCenter,
|
||||
fontSize = 16,
|
||||
normal = { textColor = Color.white }
|
||||
};
|
||||
|
||||
string prompt = focusedInteractable.IsInteractionAvailable
|
||||
? "[E] " + focusedInteractable.InteractionPrompt
|
||||
: "In use";
|
||||
|
||||
Rect area = new(0f, Screen.height * 0.62f, Screen.width, 28f);
|
||||
GUI.Label(area, prompt, style);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0cd5aa652e2f0b145ac4579b39076584
|
||||
@@ -0,0 +1,43 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public sealed class PlayerSpawnController : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
[Header("Random spawn area")]
|
||||
[SerializeField] private Vector3 spawnCenter = new(0f, 1f, 0f);
|
||||
[SerializeField, Min(0f)] private float spawnRadius = 8f;
|
||||
|
||||
private readonly NetworkVariable<Vector3> assignedSpawnPosition = new(
|
||||
Vector3.zero,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server);
|
||||
|
||||
public void Initialize(PlayerFacade facade) { }
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
assignedSpawnPosition.OnValueChanged += HandleSpawnPositionChanged;
|
||||
|
||||
if (IsServer)
|
||||
{
|
||||
Vector2 randomOffset = Random.insideUnitCircle * spawnRadius;
|
||||
assignedSpawnPosition.Value = spawnCenter + new Vector3(randomOffset.x, 0f, randomOffset.y);
|
||||
}
|
||||
|
||||
ApplySpawnPosition(assignedSpawnPosition.Value);
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
assignedSpawnPosition.OnValueChanged -= HandleSpawnPositionChanged;
|
||||
}
|
||||
|
||||
private void HandleSpawnPositionChanged(Vector3 _, Vector3 position) =>
|
||||
ApplySpawnPosition(position);
|
||||
|
||||
private void ApplySpawnPosition(Vector3 position)
|
||||
{
|
||||
if (IsOwner)
|
||||
transform.position = position;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb5685deb3f0e014583330890079ebcd
|
||||
Reference in New Issue
Block a user