Files

321 lines
9.0 KiB
C#
Raw Permalink Normal View History

2026-07-15 16:19:07 +08:00
using DG.Tweening;
using System.Collections.Generic;
using System.Linq;
using Roy.ObjectPool;
using ScrewsMaster;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace SGame
{
public class PanelLogic : ObjectPoolItem
{
private List<Sprite> _availableScrews;
private int _minCount;
public Text layerText;
#region 新版代码(测试)
private List<ScrewsLogic> _screwLogics;
private List<PanelHole> _holes;
private Image _backgroundImage;
public Image frameImage;
private Rigidbody2D _rigidbody2D;
private int _activeScrewCount;
public PolygonCollider2D polygonCollider2D;
private int _layer;
private bool _isLock;
private Color _curColor;
private int _curShowScrewCount;
private static readonly Color LockColor = new Color(0,0,0, 0.4f);
private static readonly Color LockFrameColor = new Color(1,1,1, 0f);
/// <summary>
/// 开始检查掉落
/// </summary>
private bool _startCheckDrop;
private void Awake()
{
_rigidbody2D = GetComponent<Rigidbody2D>();
_backgroundImage = GetComponent<Image>();
polygonCollider2D = GetComponent<PolygonCollider2D>();
// InitScrews();
InitHoles();
}
private void Update()
{
if (_startCheckDrop)
{
if (gameObject.transform.localPosition.y < -1200)
{
ShowScrews.Instance.RemovePanelLogic(this);
// 销毁游戏对象
// Destroy(gameObject);
PoolManager.Instance.ReturnObject(objectName, this);
}
}
}
/// <summary>
/// 隐藏所有螺丝孔
/// </summary>
public void HideAllHole()
{
foreach (var hole in _holes)
{
hole.gameObject.SetActive(false);
}
}
/// <summary>
/// 初始化孔位
/// </summary>
private void InitHoles()
{
_holes = gameObject.GetComponentsInChildren<PanelHole>().ToList();
// foreach (var hole in _holes)
// {
// hole.gameObject.SetActive(false);
// }
for (int i = 0; i < _holes.Count; i++)
{
_holes[i].gameObject.name = i.ToString();
_holes[i].gameObject.SetActive(false);
}
}
/// <summary>
/// 初始化螺丝数据
/// </summary>
private void InitScrews()
{
_screwLogics = gameObject.GetComponentsInChildren<ScrewsLogic>().ToList();
foreach (var screwsLogic in _screwLogics)
{
screwsLogic.transform.parent.gameObject.SetActive(false);
}
}
/// <summary>
/// 显示螺丝孔,并初始化螺丝
/// </summary>
/// <param name="types"></param>
// public void ShowHole(List<Sprite> sprites)
public void ShowHole(List<int> types)
{
// _curShowScrewCount = Mathf.Min(types.Count, _screwLogics.Count);
// for (int i = 0; i < _curShowScrewCount; i++)
// {
// _screwLogics[i].Init(types[i]);
// _screwLogics[i].transform.parent.gameObject.SetActive(true);
// }
_curShowScrewCount = Mathf.Min(types.Count, _holes.Count);
_screwLogics ??= new List<ScrewsLogic>();
for (int i = 0; i < _curShowScrewCount; i++)
{
_holes[i].gameObject.SetActive(true);
var screwsLogic = PoolManager.Instance.GetObject<ScrewsLogic>("Screw");
screwsLogic.transform.SetParent(_holes[i].transform);
screwsLogic.transform.localPosition = Vector3.zero;
screwsLogic.Init(types[i]);
_screwLogics.Add(screwsLogic);
}
_startCheckDrop = false;
_activeScrewCount = _curShowScrewCount;
RemoveActiveScrewCount(null);
}
public void RemoveActiveScrewCount(ScrewsLogic screwsLogic)
{
if (screwsLogic != null)
{
if (_screwLogics.Remove(screwsLogic))
{
_activeScrewCount--;
}
else
{
Debug.LogError("尝试删除了不属于该面板的螺丝");
}
}
if (_activeScrewCount == 1)
{
SetRigidbody2D();
}
if (_activeScrewCount <= 0)
{
_startCheckDrop = true;
}
}
/// <summary>
/// 获得螺丝孔数量
/// </summary>
/// <returns></returns>
public int GetHoleCount()
{
return _holes.Count;
}
/// <summary>
/// 将螺钉旋转对齐
/// </summary>
public void AlignScrewsToRotation()
{
foreach (var screwLogic in _screwLogics)
{
screwLogic.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, -transform.eulerAngles.z));
}
}
private void SetRigidbody2D()
{
_rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
_rigidbody2D.angularDrag = 1.5f;
_rigidbody2D.mass = 5f;
}
public void SetLayerText(string layerName)
{
layerText.text = layerName;
layerText.gameObject.SetActive(true);
}
public void SetBackgroundColor(Color color)
{
_curColor = color;
_backgroundImage.color = _curColor;
}
public void UpdatePolygonCollider2D()
{
polygonCollider2D = GetComponent<PolygonCollider2D>();
}
public float GetRectMaxEdge()
{
var sizeDelta = _backgroundImage.rectTransform.sizeDelta;
return Mathf.Max(sizeDelta.x, sizeDelta.y);
}
public void SetLayer(int layer)
{
_layer = layer;
}
public int GetLayer()
{
return _layer;
}
public void SetLock(bool b, bool animateEffect = false)
{
_isLock = b;
void UpdatePhysicalEffect()
{
if (_isLock)
{
SetStaticRigidbody();
}
else
{
RemoveActiveScrewCount(null);
}
}
if (!animateEffect)
{
_backgroundImage.color = b ? LockColor : _curColor;
frameImage.color = b ? LockFrameColor : Color.white;
for (int i = 0; i < _curShowScrewCount; i++)
{
_holes[i].transform.parent.gameObject.SetActive(!b);
}
UpdatePhysicalEffect();
}
else
{
var sequence = DOTween.Sequence();
sequence.Append(_backgroundImage.DOColor(b ? LockColor : _curColor, 0.5f));
sequence.Append(frameImage.DOColor(b ? LockFrameColor : Color.white, 0.5f));
for (int i = 0; i < _curShowScrewCount; i++)
{
_holes[i].gameObject.SetActive(!b);
_screwLogics[i].SetAlpha(b ? 0 : 1);
if (_isLock) continue;
if (i == 0)
sequence.Append(_screwLogics[i].ShowAnim());
else
sequence.Join(_screwLogics[i].ShowAnim());
}
sequence.AppendCallback(UpdatePhysicalEffect);
}
if (!_isLock)
{
UpdateScrewRecord();
}
}
public bool IsLock()
{
return _isLock;
}
private void UpdateScrewRecord()
{
for (int i = 0; i < _curShowScrewCount; i++)
{
var screwsType = _screwLogics[i].screwsType;
ShowScrews.Instance.AddScrewTypeRecord(screwsType);
}
}
public void SetStaticRigidbody()
{
_rigidbody2D.bodyType = RigidbodyType2D.Static;
}
public override void OnRecycle()
{
base.OnRecycle();
foreach (var screwLogic in _screwLogics)
{
PoolManager.Instance.ReturnObject(screwLogic.objectName, screwLogic);
}
_screwLogics.Clear();
HideAllHole();
_startCheckDrop = false;
SetStaticRigidbody();
}
#endregion
}
}