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 _availableScrews; private int _minCount; public Text layerText; #region 新版代码(测试) private List _screwLogics; private List _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); /// /// 开始检查掉落 /// private bool _startCheckDrop; private void Awake() { _rigidbody2D = GetComponent(); _backgroundImage = GetComponent(); polygonCollider2D = GetComponent(); // InitScrews(); InitHoles(); } private void Update() { if (_startCheckDrop) { if (gameObject.transform.localPosition.y < -1200) { ShowScrews.Instance.RemovePanelLogic(this); // 销毁游戏对象 // Destroy(gameObject); PoolManager.Instance.ReturnObject(objectName, this); } } } /// /// 隐藏所有螺丝孔 /// public void HideAllHole() { foreach (var hole in _holes) { hole.gameObject.SetActive(false); } } /// /// 初始化孔位 /// private void InitHoles() { _holes = gameObject.GetComponentsInChildren().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); } } /// /// 初始化螺丝数据 /// private void InitScrews() { _screwLogics = gameObject.GetComponentsInChildren().ToList(); foreach (var screwsLogic in _screwLogics) { screwsLogic.transform.parent.gameObject.SetActive(false); } } /// /// 显示螺丝孔,并初始化螺丝 /// /// // public void ShowHole(List sprites) public void ShowHole(List 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(); for (int i = 0; i < _curShowScrewCount; i++) { _holes[i].gameObject.SetActive(true); var screwsLogic = PoolManager.Instance.GetObject("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; } } /// /// 获得螺丝孔数量 /// /// public int GetHoleCount() { return _holes.Count; } /// /// 将螺钉旋转对齐 /// 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(); } 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 } }