262 lines
8.5 KiB
C#
262 lines
8.5 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using asap.core;
|
||
using DataCenter;
|
||
using UnityEngine;
|
||
|
||
namespace EventPartnerGather.Mining
|
||
{
|
||
/// <summary>
|
||
/// 事件合作伙伴挖矿点管理器
|
||
/// 管理所有挖矿点的位置,提供按顺序获取挖矿点的功能
|
||
///
|
||
/// 建议挂载在:MiningPoints容器对象上
|
||
///
|
||
/// 场景结构建议:
|
||
/// MiningPoints (挂载此脚本)
|
||
/// ├── MiningPoint1 (挖矿点1)
|
||
/// ├── MiningPoint2 (挖矿点2)
|
||
/// ├── MiningPoint3 (挖矿点3)
|
||
/// └── ... (更多挖矿点)
|
||
///
|
||
/// 使用说明:
|
||
/// - 将所有挖矿点的Transform拖拽到points数组中
|
||
/// - 或者脚本会自动获取所有子Transform作为挖矿点
|
||
/// </summary>
|
||
public class EventPartnerMiningPointManager : MonoBehaviour
|
||
{
|
||
[Header("挖矿点配置")]
|
||
[Tooltip("挖矿点Transform数组 - 手动拖拽或自动获取子Transform")]
|
||
public List<Transform> points = new();
|
||
|
||
[Header("奖杯点配置")]
|
||
public Transform trophyPoints;
|
||
public float trophyAniDuration = 2.2f;
|
||
|
||
|
||
[Header("矿石间隔 x方向" )]
|
||
[SerializeField]
|
||
public float cubeWidthX = 1.11f;
|
||
|
||
[Header("矿石间隔 z方向" )]
|
||
[SerializeField]
|
||
public float cubeWidthZ = 1.12f;
|
||
|
||
|
||
[Header("矿石Prefab")]
|
||
[Tooltip("挖矿点矿石Prefab")]
|
||
public GameObject[] miningResourcePrefabs;
|
||
|
||
|
||
|
||
[Header("调试信息")]
|
||
[Tooltip("当前挖矿点索引")]
|
||
[SerializeField] private int _index = 0;
|
||
|
||
private EventPartnerGatherManager _manager;
|
||
|
||
private void Awake()
|
||
{
|
||
_manager = GContext.container.Resolve<EventPartnerGatherManager>();
|
||
// 如果没有手动设置挖矿点,自动获取子Transform
|
||
if (points == null || points.Count == 0)
|
||
{
|
||
AutoCollectMiningPoints();
|
||
}
|
||
|
||
ValidateMiningPoints();
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
InitGenerateOreGroups();
|
||
if (trophyPoints)
|
||
{
|
||
var finished = trophyPoints?.Find("audio_finished");
|
||
finished?.gameObject.SetActive(false);
|
||
|
||
var trophyAnim = trophyPoints?.GetComponent<Animation>();
|
||
if (trophyAnim)
|
||
{
|
||
trophyAnim.GoToFrame(0);
|
||
}
|
||
// P_miningtrophy
|
||
//
|
||
}
|
||
|
||
}
|
||
|
||
private void OnEnable()
|
||
{
|
||
var finished = trophyPoints?.Find("audio_finished");
|
||
finished?.gameObject.SetActive(false);
|
||
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
var finished = trophyPoints?.Find("audio_finished");
|
||
finished?.gameObject.SetActive(false);
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public void InitGenerateOreGroups()
|
||
{
|
||
// ELog("InitGenerateOreGroups");
|
||
var stagePoints = _manager.EventPartnerMineMainData.StagePoint;
|
||
var num = points.Count;
|
||
var minePoint = _manager.EventPartnerMineMainData.MinePoint;
|
||
var minePointShow = _manager.EventPartnerMineMainData.MinePointShow;
|
||
for (var i = 0; i < num; ++i)
|
||
{
|
||
var oreNum = stagePoints[i] / minePoint;
|
||
// ELog($"InitOreGroup stageIndex={i} oreNum={oreNum}");
|
||
var orePerCol = minePointShow[i];
|
||
var oreRoot = points[i].Find("ores");
|
||
var orePoint = points[i].Find("ore_point");
|
||
var zeroPos = orePoint.position;
|
||
var halfColNum = orePerCol / 2;
|
||
if (orePerCol % 2 == 1) // 奇数
|
||
{
|
||
zeroPos.z -= halfColNum * (cubeWidthZ);
|
||
}
|
||
else
|
||
{
|
||
zeroPos.z -= (halfColNum - 0.5f)* cubeWidthZ ;
|
||
}
|
||
|
||
for (var n = 0; n < oreNum; ++n)
|
||
{
|
||
var prefab = miningResourcePrefabs[i];
|
||
var ore = Instantiate(prefab,oreRoot);
|
||
ore.name = $"ore_{i}_{n}";
|
||
ore.transform.position = CalcPosition(zeroPos,n / orePerCol, n % orePerCol);
|
||
}
|
||
}
|
||
return;
|
||
Vector3 CalcPosition(Vector3 zeroPos,int col, int row)
|
||
{
|
||
// ELog($"CalcPosition zeroPos={zeroPos} col={col} row={row}");
|
||
return new Vector3(zeroPos.x + col * cubeWidthX, zeroPos.y, zeroPos.z + row * cubeWidthZ);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 自动收集所有子Transform作为挖矿点
|
||
/// </summary>
|
||
private void AutoCollectMiningPoints()
|
||
{
|
||
points = new List<Transform>();
|
||
|
||
for (int i = 0; i < transform.childCount; i++)
|
||
{
|
||
var child = transform.GetChild(i);
|
||
if (child.gameObject.activeInHierarchy && child.gameObject.name.StartsWith("ore"))
|
||
{
|
||
points.Add(child);
|
||
}
|
||
}
|
||
|
||
ELog($"AutoCollectMiningPoints count={points.Count}");
|
||
}
|
||
/// <summary>
|
||
/// 验证挖矿点配置
|
||
/// </summary>
|
||
private void ValidateMiningPoints()
|
||
{
|
||
if (points == null || points.Count == 0)
|
||
{
|
||
LogWarning("ValidateMiningPointsFailed reason=NoPoints");
|
||
return;
|
||
}
|
||
|
||
// 移除空引用
|
||
for (int i = points.Count - 1; i >= 0; i--)
|
||
{
|
||
if (!points[i])
|
||
{
|
||
points.RemoveAt(i);
|
||
LogWarning($"RemoveNullMiningPoint index={i}");
|
||
}
|
||
}
|
||
ELog($"ValidateMiningPointsDone validCount={points.Count}");
|
||
}
|
||
public Transform GetNextPoint()
|
||
{
|
||
if (points == null || points.Count == 0) return null;
|
||
var t = points[_index % points.Count];
|
||
_index++;
|
||
return t;
|
||
}
|
||
//
|
||
public Transform GetPoint(int curIndex)
|
||
{
|
||
_index = curIndex;
|
||
if (points == null || points.Count == 0) return null;
|
||
var t = points[curIndex % points.Count];
|
||
return t;
|
||
}
|
||
|
||
public Transform GetTrophyPoint()
|
||
{
|
||
return trophyPoints;
|
||
}
|
||
public void ResetPoints()
|
||
{
|
||
_index = 0;
|
||
var num = points.Count;
|
||
const string defaultOreName = "rock1";
|
||
for (int i = 0; i < num; i++)
|
||
{
|
||
var oreRoot = points[i].Find("ores");
|
||
foreach (Transform ore in oreRoot)
|
||
{
|
||
ore.gameObject.SetActive(true);
|
||
var oreController = ore.gameObject.GetComponent<OreController>();
|
||
if (oreController)
|
||
{
|
||
Destroy(oreController);
|
||
}
|
||
foreach (var child in ore.Cast<Transform>())
|
||
{
|
||
var childName = child.gameObject.name;
|
||
child.gameObject.SetActive(childName.Equals(defaultOreName));
|
||
}
|
||
}
|
||
}
|
||
|
||
if (trophyPoints)
|
||
{
|
||
var finished = trophyPoints?.Find("audio_finished");
|
||
finished?.gameObject.SetActive(false);
|
||
var trophyAnim = trophyPoints?.GetComponent<Animation>();
|
||
if (trophyAnim)
|
||
{
|
||
trophyAnim.GoToFrame(0);
|
||
}
|
||
var fxCelebrate = trophyPoints.Find("fx_partnermining_celebrate");
|
||
if (fxCelebrate)
|
||
{
|
||
fxCelebrate.gameObject.SetActive(false);
|
||
}
|
||
}
|
||
}
|
||
private static void ELog(object t)
|
||
{
|
||
#if UNITY_EDITOR
|
||
Debug.Log($"<color=cyan>EventPartnerMiningPointManager-> {t} </color>");
|
||
#endif
|
||
}
|
||
|
||
private static void LogWarning(object t)
|
||
{
|
||
Debug.LogWarning($"<color=yellow>EventPartnerMiningPointManager-> {t} </color>");
|
||
}
|
||
|
||
//
|
||
|
||
}
|
||
}
|