using System.Collections; using System.Collections.Generic; using UnityEngine; using AX.MessageSystem; using System; using AX.InputSystem; using AX.NetworkSystem; public class ObjDrag : MonoBehaviour { private Vector3 prevPos = Vector3.zero;//进入拖动过程前(开始拖动)时鼠标在屏幕坐标系中的位置 private Vector3 mousePostion = Vector3.zero; private bool dragStart; private GameObject hitObj; public Vector3 prevTranformPos = Vector3.zero;//进入拖动记录的位置,拖动到不能放下的地方,会弹回到此位置 // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (dragStart) ObjTranslate(); } void OnEnable() { MessageDispatcher.AddListener("OBJ_DRAG_ENTER_COMMAND", ObjDragEnterExecute); MessageDispatcher.AddListener("OBJ_DRAGING_COMMAND", ObjDragingExecute); MessageDispatcher.AddListener("OBJ_DRAG_EXIT_COMMAND", ObjDragExitExecute); } void OnDisable() { MessageDispatcher.RemoveListener("OBJ_DRAG_ENTER_COMMAND", ObjDragEnterExecute); MessageDispatcher.RemoveListener("OBJ_DRAGING_COMMAND", ObjDragingExecute); MessageDispatcher.RemoveListener("OBJ_DRAG_EXIT_COMMAND", ObjDragExitExecute); } void OnDestroy() { MessageDispatcher.RemoveListener("OBJ_DRAG_ENTER_COMMAND", ObjDragEnterExecute); MessageDispatcher.RemoveListener("OBJ_DRAGING_COMMAND", ObjDragingExecute); MessageDispatcher.RemoveListener("OBJ_DRAG_EXIT_COMMAND", ObjDragExitExecute); } private void ObjDragEnterExecute(IMessage obj) { var data = (ObjDragCmdArgs)obj.Data; mousePostion = data.mousePosition; dragStart = data.dragStart; var screenSpace = Camera.main.WorldToScreenPoint(transform.position); prevPos = new Vector3(mousePostion.x, mousePostion.y, screenSpace.z); var gameObjID = (long)obj.Sender; hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID); //特殊处理集结区,安全区的移动:进入时记录下拖动前的位置 if (gameObjID == transform.GetComponent().gameObjID) { if (GetComponent().gameObjType == CloneObjType.stagingArea || GetComponent().gameObjType == CloneObjType.SafeArea) { prevTranformPos = hitObj.transform.position; } } } private void ObjDragingExecute(IMessage obj) { var data = (ObjDragCmdArgs)obj.Data; mousePostion = data.mousePosition; dragStart = data.dragStart; var gameObjID = (long)obj.Sender; hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID); //拖拽过程中,集结区,安全区提示是否能放下(当前位置不能寻路则不能放下) if (gameObjID == transform.GetComponent().gameObjID) { if (GetComponent().gameObjType == CloneObjType.stagingArea || GetComponent().gameObjType == CloneObjType.SafeArea) { if (transform.parent.GetComponent()) { if (!transform.parent.GetComponent().CheckCloneAble(transform.position)) { transform.GetComponent().material.color = Color.red; } else { transform.GetComponent().material.color = Color.green; } } if (transform.parent.GetComponent()) { if (!transform.parent.GetComponent().CheckCloneAble(transform.position)) { transform.GetComponent().material.color = Color.red; } else { transform.GetComponent().material.color = Color.green; } } } } } private void ObjDragExitExecute(IMessage obj) { var data = (ObjDragCmdArgs)obj.Data; prevPos = data.mousePosition; dragStart = data.dragStart; var gameObjID = (long)obj.Sender; hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID); //拖动结束时,集结区,安全区判断能否停放到当前结束拖动的位置 if (gameObjID == transform.GetComponent().gameObjID) { if (GetComponent().gameObjType == CloneObjType.stagingArea || GetComponent().gameObjType == CloneObjType.SafeArea) { if (transform.parent.GetComponent()) { if (!transform.parent.GetComponent().CheckCloneAble(transform.position)) { transform.position = prevTranformPos; transform.GetComponent().material.color = Color.white; } else { transform.GetComponent().material.color = Color.white; } } if (transform.parent.GetComponent()) { if (!transform.parent.GetComponent().CheckCloneAble(transform.position)) { transform.position = prevTranformPos; transform.GetComponent().material.color = Color.white; var arg = new ObjDragSyncData(); arg.SendUserID = CurrentUserInfo.mySelf.Id; arg.position = prevTranformPos; arg.gameObjID = GetComponent().gameObjID; NetworkManager.Default.SendAsync("OBJ_DRAG_SYNC", arg); } else { transform.GetComponent().material.color = Color.white; } } } } } /// /// 拖拽克隆对象平面移动 /// private void ObjTranslate() { if (gameObject == hitObj) { if (gameObject.GetComponent()) { if (gameObject.GetComponent().fireAttribute.spreadWay != SpreadWay.None) { LoadPromptWin.Instance.LoadTextPromptWindow("开始蔓延的火不能拖动!", 1f); return; } } if (mousePostion.x > Screen.width || mousePostion.y > Screen.height) { return; } var screenSpace = Camera.main.WorldToScreenPoint(transform.position); var currPos = new Vector3(mousePostion.x, mousePostion.y, screenSpace.z); var prev = Camera.main.ScreenToWorldPoint(prevPos); var curr = Camera.main.ScreenToWorldPoint(currPos); var offset = curr - prev; offset.y = 0; transform.position += offset; //拖拽同步 if (GameSettings.othersSettings.mode!=Mode.DisasterManagement) { var arg = new ObjDragSyncData(); arg.SendUserID = CurrentUserInfo.mySelf.Id; arg.position = transform.position; arg.gameObjID = GetComponent().gameObjID; NetworkManager.Default.SendAsync("OBJ_DRAG_SYNC", arg); } //如果是被困人员,更改被困人员出生位置 if (GetComponent()) { GetComponent().originalpoint = transform.position; } prevPos = currPos; } } }