Files
MRArcadia/Assets/_Arcadia/Scripts/Item/PeachTree.cs

239 lines
6.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 桃树管理器
/// </summary>
public class PeachTree : MonoBehaviour
{
[Header("桃子设置")]
[SerializeField] private List<Peach> peaches = new List<Peach>(); // 树上的所有桃子
[SerializeField] private int glowingPeachCount = 1; // 发光桃子数量默认1个
[Header("重置设置")]
[SerializeField] private bool autoRespawn = false; // 是否自动重生桃子
[SerializeField] private float respawnDelay = 3f; // 重生延迟时间
[SerializeField] private int maxRespawnCount = 3; // 最大重生次数(-1为无限
private int respawnCount = 0;
private List<Vector3> originalPeachPositions = new List<Vector3>();
private List<Quaternion> originalPeachRotations = new List<Quaternion>();
private void Start()
{
// 记录所有桃子的初始位置和旋转
SaveOriginalPeachStates();
// 随机选择发光桃子
SelectGlowingPeaches();
}
/// <summary>
/// 保存桃子的初始状态
/// </summary>
private void SaveOriginalPeachStates()
{
originalPeachPositions.Clear();
originalPeachRotations.Clear();
foreach (Peach peach in peaches)
{
if (peach != null)
{
originalPeachPositions.Add(peach.transform.position);
originalPeachRotations.Add(peach.transform.rotation);
}
}
}
/// <summary>
/// 随机选择发光桃子
/// </summary>
public void SelectGlowingPeaches()
{
// 先关闭所有桃子的发光
foreach (Peach peach in peaches)
{
if (peach != null)
{
peach.SetGlowing(false);
}
}
// 检查桃子数量
if (peaches.Count == 0)
{
Debug.LogWarning("桃树上没有桃子!");
return;
}
// 检查发光桃子数量
int actualGlowingCount = Mathf.Min(glowingPeachCount, peaches.Count);
// 随机打乱桃子列表
List<Peach> shuffledPeaches = new List<Peach>(peaches);
for (int i = 0; i < shuffledPeaches.Count; i++)
{
Peach temp = shuffledPeaches[i];
int randomIndex = Random.Range(i, shuffledPeaches.Count);
shuffledPeaches[i] = shuffledPeaches[randomIndex];
shuffledPeaches[randomIndex] = temp;
}
// 选择前actualGlowingCount个桃子设为发光
for (int i = 0; i < actualGlowingCount; i++)
{
if (shuffledPeaches[i] != null)
{
shuffledPeaches[i].SetGlowing(true);
Debug.Log($"桃子 {i + 1} 设为发光桃子");
}
}
Debug.Log($"已选择 {actualGlowingCount} 个发光桃子");
}
/// <summary>
/// 检查是否所有桃子都被收集
/// </summary>
public bool AreAllPeachesCollected()
{
int collectedCount = 0;
foreach (Peach peach in peaches)
{
if (peach == null)
{
collectedCount++;
}
}
return collectedCount >= peaches.Count;
}
/// <summary>
/// 获取剩余桃子数量
/// </summary>
public int GetRemainingPeachCount()
{
int count = 0;
foreach (Peach peach in peaches)
{
if (peach != null)
{
count++;
}
}
return count;
}
/// <summary>
/// 重生所有桃子
/// </summary>
public void RespawnAllPeaches()
{
StartCoroutine(RespawnPeachesCoroutine());
}
/// <summary>
/// 重生桃子协程
/// </summary>
private IEnumerator RespawnPeachesCoroutine()
{
// 检查是否达到最大重生次数
if (maxRespawnCount >= 0 && respawnCount >= maxRespawnCount)
{
Debug.Log("已达到最大重生次数,不再重生桃子");
yield break;
}
// 等待重生延迟
yield return new WaitForSeconds(respawnDelay);
// 清理所有桃子引用
peaches.Clear();
// 重新生成桃子
for (int i = 0; i < originalPeachPositions.Count; i++)
{
// 这里应该从预制体生成桃子
// 但由于我们没有桃子预制体的引用,所以只是记录位置
Debug.Log($"需要重生桃子到位置: {originalPeachPositions[i]}");
}
respawnCount++;
Debug.Log($"桃子重生完成,当前重生次数: {respawnCount}");
// 如果设置了自动重生,继续重生
if (autoRespawn)
{
// 等待所有桃子被收集后再重生
while (!AreAllPeachesCollected())
{
yield return new WaitForSeconds(1f);
}
// 递归调用重生
StartCoroutine(RespawnPeachesCoroutine());
}
}
/// <summary>
/// 设置所有桃子的硬币预制体
/// </summary>
public void SetAllPeachesCoinPrefab(GameObject coinPrefab)
{
foreach (Peach peach in peaches)
{
if (peach != null)
{
peach.SetCoinPrefab(coinPrefab);
}
}
}
/// <summary>
/// 添加桃子到列表
/// </summary>
public void AddPeach(Peach peach)
{
if (peach != null && !peaches.Contains(peach))
{
peaches.Add(peach);
}
}
/// <summary>
/// 移除桃子从列表
/// </summary>
public void RemovePeach(Peach peach)
{
if (peach != null)
{
peaches.Remove(peach);
}
}
/// <summary>
/// 获取所有桃子
/// </summary>
public List<Peach> GetPeaches()
{
return new List<Peach>(peaches);
}
private void OnDrawGizmosSelected()
{
// 在Scene视图中显示桃子位置
if (peaches != null)
{
Gizmos.color = Color.green;
foreach (Peach peach in peaches)
{
if (peach != null)
{
Gizmos.DrawWireSphere(peach.transform.position, 0.1f);
}
}
}
}
}