174 lines
4.7 KiB
C#
174 lines
4.7 KiB
C#
/// <summary>
|
|
///********************************************************
|
|
/// 作者: DDG
|
|
/// 日期: 2024/10/21 13:43:22
|
|
/// 功能: 顺序拖拽拼装管理器
|
|
///********************************************************
|
|
/// <summary>
|
|
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using ZXKFramework;
|
|
|
|
public class OrderDragManager : MonoSingleton<OrderDragManager>
|
|
{
|
|
/// <summary>
|
|
/// 拖拽序号
|
|
/// </summary>
|
|
int orderID;
|
|
|
|
string[] OrderDragNames;
|
|
int OrderDragCount = 0;
|
|
|
|
/// <summary>
|
|
/// 所有拖拽物体的初始位置(用于拖拽顺序错误时,返回初始位置)
|
|
/// </summary>
|
|
Dictionary<string, Vector3> 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<GameModel>();
|
|
allDragStartPos = new Dictionary<string, Vector3>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将需要拖拽物体的初始位置保存下来
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="startPos"></param>
|
|
public void AddDragGame(string name, Vector3 startPos)
|
|
{
|
|
if (!allDragStartPos.ContainsKey(name))
|
|
{
|
|
allDragStartPos.Add(name, startPos);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前拖拽是否顺序正确且放到了正确的位置
|
|
/// </summary>
|
|
/// <param name="go"></param>
|
|
/// <returns></returns>
|
|
public bool isDragRight(GameObject go)
|
|
{
|
|
if (IsDragOrderRight(go) && IsDragRightPos(go))
|
|
{
|
|
OrderDragCount++;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前拖拽物体是否放到了正确的位置
|
|
/// </summary>
|
|
/// <param name="go"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断拖拽顺序是否正确
|
|
/// </summary>
|
|
/// <param name="go"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 如果当前拖拽的物体顺序不正确或者没有放到正确的位置则返回该物体炸开的初始位置
|
|
/// </summary>
|
|
/// <param name="go"></param>
|
|
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}的初始位置,请查看是否字典中是否保存");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拖拽正确以后顺序ID增加
|
|
/// </summary>
|
|
public void AddOrderID()
|
|
{
|
|
orderID++;
|
|
MVC.GetView<LeftVer>().changeState(orderID + 1);
|
|
GetOrderDragNames();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化拖拽顺序为0
|
|
/// </summary>
|
|
public void InitDragOrder(int a = 0)
|
|
{
|
|
orderID = a;
|
|
GetOrderDragNames();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 所有物体是否拼装完毕
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool IsPieceTogetherFinish()
|
|
{
|
|
if (orderID.Equals(gameModel.excelData.allOrderDragData.Count))
|
|
{
|
|
this.Log("恭喜您,所有物体按照顺序拼装完毕");
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int GetorderID()
|
|
{
|
|
return orderID;
|
|
}
|
|
}
|