Advertisement
azeTrom

Untitled

May 18th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FishNet.Object;
  5.  
  6. public class Player : NetworkBehaviour
  7. {
  8.  
  9. private Player[] players; //this is a card game where people create 'teams' then battle other players with them. This script
  10. //is just meant to spawn the teams initially.
  11. static public int playerNumber;
  12.  
  13. public GameObject mainCamera;
  14. public override void OnStartClient()
  15. {
  16. base.OnStartClient();
  17.  
  18. mainCamera = GameObject.Find("MainCamera");
  19. if (IsClientOnly)
  20. {
  21. mainCamera.transform.SetPositionAndRotation(new Vector3(0, 0, 189.9f), Quaternion.Euler(180, 0, 0));
  22. }
  23. if (IsOwner)
  24. {
  25. if (IsClientOnly)
  26. {
  27. playerNumber = 2;
  28. }
  29. else
  30. {
  31. playerNumber = 1;
  32. }
  33. }
  34.  
  35. if (IsOwner && IsClientOnly) //tugboat is set up to allow only 2 players max
  36. {
  37. RpcServerGetTeam(); //this is called only when the second (final) player joins
  38. }
  39. }
  40.  
  41. //right now, objects won't get deleted when connection breaks...but when I begin adding different scenes, I'll ensure that
  42. //when the connection breaks, the whole scene is reset.
  43.  
  44. [ServerRpc]
  45. private void RpcServerGetTeam() //tell the server to tell each client to get their teams and resend them to the server
  46. {
  47. RpcClientGetTeam();
  48. }
  49.  
  50. [ObserversRpc]
  51. private void RpcClientGetTeam()
  52. {
  53. players = FindObjectsOfType<Player>();
  54. foreach (Player player in players) //find both players in the scene
  55. {
  56. player.GetTeam();
  57. }
  58. }
  59.  
  60. private TeamBuilder teambuilder;
  61. public void GetTeam()
  62. {
  63. if (IsOwner)
  64. {
  65. teambuilder = GameObject.Find("MiscScripts").GetComponent<TeamBuilder>();
  66. RpcServerSpawnTeam(teambuilder.MyTeam, IsClientOnly, LocalConnection);
  67. }
  68. }
  69.  
  70. [ServerRpc]
  71. private void RpcServerSpawnTeam(GameObject[] myTeam, bool isClientOnly, FishNet.Connection.NetworkConnection ownerConnection)
  72. {
  73. GameObject nobCanvas = GameObject.Find("NobCanvas");
  74. GameObject[] teamSetup = new GameObject[6];
  75.  
  76. int y;
  77. if (isClientOnly)
  78. {
  79. y = 1;
  80. }
  81. else
  82. {
  83. y = -1;
  84. }
  85.  
  86. for (int i = 0; i < 6; i++)
  87. {
  88. teamSetup[i] = Instantiate(myTeam[i]);
  89. }
  90.  
  91. teamSetup[0].transform.SetParent(nobCanvas.transform);
  92. teamSetup[0].transform.localPosition = new Vector2(-50, 320 * y);
  93.  
  94. teamSetup[1].transform.SetParent(teamSetup[0].transform);
  95. teamSetup[1].transform.localPosition = new Vector2(220, 115.625f);
  96.  
  97. teamSetup[2].transform.SetParent(teamSetup[0].transform);
  98. teamSetup[2].transform.localPosition = new Vector2(220, 0);
  99.  
  100. teamSetup[3].transform.SetParent(nobCanvas.transform);
  101. teamSetup[3].transform.localPosition = new Vector2(550, 320 * y);
  102.  
  103. teamSetup[4].transform.SetParent(teamSetup[3].transform);
  104. teamSetup[4].transform.localPosition = new Vector2(220, 115.625f);
  105.  
  106. teamSetup[5].transform.SetParent(teamSetup[3].transform);
  107. teamSetup[5].transform.localPosition = new Vector2(220, 0);
  108.  
  109. for (int i = 0; i < 6; i++)
  110. {
  111. teamSetup[i].transform.localScale = new Vector2(1, 1);
  112. ServerManager.Spawn(teamSetup[i], ownerConnection);
  113. }
  114.  
  115. RpcClientSpawnTeam(teamSetup, y);
  116. }
  117.  
  118. [ObserversRpc]
  119. private void RpcClientSpawnTeam(GameObject[] teamSetup, int y)
  120. {
  121. GameObject nobCanvas = GameObject.Find("NobCanvas");
  122.  
  123. teamSetup[0].transform.SetParent(nobCanvas.transform);
  124. teamSetup[0].transform.localPosition = new Vector2(-50, 320 * y);
  125.  
  126. teamSetup[1].transform.SetParent(teamSetup[0].transform);
  127. teamSetup[1].transform.localPosition = new Vector2(220, 115.625f);
  128.  
  129. teamSetup[2].transform.SetParent(teamSetup[0].transform);
  130. teamSetup[2].transform.localPosition = new Vector2(220, 0);
  131.  
  132. teamSetup[3].transform.SetParent(nobCanvas.transform);
  133. teamSetup[3].transform.localPosition = new Vector2(550, 320 * y);
  134.  
  135. teamSetup[4].transform.SetParent(teamSetup[3].transform);
  136. teamSetup[4].transform.localPosition = new Vector2(220, 115.625f);
  137.  
  138. teamSetup[5].transform.SetParent(teamSetup[3].transform);
  139. teamSetup[5].transform.localPosition = new Vector2(220, 0);
  140.  
  141. for (int i = 0; i < 6; i++)
  142. {
  143. teamSetup[i].transform.localScale = new Vector2(1, 1);
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement