NetPlayer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Mirror;
  4. using UnityEngine;
  5. public class NetPlayer : NetworkBehaviour
  6. {
  7. public Behaviour[] LocalComponents;
  8. public SpriteRenderer characterSprite;
  9. [SyncVar]
  10. public bool insideDoor;
  11. public LayerMask friendLayer;
  12. public List<NetPlayer> touchingNeighbours = new List<NetPlayer>();
  13. [SyncVar]
  14. public Color pColor;
  15. [SyncVar]
  16. public string playerName;
  17. public Color[] playerColors;
  18. void Start()
  19. {
  20. DontDestroyOnLoad(gameObject);
  21. if (!isLocalPlayer)
  22. {
  23. // gameObject.layer = LayerMask.NameToLayer("Gnd");
  24. //GetComponent<BoxCollider2D>().size = new Vector2(GetComponent<BoxCollider2D>().size.x/2f,GetComponent<BoxCollider2D>().size.y);
  25. foreach (Behaviour localComponent in LocalComponents)
  26. {
  27. localComponent.enabled = false;
  28. }
  29. }
  30. if (isLocalPlayer)
  31. {
  32. SceneData.localPlayer = gameObject;
  33. // if(SceneData.netSceneData==null){Debug.Log("Scene Data is not init yet");}else{
  34. // transform.position = SceneData.netSceneData.spawnPoint.position;
  35. // }
  36. ReturnToSpawn();
  37. }
  38. if(isServer){
  39. pColor = playerColors[NetworkServer.connections.Count-1];
  40. }
  41. GetComponent<SpriteRenderer>().color = pColor;
  42. }
  43. public void ReturnToSpawn()
  44. {
  45. if (isServer)
  46. {
  47. StartCoroutine(returnToSpawn());
  48. }
  49. else
  50. {
  51. CmdReturnToSpawn();
  52. }
  53. }
  54. [Command]
  55. void CmdReturnToSpawn()
  56. {
  57. StartCoroutine(returnToSpawn());
  58. }
  59. IEnumerator returnToSpawn()
  60. {
  61. while (SceneData.netSceneData == null)
  62. {
  63. yield return new WaitForSeconds(0.1f);
  64. }
  65. while (SceneData.netSceneData.spawnPoint == null)
  66. {
  67. yield return new WaitForSeconds(0.1f);
  68. }
  69. transform.position = SceneData.netSceneData.spawnPoint.position;
  70. }
  71. [SyncVar]
  72. public Transform parentFrnd;
  73. bool oldFlipVal = false;
  74. Transform _parentFrnd;
  75. // [Command]
  76. // void CmdChangeParent(Transform newParent)
  77. // {
  78. // transform.parent = newParent;
  79. // RpcChangeParent(newParent);
  80. // parentFrnd = newParent;
  81. // }
  82. // [ClientRpc]
  83. // void RpcChangeParent(Transform newParent)
  84. // {
  85. // transform.parent = newParent;
  86. // parentFrnd = newParent;
  87. // }
  88. float t = 0;
  89. void FixedUpdate()
  90. {
  91. if (isServer)
  92. {
  93. parentFrnd = getOnFriend();
  94. if (collisionImpact != Vector3.zero)
  95. {
  96. GetComponent<Rigidbody2D>().AddForce(-collisionImpact, ForceMode2D.Impulse);
  97. collisionImpact = Vector3.zero;
  98. }
  99. if (oldFlipVal != characterSprite.flipX)
  100. {
  101. if (isServer)
  102. {
  103. RpcFlipX(characterSprite.flipX);
  104. }
  105. else
  106. {
  107. CmdFlipX(characterSprite.flipX);
  108. }
  109. oldFlipVal = characterSprite.flipX;
  110. }
  111. }
  112. transform.parent = parentFrnd;
  113. if (transform.parent != null)
  114. {
  115. if (t < 1)
  116. {
  117. t += Time.deltaTime;
  118. }
  119. else
  120. {
  121. GetComponent<NetworkTransform>().useLocalSpace = true;
  122. }
  123. }
  124. else
  125. {
  126. GetComponent<NetworkTransform>().useLocalSpace = false;
  127. t = 0;
  128. }
  129. if (!isLocalPlayer) { return; }
  130. // bool someoneOnTop = false;
  131. // foreach(NetPlayer neighbour in touchingNeighbours){
  132. // if(neighbour.parentFrnd == this){
  133. // someoneOnTop=true;
  134. // break;
  135. // }
  136. // }
  137. // GetComponent<PlayerController>().isSomeoneOnTop =someoneOnTop;
  138. if (!isServer) { return; }
  139. }
  140. [Command]
  141. void CmdFlipX(bool value)
  142. {
  143. FlipX(value);
  144. RpcFlipX(value);
  145. }
  146. [ClientRpc]
  147. void RpcFlipX(bool value)
  148. {
  149. FlipX(value);
  150. }
  151. void FlipX(bool value)
  152. {
  153. characterSprite.flipX = value;
  154. }
  155. public void CallChangeInsideDoor(bool value)
  156. {
  157. if (isServer)
  158. {
  159. insideDoor = value;
  160. }
  161. else
  162. {
  163. CmdChangeInsideDoor(value);
  164. }
  165. }
  166. [Command]
  167. void CmdChangeInsideDoor(bool value)
  168. {
  169. insideDoor = value;
  170. }
  171. public Transform getOnFriend()
  172. {
  173. Transform friend = null;
  174. //return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
  175. Collider2D col = GetComponentInChildren<Collider2D>();
  176. RaycastHit2D hit = Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y), 0, Vector2.down, 0.1f, friendLayer);
  177. friend = (hit) ? ((hit.collider.transform.GetComponent<NetPlayer>() != null && hit.collider.transform.GetComponent<NetPlayer>() != this) ? hit.collider.transform : null) : null;
  178. return friend;
  179. }
  180. Vector3 collisionImpact;
  181. void OnCollisionEnter2D(Collision2D col)
  182. {
  183. NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
  184. if (obj != null)
  185. {
  186. collisionImpact = col.contacts[0].normal * col.contacts[0].relativeVelocity * col.otherRigidbody.mass;
  187. Debug.Log("Collision impact : " + collisionImpact);
  188. if (!touchingNeighbours.Contains(obj))
  189. {
  190. touchingNeighbours.Add(obj);
  191. }
  192. }
  193. }
  194. void OnCollisionExit2D(Collision2D col)
  195. {
  196. NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
  197. if (obj != null)
  198. {
  199. if (touchingNeighbours.Contains(obj))
  200. {
  201. touchingNeighbours.Remove(obj);
  202. }
  203. }
  204. }
  205. void UpdatePushBoxes()
  206. {
  207. foreach (PushBox box in FindObjectsOfType<PushBox>())
  208. {
  209. box.UpdateNeighbourCount();
  210. }
  211. }
  212. public void OnSceneChanged(){
  213. if(!isServer){return;}
  214. foreach(GameObject obj in SceneData.netSceneData.networkObjects){
  215. GameObject go = Instantiate(obj);
  216. NetworkServer.Spawn(go, NetworkServer.localConnection);
  217. }
  218. }
  219. }