123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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>();
- void Start()
- {
- DontDestroyOnLoad(gameObject);
- if(!isLocalPlayer){
- gameObject.layer = LayerMask.NameToLayer("Gnd");
- //GetComponent<BoxCollider2D>().size = new Vector2(GetComponent<BoxCollider2D>().size.x/2f,GetComponent<BoxCollider2D>().size.y);
- foreach(Behaviour localComponent in LocalComponents){
- localComponent.enabled=false;
- }
- }
- if(isLocalPlayer){
- SceneData.localPlayer = gameObject;
- if(SceneData.netSceneData==null){Debug.Log("Scene Data is not init yet");}else{
- transform.position = SceneData.netSceneData.spawnPoint.position;
- }
- }
- }
- public void ReturnToSpawn(){
- if(isLocalPlayer){
- 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;
- [Command]
- void CmdChangeParent(Transform newParent){
- transform.parent = newParent;
- RpcChangeParent(newParent);
- }
- [ClientRpc]
- void RpcChangeParent(Transform newParent){
- transform.parent = newParent;
-
- }
- float t=0;
- void FixedUpdate()
- {
- 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;}
- parentFrnd = getOnFriend();
- transform.parent = parentFrnd;
- if(_parentFrnd != parentFrnd){
- if(isServer){
- transform.parent = parentFrnd;
- RpcChangeParent(parentFrnd);
- }else{
- CmdChangeParent(parentFrnd);
- }
- _parentFrnd=parentFrnd;
- }
- if(oldFlipVal != characterSprite.flipX){
- if(isServer){
- RpcFlipX(characterSprite.flipX);
- }else{
- CmdFlipX(characterSprite.flipX);
- }
- oldFlipVal=characterSprite.flipX;
- }
-
- // bool someoneOnTop = false;
- // foreach(NetPlayer neighbour in touchingNeighbours){
- // if(neighbour.parentFrnd == this){
- // someoneOnTop=true;
- // break;
- // }
- // }
- // GetComponent<PlayerController>().isSomeoneOnTop =someoneOnTop;
- if(!isServer){return;}
- }
- [Command]
- void CmdFlipX(bool value){
- FlipX(value);
- RpcFlipX(value);
- }
- [ClientRpc]
- void RpcFlipX(bool value){
- if(!isLocalPlayer)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;
- //return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
- 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 : null) : null;
- return friend;
- }
- void OnCollisionEnter2D(Collision2D col){
- NetPlayer obj = col.collider.transform.GetComponent<NetPlayer>();
- if(obj!=null){
- 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();
- }
- }
- }
|