330 lines
12 KiB
C#
330 lines
12 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using DG.Tweening;
|
||
using DragonLi.Core;
|
||
using UnityEngine;
|
||
using Random = UnityEngine.Random;
|
||
|
||
public enum TaskBoxType
|
||
{
|
||
TaskStartBox,
|
||
TaskEndBox,
|
||
}
|
||
|
||
public class TaskBox : MonoBehaviour
|
||
{
|
||
public Animator boxAnimator;
|
||
|
||
public GameObject[] effectObjs;
|
||
|
||
public TaskBoxType taskBoxType;
|
||
|
||
public Transform lockPos;
|
||
|
||
[Header("References")]
|
||
public ParticleSystem explosionEffect; // 宝箱爆炸烟雾粒子
|
||
public Transform pileCenter; // 堆叠中心点
|
||
|
||
[Header("Gem Pile Settings")]
|
||
public int gemCount = 20;
|
||
public float pileRadius = 1f;
|
||
public float pileHeight = 1f;
|
||
public float spawnDelayBetween = 0.05f; // 宝石生成间隔
|
||
|
||
public int id;
|
||
|
||
public bool isOpen = false;
|
||
|
||
public void Start()
|
||
{
|
||
isOpen = false;
|
||
foreach (var obj in effectObjs)
|
||
{
|
||
obj.SetActive(false);
|
||
}
|
||
}
|
||
|
||
public void Init(int curId,TaskBoxType boxType)
|
||
{
|
||
id = curId;
|
||
taskBoxType = boxType;
|
||
if (boxType == TaskBoxType.TaskStartBox)
|
||
{
|
||
effectObjs[id].SetActive(true);
|
||
StartCoroutine(ShowBox());
|
||
}
|
||
else if (boxType == TaskBoxType.TaskEndBox)
|
||
{
|
||
transform.localScale = Vector3.one*1.5f;
|
||
transform.eulerAngles = new Vector3(0, 180, 0);
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
if(!isOpen)
|
||
GameManager.Ins.playerRightHand.MoveGemKey(lockPos,GetKey);
|
||
},12f);
|
||
}
|
||
|
||
}
|
||
|
||
IEnumerator ShowBox()
|
||
{
|
||
transform.DOMoveY(0, 1f).OnComplete(() =>
|
||
{
|
||
GameManager.Ins.PlaySound3DRPC("1.27",transform);
|
||
});
|
||
yield return new WaitForSeconds(1);
|
||
float duration = 0.6f; // 总持续时间
|
||
float shrinkScale = 0.7f; // 最小缩放比例
|
||
float expandScale = 1.2f; // 最大缩放比例
|
||
transform.localScale = Vector3.one; // 确保初始是原始大小
|
||
Sequence seq = DOTween.Sequence();
|
||
seq.Append(transform.DOScale(shrinkScale, duration * 0.3f).SetEase(Ease.InQuad)); // 缩小
|
||
seq.Append(transform.DOScale(expandScale, duration * 0.4f).SetEase(Ease.OutBack)); // 放大到略大于原始
|
||
seq.Append(transform.DOScale(1f, duration * 0.3f).SetEase(Ease.OutElastic)); // 回弹到正常大小
|
||
yield return new WaitForSeconds(duration);
|
||
GameManager.Ins.PlaySound3DRPC("1.28",transform);
|
||
boxAnimator.SetBool("isOpen",true);
|
||
yield return new WaitForSeconds(2f);
|
||
StartCoroutine(ExplosionRoutine());
|
||
}
|
||
|
||
private IEnumerator ExplosionRoutine()
|
||
{
|
||
explosionEffect.gameObject.SetActive(true);
|
||
// 1. 播放爆炸烟雾
|
||
if (explosionEffect != null)
|
||
explosionEffect.Play();
|
||
|
||
// 2. 隐藏宝箱模型
|
||
boxAnimator.gameObject.SetActive(false);
|
||
|
||
// 3. 等待烟雾效果(可根据粒子时长调整)
|
||
float waitTime = explosionEffect != null ? explosionEffect.main.duration : 0.5f;
|
||
yield return new WaitForSeconds(0.1f);
|
||
switch (GameManager.Ins.taskManager.curTaskId)
|
||
{
|
||
case 0:
|
||
StartCoroutine(CreateGemPile());
|
||
//CreateGemPile();
|
||
break;
|
||
case 1:
|
||
//创建宝石泡泡堆
|
||
SpawnBubbles();
|
||
break;
|
||
case 2:
|
||
CreateCarrot();
|
||
break;
|
||
}
|
||
}
|
||
|
||
public Vector3 spawnCenter = Vector3.zero; // 生成中心点(相对于Spawner的本地坐标)
|
||
private Vector3 spawnRange = new Vector3(); // 在 X/Y/Z 方向的随机范围
|
||
|
||
|
||
//生成宝石堆
|
||
IEnumerator CreateGemPile()
|
||
{
|
||
for (int i = 0; i < gemCount; i++)
|
||
{
|
||
// 计算堆叠位置(圆锥形堆)
|
||
float angle = Random.Range(0f, Mathf.PI * 2f);
|
||
float radius = Random.Range(0.1f, pileRadius);
|
||
Vector3 targetPos = pileCenter.position.ReflectVectorXOZ() + new Vector3(
|
||
Mathf.Cos(angle) * radius,
|
||
0,
|
||
Mathf.Sin(angle) * radius
|
||
);
|
||
|
||
// 实例化并初始化
|
||
GameManager.Ins.taskManager.CreateGem(0, targetPos,i);
|
||
yield return new WaitForSeconds(0.1f);
|
||
}
|
||
Destroy(gameObject);
|
||
GameManager.Ins.PlaySound2DRPC("1.4");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成泡泡
|
||
/// </summary>
|
||
public void SpawnBubbles()
|
||
{
|
||
GameManager.Ins.PlaySound2DRPC("1.7");
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
GameManager.Ins.PlaySound2DRPC("1.8");
|
||
},6f);
|
||
for (int i = 0; i < gemCount; i++)
|
||
{
|
||
|
||
if (GameLocal.Ins.place == Place.Nanjing_Qixia_Yaohuamen_Jindiguangchang )
|
||
{
|
||
spawnRange = new Vector3(1.5f, 1f, 2f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Shandong_Jinan_Huaiyin_ShengfutongShangmao
|
||
||GameLocal.Ins.place == Place.Hunan_Jishou_Qianzhou_Tianhong)
|
||
{
|
||
spawnRange = new Vector3(1f, 0.5f, 1f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Shandong_Jinan_Huaiyin_ShengfutongShangmao_wai
|
||
||GameLocal.Ins.place == Place.Anhui_Suzhou_Yueshan_Guchengshangyejie_Shinei
|
||
|
||
|
||
)
|
||
{
|
||
spawnRange = new Vector3(2f, 1f, 2f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Wulanhaote_Wanda_Shangchang)
|
||
{
|
||
spawnRange = new Vector3(2f, 1f, 1.8f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Xinjiang_Yili_Yining_Wanrong
|
||
||GameLocal.Ins.place == Place.Jiangsu_Xvzhou_Guolou_Oulebao_2
|
||
||GameLocal.Ins.place == Place.Chongqing_Yuzhong_Hongyadong_Xiakexing_shiwan
|
||
|| GameLocal.Ins.place == Place.Zhejiang_Wenzhou_Cangnan_Yintaicheng
|
||
|| GameLocal.Ins.place == Place.Jilin_Changchun_Chaoyang_OuyaMaichang
|
||
)
|
||
{
|
||
spawnRange = new Vector3(1.7f, 1f, 1.7f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Yunnan_Lincang_Linxiang_Hengji_Dixia)
|
||
{
|
||
spawnRange = new Vector3(0.2f, 0.5f, 0.2f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Guangxi_Guilin_Gongcheng_Shijixincheng
|
||
||GameLocal.Ins.place == Place.Guangdong_Shenzhen_Guangming_Wanda)
|
||
{
|
||
spawnRange = new Vector3(1.5f, 1f, 1.5f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Gansu_Jinchang_Jinchuan_Shijiguangchang
|
||
)
|
||
{
|
||
spawnRange = new Vector3(1.5f, 1f, 1.5f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Henan_Xinxiang_Wandaguangchang_Shinei||GameLocal.Ins.place == Place.Anhui_Suzhou_Yueshan_Guchengshangyejie_2)
|
||
{
|
||
spawnRange = new Vector3(0.5f, 1f, 0.5f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Jiangsu_Xvzhou_Fengxian_Wuyueguangchang
|
||
|| GameLocal.Ins.place == Place.Hebei_Tangshan_Qianan_Tianyuangu
|
||
|| GameLocal.Ins.place == Place.Shandong_Jining_Shangchang_3
|
||
|
||
|
||
)
|
||
{
|
||
spawnRange = new Vector3(2f, 1f, 2f);
|
||
}
|
||
if(GameLocal.Ins.place == Place.Henan_Xinxiang_Wandaguangchang
|
||
|| GameLocal.Ins.place == Place.Hebei_Tangshan_Qianan_Tianyuangu
|
||
|| GameLocal.Ins.place == Place.Hunan_Hengyang_Zhuhui_Dongzhoudao_nei
|
||
|| GameLocal.Ins.place == Place.Jiangsu_Xvzhou_Suning_Guangchang
|
||
|| GameLocal.Ins.place == Place.Jiangsu_Xvzhou_Guolou_Oulebao
|
||
|| GameLocal.Ins.place == Place.Chongqing_Yuzhong_Hongyadong_Xiakexing
|
||
|| GameLocal.Ins.place == Place.Hebei_Hengshui_Xinji_WandaGuangchang
|
||
|| GameLocal.Ins.place == Place.Ningxia_Yinchuan_Jinfeng_XinhualianGuangchang
|
||
|| GameLocal.Ins.place == Place.Hebei_Hengshui_Taocheng_WandaGuangchang
|
||
|| GameLocal.Ins.place == Place.Shandong_Weifang_Linqu_WandaGuangchang
|
||
|| GameLocal.Ins.place == Place.Nanjing_Pukou_Longhutianjie
|
||
)
|
||
{
|
||
spawnRange = new Vector3(1f, 1f, 1f);
|
||
}
|
||
if (GameLocal.Ins.place == Place.Hunan_Zhuzhou_Wanda_Shennongcheng_Chaowanshe)
|
||
{
|
||
spawnRange = new Vector3(1f, 1f, 1f);
|
||
|
||
}
|
||
else
|
||
{
|
||
spawnRange = new Vector3(3f, 1f, 3f);
|
||
}
|
||
// 计算一个随机位置
|
||
Vector3 randomOffset = new Vector3(
|
||
Random.Range(-spawnRange.x, spawnRange.x),
|
||
Random.Range(1, 3),
|
||
////修改山东济南槐荫晟芙桐场景
|
||
//Random.Range(0.5f, 1),
|
||
Random.Range(-spawnRange.z, spawnRange.z)
|
||
);
|
||
Vector3 spawnPos = transform.position.ReflectVectorXOZ() + randomOffset;
|
||
|
||
// 实例化,并让它成为Spawner的子物体(方便清理/组织)
|
||
GameObject bubble = GameManager.Ins.taskManager.CreateGem(1, spawnPos,i);
|
||
// 添加浮动脚本,并随机化参数
|
||
BubblesGem bf = bubble.GetComponent<BubblesGem>();
|
||
bf.amplitude = Random.Range(0.3f, 1.2f);
|
||
bf.speed = Random.Range(0.5f, 1.5f);
|
||
bf.phaseOffset = Random.Range(0f, Mathf.PI * 2f);
|
||
}
|
||
GameManager.Ins.PlaySound2DRPC("1.33");
|
||
|
||
Destroy(gameObject);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成胡萝卜
|
||
/// </summary>
|
||
public void CreateCarrot()
|
||
{
|
||
GameManager.Ins.PlaySound2DRPC("1.11");
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
GameManager.Ins.PlaySound2DRPC("1.12");
|
||
},6f);
|
||
var carrotGemTask = GameManager.Ins.taskManager.tasks[2].GetComponent<CarrotGemTask>();
|
||
GameManager.Ins.taskManager.task3Tu.SetActive(true);
|
||
for (int i = 0; i < carrotGemTask.carrotPosList.Count; i++)
|
||
{
|
||
var pos= carrotGemTask.carrotPosList[i].transform;
|
||
if (GameLocal.Ins.place == Place.Yunnan_Lincang_Linxiang_Hengji_Dixia)
|
||
{
|
||
GameManager.Ins.taskManager.CreateGem(2, pos.position, i);
|
||
}
|
||
else
|
||
GameManager.Ins.taskManager.CreateGem(2, pos.position.ReflectVectorXOZ (), i);
|
||
}
|
||
}
|
||
|
||
IEnumerator CreatePuzzle()
|
||
{
|
||
yield return new WaitForSeconds(1f);
|
||
boxAnimator.SetBool("isOpen",true);
|
||
yield return new WaitForSeconds(2f);
|
||
explosionEffect.gameObject.SetActive(true);
|
||
// 1. 播放爆炸烟雾
|
||
if (explosionEffect != null)
|
||
explosionEffect.Play();
|
||
// 2. 隐藏宝箱模型
|
||
boxAnimator.gameObject.SetActive(false);
|
||
yield return new WaitForSeconds(1f);
|
||
GameManager.Ins.taskManager.StartTask();
|
||
Destroy(gameObject);
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (taskBoxType == TaskBoxType.TaskEndBox&&GameManager.Ins.playerRightHand.gemKeyUp!=null)
|
||
{
|
||
var dir = Vector3.Distance(GameManager.Ins.playerRightHand.gemKeyUp.position, lockPos.position);
|
||
if (dir < 0.1f)
|
||
{
|
||
GetKey();
|
||
}
|
||
}
|
||
}
|
||
|
||
public void GetKey()
|
||
{
|
||
if(isOpen)
|
||
return;
|
||
GameManager.Ins.playerRightHand.ChangeHand();
|
||
var key = GameManager.Ins.playerRightHand.gemKey;
|
||
_gemKey= GameManager.Ins.CreateGemKey(new Vector3(0,0.2f,0.3f),new Vector3(0,90,90),transform);
|
||
GameManager.Ins.PlaySound2DRPC("1.39");
|
||
StartCoroutine(CreatePuzzle());
|
||
Destroy(key);
|
||
isOpen = true;
|
||
}
|
||
private GameObject _gemKey;
|
||
}
|