Advertisement
Dieton

GameManager V3

Jun 23rd, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | Gaming | 0 0
  1. ///By: Nathan Rumsey 2023
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Unity.Netcode;
  7.  
  8. [HelpURL("https://pastebin.com/u/Dieton")]
  9. public class GameManager : NetworkBehaviour
  10. {
  11.     #region Variables
  12.  
  13.     //---- Dedicated Server Options ----//
  14.     [SerializeField] private bool dedicatedServerIO = false;
  15.  
  16.     //---- Network ----//
  17.     [SerializeField] private NetworkManager networkManager;
  18.     [SerializeField] private NetworkPrefabsList networkPrefabsList;
  19.  
  20.     //---- Spawn System ----//
  21.     //0 = BoyTeacher, 1 = GirlTeacher, 2 = BoyStudent, 3 = GirlStudent
  22.     private int playerTypeID;
  23.     [SerializeField] private Transform[] teacherSpawnPosition;
  24.     [SerializeField] private Transform[] studentSpawnPosition;
  25.     //[SerializeField] private GameObject NetPlayerPrefabClone;
  26.  
  27.     //---- Students ----//
  28.     public NetworkVariable<bool> moveStudent;
  29.     public List<Student> student = new List<Student>();
  30.  
  31.     //---- Login ----//
  32.     public GameObject loginButton;
  33.     public Transform spawnLoginLocation;
  34.     public GameObject loginWindow;
  35.  
  36.     //---- Debug ----//
  37.     [SerializeField] private bool debugIO = false;
  38.  
  39.     #endregion
  40.  
  41.     //==== Start ====//
  42.     private void Start()
  43.     {
  44.         if (dedicatedServerIO)
  45.         {
  46.             Debug.Log("DEDICATED_SERVER By: Nathan Rumsey 2023");
  47.             networkManager.StartServer();
  48.         }
  49.  
  50.         /*
  51.         #if DEDICATED_SERVER
  52.             Debug.Log("DEDICATED_SERVER");
  53.             networkManager.StartServer();
  54.         #endif
  55.         /*
  56.         /*
  57.         #if UNITY_SERVER
  58.         GmDebugger("!!!! Starting Server !!!!");
  59.         networkManager.StartServer();
  60.         #endif
  61.         */
  62.     }
  63.  
  64.     //==== On Network Spawn ====//
  65.     public override void OnNetworkSpawn()
  66.     {
  67.         GmDebugger("On Network Spawn");
  68.  
  69.         GmDebugger($"Local Client ID: " + networkManager.LocalClientId);
  70.  
  71.         if (IsHost || IsClient || IsLocalPlayer)
  72.         {
  73.             if (playerTypeID < 2)
  74.             {
  75.                 SpawnPlayerServerRpc(teacherSpawnPosition[0].position, playerTypeID);
  76.             }
  77.             if (playerTypeID > 1)
  78.             {
  79.                 SpawnPlayerServerRpc(studentSpawnPosition[0].position, playerTypeID);
  80.             }
  81.         }
  82.  
  83.         moveStudent.OnValueChanged += (bool oldValue, bool newValue) =>
  84.         {
  85.             MoveStudentsServerRpc();
  86.         };
  87.     }
  88.  
  89.     [ServerRpc(RequireOwnership = false)]
  90.     public void MoveStudentsServerRpc() {
  91.         Debug.Log("Game Manager - Atempting to move students");
  92.  
  93.         GameObject[] studentChairs;
  94.         studentChairs = GameObject.FindGameObjectsWithTag("sit");
  95.  
  96.         for (int i = 0; i < student.Count; i++)
  97.         {
  98.             student[i].characterController.enabled = false;
  99.             student[i].transform.position = studentChairs[i].transform.position;
  100.         }
  101.  
  102.         Debug.Log("Game Manager - Students Moved");
  103.     }
  104.  
  105.  
  106.     //==== Start Host Button
  107.     public void StartHostButton(int _playerTypeId)
  108.     {
  109.         GmDebugger("Host Started");
  110.  
  111.         playerTypeID = _playerTypeId;
  112.  
  113.         networkManager.StartHost();
  114.  
  115.     }
  116.  
  117.     //==== Start Client Button
  118.     public void StartClientButton(int _playerTypeId)
  119.     {
  120.         GmDebugger("Client Started");
  121.  
  122.         loginWindow.SetActive(false);
  123.  
  124.         playerTypeID = _playerTypeId;
  125.  
  126.         networkManager.StartClient();
  127.  
  128.     }
  129.  
  130.     //==== player Spawn System ====//
  131.     [ServerRpc(RequireOwnership = false)]
  132.     public void SpawnPlayerServerRpc(Vector3 spawnPosition, int _playerTypeID, ServerRpcParams serverRpcParams = default)
  133.     {
  134.  
  135.         Debug.Log(networkPrefabsList.PrefabList[_playerTypeID].Prefab);
  136.         GameObject NetPlayerPrefabClone = Instantiate(networkPrefabsList.PrefabList[_playerTypeID].Prefab);
  137.  
  138.         NetPlayerPrefabClone.transform.position = spawnPosition;
  139.  
  140.         ulong clientId = serverRpcParams.Receive.SenderClientId;
  141.         NetPlayerPrefabClone.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
  142.  
  143.         if (_playerTypeID > 1)
  144.         {
  145.             student.Add(NetPlayerPrefabClone.GetComponent<Student>());
  146.         }
  147.  
  148.         GmDebugger("Spawned as Player");
  149.         //networkManager.AddNetworkPrefab(networkPrefabsList.PrefabList[playerTypeId.Value].Prefab);
  150.     }
  151.  
  152.     //==== Game Manager Debugger ====//
  153.     public void GmDebugger(object debugObj)
  154.     {
  155.         if (debugIO) Debug.Log($"Network Manager {networkManager.LocalClientId}: { debugObj}");
  156.     }
  157. }
  158.  
Tags: Unity Netcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement