255 lines
8.9 KiB
C#
255 lines
8.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using BingoBrain;
|
|
|
|
public class level1 : MonoBehaviour
|
|
{
|
|
public GameObject plate_parent;
|
|
public GameObject item_parent;
|
|
private List<ItemState> _itemList;
|
|
public Camera cam;
|
|
private GameObject selected;
|
|
private Vector3 offset;
|
|
private int out_layers = 30;
|
|
private GameObject guo;
|
|
private List<Vector3> originalPositions = new List<Vector3>();
|
|
void Awake()
|
|
{
|
|
Camera orthoCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
|
|
|
|
float size = (float)System.Math.Round(5.5f / ((float)Screen.width / Screen.height), 4);
|
|
// string type = SystemInfo.deviceModel.ToLower().Trim();
|
|
// // Debug.Log($"type==========={type}");
|
|
// if (type.Substring(0, 3) == "ipa")
|
|
// {//iPad机型
|
|
// size = 49.9f;
|
|
// }
|
|
Debug.Log(size + "////////////////");
|
|
orthoCamera.orthographicSize = size;
|
|
}
|
|
void Start()
|
|
{
|
|
guo = GameObject.Find("guo");
|
|
_itemList = new List<ItemState>();
|
|
foreach (Transform child in plate_parent.transform)
|
|
{
|
|
Debug.Log("子物体: " + child.name);
|
|
originalPositions.Add(child.position);
|
|
|
|
// 先把它们全部移到 (0,14)
|
|
child.position = new Vector3(0f, 14f, child.position.z);
|
|
}
|
|
originalPositions.Add(guo.transform.position);
|
|
guo.transform.position = new Vector3(0f, -15f, 0);
|
|
// 启动协程,逐个缓动回原位置
|
|
StartCoroutine(MoveBackCoroutine());
|
|
guo.transform.DOMove(originalPositions[originalPositions.Count - 1], 1f).SetEase(Ease.OutQuad);
|
|
|
|
|
|
foreach (Transform child in item_parent.transform)
|
|
{
|
|
Debug.Log("子物体: " + child.name);
|
|
Debug.Log("子物体: " + child.tag);
|
|
_itemList.Add(new ItemState()
|
|
{
|
|
_position = child.position,
|
|
_orderInLayer = child.GetComponent<SpriteRenderer>().sortingOrder,
|
|
_tag = child.tag,
|
|
_isFull = false
|
|
});
|
|
}
|
|
foreach (Transform child in item_parent.transform)
|
|
{
|
|
float randomX = Random.Range(-3f, 3f); // X 在 -2 ~ 2
|
|
float randomY = 14f + Random.Range(-2f, 2f); // Y 在 -16 ~ -12
|
|
child.position = new Vector3(randomX, randomY, child.position.z);
|
|
}
|
|
DOVirtual.DelayedCall(2.5f, () =>
|
|
{
|
|
StartCoroutine(MoveChildrenCoroutine());
|
|
});
|
|
Debug.Log(JsonConvert.SerializeObject(_itemList));
|
|
}
|
|
IEnumerator MoveChildrenCoroutine()
|
|
{
|
|
foreach (Transform child in item_parent.transform)
|
|
{
|
|
// 随机目标 Y 在 -5 ~ 5
|
|
float targetY = Random.Range(-5f, 5f);
|
|
Vector3 targetPos = new Vector3(child.position.x, targetY, child.position.z);
|
|
|
|
// DOTween 缓动到目标位置
|
|
child.DOMove(targetPos, 1.7f).SetEase(Ease.InCubic);
|
|
|
|
// 每个间隔 0.1s
|
|
yield return new WaitForSeconds(0.05f);
|
|
}
|
|
}
|
|
IEnumerator MoveBackCoroutine()
|
|
{
|
|
int index = 0;
|
|
foreach (Transform child in plate_parent.transform)
|
|
{
|
|
// 缓动到原始位置
|
|
child.DOMove(originalPositions[index], 0.5f).SetEase(Ease.OutQuad);
|
|
|
|
index++;
|
|
// 每隔 0.3s 执行一个
|
|
yield return new WaitForSeconds(0.2f);
|
|
}
|
|
}
|
|
private GameObject Popup;
|
|
void Update()
|
|
{
|
|
// 鼠标按下时检测
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
if (Popup == null) Popup = GameObject.Find("Popup");
|
|
if (Popup != null && Popup.transform.childCount != 0) return;
|
|
Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
|
|
RaycastHit2D[] hits = Physics2D.RaycastAll(mousePos, Vector2.zero);
|
|
|
|
if (hits.Length > 0)
|
|
{
|
|
selected = GetTopSprite(hits);
|
|
if (selected != null)
|
|
{
|
|
// ✅ 如果该物体已经在 _itemList 中,释放槽位
|
|
foreach (var item in _itemList)
|
|
{
|
|
if (item._inGameobject == selected)
|
|
{
|
|
item._isFull = false;
|
|
item._inGameobject = null;
|
|
break;
|
|
}
|
|
}
|
|
|
|
selected.GetComponent<SpriteRenderer>().sortingLayerName = "layer_4_drag";
|
|
|
|
// 点击时强制修正为 0 度旋转
|
|
selected.transform.DORotate(Vector3.zero, 0.2f).SetEase(Ease.OutQuad);
|
|
|
|
// DOTween 缩放到固定 1.1 倍
|
|
selected.transform.DOScale(Vector3.one * 1.1f, 0.2f).SetEase(Ease.OutBack);
|
|
|
|
Vector3 pos = cam.ScreenToWorldPoint(Input.mousePosition);
|
|
offset = selected.transform.position - new Vector3(pos.x, pos.y, selected.transform.position.z);
|
|
selected.GetComponent<SpriteRenderer>().sortingOrder = out_layers;
|
|
out_layers++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 拖拽中
|
|
if (Input.GetMouseButton(0) && selected != null)
|
|
{
|
|
Vector3 pos = cam.ScreenToWorldPoint(Input.mousePosition);
|
|
selected.transform.position = new Vector3(pos.x, pos.y, selected.transform.position.z) + offset;
|
|
}
|
|
|
|
// 鼠标松开
|
|
if (Input.GetMouseButtonUp(0) && selected != null)
|
|
{
|
|
// DOTween 缩放还原到固定 1 倍
|
|
selected.transform.DOScale(Vector3.one, 0.2f).SetEase(Ease.InOutSine);
|
|
|
|
bool snapped = false;
|
|
|
|
// 吸附逻辑:检查标签和位置
|
|
string tag = selected.tag;
|
|
foreach (var item in _itemList)
|
|
{
|
|
if (item._isFull) continue;
|
|
if (item._tag == tag)
|
|
{
|
|
float dist = Vector3.Distance(selected.transform.position, item._position);
|
|
if (dist < 1f)
|
|
{
|
|
// 吸附过去并旋转归零
|
|
selected.transform.DOMove(item._position, 0.2f).SetEase(Ease.OutQuad);
|
|
selected.transform.DORotate(Vector3.zero, 0.2f).SetEase(Ease.OutQuad);
|
|
selected.GetComponent<SpriteRenderer>().sortingLayerName = "layer_2_inslot";
|
|
selected.GetComponent<SpriteRenderer>().sortingOrder = item._orderInLayer;
|
|
item._isFull = true;
|
|
item._inGameobject = selected;
|
|
snapped = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 如果吸附成功 → 检查整个列表
|
|
if (snapped)
|
|
{
|
|
bool allFull = true;
|
|
foreach (var item in _itemList)
|
|
{
|
|
if (!item._isFull)
|
|
{
|
|
allFull = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (allFull)
|
|
{
|
|
Debug.Log("所有物体都已吸附完成!");
|
|
// GameDispatcher.Instance.Dispatch(GameMsg.GameSuccess);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 如果没有吸附成功 → 随机旋转 ±8°
|
|
selected.GetComponent<SpriteRenderer>().sortingLayerName = "layer_3_out";
|
|
int randomChoice = Random.Range(0, 2); // 0 或 1
|
|
float randomAngle = (randomChoice == 0) ? -8f : 8f;
|
|
selected.transform.DORotate(new Vector3(0, 0, randomAngle), 0.3f).SetEase(Ease.OutQuad);
|
|
|
|
// GameDispatcher.Instance.Dispatch(GameMsg.GameSuccess);//zhushi
|
|
}
|
|
|
|
selected = null;
|
|
}
|
|
|
|
}
|
|
|
|
private GameObject GetTopSprite(RaycastHit2D[] hits)
|
|
{
|
|
GameObject top = null;
|
|
int topLayer = int.MinValue;
|
|
int topOrder = int.MinValue;
|
|
|
|
foreach (var hit in hits)
|
|
{
|
|
SpriteRenderer sr = hit.collider.GetComponent<SpriteRenderer>();
|
|
if (sr != null)
|
|
{
|
|
int layerID = SortingLayer.GetLayerValueFromID(sr.sortingLayerID);
|
|
int order = sr.sortingOrder;
|
|
|
|
if (layerID > topLayer || (layerID == topLayer && order > topOrder))
|
|
{
|
|
top = sr.gameObject;
|
|
topLayer = layerID;
|
|
topOrder = order;
|
|
}
|
|
}
|
|
}
|
|
return top;
|
|
}
|
|
}
|
|
|
|
public class ItemState
|
|
{
|
|
[JsonIgnore]
|
|
public Vector2 _position;
|
|
public int _orderInLayer;
|
|
public string _tag;
|
|
public bool _isFull;
|
|
public GameObject _inGameobject;
|
|
}
|