Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///By: Nathan Rumsey 2023
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Netcode;
- [HelpURL("https://pastebin.com/u/Dieton")]
- public class GameManager : NetworkBehaviour
- {
- #region Variables
- //---- Dedicated Server Options ----//
- [SerializeField] private bool dedicatedServerIO = false;
- //---- Network ----//
- [SerializeField] private NetworkManager networkManager;
- [SerializeField] private NetworkPrefabsList networkPrefabsList;
- //---- Spawn System ----//
- //0 = BoyTeacher, 1 = GirlTeacher, 2 = BoyStudent, 3 = GirlStudent
- private int playerTypeID;
- [SerializeField] private Transform[] teacherSpawnPosition;
- [SerializeField] private Transform[] studentSpawnPosition;
- //[SerializeField] private GameObject NetPlayerPrefabClone;
- //---- Students ----//
- public NetworkVariable<bool> moveStudent;
- public List<Student> student = new List<Student>();
- //---- Login ----//
- public GameObject loginButton;
- public Transform spawnLoginLocation;
- public GameObject loginWindow;
- //---- Debug ----//
- [SerializeField] private bool debugIO = false;
- #endregion
- //==== Start ====//
- private void Start()
- {
- if (dedicatedServerIO)
- {
- Debug.Log("DEDICATED_SERVER By: Nathan Rumsey 2023");
- networkManager.StartServer();
- }
- /*
- #if DEDICATED_SERVER
- Debug.Log("DEDICATED_SERVER");
- networkManager.StartServer();
- #endif
- /*
- /*
- #if UNITY_SERVER
- GmDebugger("!!!! Starting Server !!!!");
- networkManager.StartServer();
- #endif
- */
- }
- //==== On Network Spawn ====//
- public override void OnNetworkSpawn()
- {
- GmDebugger("On Network Spawn");
- GmDebugger($"Local Client ID: " + networkManager.LocalClientId);
- if (IsHost || IsClient || IsLocalPlayer)
- {
- if (playerTypeID < 2)
- {
- SpawnPlayerServerRpc(teacherSpawnPosition[0].position, playerTypeID);
- }
- if (playerTypeID > 1)
- {
- SpawnPlayerServerRpc(studentSpawnPosition[0].position, playerTypeID);
- }
- }
- moveStudent.OnValueChanged += (bool oldValue, bool newValue) =>
- {
- MoveStudentsServerRpc();
- };
- }
- [ServerRpc(RequireOwnership = false)]
- public void MoveStudentsServerRpc() {
- Debug.Log("Game Manager - Atempting to move students");
- GameObject[] studentChairs;
- studentChairs = GameObject.FindGameObjectsWithTag("sit");
- for (int i = 0; i < student.Count; i++)
- {
- student[i].characterController.enabled = false;
- student[i].transform.position = studentChairs[i].transform.position;
- }
- Debug.Log("Game Manager - Students Moved");
- }
- //==== Start Host Button
- public void StartHostButton(int _playerTypeId)
- {
- GmDebugger("Host Started");
- playerTypeID = _playerTypeId;
- networkManager.StartHost();
- }
- //==== Start Client Button
- public void StartClientButton(int _playerTypeId)
- {
- GmDebugger("Client Started");
- loginWindow.SetActive(false);
- playerTypeID = _playerTypeId;
- networkManager.StartClient();
- }
- //==== player Spawn System ====//
- [ServerRpc(RequireOwnership = false)]
- public void SpawnPlayerServerRpc(Vector3 spawnPosition, int _playerTypeID, ServerRpcParams serverRpcParams = default)
- {
- Debug.Log(networkPrefabsList.PrefabList[_playerTypeID].Prefab);
- GameObject NetPlayerPrefabClone = Instantiate(networkPrefabsList.PrefabList[_playerTypeID].Prefab);
- NetPlayerPrefabClone.transform.position = spawnPosition;
- ulong clientId = serverRpcParams.Receive.SenderClientId;
- NetPlayerPrefabClone.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
- if (_playerTypeID > 1)
- {
- student.Add(NetPlayerPrefabClone.GetComponent<Student>());
- }
- GmDebugger("Spawned as Player");
- //networkManager.AddNetworkPrefab(networkPrefabsList.PrefabList[playerTypeId.Value].Prefab);
- }
- //==== Game Manager Debugger ====//
- public void GmDebugger(object debugObj)
- {
- if (debugIO) Debug.Log($"Network Manager {networkManager.LocalClientId}: { debugObj}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement