215 lines
7.5 KiB
C#
215 lines
7.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using Newtonsoft.Json;
|
||
using UnityEngine;
|
||
using DG.Tweening;
|
||
using TowerClimberChronicles;
|
||
|
||
public class level3 : 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 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()
|
||
{
|
||
_itemList = new List<ItemState>();
|
||
|
||
|
||
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)
|
||
{
|
||
Rigidbody2D rb = child.GetComponent<Rigidbody2D>();
|
||
if (rb != null)
|
||
{
|
||
rb.simulated = true;
|
||
|
||
// 随机方向
|
||
Vector2 randomDir = Random.insideUnitCircle.normalized;
|
||
|
||
// 随机力度(比如 5~10)
|
||
float randomForce = Random.Range(2f,3f);
|
||
|
||
// 添加力
|
||
rb.AddForce(randomDir * randomForce, ForceMode2D.Impulse);
|
||
}
|
||
}
|
||
|
||
Debug.Log(JsonConvert.SerializeObject(_itemList));
|
||
}
|
||
|
||
|
||
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";
|
||
selected.GetComponent<Rigidbody2D>().simulated = false;
|
||
// 点击时强制修正为 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 < 0.5f)
|
||
{
|
||
// 吸附过去并旋转归零
|
||
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;
|
||
selected.GetComponent<PolygonCollider2D>().enabled = false;
|
||
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";
|
||
selected.GetComponent<Rigidbody2D>().simulated = true;
|
||
// 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;
|
||
}
|
||
}
|
||
|