303 lines
8.2 KiB
C#
303 lines
8.2 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using DarkTonic.MasterAudio;
|
||
using DG.Tweening;
|
||
using DragonLi.Core;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class TreeTask : MonoBehaviour
|
||
{
|
||
public static TreeTask Ins;
|
||
[Header("核心物体")]
|
||
public Canvas painting; // 画卷整体
|
||
public GameObject rope; // 绳子
|
||
public Transform bronzeTree; // 青铜神树(用于脉冲)
|
||
|
||
|
||
[Header("UI")]
|
||
//public CanvasGroup paintingUI;
|
||
//public Canvas maskUICanvas; // 两张面具UI父物体
|
||
public TMP_Text tipText;
|
||
public Image leftIcon;
|
||
public Image rightIcon;
|
||
|
||
[Header("资源")]
|
||
public Sprite[] leftSprites;//左侧图片
|
||
public Sprite[] rightSprites;//右侧图片
|
||
|
||
|
||
public RectTransform[] leftCorrectPos;
|
||
public RectTransform[] rightCorrectPos;
|
||
|
||
public Image correctUiPre;
|
||
public Image wrongUiPre;
|
||
|
||
public ScrollOpenUI scrollOpenUI;
|
||
|
||
[Header("音效")]
|
||
[SoundGroup]
|
||
public string pullRopeClip;//拉绳子
|
||
[SoundGroup]
|
||
public string showPaintingClip;//画卷展开音效
|
||
[SoundGroup]
|
||
public string correctClip;//正确点击音效
|
||
[SoundGroup]
|
||
public string wrongClip;//点击错误音效
|
||
[SoundGroup]
|
||
public string voiceClip;// 语音讲解
|
||
|
||
[Header("参数")]
|
||
public float maxTime = 120f;
|
||
private float timer;
|
||
private bool isSolved;
|
||
|
||
private int _curTaskId;
|
||
private bool _isWait;//等待
|
||
private int _overIndex;//完成次数
|
||
private Dictionary<int,bool> _posDic=new Dictionary<int, bool>();
|
||
private void Start()
|
||
{
|
||
Ins=this;
|
||
painting.gameObject.SetActive(false);
|
||
//paintingUI.alpha = 0;
|
||
_curTaskId = 0;
|
||
for (int i = 0; i < 9; i++)
|
||
{
|
||
_posDic.Add(i, false);
|
||
}
|
||
|
||
for (int i = 0; i < 9; i++)
|
||
{
|
||
if(leftCorrectPos[i].GetComponent<TreeTaskPoint>()!=null)
|
||
leftCorrectPos[i].GetComponent<TreeTaskPoint>().id = i;
|
||
if(rightCorrectPos[i].GetComponent<TreeTaskPoint>()!=null)
|
||
rightCorrectPos[i].GetComponent<TreeTaskPoint>().id = i;
|
||
}
|
||
}
|
||
|
||
// ✅ 任务开始(玩家进入区域调用)
|
||
public void StartTask()
|
||
{
|
||
timer = maxTime;
|
||
GameManager.Ins.PlaySound3DRPC(pullRopeClip,transform);
|
||
rope.SetActive(true);
|
||
tipText.text = "拉动青铜神树垂下的绳索……";
|
||
GameManager.Ins.SetTaskRunning(true);
|
||
painting.gameObject.SetActive(true);
|
||
GameManager.Ins.SetHandState(true);
|
||
GameManager.Ins.PlaySound3DRPC(voiceClip,transform);
|
||
rope.transform.DOShakePosition(1f, 0.02f, 20).OnComplete(() =>
|
||
{
|
||
ShowPainting();
|
||
});
|
||
// painting.transform.localScale = Vector3.zero;
|
||
//
|
||
// painting.transform
|
||
// .DOScale(Vector3.one, 0.6f)
|
||
// .SetEase(Ease.OutBack)
|
||
// .OnComplete(() =>
|
||
// {
|
||
// // ✅ 播放语音
|
||
// GameManager.Ins.PlaySound3DRPC(voiceClip,transform);;
|
||
// // ✅ 显示面具解密UI
|
||
// if (maskUICanvas)
|
||
// maskUICanvas.gameObject.SetActive(true);
|
||
// });
|
||
}
|
||
|
||
// ✅ 玩家拽绳子时调用(你手部交互调用这个)
|
||
public void PullRope()
|
||
{
|
||
|
||
// ✅ 绳子抖动动画
|
||
rope.transform.DOShakePosition(1f, 0.02f, 20).OnComplete(() =>
|
||
{
|
||
ShowPainting();
|
||
});
|
||
}
|
||
|
||
// ✅ 画卷展开
|
||
private void ShowPainting()
|
||
{
|
||
painting.gameObject.SetActive(true);
|
||
//paintingUI.DOFade(1, 0.5f);
|
||
|
||
tipText.text = "找出两幅青铜面具中不属于三星堆工艺的地方(3处)";
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
_isWait = true;
|
||
RefreshUI();
|
||
GameManager.Ins.PlaySound3DRPC(showPaintingClip,transform);
|
||
scrollOpenUI.Open(() =>
|
||
{
|
||
_isWait = false;
|
||
StartCoroutine(TimeCountDown());
|
||
});
|
||
},2f);
|
||
}
|
||
|
||
//刷新画卷
|
||
public void RefreshUI()
|
||
{
|
||
_overIndex = 0;
|
||
leftIcon.sprite = leftSprites[_curTaskId];
|
||
rightIcon.sprite = rightSprites[_curTaskId];
|
||
}
|
||
|
||
public Vector2 SpawnUIAtHit(Vector3 hit)
|
||
{
|
||
// 1. 世界坐标 → 屏幕坐标
|
||
Vector2 screenPos =
|
||
RectTransformUtility.WorldToScreenPoint(
|
||
Camera.main,
|
||
hit
|
||
);
|
||
|
||
// 2. 屏幕坐标 → Canvas 本地坐标
|
||
RectTransform canvasRect = painting.transform as RectTransform;
|
||
|
||
Vector2 localPos;
|
||
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
||
canvasRect,
|
||
screenPos,
|
||
painting.worldCamera,
|
||
out localPos
|
||
);
|
||
|
||
return localPos;
|
||
}
|
||
|
||
|
||
public void CheckPoint(Vector3 hitPoint,GameObject target)
|
||
{
|
||
if (_isWait || isSolved)
|
||
return;
|
||
|
||
_isWait = true;
|
||
TreeTaskPoint taskPoint = target.GetComponent<TreeTaskPoint>();
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
if (taskPoint!=null&&taskPoint.id==(3 * _curTaskId + i) && !_posDic[3 * _curTaskId + i])
|
||
{
|
||
_posDic[3 * _curTaskId + i] = true;
|
||
OnCorrectClick(3 * _curTaskId + i);
|
||
return;
|
||
}
|
||
}
|
||
|
||
OnWrongClick(hitPoint);
|
||
}
|
||
|
||
// ✅ 正确点击
|
||
public void OnCorrectClick(int posId)
|
||
{
|
||
_overIndex++;
|
||
GameManager.Ins.PlaySound3DRPC(correctClip,transform);
|
||
// ✅ 青铜神树脉冲
|
||
bronzeTree.DOPunchScale(Vector3.one * 0.15f, 0.4f);
|
||
|
||
// ✅ 正确圈中提示
|
||
EffectManager.Ins.ShowCorrectCircle(leftCorrectPos[posId]);
|
||
EffectManager.Ins.ShowCorrectCircle(rightCorrectPos[posId]);
|
||
tipText.text = "这是后期仿制的细节!";
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
_isWait = false;
|
||
},1f);
|
||
CheckFinish();
|
||
}
|
||
|
||
// ✅ 错误点击
|
||
public void OnWrongClick(Vector3 hitPoint)
|
||
{
|
||
Vector2 uiPos = SpawnUIAtHit(hitPoint);
|
||
|
||
GameManager.Ins.PlaySound3DRPC(wrongClip, transform);
|
||
|
||
EffectManager.Ins.ShowWrongEffect(
|
||
uiPos, // ✅ 直接传 UI 坐标
|
||
painting.transform
|
||
);
|
||
|
||
tipText.text = "这个位置是正确的青铜工艺。";
|
||
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
_isWait = false;
|
||
}, 1.5f);
|
||
}
|
||
|
||
|
||
// ✅ 判定是否完成
|
||
private void CheckFinish()
|
||
{
|
||
|
||
if (_overIndex >= 3)
|
||
{
|
||
_curTaskId++;
|
||
_isWait = true;
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
EffectManager.Ins.CloseEffectList();
|
||
scrollOpenUI.Close(() =>
|
||
{
|
||
if (_curTaskId>=3)
|
||
{
|
||
TaskSuccess();
|
||
return;
|
||
}
|
||
RefreshUI();
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
_isWait = false;
|
||
scrollOpenUI.Open(null);
|
||
},1f);
|
||
});
|
||
},2f);
|
||
}
|
||
}
|
||
|
||
// ✅ 成功
|
||
private void TaskSuccess()
|
||
{
|
||
isSolved = true;
|
||
|
||
tipText.text = "你成功识破了伪造青铜工艺!获得【鉴伪大师】徽章";
|
||
|
||
GameManager.Ins.FinishTreeTask(true);
|
||
|
||
HideAll();
|
||
}
|
||
|
||
// ✅ 超时失败
|
||
private void TaskFail()
|
||
{
|
||
tipText.text = "时间到了,画卷自动封印……";
|
||
GameManager.Ins.FinishTreeTask(false);
|
||
HideAll();
|
||
}
|
||
|
||
// ✅ 收回画卷 & 绳子
|
||
private void HideAll()
|
||
{
|
||
|
||
}
|
||
|
||
// ✅ 90 秒计时
|
||
IEnumerator TimeCountDown()
|
||
{
|
||
while (timer > 0)
|
||
{
|
||
timer -= Time.deltaTime;
|
||
yield return null;
|
||
}
|
||
|
||
if (!isSolved)
|
||
{
|
||
TaskFail();
|
||
}
|
||
}
|
||
}
|