adding multiplayer
This commit is contained in:
@@ -1,89 +1,106 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Main;
|
||||
using Unity.Mathematics;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.InputSystem;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace RebootReality.jelycho.Player {
|
||||
public class PlayerController : IController {
|
||||
readonly Logger m_Logger = new Logger(nameof(PlayerController));
|
||||
readonly Config m_Config;
|
||||
public class PlayerController : NetworkPlayerController {
|
||||
static readonly Logger s_Logger = new Logger(nameof(PlayerController));
|
||||
|
||||
PlayerActor m_FPPActor;
|
||||
[SerializeField] Config m_Config;
|
||||
|
||||
public PlayerController(Config config) {
|
||||
m_Config = config;
|
||||
PlayerActor m_Actor;
|
||||
|
||||
public override void OnNetworkSpawn() {
|
||||
base.OnNetworkSpawn();
|
||||
|
||||
if (IsOwner) {
|
||||
RR.Input.LockCursor();
|
||||
RR.Input.EnableControls();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
public override void OnNetworkDespawn() {
|
||||
base.OnNetworkDespawn();
|
||||
|
||||
if (IsOwner) {
|
||||
RR.Input.UnlockCursor();
|
||||
RR.Input.DisableControls();
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask OnStart(CancellationToken cancellationToken) {
|
||||
RR.Input.LockCursor();
|
||||
RR.Input.EnableControls();
|
||||
|
||||
m_FPPActor = await RR.World.SpawnActor<PlayerActor>(m_Config.playerActorPrefab, cancellationToken);
|
||||
m_FPPActor.IsPlaying = true;
|
||||
|
||||
if (RR.World.Context is WorldContext worldContext) {
|
||||
m_FPPActor.WarpTo(worldContext.PlayerSpawnPoint.position);
|
||||
[ClientRpc]
|
||||
public void SetActorClientRpc(ulong networkObjectID) {
|
||||
if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(networkObjectID,
|
||||
out NetworkObject networkObject)) {
|
||||
m_Actor = networkObject.GetComponent<PlayerActor>();
|
||||
s_Logger.Info($"Found Player Actor NetworkObject ID: {networkObjectID}");
|
||||
} else {
|
||||
s_Logger.Error($"Failed to find PlayerActor with NetworkObject ID: {networkObjectID}");
|
||||
m_Actor = null;
|
||||
}
|
||||
|
||||
await UniTask.Yield(cancellationToken);
|
||||
if (m_Actor is null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsServer) {
|
||||
if (RR.World.Context is WorldContext worldContext) {
|
||||
m_Actor.WarpToServerRpc(worldContext.PlayerSpawnPoint.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnStop() {
|
||||
RR.Input.DisableControls();
|
||||
RR.Input.UnlockCursor();
|
||||
|
||||
RR.World.KillActor(m_FPPActor);
|
||||
[ClientRpc]
|
||||
public void SetNullActorClientRpc() {
|
||||
m_Actor = null;
|
||||
}
|
||||
|
||||
public void OnTick() {
|
||||
if (m_FPPActor is null) {
|
||||
m_Logger.Error("Player actor is not initialized. Make sure to call OnStart() before OnTick().");
|
||||
void Update() {
|
||||
if (m_Actor is null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!IsOwner) {
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 lookInput = SensitivityReader.Read(m_Config.lookActionReference.action);
|
||||
m_FPPActor.Look(lookInput * FPPConfig.MouseSensitivity.FloatValue);
|
||||
m_Actor.Look(lookInput * FPPConfig.MouseSensitivity.FloatValue);
|
||||
|
||||
Vector2 moveInput = m_Config.moveActionReference.action.ReadValue<Vector2>();
|
||||
m_FPPActor.SetMoveInput(moveInput);
|
||||
m_Actor.SetMoveInput(moveInput);
|
||||
|
||||
m_FPPActor.SetSprint(m_Config.sprintActionReference.action.IsPressed());
|
||||
m_Actor.SetSprint(m_Config.sprintActionReference.action.IsPressed());
|
||||
|
||||
if (m_Config.jumpActionReference.action.WasPerformedThisFrame()) {
|
||||
m_FPPActor.Jump();
|
||||
m_Actor.Jump();
|
||||
}
|
||||
|
||||
if (m_Config.dragObjectActionReference.action.WasPressedThisFrame()) {
|
||||
m_FPPActor.StartDrag();
|
||||
m_Actor.StartDrag();
|
||||
}
|
||||
|
||||
if (m_Config.dragObjectActionReference.action.WasReleasedThisFrame()) {
|
||||
m_FPPActor.StopDrag();
|
||||
m_Actor.StopDrag();
|
||||
}
|
||||
|
||||
if (m_Config.primaryActionReference.action.WasReleasedThisFrame()) {
|
||||
m_FPPActor.PrimaryAction();
|
||||
m_Actor.PrimaryAction();
|
||||
}
|
||||
|
||||
if (m_Config.secondaryActionReference.action.WasReleasedThisFrame()) {
|
||||
m_FPPActor.SecondaryAction();
|
||||
m_Actor.SecondaryAction();
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Config {
|
||||
public AssetReferenceT<GameObject> playerActorPrefab;
|
||||
|
||||
public InputActionReference moveActionReference;
|
||||
public InputActionReference lookActionReference;
|
||||
public InputActionReference jumpActionReference;
|
||||
|
||||
Reference in New Issue
Block a user