123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using System.Collections;
- using System.Collections.Generic;
- using Mirror;
- using UnityEngine;
- public class NetPlayer : NetworkBehaviour
- {
- public Behaviour[] LocalComponents;
- public SpriteRenderer characterSprite;
- [SyncVar]
- public bool insideDoor;
- public LayerMask friendLayer;
- public List<NetPlayer> touchingNeighbours = new List<NetPlayer>();
- [SyncVar]
- public Color pColor;
- [SyncVar]
- public string playerName;
- public Color[] playerColors;
- void Start()
- {
- DontDestroyOnLoad(gameObject);
- if (!isLocalPlayer)
- {
-
-
- foreach (Behaviour localComponent in LocalComponents)
- {
- localComponent.enabled = false;
- }
- }
- if (isLocalPlayer)
- {
- SceneData.localPlayer = gameObject;
-
-
-
- ReturnToSpawn();
- }
- if(isServer){
- pColor = playerColors[NetworkServer.connections.Count-1];
- }
- GetComponent<SpriteRenderer>().color = pColor;
- }
- public void ReturnToSpawn()
- {
- if (isServer)
- {
- StartCoroutine(returnToSpawn());
- }
- else
- {
- CmdReturnToSpawn();
- }
- }
- [Command]
- void CmdReturnToSpawn()
- {
- StartCoroutine(returnToSpawn());
- }
- IEnumerator returnToSpawn()
- {
- while (SceneData.netSceneData == null)
- {
- yield return new WaitForSeconds(0.1f);
- }
- while (SceneData.netSceneData.spawnPoint == null)
- {
- yield return new WaitForSeconds(0.1f);
- }
- transform.position = SceneData.netSceneData.spawnPoint.position;
- }
- [SyncVar]
- public Transform parentFrnd;
- bool oldFlipVal = false;
- Transform _parentFrnd;
-
-
-
-
-
-
-
-
-
-
-
-
-
- float t = 0;
- void FixedUpdate()
- {
- if (isServer)
- {
- parentFrnd = getOnFriend();
- if (collisionImpact != Vector3.zero)
- {
- GetComponent<Rigidbody2D>().AddForce(-collisionImpact, ForceMode2D.Impulse);
- collisionImpact = Vector3.zero;
- }
- if (oldFlipVal != characterSprite.flipX)
- {
- if (isServer)
- {
- RpcFlipX(characterSprite.flipX);
- }
- else
- {
- CmdFlipX(characterSprite.flipX);
- }
- oldFlipVal = characterSprite.flipX;
- }
- }
- transform.parent = parentFrnd;
- if (transform.parent != null)
- {
- if (t < 1)
- {
- t += Time.deltaTime;
- }
- else
- {
- GetComponent<NetworkTransform>().useLocalSpace = true;
- }
- }
- else
- {
- GetComponent<NetworkTransform>().useLocalSpace = false;
- t = 0;
- }
- if (!isLocalPlayer) { return; }
-
-
-
-
-
-
-
-
- if (!isServer) { return; }
- }
- [Command]
- void CmdFlipX(bool value)
- {
- FlipX(value);
- RpcFlipX(value);
- }
- [ClientRpc]
- void RpcFlipX(bool value)
- {
- FlipX(value);
- }
- void FlipX(bool value)
- {
- characterSprite.flipX = value;
- }
- public void CallChangeInsideDoor(bool value)
- {
- if (isServer)
- {
- insideDoor = value;
- }
- else
- {
- CmdChangeInsideDoor(value);
- }
- }
- [Command]
- void CmdChangeInsideDoor(bool value)
- {
- insideDoor = value;
- }
- public Transform getOnFriend()
- {
- Transform friend = null;
-
- Collider2D col = GetComponentInChildren<Collider2D>();
- 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);
- friend = (hit) ? ((hit.collider.transform.GetComponent<NetPlayer>() != null && hit.collider.transform.GetComponent<NetPlayer>() != this) ? hit.collider.transform : null) : null;
- return friend;
- }
- Vector3 collisionImpact;
- void OnCollisionEnter2D(Collision2D col)
- {
- NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
- if (obj != null)
- {
- collisionImpact = col.contacts[0].normal * col.contacts[0].relativeVelocity * col.otherRigidbody.mass;
- Debug.Log("Collision impact : " + collisionImpact);
- if (!touchingNeighbours.Contains(obj))
- {
- touchingNeighbours.Add(obj);
- }
- }
- }
- void OnCollisionExit2D(Collision2D col)
- {
- NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
- if (obj != null)
- {
- if (touchingNeighbours.Contains(obj))
- {
- touchingNeighbours.Remove(obj);
- }
- }
- }
- void UpdatePushBoxes()
- {
- foreach (PushBox box in FindObjectsOfType<PushBox>())
- {
- box.UpdateNeighbourCount();
- }
- }
- public void OnSceneChanged(){
- if(!isServer){return;}
- foreach(GameObject obj in SceneData.netSceneData.networkObjects){
- GameObject go = Instantiate(obj);
- NetworkServer.Spawn(go, NetworkServer.localConnection);
- }
- }
- }
|