Files
2026-07-15 16:19:07 +08:00

222 lines
6.9 KiB
C#

using System;
using ScrewsMaster;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using Roy.ObjectPool;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using ColorUtility = UnityEngine.ColorUtility;
using Sequence = DG.Tweening.Sequence;
namespace SGame
{
public class ScrewsLogic : ObjectPoolItem
{
public PanelLogic panelLogic;
public PolygonCollider2D polygonCollider2D;
private HingeJoint2D _hingeJoint2D;
private Button _button;
public Text testText;
private CanvasGroup _canvasGroup;
private void Awake()
{
// panelLogic = GetComponentInParent<PanelLogic>();
polygonCollider2D = GetComponentInChildren<PolygonCollider2D>();
_hingeJoint2D = GetComponentInChildren<HingeJoint2D>(); ;
_button = GetComponentInChildren<Button>();
_canvasGroup = gameObject.AddComponent<CanvasGroup>();
// if (panelLogic != null)
// {
// _hingeJoint2D.connectedBody = panelLogic.GetComponent<Rigidbody2D>();
// _hingeJoint2D.enabled = true;//初始化时都启用一下物理组件,有的预制体里面没启用
// }
}
void Start()
{
_button.onClick.AddListener(OnClick);
}
#region 新版代码(测试)
public int screwsType;
public Image head;
public Image thread;
public float threadHeightMax = 59f;
public float threadHeightMin = 30f;
// public Vector2 threadDefault = 32f;
public void Init(int t)
{
screwsType = t;
SetSprite(ShowScrews.Instance.GetScrewsSprite(screwsType));
thread.color = ColorUtility.TryParseHtmlString(ShowScrews.ScrewColors[screwsType], out Color _color)
? _color
: Color.white;
panelLogic = GetComponentInParent<PanelLogic>();
if (panelLogic != null)
{
_hingeJoint2D.connectedBody = panelLogic.GetComponent<Rigidbody2D>();
_hingeJoint2D.enabled = true;//初始化时都启用一下物理组件,有的预制体里面没启用
}
_button.enabled = true;
transform.localScale = Vector3.one;
}
#if !JarvisRelease
private void Update()
{
if (testText.gameObject.activeSelf)
{
var point = RectTransformUtility.WorldToScreenPoint(ShowScrews.Instance.myCamera, transform.position);
testText.text = point.ToString();
}
}
#endif
private void SetSprite(Sprite sprite)
{
head.sprite = sprite;
}
public void OnClick()
{
ShowScrews.Instance.is_resurgence = false;
if (ShowScrews.Instance.gameOver) return;//游戏结束
var panelLayerDic = ShowScrews.Instance.PanelLayerDic;
for (int i = panelLogic.GetLayer() + 1; i <= 100; i++)
{
if (panelLayerDic.TryGetValue(i, out var layer))
{
if (ShowScrews.Instance.CheckOverlapWithExistingObjects(polygonCollider2D,
layer))
{
return;
}
}
}
AudioManager.Instance.PlayDynamicEffect(AudioConst.UIButtonDefault);//播放音效
var boxTypeLogic = ShowScrews.Instance.GetBoxTypeLogic(screwsType);
if (boxTypeLogic != null)
{
if (boxTypeLogic.CanUseKong(this))
{
UnlinkFromPanel();
return;
}
}
var kongLogic = ShowScrews.Instance.GetKongLogic();
if (kongLogic != null)
{
kongLogic.SetCurScrews(this);
FlyToTarget(kongLogic.transform, () =>
{
if (ShowScrews.Instance.GetIdleHoleCount() <= 0)
{
ShowScrews.Instance.OnGameFailed();//游戏失败,等待飞螺丝动画后执行失败逻辑
}
});
if (ShowScrews.Instance.GetIdleHoleCount() <= 0)
{
ShowScrews.Instance.gameOver = true;//游戏结束,设置结束标记
}
UnlinkFromPanel();
return;
}
ShowScrews.Instance.OnGameFailed();//游戏失败
}
public Sequence FlyToTarget(Transform target, UnityAction action = null)
{
if (panelLogic != null)
{
panelLogic.RemoveActiveScrewCount(this);
panelLogic = null;
}
transform.rotation = Quaternion.Euler(Vector3.zero);
transform.SetParent(ShowScrews.Instance.flightControlLayer);
var sequence = DOTween.Sequence();
sequence.Append(head.rectTransform.DOLocalRotate(new Vector3(0, 0, 360), 0.25f, RotateMode.FastBeyond360));
sequence.Join(thread.rectTransform.DOSizeDelta(new Vector2(thread.rectTransform.sizeDelta.x, threadHeightMax), 0.25f));
// sequence.AppendCallback(() =>
// {
// transform.SetParent(target);
// });
// sequence.AppendInterval(0.05f);
// sequence.Append(transform.DOLocalMove(Vector3.zero, 0.5f).SetEase(Ease.OutBack));
sequence.Append(transform.DOMove(target.position, 0.3f).SetEase(Ease.OutBack));
sequence.AppendCallback(() =>
{
transform.SetParent(target);
transform.localPosition = Vector3.zero;
});
sequence.AppendInterval(0.05f);
sequence.AppendCallback(() => { AudioManager.Instance.PlayDynamicEffect(AudioConst.FlyToCacheKong); });
sequence.Append(head.rectTransform.DOLocalRotate(new Vector3(0, 0, -360), 0.25f, RotateMode.FastBeyond360));
sequence.Join(thread.rectTransform.DOSizeDelta(new Vector2(thread.rectTransform.sizeDelta.x, threadHeightMin), 0.25f));
sequence.AppendCallback(() =>
{
action?.Invoke();
});
return sequence;
}
/// <summary>
/// 禁用触控与物理组件
/// </summary>
private void UnlinkFromPanel()
{
_button.enabled = false;
_hingeJoint2D.enabled = false;
}
public TweenerCore<float, float, FloatOptions> ShowAnim()
{
return _canvasGroup.DOFade(1f, 0.5f);
}
public void SetAlpha(float alpha)
{
_canvasGroup.alpha = alpha;
}
public override void OnRecycle()
{
base.OnRecycle();
panelLogic = null;
}
#endregion
}
}