Advertisement
Dieton

GameManager V1

Jun 13th, 2023 (edited)
85
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.04 KB | Gaming | 0 0
  1. using System.Collections.Generic;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using Unity.Netcode;
  5. using UnityEngine;
  6. using TMPro;
  7. //using System;
  8.  
  9. public class GameManager : NetworkBehaviour
  10. {
  11.     public static GameManager gameManager;
  12.     public float spawnTime = 10.5f;
  13.  
  14.     [SerializeField]
  15.     private GameObject
  16.         gameItemPrefab,
  17.         itemPrefab,
  18.         cubePrefab;
  19.  
  20.     [SerializeField]
  21.     private Transform prefabSpawnpoint;
  22.  
  23.     public List<GameObject> customePlayers;
  24.  
  25.     [SerializeField]
  26.     private NetworkManager networkManager;
  27.  
  28.     public int selectedPlayerID = 0;
  29.  
  30.     public NetworkPrefabsList plrefablist;
  31.     //public NetworkPrefab netPrefab;
  32.  
  33.     List<GameObject> playerClones = new List<GameObject>();
  34.     GameObject playerClone;
  35.  
  36.  
  37.  
  38.     // Start is called before the first frame update
  39.     void Start()
  40.     {
  41.         gameManager = this;
  42.  
  43.         if (IsServer)
  44.         {
  45.             StartServerActions();
  46.         }
  47.     }
  48.  
  49.     #region ServerSide
  50.  
  51.     public void StartServerActions()
  52.     {
  53.         //MyDebugger.myDebugger.Log("checking to see if this is a server.");
  54.         if (IsHost || IsServer)
  55.         {
  56.             StartCoroutine(TimedAction());
  57.         }
  58.     }
  59.  
  60.     IEnumerator TimedAction() {
  61.         while (true)
  62.         {
  63.             yield return new WaitForSeconds(spawnTime);
  64.  
  65.             Color colorA = Random.value > 0.5f ? Color.green : Color.yellow;
  66.             Color colorB = Random.value > 0.5f ? Color.red : Color.blue;
  67.             Color colorC = Random.value > 0.5f ? Color.red : Color.blue;
  68.             Color colorD = Random.value > 0.5f ? Color.red : Color.blue;
  69.  
  70.             SpawnSomethingClientRpc
  71.             (
  72.                 colorA,
  73.                 colorB,
  74.                 colorC,
  75.                 colorD
  76.             );
  77.         }
  78.     }
  79.  
  80.     [ClientRpc]
  81.     private void SpawnSomethingClientRpc(
  82.         Color colorOne,
  83.         Color colorTwo,
  84.         Color colorThree,
  85.         Color colorFour
  86.     )
  87.     {
  88.         Color[] myColors = new Color[] { colorOne , colorTwo, colorThree, colorFour };
  89.         GameObject gameItemClone = Instantiate(gameItemPrefab, prefabSpawnpoint.position, prefabSpawnpoint.rotation);
  90.         GameItem gameItem = gameItemClone.GetComponent<GameItem>();
  91.  
  92.         for (int i = 0; i < 4; i++)
  93.         {
  94.             GameObject itemClone = Instantiate(itemPrefab, gameItem.itemSpawnpoints[i].position, gameItem.itemSpawnpoints[i].rotation);
  95.             itemClone.transform.SetParent(gameItemClone.transform);
  96.             itemClone.GetComponent<Renderer>().material.color = myColors[i];
  97.             gameItemClone.GetComponent<Rigidbody>().isKinematic = false;
  98.         }
  99.  
  100.  
  101.         //NetworkObject.Despawn();
  102.         //Destroy(gameItemClone.gameObject, 4.5f);
  103.         StartCoroutine(DespawnTime(4.5f, gameItemClone.GetComponent<NetworkObject>()));
  104.     }
  105.  
  106.     IEnumerator DespawnTime(float waitTime, NetworkObject netObj) {
  107.         yield return new WaitForSeconds(waitTime);
  108.         netObj.Despawn();
  109.     }
  110.  
  111.     #endregion
  112.  
  113.     #region ServerToClient
  114.  
  115.     public void PublicColor(int colorId) {
  116.         ColorPickServerRpc(colorId);
  117.     }
  118.  
  119.     [ServerRpc(RequireOwnership = false)]//I dont think i need to request onership in this situation but i did so i can see how to use it if i need it in the future
  120.     private void ColorPickServerRpc(int colorId)
  121.     {
  122.         ColorPickClientRpc(colorId);
  123.     }
  124.  
  125.     [ClientRpc]
  126.     private void ColorPickClientRpc(int colorId)
  127.     {
  128.         GameObject cubePrefabClone = Instantiate(cubePrefab);
  129.  
  130.         if (colorId == 0)
  131.         {
  132.             cubePrefabClone.GetComponent<Renderer>().material.color = Color.red;
  133.         }
  134.         if (colorId == 1)
  135.         {
  136.             cubePrefabClone.GetComponent<Renderer>().material.color = Color.green;
  137.         }
  138.         if (colorId == 2)
  139.         {
  140.             cubePrefabClone.GetComponent<Renderer>().material.color = Color.blue;
  141.         }
  142.         if (colorId == 3)
  143.         {
  144.             cubePrefabClone.GetComponent<Renderer>().material.color = Color.yellow;
  145.         }
  146.     }
  147.  
  148.     #endregion
  149.  
  150.     // Update is called once per frame
  151.     void Update()
  152.     {
  153.        
  154.     }
  155.  
  156.     public void StartHostButton(int _playerId)
  157.     {
  158.         selectedPlayerID = _playerId;
  159.         //Instantiate(customePlayers[0]);
  160.         MyDebugger.myDebugger.Log("Host Started");
  161.         //SetupPlayerServerRpc();
  162.         //networkManager.AddNetworkPrefab
  163.         //networkManager.SpawnManager.GetPlayerNetworkObject(0);
  164.         //networkManager.SpawnManager//(networkManager.SpawnManager.SpawnedObjectsList,Vector3.zero,Quaternion.identity);
  165.  
  166.         NetworkManager.Singleton.StartHost();
  167.         StartServerActions();
  168.  
  169.  
  170.         SetupPlayer();
  171.  
  172.  
  173.     }
  174.  
  175.     [ContextMenu("hat")]//can be run from eidter
  176.     public void StartClientButton(int _playerId)
  177.     {
  178.         selectedPlayerID = _playerId;
  179.         //Instantiate(customePlayers[1]);
  180.         MyDebugger.myDebugger.Log("Client Started");
  181.         //SetupPlayerServerRpc();
  182.         NetworkManager.Singleton.StartClient();
  183.  
  184.         SetupPlayer();
  185.     }
  186.  
  187.     public void StartServerButton()
  188.     {
  189.         MyDebugger.myDebugger.Log("Server Started");
  190.         NetworkManager.Singleton.StartServer();
  191.         StartServerActions();
  192.     }
  193.  
  194.     public void SetupPlayer() {
  195.         MyDebugger.myDebugger.Log("SetupPlayer");
  196.  
  197.         //,customePlayers[0].GetComponent<NetPlayer>().GetNetwokObject());
  198.         //SetupPlayerServerRpc();
  199.     }
  200.  
  201.     //public override on
  202.  
  203.     public override void OnNetworkSpawn()
  204.     {
  205.         MyDebugger.myDebugger.Log("Setup Player Server Rpc");
  206.  
  207.         if (IsServer)
  208.         {
  209.             MyDebugger.myDebugger.Log("On Network Spawn - Is Server");
  210.             //return;
  211.         }
  212.  
  213.         if (IsClient) {
  214.             MyDebugger.myDebugger.Log("On Network Spawn - Is Client");
  215.         }
  216.  
  217.         if (IsHost) {
  218.             MyDebugger.myDebugger.Log("On Network Spawn - Is Host");
  219.         }
  220.        
  221.         MyDebugger.myDebugger.Log($"Prefab List " + plrefablist);
  222.  
  223.         for (int i = 0; i < plrefablist.PrefabList.Count; i++)
  224.         {
  225.             if (i == selectedPlayerID)
  226.             {
  227.                 playerClone = Instantiate(plrefablist.PrefabList[i].Prefab);
  228.                 playerClones.Add(playerClone);
  229.                
  230.                 MyDebugger.myDebugger.Log($"Prefab Name " + plrefablist.PrefabList[i].Prefab);
  231.             }
  232.         }
  233.  
  234.         SetupPlayerServerRpc();
  235.     }
  236.  
  237.     [ServerRpc(RequireOwnership = false)]
  238.     private void SetupPlayerServerRpc()
  239.     {
  240.         MyDebugger.myDebugger.Log($"Setup PlayerServer Rpc");
  241.  
  242.         foreach (GameObject playerClone in playerClones)
  243.         {
  244.             NetworkObject playerNetObj = playerClone.GetComponent<NetworkObject>();
  245.             playerNetObj.Spawn();
  246.             //playerNetObjRef.TryGet(out NetworkObject playerNetObj);
  247.         }
  248.  
  249.     }
  250.  
  251.     public void StopServer() {
  252.         if (IsServer)
  253.         {
  254.             networkManager.Shutdown();
  255.            
  256.         }
  257.     }
  258. }
  259.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement