376 lines
12 KiB
C#
376 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Data.ScriptableObjects;
|
|
using FishNet.Connection;
|
|
using FishNet.Object;
|
|
using FishNet.Object.Synchronizing;
|
|
using Managers.Item;
|
|
using Types;
|
|
using Types.Items;
|
|
using Types.UI;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Managers
|
|
{
|
|
// Manages a INSTANCE of a single player.
|
|
public class PlayerManager : NetworkBehaviour
|
|
{
|
|
private static readonly Dictionary<int, PlayerManager> _playerManagers = new();
|
|
|
|
|
|
[SerializeField] private InventoryManager inventoryManager;
|
|
[SerializeField] private ToolbarManager toolbarManager;
|
|
[SerializeField] private EquipmentManager equipmentManager;
|
|
|
|
[SerializeField] private GameObject playerGo;
|
|
|
|
[SerializeField] private Camera playerCamera;
|
|
|
|
[SerializeField] private NetworkObject gameManagerPrefab;
|
|
|
|
[SerializeField] private GameManager gameManager;
|
|
|
|
private readonly SyncVar<GamePlayer> _playerData = new(new SyncTypeSettings(1f));
|
|
|
|
private NetworkConnection _connection;
|
|
|
|
private void Awake()
|
|
{
|
|
_playerData.OnChange += playerData_OnChange;
|
|
_connection = null;
|
|
}
|
|
|
|
|
|
public void SetupCamera()
|
|
{
|
|
playerCamera = GetComponentInChildren<Camera>();
|
|
if (playerCamera == null)
|
|
{
|
|
Debug.LogError("No camera found for player");
|
|
return;
|
|
}
|
|
|
|
playerCamera.enabled = IsOwner;
|
|
if (IsOwner)
|
|
{
|
|
Camera.main.gameObject.SetActive(false);
|
|
playerCamera.tag = "MainCamera";
|
|
}
|
|
}
|
|
|
|
public void playerData_OnChange(GamePlayer prev, GamePlayer next, bool asServer)
|
|
{
|
|
Debug.Log("PlayerData changed:");
|
|
if (prev != null)
|
|
Debug.Log($"PlayerData changed PREV: {prev.ID} {prev.PlayerName} {prev.MoveSpeed} {asServer}");
|
|
if (next != null)
|
|
Debug.Log($"PlayerData changed NEXT: {next.ID} {next.PlayerName} {next.MoveSpeed} {asServer}");
|
|
}
|
|
|
|
public override void OnStartServer()
|
|
{
|
|
base.OnStartServer();
|
|
|
|
// reset player managers
|
|
_playerManagers.Clear();
|
|
|
|
if (gameManager == null) gameManager = FindObjectOfType<GameManager>();
|
|
if (gameManager == null)
|
|
{
|
|
Debug.Log("GameManager not found, creating new one");
|
|
GameInit.Instance.InitializeGameManager();
|
|
gameManager = FindObjectOfType<GameManager>();
|
|
}
|
|
}
|
|
|
|
|
|
public override void OnStartClient()
|
|
{
|
|
base.OnStartClient();
|
|
|
|
//c check if server is active
|
|
if (!IsServerInitialized)
|
|
{
|
|
Debug.LogError("Server is not active");
|
|
StartCoroutine(WaitForServerAndRegister());
|
|
return;
|
|
}
|
|
|
|
Debug.Log("Total existing playerManagers: " + _playerManagers.Count);
|
|
|
|
if (IsOwner)
|
|
{
|
|
_connection = Owner;
|
|
_playerData.Value = GetPlayerData();
|
|
// StartCoroutine(WaitForClientActiveAndRegister());
|
|
RegisterPlayer();
|
|
try
|
|
{
|
|
_playerManagers.Add(Owner.ClientId, this);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e);
|
|
Debug.LogError("Failed to add player manager");
|
|
}
|
|
}
|
|
// ... rest of your OnStartClient code ...
|
|
}
|
|
|
|
private System.Collections.IEnumerator WaitForServerAndRegister() {
|
|
|
|
while (!IsServerInitialized)
|
|
{
|
|
yield return new WaitForSeconds(0.5f); // Wait a short time to ensure everything is set up
|
|
}
|
|
|
|
Debug.Log("Server is ready");
|
|
RegisterPlayer();
|
|
}
|
|
|
|
// private IEnumerator WaitForClientActiveAndRegister()
|
|
// {
|
|
// yield return new WaitForSeconds(0.5f); // Wait a short time to ensure everything is set up
|
|
//
|
|
// while (!gameManager)
|
|
// {
|
|
// gameManager = FindObjectOfType<GameManager>();
|
|
// yield return null;
|
|
// }
|
|
//
|
|
// Debug.Log("Attempting to register player");
|
|
// RegisterPlayer();
|
|
// }
|
|
|
|
[ServerRpc(RequireOwnership = false)]
|
|
private void RegisterPlayer()
|
|
{
|
|
if (_playerData.Value == null)
|
|
{
|
|
Debug.LogWarning("PlayerData not found, loading default");
|
|
// load a scriptable objects default player data
|
|
var playerData =
|
|
AssetDatabase.LoadAssetAtPath<GamePlayerSo>("Assets/Resources/DefaultPlayer.asset");
|
|
|
|
_playerData.Value = new GamePlayer(playerData)
|
|
{
|
|
PlayerName = "Player " + Owner.ClientId,
|
|
ID = Guid.NewGuid()
|
|
};
|
|
}
|
|
|
|
|
|
if (gameManager != null && _playerData.Value != null)
|
|
{
|
|
Debug.Log("Calling RegisterPlayerServerRpc");
|
|
gameManager.RegisterPlayerServerRpc(Owner, _playerData.Value);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError(
|
|
$"Failed to register player. GameManager null: {gameManager == null}, PlayerData null: {_playerData.Value == null}");
|
|
}
|
|
}
|
|
|
|
public GameObject GetPlayerGo()
|
|
{
|
|
return playerGo;
|
|
}
|
|
|
|
public GamePlayer GetPlayerData()
|
|
{
|
|
return _playerData.Value;
|
|
}
|
|
|
|
|
|
public static PlayerManager GetPlayerManager(int clientId)
|
|
{
|
|
return _playerManagers[clientId];
|
|
}
|
|
|
|
public Camera GetPlayerCamera()
|
|
{
|
|
return playerCamera;
|
|
}
|
|
|
|
/*private IEnumerator WaitForClientActiveAndRegister()
|
|
{
|
|
while (!IsClientInitialized && !IsClientStarted)
|
|
{
|
|
Debug.Log("Waiting for client to be initialized");
|
|
yield return null;
|
|
}
|
|
|
|
Debug.Log("Client is Ready to RegisterPlayer");
|
|
if (Owner == null) Debug.LogError("OwnerConnection not found");
|
|
if (gameManager == null) gameManager = FindObjectOfType<GameManager>();
|
|
Debug.Log("GameManager should be found");
|
|
gameManager.RegisterPlayerServerRpc(Owner, _playerData.Value);
|
|
}*/
|
|
|
|
/*public override void OnStartClient()
|
|
{
|
|
base.OnStartClient();
|
|
|
|
if (gameManager == null)
|
|
{
|
|
Debug.Log("Trying to find GameManager");
|
|
GameInit.Instance.InitializeGameManager();
|
|
gameManager = FindObjectOfType<GameManager>();
|
|
|
|
if (gameManager == null)
|
|
{
|
|
Debug.LogError("GameManager not found");
|
|
return;
|
|
}
|
|
|
|
if (gameManager) Debug.Log("GameManager found");
|
|
}
|
|
|
|
if (IsOwner)
|
|
{
|
|
if (_playerData.Value == null)
|
|
{
|
|
Debug.LogWarning("PlayerData not found");
|
|
// load a scriptable objects default player data
|
|
var playerData =
|
|
AssetDatabase.LoadAssetAtPath<GamePlayerSo>("Assets/Resources/DefaultPlayer.asset");
|
|
|
|
_playerData.Value = new GamePlayer(playerData)
|
|
{
|
|
PlayerName = "Default Player"
|
|
};
|
|
}
|
|
|
|
InitializeManager();
|
|
|
|
Debug.Log("PlayerManager RegisterPlayer");
|
|
|
|
if (Owner == null) Debug.LogError("OwnerConnection not found");
|
|
|
|
StartCoroutine(WaitForClientActiveAndRegister());
|
|
// gameManager.RegisterPlayerServerRpc(Owner, _playerData.Value);
|
|
// gameManager.AddPlayer(this);
|
|
LocalPlayerManager.Instance = this;
|
|
}
|
|
else
|
|
{
|
|
// disables other peoples character controls.
|
|
gameObject.GetComponent<PlayerManager>().enabled = false;
|
|
gameObject.GetComponent<PlayerController>().enabled = false;
|
|
}
|
|
}*/
|
|
|
|
|
|
// should only run on the server, NOT clients
|
|
[ServerRpc]
|
|
public void InitializeManager()
|
|
{
|
|
/*inventoryManager = playerGo.AddComponent<InventoryManager>();
|
|
toolbarManager = playerGo.AddComponent<ToolbarManager>();
|
|
equipmentManager = playerGo.AddComponent<EquipmentManager>();*/
|
|
|
|
inventoryManager.Initialize(_playerData.Value);
|
|
toolbarManager.Initialize(_playerData.Value);
|
|
equipmentManager.Initialize(_playerData.Value);
|
|
}
|
|
|
|
|
|
// gets manager by slot type
|
|
public IItemManager<GameSlot> GetManagerBySlotType(SlotType slotType)
|
|
{
|
|
switch (slotType)
|
|
{
|
|
case SlotType.Inventory:
|
|
Debug.Log("Getting Inventory Manager");
|
|
return inventoryManager;
|
|
case SlotType.Toolbar:
|
|
Debug.Log("Getting Toolbar Manager");
|
|
return toolbarManager;
|
|
case SlotType.Equipment:
|
|
Debug.Log("Getting Equipment Manager");
|
|
return equipmentManager;
|
|
case SlotType.Box:
|
|
Debug.Log("Getting Box Manager");
|
|
return BoxManager.Instance;
|
|
case SlotType.Crafting:
|
|
break;
|
|
case SlotType.Shop:
|
|
break;
|
|
case SlotType.Quest:
|
|
break;
|
|
default:
|
|
return null;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public GameSlot GetSlot(SlotType slotType, int slotId)
|
|
{
|
|
Debug.Log("Getting SlotType " + slotType + " / " + slotId);
|
|
var manager = GetManagerBySlotType(slotType);
|
|
if (manager == null) Debug.LogError("Manager not found");
|
|
return manager.GetSlot(slotId);
|
|
}
|
|
|
|
// Load save data
|
|
|
|
|
|
public GameSlot GetActiveToolbarSlot()
|
|
{
|
|
return toolbarManager.GetActiveToolbarSlot();
|
|
}
|
|
|
|
|
|
[Server]
|
|
public void Initialize(GamePlayer data)
|
|
{
|
|
Debug.Log("Initializing PlayerManager");
|
|
_playerData.Value = data;
|
|
inventoryManager.Initialize(data);
|
|
toolbarManager.Initialize(data);
|
|
equipmentManager.Initialize(data);
|
|
}
|
|
|
|
|
|
public InventoryManager GetInventoryManager()
|
|
{
|
|
if (inventoryManager != null) return inventoryManager;
|
|
inventoryManager = playerGo.AddComponent<InventoryManager>();
|
|
inventoryManager.Initialize(_playerData.Value);
|
|
return inventoryManager;
|
|
}
|
|
|
|
public ToolbarManager GetToolbarManager()
|
|
{
|
|
if (toolbarManager != null) return toolbarManager;
|
|
return toolbarManager;
|
|
}
|
|
|
|
public EquipmentManager GetEquipmentManager()
|
|
{
|
|
if (equipmentManager != null) return equipmentManager;
|
|
return equipmentManager;
|
|
}
|
|
|
|
public void AddItemToInventorySlot(GameItem item, int slotId)
|
|
{
|
|
// inventoryManager.AddItemToSlot(item, slotId);
|
|
}
|
|
|
|
[ServerRpc]
|
|
public void AddItemToInventory(GameItem item)
|
|
{
|
|
// inventoryManager.AddItemToOpenSlot(item);
|
|
}
|
|
|
|
|
|
[ServerRpc]
|
|
public void SetPlayerData(GamePlayer pd)
|
|
{
|
|
_playerData.Value = pd;
|
|
}
|
|
}
|
|
} |