NetPlayer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. void Start()
  14. {
  15. DontDestroyOnLoad(gameObject);
  16. if(!isLocalPlayer){
  17. gameObject.layer = LayerMask.NameToLayer("Gnd");
  18. //GetComponent<BoxCollider2D>().size = new Vector2(GetComponent<BoxCollider2D>().size.x/2f,GetComponent<BoxCollider2D>().size.y);
  19. foreach(Behaviour localComponent in LocalComponents){
  20. localComponent.enabled=false;
  21. }
  22. }
  23. if(isLocalPlayer){
  24. SceneData.localPlayer = gameObject;
  25. if(SceneData.netSceneData==null){Debug.Log("Scene Data is not init yet");}else{
  26. transform.position = SceneData.netSceneData.spawnPoint.position;
  27. }
  28. }
  29. }
  30. public void ReturnToSpawn(){
  31. if(isLocalPlayer){
  32. StartCoroutine(returnToSpawn());
  33. }
  34. }
  35. IEnumerator returnToSpawn(){
  36. while(SceneData.netSceneData==null){
  37. yield return new WaitForSeconds(0.1f);
  38. }
  39. while(SceneData.netSceneData.spawnPoint==null){
  40. yield return new WaitForSeconds(0.1f);
  41. }
  42. transform.position = SceneData.netSceneData.spawnPoint.position;
  43. }
  44. [SyncVar]
  45. public Transform parentFrnd;
  46. bool oldFlipVal = false;
  47. Transform _parentFrnd;
  48. [Command]
  49. void CmdChangeParent(Transform newParent){
  50. transform.parent = newParent;
  51. RpcChangeParent(newParent);
  52. }
  53. [ClientRpc]
  54. void RpcChangeParent(Transform newParent){
  55. transform.parent = newParent;
  56. }
  57. float t=0;
  58. void FixedUpdate()
  59. {
  60. if(transform.parent!=null){
  61. if(t <1){
  62. t+=Time.deltaTime;
  63. }else{
  64. GetComponent<NetworkTransform>().useLocalSpace=true;
  65. }
  66. }else{
  67. GetComponent<NetworkTransform>().useLocalSpace=false;
  68. t=0;
  69. }
  70. if(!isLocalPlayer){return;}
  71. parentFrnd = getOnFriend();
  72. transform.parent = parentFrnd;
  73. if(_parentFrnd != parentFrnd){
  74. if(isServer){
  75. transform.parent = parentFrnd;
  76. RpcChangeParent(parentFrnd);
  77. }else{
  78. CmdChangeParent(parentFrnd);
  79. }
  80. _parentFrnd=parentFrnd;
  81. }
  82. if(oldFlipVal != characterSprite.flipX){
  83. if(isServer){
  84. RpcFlipX(characterSprite.flipX);
  85. }else{
  86. CmdFlipX(characterSprite.flipX);
  87. }
  88. oldFlipVal=characterSprite.flipX;
  89. }
  90. // bool someoneOnTop = false;
  91. // foreach(NetPlayer neighbour in touchingNeighbours){
  92. // if(neighbour.parentFrnd == this){
  93. // someoneOnTop=true;
  94. // break;
  95. // }
  96. // }
  97. // GetComponent<PlayerController>().isSomeoneOnTop =someoneOnTop;
  98. if(!isServer){return;}
  99. }
  100. [Command]
  101. void CmdFlipX(bool value){
  102. FlipX(value);
  103. RpcFlipX(value);
  104. }
  105. [ClientRpc]
  106. void RpcFlipX(bool value){
  107. if(!isLocalPlayer)FlipX(value);
  108. }
  109. void FlipX(bool value){
  110. characterSprite.flipX = value;
  111. }
  112. public void CallChangeInsideDoor(bool value){
  113. if(isServer){
  114. insideDoor=value;
  115. }else{
  116. CmdChangeInsideDoor(value);
  117. }
  118. }
  119. [Command]
  120. void CmdChangeInsideDoor(bool value){
  121. insideDoor=value;
  122. }
  123. public Transform getOnFriend()
  124. {
  125. Transform friend =null;
  126. //return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
  127. Collider2D col = GetComponentInChildren<Collider2D>();
  128. 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);
  129. friend = (hit) ? ((hit.collider.transform.GetComponent<NetPlayer>()!=null) ? hit.collider.transform : null) : null;
  130. return friend;
  131. }
  132. void OnCollisionEnter2D(Collision2D col){
  133. NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
  134. if(obj!=null){
  135. if(!touchingNeighbours.Contains(obj)){
  136. touchingNeighbours.Add(obj);
  137. }
  138. }
  139. }
  140. void OnCollisionExit2D(Collision2D col){
  141. NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
  142. if(obj!=null){
  143. if(touchingNeighbours.Contains(obj)){
  144. touchingNeighbours.Remove(obj);
  145. }
  146. }
  147. }
  148. void UpdatePushBoxes(){
  149. foreach(PushBox box in FindObjectsOfType<PushBox>()){
  150. box.UpdateNeighbourCount();
  151. }
  152. }
  153. }