215 lines
5.8 KiB
C#
215 lines
5.8 KiB
C#
using System;
|
|
using System.Linq;
|
|
using AibisDream.FixSystem;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using Framework.Core;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class GearSystem : SystemHasData
|
|
{
|
|
#region 索引
|
|
|
|
[SerializeField] private GameObject gearPrefab;
|
|
[SerializeField] private GameObject shaftPrefab;
|
|
private Shaft[] _shafts;
|
|
|
|
#endregion
|
|
|
|
private ShaftGraph _shaftGraph;
|
|
|
|
private void Awake()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
Enable();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
Disable();
|
|
}
|
|
|
|
#region 生命周期管理
|
|
|
|
public void Init()
|
|
{
|
|
FixSystemCenter.SystemDic.Register(this);
|
|
this.FindComponents();
|
|
_shafts = GetComponentsInChildren<Shaft>();
|
|
}
|
|
|
|
public void Enable()
|
|
{
|
|
_shaftGraph = new ShaftGraph(_shafts);
|
|
}
|
|
|
|
public void Disable()
|
|
{
|
|
_shaftGraph = null;
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 判断吸附位置
|
|
/// </summary>
|
|
/// <param name="pos">齿轮当前位置</param>
|
|
/// <param name="gearRadius">齿轮半径</param>
|
|
/// <returns>能够被吸附的轴</returns>
|
|
public Shaft CheckClosestShaft(Vector3 pos, float gearRadius)
|
|
{
|
|
return _shaftGraph.QuerySnapShaft(pos, gearRadius);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断齿轮能不能放进轴内
|
|
/// </summary>
|
|
/// <param name="sourceShaft">当前轴</param>
|
|
/// <param name="gearRadius">齿轮半径</param>
|
|
/// <returns>是否可用</returns>
|
|
public bool CheckGearSuitable(Shaft sourceShaft, float gearRadius)
|
|
{
|
|
return _shaftGraph.CheckGearSuitable(sourceShaft, gearRadius);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成齿轮
|
|
/// </summary>
|
|
/// <param name="pos">位置</param>
|
|
/// <param name="radius">半径</param>
|
|
/// <param name="sprite">齿轮图片</param>
|
|
/// <param name="speed">速度</param>
|
|
/// <returns>齿轮Obj</returns>
|
|
public GameObject InitGear(Vector3 pos, float radius, Sprite sprite)
|
|
{
|
|
GameObject gearObj = Instantiate(gearPrefab, pos, Quaternion.identity);
|
|
gearObj.transform.parent = transform.Find("Gears").transform;
|
|
Gear gear = gearObj.GetComponent<Gear>();
|
|
gear.radius = radius;
|
|
gear.gearSprite = sprite;
|
|
return gearObj;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 某个轴上齿轮发生变化时更新临接关系
|
|
/// </summary>
|
|
/// <param name="shaftId">轴ID</param>
|
|
public void ChangeGraphVertex(int shaftId)
|
|
{
|
|
_shaftGraph?.ChangeVertex(shaftId);
|
|
}
|
|
|
|
public void Succeed()
|
|
{
|
|
Debug.Log("达成目标");
|
|
}
|
|
|
|
#region 数据保存与加载
|
|
|
|
public static string DataPath = Application.streamingAssetsPath + "/LevelData/GearSystem/";
|
|
private string[] _keyOptions;
|
|
public override string CurDataKey { get; set; }
|
|
|
|
public override string[] GetDataKeyOptions()
|
|
{
|
|
return _keyOptions ??= ConfigUtil.Instance.GetFileNames(DataPath);
|
|
}
|
|
|
|
public override void Load()
|
|
{
|
|
if (string.IsNullOrEmpty(CurDataKey))
|
|
{
|
|
Debug.Log("配置项名称为空");
|
|
return;
|
|
}
|
|
|
|
if (!ConfigUtil.Instance.TryLoadConfig<GearSystemData>(DataPath + CurDataKey, out var data))
|
|
{
|
|
Debug.Log("没有找到配置项");
|
|
return;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
// 现将当前配置保存为temp避免丢失
|
|
if (CurDataKey != "temp.json")
|
|
{
|
|
SaveByKey("temp.json");
|
|
}
|
|
#endif
|
|
|
|
LoadByData(data);
|
|
}
|
|
|
|
private void LoadByData(GearSystemData gearSystemData)
|
|
{
|
|
// 先清除
|
|
var shaftRoot = transform.Find("Shafts");
|
|
var shaftsObj = shaftRoot.GetComponentsInChildren<Shaft>();
|
|
foreach (var oldShaft in shaftsObj)
|
|
{
|
|
#if UNITY_EDITOR
|
|
DestroyImmediate(oldShaft.gameObject);
|
|
#else
|
|
Destory(oldShaft.gameObject);
|
|
#endif
|
|
}
|
|
|
|
// 再添加
|
|
foreach (var shaftData in gearSystemData.shaftLevelDataArray)
|
|
{
|
|
var newModule = Instantiate(shaftPrefab, shaftRoot).GetComponent<Shaft>();
|
|
newModule.Load(shaftData);
|
|
}
|
|
}
|
|
|
|
public override void Save()
|
|
{
|
|
if (string.IsNullOrEmpty(CurDataKey)) return;
|
|
|
|
SaveByKey(CurDataKey);
|
|
if (!_keyOptions.Contains(CurDataKey))
|
|
{
|
|
// 重新读取key
|
|
_keyOptions = ConfigUtil.Instance.GetFileNames(DataPath);
|
|
}
|
|
}
|
|
|
|
private void SaveByKey(string key)
|
|
{
|
|
var shaftsObj = transform.Find("Shafts").GetComponentsInChildren<Shaft>();
|
|
var shaftDataArray = new ShaftLevelData[shaftsObj.Length];
|
|
|
|
for (int i = 0; i < shaftDataArray.Length; i++)
|
|
{
|
|
shaftDataArray[i] = shaftsObj[i].Save();
|
|
}
|
|
|
|
var data = new GearSystemData
|
|
{
|
|
shaftLevelDataArray = shaftDataArray
|
|
};
|
|
|
|
ConfigUtil.Instance.SaveConfig(data, DataPath + key);
|
|
}
|
|
|
|
public void AddShaft()
|
|
{
|
|
var shaftRoot = transform.Find("Shafts");
|
|
Instantiate(shaftPrefab, shaftRoot);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
[Serializable]
|
|
public struct GearSystemData
|
|
{
|
|
public ShaftLevelData[] shaftLevelDataArray;
|
|
}
|
|
} |