/// ///******************************************************** /// 作者: DDG /// 日期: 2024/10/21 13:43:22 /// 功能: 顺序拖拽拼装管理器 ///******************************************************** /// using System.Collections; using System.Collections.Generic; using UnityEngine; using ZXKFramework; public class OrderDragManager : MonoSingleton { /// /// 拖拽序号 /// int orderID; string[] OrderDragNames; int OrderDragCount = 0; /// /// 所有拖拽物体的初始位置(用于拖拽顺序错误时,返回初始位置) /// Dictionary allDragStartPos; GameModel gameModel; public bool NextDrag() { return OrderDragCount == OrderDragNames.Length; } void GetOrderDragNames() { OrderDragNames = null; OrderDragCount = 0; if (orderID >= gameModel.excelData.allOrderDragData.Count) return ; OrderDragNames = gameModel.excelData.GetOrderDragDataorderID(orderID).dragGameName.Split(new char[] {'|'}); } public void Init() { orderID = 0; gameModel = MVC.GetModel(); allDragStartPos = new Dictionary(); } /// /// 将需要拖拽物体的初始位置保存下来 /// /// /// public void AddDragGame(string name, Vector3 startPos) { if (!allDragStartPos.ContainsKey(name)) { allDragStartPos.Add(name, startPos); } } /// /// 当前拖拽是否顺序正确且放到了正确的位置 /// /// /// public bool isDragRight(GameObject go) { if (IsDragOrderRight(go) && IsDragRightPos(go)) { OrderDragCount++; return true; } return false; } /// /// 当前拖拽物体是否放到了正确的位置 /// /// /// public bool IsDragRightPos(GameObject go) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] hits = Physics.RaycastAll(ray, 10000); //RaycastHit2D[] hits = Physics2D.GetRayIntersectionAll(ray); for (int i = 0; i < hits.Length; i++) { if (hits[i].collider.name.Equals($"{go.name}_OutLine"))//如果为true说明当前拖拽物体放到了正确的位置 { hits[i].collider.enabled = false; return true; } } return false; } /// /// 判断拖拽顺序是否正确 /// /// /// public bool IsDragOrderRight(GameObject go) { foreach (string str in OrderDragNames) { if (go.name.Equals(str)) return true; } return false; //if (go.name.Equals(gameModel.excelData.GetOrderDragDataorderID(orderID).dragGameName)) //{ // return true; //} //return false; } /// /// 如果当前拖拽的物体顺序不正确或者没有放到正确的位置则返回该物体炸开的初始位置 /// /// public void ReturnToStartPos(GameObject go) { if (allDragStartPos.ContainsKey(go.name)) { go.transform.localPosition = allDragStartPos[go.name]; } else { AddDragGame(go.name, go.transform.localPosition); this.ColorLog(GDLog.LogColorState.Magenta, $"没有找到当前物体:{go.name}的初始位置,请查看是否字典中是否保存"); } } /// /// 拖拽正确以后顺序ID增加 /// public void AddOrderID() { orderID++; MVC.GetView().changeState(orderID + 1); GetOrderDragNames(); } /// /// 初始化拖拽顺序为0 /// public void InitDragOrder(int a = 0) { orderID = a; GetOrderDragNames(); } /// /// 所有物体是否拼装完毕 /// /// public bool IsPieceTogetherFinish() { if (orderID.Equals(gameModel.excelData.allOrderDragData.Count)) { this.Log("恭喜您,所有物体按照顺序拼装完毕"); return true; } return false; } public int GetorderID() { return orderID; } }