137 lines
3.8 KiB
C#
137 lines
3.8 KiB
C#
using UnityEngine;
|
||
using DG.Tweening;
|
||
using System.Collections;
|
||
using TowerClimberChronicles;
|
||
public class ChildItem : MonoBehaviour
|
||
{
|
||
// 引用父物体的管理器
|
||
private ParentItem parentManager;
|
||
public int type;
|
||
private GameObject light;
|
||
|
||
private void Awake()
|
||
{
|
||
parentManager = GetComponentInParent<ParentItem>();
|
||
GameDispatcher.Instance.AddListener(GameMsg.FlowerShowclickAni, ShowclickAni);
|
||
GameDispatcher.Instance.AddListener(GameMsg.FlowerHideclickAni, HideclickAni);
|
||
light = transform.Find("light").gameObject;
|
||
light.GetComponent<SpriteRenderer>().maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
|
||
// Debug.Log(light);
|
||
light.SetActive(false);
|
||
starPrefab = Resources.Load<GameObject>("flower/scene/star");
|
||
}
|
||
|
||
|
||
// 子物体自己的逻辑方法
|
||
private void ShowclickAni(object a = null)
|
||
{
|
||
if (CreatFlower.instance.slot_type_list.Contains(type))
|
||
{
|
||
if (light == null) return;
|
||
light.SetActive(true);
|
||
}
|
||
DOVirtual.DelayedCall(3, () =>
|
||
{
|
||
HideclickAni();
|
||
});
|
||
|
||
}
|
||
private void HideclickAni(object a = null)
|
||
{
|
||
if (light == null) return;
|
||
light.SetActive(false);
|
||
}
|
||
public void OnChildClicked()
|
||
{
|
||
parentManager.OnChildClicked(this);
|
||
// Destroy(gameObject);
|
||
Debug.Log($"{name} 执行了 DoSomething 方法");
|
||
transform.GetComponent<Collider2D>().enabled = false;
|
||
|
||
Vector3 euler = transform.eulerAngles;
|
||
euler.z = 0f;
|
||
transform.eulerAngles = euler;
|
||
|
||
|
||
SpriteRenderer sprite_render = transform.GetComponent<SpriteRenderer>();
|
||
sprite_render.maskInteraction = SpriteMaskInteraction.None;
|
||
|
||
GameObject top_area = GameObject.Find("top_area");
|
||
|
||
SetParentAndFly(top_area.transform, CreatFlower.instance.AddFlowerInTemp(gameObject, type), 0.5f);
|
||
|
||
CreatFlower.instance.CheckPot();
|
||
|
||
|
||
}
|
||
void OnDestroy()
|
||
{
|
||
GameDispatcher.Instance.RemoveListener(GameMsg.FlowerShowclickAni, ShowclickAni);
|
||
GameDispatcher.Instance.RemoveListener(GameMsg.FlowerHideclickAni, HideclickAni);
|
||
}
|
||
|
||
|
||
public GameObject starPrefab; // 拖尾星星的预制体(SpriteRenderer)
|
||
public float fadeDuration = 0.5f; // 星星淡出时间
|
||
|
||
private bool isTrailing = false;
|
||
|
||
private int frameCounter = 0;
|
||
|
||
void Update()
|
||
{
|
||
if (!isTrailing) return;
|
||
|
||
frameCounter++;
|
||
|
||
// 每隔 1 帧生成一次(即隔帧调用)
|
||
if (frameCounter % 2 == 0)
|
||
{
|
||
GameObject star = Instantiate(starPrefab, transform.position, Quaternion.identity);
|
||
star.transform.SetParent(null);
|
||
|
||
SpriteRenderer sr = star.GetComponent<SpriteRenderer>();
|
||
if (sr != null)
|
||
{
|
||
sr.DOFade(0.2f, fadeDuration).OnComplete(() =>
|
||
{
|
||
Destroy(star);
|
||
});
|
||
}
|
||
else
|
||
{
|
||
Destroy(star, fadeDuration);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public void SetParentAndFly(Transform newParent, Transform targetChild, float duration)
|
||
{
|
||
if (newParent == null || targetChild == null)
|
||
{
|
||
Debug.LogError("父物体或目标子物体为空!");
|
||
return;
|
||
}
|
||
|
||
transform.SetParent(newParent);
|
||
|
||
// 开启拖尾
|
||
isTrailing = true;
|
||
|
||
|
||
// 移动和旋转
|
||
DG.Tweening.Sequence seq = DOTween.Sequence();
|
||
seq.Join(transform.DOMove(targetChild.position, duration).SetEase(Ease.InOutQuad));
|
||
seq.Join(transform.DORotateQuaternion(targetChild.rotation, duration).SetEase(Ease.InOutQuad));
|
||
|
||
// 动画完成后关闭拖尾
|
||
seq.OnComplete(() =>
|
||
{
|
||
isTrailing = false;
|
||
});
|
||
}
|
||
|
||
|
||
}
|