Main/Assets/Code/Logic/SkillSelectFiled/SkillSelectFiledManager.cs
2025-01-25 04:38:09 +08:00

363 lines
14 KiB
C#

using Thousandto.Code.Center;
using Thousandto.Code.Logic.WarningField;
using Thousandto.Core.Asset;
using Thousandto.Core.Base;
using Thousandto.Plugins.Common.UniScene;
using System.Collections.Generic;
using UnityEngine;
namespace Thousandto.Code.Logic
{
public class SkillSelectFiledManager
{
#region//静态变量和常量
private const string RootName = "[SkillSelectFiledRoot]";
private static Transform _root = null;
private static GameObject _rootGo = null;
private static GameObject _rectRes = null;
private static GameObject _bigRoundRes = null;
private static GameObject _smalRoundRes = null;
private static GameObject _fanShapedCenterRes = null;
private static GameObject _fanShapedLeftRes = null;
private static GameObject _fanShapedRightRes = null;
private static bool _isLoadRes = false;
private const float _roundRadious = 0.1f;
private const float _rectWidth = 0.1f;
private const float _rectHeight = 0.1f;
private const float _fanShapeRadious = 0.1f;
private const float _fanShapeAngle = 30f;
#endregion
#region//私有变量
private List<GameObject> _instGoList = new List<GameObject>();
private List<Renderer> _renderList = new List<Renderer>();
private Transform _handleTrans = null;
private SkillSelectFiledType _filedType = SkillSelectFiledType.SelectDir;
private Color _curColor = Color.white;
#endregion
#region//属性
public static Transform Root
{
get
{
return _root;
}
}
#endregion
#region//构造函数
static SkillSelectFiledManager()
{
_rootGo = new GameObject(RootName);
_root = _rootGo.transform;
_root.parent = AppRoot.Transform;
//GameObject.DontDestroyOnLoad(_rootGo);
}
#endregion
#region//公有函数
//初始化处理
public void Initialize()
{
}
//卸载处理
public void Uninitialize()
{
}
/// <summary>
///显示选择位置效果
/// </summary>
/// 大圈size
/// <param name="bigSize"></param>
/// 小圈size
/// <param name="smallSize"></param>
public void ShowSelectPosFiled(float bigSize, float smallSize)
{
DestoryFiled();
//尝试加载资源,防止资源未加载
CheckRes();
if (_bigRoundRes == null || _smalRoundRes == null)
return;
_filedType = SkillSelectFiledType.SelectPos;
var newGo = new GameObject("[SelectPosFiled]");
newGo.transform.parent = Root;
newGo.transform.localPosition = Vector3.zero;
newGo.transform.localScale = Vector3.one;
newGo.transform.localRotation = Quaternion.identity;
//外圈
var bigRoundGo = GameObject.Instantiate(_bigRoundRes) as GameObject;
bigRoundGo.transform.parent = newGo.transform;
bigRoundGo.transform.localPosition = Vector3.zero;
bigRoundGo.transform.localScale = new Vector3(bigSize / _roundRadious, 1f, bigSize / _roundRadious);
bigRoundGo.transform.localRotation = Quaternion.identity;
bigRoundGo.SetActive(true);
//内圈
var smallRoundGo = GameObject.Instantiate(_smalRoundRes) as GameObject;
smallRoundGo.transform.parent = newGo.transform;
smallRoundGo.transform.localPosition = Vector3.zero;
smallRoundGo.transform.localScale = new Vector3(smallSize / _roundRadious, 1f, smallSize / _roundRadious);
smallRoundGo.transform.localRotation = Quaternion.identity;
smallRoundGo.SetActive(true);
//控制小的位置
_handleTrans = smallRoundGo.transform;
_instGoList.Add(smallRoundGo);
_instGoList.Add(bigRoundGo);
_instGoList.Add(newGo);
var renderArray = newGo.transform.GetComponentsInChildren<Renderer>();
_renderList.Clear();
if (renderArray.Length > 0)
{
for(int i=0;i< renderArray.Length;++i)
{
renderArray[i].sharedMaterial.SetColor(ShaderPropertyIDDefine.Color, Color.white);
_renderList.Add(renderArray[i]);
}
}
_curColor = Color.white;
UnityUtils.SetLayer(newGo.transform, LayerUtils.LocalPlayer, true);
}
/// <summary>
///显示选择方向效果
/// </summary>
/// 方向效果类型,分为矩形和扇形
/// <param name="type"></param>
/// 矩形代表高,扇形代表半径
/// <param name="param1"></param>
/// 矩形代表宽,扇形代表角度
/// <param name="param2"></param>
public void ShowSelectDirFiled(SkillSelectDirType type, float param1, float param2)
{
DestoryFiled();
//尝试加载资源,防止资源未加载
CheckRes();
if (_rectRes == null || _fanShapedCenterRes == null || _fanShapedLeftRes == null || _fanShapedRightRes == null)
return;
_filedType = SkillSelectFiledType.SelectDir;
var newGo = new GameObject("[SelectDirFiled]");
newGo.transform.parent = Root;
newGo.transform.localPosition = Vector3.zero;
newGo.transform.localScale = Vector3.one;
newGo.transform.localRotation = Quaternion.identity;
//外圈
var bigRoundGo = GameObject.Instantiate(_bigRoundRes) as GameObject;
bigRoundGo.transform.parent = newGo.transform;
bigRoundGo.transform.localPosition = Vector3.zero;
bigRoundGo.transform.localScale = new Vector3(param1 / _roundRadious, 1f, param1 / _roundRadious);
bigRoundGo.transform.localRotation = Quaternion.identity;
bigRoundGo.SetActive(true);
switch (type)
{
case SkillSelectDirType.Rect:
{
var rectGo = GameObject.Instantiate(_rectRes) as GameObject;
rectGo.transform.parent = newGo.transform;
rectGo.transform.localPosition = Vector3.zero;
rectGo.transform.localScale = new Vector3(param2 / _rectWidth, 1f, param1 / _rectHeight);
rectGo.transform.localRotation = Quaternion.identity;
rectGo.SetActive(true);
_instGoList.Add(rectGo);
_handleTrans = rectGo.transform;
}
break;
case SkillSelectDirType.FanShaped:
{
var angle = param2;
var fcount = angle / _fanShapeAngle;
if (fcount % 1 != 0)
{
fcount += 1;
}
int count = (int)fcount - 2;
var rootGo = new GameObject("[FanShanRoot]");
rootGo.transform.parent = newGo.transform;
UnityUtils.Reset(rootGo.transform);
rootGo.transform.localScale = new Vector3(param1 / _fanShapeRadious, 1f, param1 / _fanShapeRadious);
_handleTrans = rootGo.transform;
var leftEulerY = -(angle - _fanShapeAngle) / 2;
var leftGo = GameObject.Instantiate(_fanShapedLeftRes) as GameObject;
leftGo.transform.parent = rootGo.transform;
UnityUtils.Reset(leftGo.transform);
leftGo.transform.localEulerAngles = new Vector3(0f, leftEulerY, 0f);
_instGoList.Add(leftGo);
leftGo.SetActive(true);
var rightGo = GameObject.Instantiate(_fanShapedRightRes) as GameObject;
rightGo.transform.parent = rootGo.transform;
UnityUtils.Reset(rightGo.transform);
rightGo.transform.localEulerAngles = new Vector3(0f, (angle - _fanShapeAngle) / 2, 0f);
_instGoList.Add(rightGo);
rightGo.SetActive(true);
angle -= 60f;
for (int i = 0; i < count; ++i)
{
var centerGo = GameObject.Instantiate(_fanShapedCenterRes) as GameObject;
centerGo.transform.parent = rootGo.transform;
UnityUtils.Reset(centerGo.transform);
centerGo.transform.localEulerAngles = new Vector3(0f, leftEulerY + (i + 1) * _fanShapeAngle, 0f);
_instGoList.Add(centerGo);
centerGo.SetActive(true);
}
_instGoList.Add(rootGo);
}
break;
}
_instGoList.Add(newGo);
var renderArray = newGo.transform.GetComponentsInChildren<Renderer>();
_renderList.Clear();
if (renderArray.Length > 0)
{
for (int i = 0; i < renderArray.Length; ++i)
{
renderArray[i].sharedMaterial.SetColor(ShaderPropertyIDDefine.Color, Color.white);
_renderList.Add(renderArray[i]);
}
}
_curColor = Color.white;
UnityUtils.SetLayer(newGo.transform, LayerUtils.LocalPlayer, true);
}
public void SetPosAndDir(Vector3 pos, Vector3 dir, Color color)
{
if (_handleTrans == null)
return;
switch (_filedType)
{
case SkillSelectFiledType.SelectDir:
_handleTrans.forward = dir;
break;
case SkillSelectFiledType.SelectPos:
_handleTrans.position = pos;
break;
}
if(_curColor != color)
{
_curColor = color;
for (int i = 0; i < _renderList.Count; ++i)
{
_renderList[i].sharedMaterial.SetColor(ShaderPropertyIDDefine.Color, _curColor);
}
}
}
public void UpdatePos(Vector3 pos)
{
pos.y += 0.2f;
Root.position = pos;
}
//删除警示圈
public void DestoryFiled()
{
for (int i = 0; i < _instGoList.Count; ++i)
{
GameObject.DestroyImmediate(_instGoList[i]);
}
_instGoList.Clear();
_renderList.Clear();
_handleTrans = null;
}
#endregion
#region//私有函数
private void CheckRes()
{
if (_isLoadRes)
return;
_isLoadRes = true;
if (_rectRes == null)
{
var vfx = new FGameObjectVFX(ModelTypeCode.OtherVFX, 911, true, false);
vfx.OnLoadFinishedCallBack = (fgo) =>
{
if (fgo != null)
{
_rectRes = fgo.RealGameObject;
//GameObject.DontDestroyOnLoad(_rectRes);
}
};
}
if (_bigRoundRes == null)
{
var vfx = new FGameObjectVFX(ModelTypeCode.OtherVFX, 912, true, false);
vfx.OnLoadFinishedCallBack = (fgo) =>
{
if (fgo != null)
{
_bigRoundRes = fgo.RealGameObject;
//GameObject.DontDestroyOnLoad(_bigRoundRes);
}
};
}
if (_smalRoundRes == null)
{
var vfx = new FGameObjectVFX(ModelTypeCode.OtherVFX, 913, true, false);
vfx.OnLoadFinishedCallBack = (fgo) =>
{
if (fgo != null)
{
_smalRoundRes = fgo.RealGameObject;
//GameObject.DontDestroyOnLoad(_smalRoundRes);
}
};
}
if (_fanShapedCenterRes == null)
{
var vfx = new FGameObjectVFX(ModelTypeCode.OtherVFX, 914, true, false);
vfx.OnLoadFinishedCallBack = (fgo) =>
{
if (fgo != null)
{
_fanShapedCenterRes = fgo.RealGameObject;
//GameObject.DontDestroyOnLoad(_fanShapedCenterRes);
}
};
}
if (_fanShapedLeftRes == null)
{
var vfx = new FGameObjectVFX(ModelTypeCode.OtherVFX, 915, true, false);
vfx.OnLoadFinishedCallBack = (fgo) =>
{
if (fgo != null)
{
_fanShapedLeftRes = fgo.RealGameObject;
//GameObject.DontDestroyOnLoad(_fanShapedLeftRes);
}
};
}
if (_fanShapedRightRes == null)
{
var vfx = new FGameObjectVFX(ModelTypeCode.OtherVFX, 916, true, false);
vfx.OnLoadFinishedCallBack = (fgo) =>
{
if (fgo != null)
{
_fanShapedRightRes = fgo.RealGameObject;
//GameObject.DontDestroyOnLoad(_fanShapedRightRes);
}
};
}
}
#endregion
}
}