You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
8.1 KiB
222 lines
8.1 KiB
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<BaseGameObjInfo>().gameObjID) |
|
{ |
|
if (GetComponent<BaseGameObjInfo>().gameObjType == CloneObjType.stagingArea |
|
|| GetComponent<BaseGameObjInfo>().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<BaseGameObjInfo>().gameObjID) |
|
{ |
|
if (GetComponent<BaseGameObjInfo>().gameObjType == CloneObjType.stagingArea |
|
|| GetComponent<BaseGameObjInfo>().gameObjType == CloneObjType.SafeArea) |
|
{ |
|
if (transform.parent.GetComponent<CloneStayingAreaTool>()) |
|
{ |
|
if (!transform.parent.GetComponent<CloneStayingAreaTool>().CheckCloneAble(transform.position)) |
|
{ |
|
transform.GetComponent<Renderer>().material.color = Color.red; |
|
} |
|
else |
|
{ |
|
transform.GetComponent<Renderer>().material.color = Color.green; |
|
} |
|
} |
|
|
|
if (transform.parent.GetComponent<CloneSafeAreaTool>()) |
|
{ |
|
if (!transform.parent.GetComponent<CloneSafeAreaTool>().CheckCloneAble(transform.position)) |
|
{ |
|
transform.GetComponent<Renderer>().material.color = Color.red; |
|
} |
|
else |
|
{ |
|
transform.GetComponent<Renderer>().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<BaseGameObjInfo>().gameObjID) |
|
{ |
|
if (GetComponent<BaseGameObjInfo>().gameObjType == CloneObjType.stagingArea |
|
|| GetComponent<BaseGameObjInfo>().gameObjType == CloneObjType.SafeArea) |
|
{ |
|
if (transform.parent.GetComponent<CloneStayingAreaTool>()) |
|
{ |
|
if (!transform.parent.GetComponent<CloneStayingAreaTool>().CheckCloneAble(transform.position)) |
|
{ |
|
transform.position = prevTranformPos; |
|
transform.GetComponent<Renderer>().material.color = Color.white; |
|
} |
|
else |
|
{ |
|
transform.GetComponent<Renderer>().material.color = Color.white; |
|
} |
|
} |
|
|
|
if (transform.parent.GetComponent<CloneSafeAreaTool>()) |
|
{ |
|
if (!transform.parent.GetComponent<CloneSafeAreaTool>().CheckCloneAble(transform.position)) |
|
{ |
|
transform.position = prevTranformPos; |
|
transform.GetComponent<Renderer>().material.color = Color.white; |
|
|
|
var arg = new ObjDragSyncData(); |
|
arg.SendUserID = CurrentUserInfo.mySelf.Id; |
|
arg.position = prevTranformPos; |
|
arg.gameObjID = GetComponent<BaseGameObjInfo>().gameObjID; |
|
NetworkManager.Default.SendAsync("OBJ_DRAG_SYNC", arg); |
|
} |
|
else |
|
{ |
|
transform.GetComponent<Renderer>().material.color = Color.white; |
|
} |
|
} |
|
} |
|
} |
|
|
|
} |
|
|
|
/// <summary> |
|
/// 拖拽克隆对象平面移动 |
|
/// </summary> |
|
private void ObjTranslate() |
|
{ |
|
if (gameObject == hitObj) |
|
{ |
|
if (gameObject.GetComponent<FireSpreadCtrl>()) |
|
{ |
|
if (gameObject.GetComponent<FireSpreadCtrl>().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<BaseGameObjInfo>().gameObjID; |
|
NetworkManager.Default.SendAsync("OBJ_DRAG_SYNC", arg); |
|
} |
|
//如果是被困人员,更改被困人员出生位置 |
|
if (GetComponent<TrappedMoveFree>()) |
|
{ |
|
GetComponent<TrappedMoveFree>().originalpoint = transform.position; |
|
} |
|
|
|
prevPos = currPos; |
|
} |
|
} |
|
}
|
|
|