321 lines
10 KiB
C#
321 lines
10 KiB
C#
using System;
|
|
using AibisDream.Framework;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using System.Collections;
|
|
using DG.Tweening;
|
|
using AibisDream.Kit;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class BodyModule : MonoBehaviour, ISocket
|
|
{
|
|
#region 组件引用
|
|
|
|
private const string SocketObjName = "Socket";
|
|
|
|
private Transform _socketPos;
|
|
private Transform _socketLeft;
|
|
private Transform _socketRight;
|
|
private Tween _socketTween;
|
|
|
|
private BodyModuleSystem _bodyModuleSystem;
|
|
private Material _highlightMaterial;
|
|
private Material _alarmMaterial;
|
|
private Material _originalMaterial; // 原材质
|
|
|
|
private SpriteRenderer _targetSpriteRenderer; // 目标的 SpriteRenderer
|
|
|
|
private SpriteRenderer _cutSpriteRenderer;
|
|
private SpriteRenderer _cutTextRenderer; // 添加cutText的SpriteRenderer引用
|
|
|
|
private CutLine _cutLine;
|
|
|
|
[Header("CutText闪烁效果")] private float cutTextFlickerThreshold = 0.4f; // 开始闪烁的阈值
|
|
private float cutTextFlickerInterval = 0.1f; // 闪烁间隔
|
|
private float cutTextFlickerTimer = 0f; // 闪烁计时器
|
|
private bool isCutTextFlickering = false; // 是否正在闪烁
|
|
private Coroutine flickerCoroutine; // 闪烁协程引用
|
|
|
|
#endregion
|
|
|
|
// 模块基本数据
|
|
[SerializeField] public BodyModuleData data;
|
|
public bool available = true;
|
|
private bool _cutting;
|
|
private bool _isOpen;
|
|
|
|
private void Awake()
|
|
{
|
|
// 获取组件索引
|
|
InitReference();
|
|
}
|
|
|
|
private void InitReference()
|
|
{
|
|
_socketPos = transform.Find(SocketObjName);
|
|
_socketLeft = _socketPos.Find("Left");
|
|
_socketRight = _socketPos.Find("Right");
|
|
|
|
_bodyModuleSystem = transform.parent.parent.GetComponent<BodyModuleSystem>();
|
|
|
|
_targetSpriteRenderer = transform.Find("Module Pic").GetComponent<SpriteRenderer>();
|
|
_originalMaterial = _targetSpriteRenderer.GetComponent<SpriteRenderer>().material;
|
|
|
|
_cutSpriteRenderer = transform.Find("Cut Pic")?.GetComponent<SpriteRenderer>();
|
|
if (_cutSpriteRenderer != null)
|
|
{
|
|
_cutTextRenderer =
|
|
_cutSpriteRenderer.transform.Find("Cut Text")
|
|
?.GetComponent<SpriteRenderer>(); // 初始化cutText的SpriteRenderer
|
|
}
|
|
|
|
_highlightMaterial = Resources.Load<Material>("Materials/HighlightMaterial");
|
|
_alarmMaterial = Resources.Load<Material>("Materials/AlarmMaterial");
|
|
}
|
|
|
|
#region 插槽功能
|
|
|
|
public bool IsAvailable()
|
|
{
|
|
return available;
|
|
}
|
|
|
|
public void SetSocketAvailable(bool isAvailable)
|
|
{
|
|
available = isAvailable;
|
|
}
|
|
|
|
public virtual void PlugIn()
|
|
{
|
|
if (DialogController.Instance)
|
|
// 然后触发Yarn节点
|
|
DialogController.Instance.StartDialogNode(_bodyModuleSystem.inHotErr ? "系统发热" : data.PlugInNodeName);
|
|
}
|
|
|
|
public virtual void PlugOut()
|
|
{
|
|
}
|
|
|
|
public Vector3 GetSocketPos()
|
|
{
|
|
return _socketPos.position;
|
|
}
|
|
|
|
public void OpenPlugSlot()
|
|
{
|
|
if (!_socketLeft) return;
|
|
|
|
if (_isOpen) return;
|
|
_isOpen = true;
|
|
// 打开插槽
|
|
if (_socketTween.IsActive() && _socketTween.IsPlaying()) _socketTween.Kill(true);
|
|
var sequence = DOTween.Sequence();
|
|
sequence.Join(_socketLeft.DOScale(new Vector3(0, 1, 1), 0.2f).SetEase(Ease.InSine));
|
|
sequence.Join(_socketRight.DOScale(new Vector3(0, 1, 1), 0.2f).SetEase(Ease.InSine));
|
|
_socketTween = sequence;
|
|
}
|
|
|
|
public void ClosePlugSlot()
|
|
{
|
|
if (!_socketLeft) return;
|
|
if (!_isOpen) return;
|
|
|
|
_isOpen = false;
|
|
// 关闭插槽
|
|
if (_socketTween.IsActive() && _socketTween.IsPlaying()) _socketTween.Kill(true);
|
|
var sequence = DOTween.Sequence();
|
|
sequence.Join(_socketLeft.DOScale(new Vector3(1, 1, 1), 0.2f).SetEase(Ease.OutSine));
|
|
sequence.Join(_socketRight.DOScale(new Vector3(1, 1, 1), 0.2f).SetEase(Ease.OutSine));
|
|
_socketTween = sequence;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 高亮提示和隐藏
|
|
|
|
public void Highlight(bool enable)
|
|
{
|
|
if (_targetSpriteRenderer != null && _highlightMaterial != null &&
|
|
_targetSpriteRenderer.material != _alarmMaterial)
|
|
{
|
|
_targetSpriteRenderer.material = enable ? _highlightMaterial : _originalMaterial;
|
|
}
|
|
}
|
|
|
|
public void Alarm(bool enable)
|
|
{
|
|
if (_targetSpriteRenderer != null && _alarmMaterial != null)
|
|
{
|
|
_targetSpriteRenderer.material = enable ? _alarmMaterial : _originalMaterial;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 切割相关
|
|
|
|
public void OnCuttingDown()
|
|
{
|
|
_bodyModuleSystem.RemoveModule(this);
|
|
|
|
DialogController.Instance?.StartDialogNode($"{data.moduleName}CutDown");
|
|
EnumEventSystem.Global.Send(EventEnum.CutEnd);
|
|
Destroy(gameObject);
|
|
DialogController.Instance?.StartDialogNode($"{data.moduleName}CutDown");
|
|
EnumEventSystem.Global.Send(EventEnum.CutEnd);
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
public void SetCutTextVisible(bool visible)
|
|
{
|
|
if (_cutTextRenderer != null)
|
|
{
|
|
_cutTextRenderer.enabled = visible;
|
|
}
|
|
}
|
|
|
|
public SpriteRenderer GetCutSpriteRenderer()
|
|
{
|
|
return _cutSpriteRenderer;
|
|
}
|
|
public SpriteRenderer GetCutTextSpriteRenderer()
|
|
{
|
|
return _cutTextRenderer;
|
|
}
|
|
|
|
public void StartCutTextFlicker()
|
|
{
|
|
if (!isCutTextFlickering)
|
|
{
|
|
isCutTextFlickering = true;
|
|
if (flickerCoroutine != null)
|
|
{
|
|
StopCoroutine(flickerCoroutine);
|
|
}
|
|
|
|
flickerCoroutine = StartCoroutine(CutTextFlickerCoroutine());
|
|
}
|
|
}
|
|
|
|
public void StopCutTextFlicker()
|
|
{
|
|
isCutTextFlickering = false;
|
|
if (flickerCoroutine != null)
|
|
{
|
|
StopCoroutine(flickerCoroutine);
|
|
flickerCoroutine = null;
|
|
}
|
|
|
|
// 确保最终状态是可见的
|
|
SetCutTextVisible(true);
|
|
}
|
|
|
|
private IEnumerator CutTextFlickerCoroutine()
|
|
{
|
|
while (isCutTextFlickering)
|
|
{
|
|
// 随机决定是否显示
|
|
bool shouldShow = UnityEngine.Random.value > 0.5f;
|
|
SetCutTextVisible(shouldShow);
|
|
|
|
// 播放相应的音效
|
|
if (shouldShow)
|
|
{
|
|
AudioManager.Instance.PlaySfx("event:/ActionFB/light_flicker_on");
|
|
}
|
|
else
|
|
{
|
|
AudioManager.Instance.PlaySfx("event:/ActionFB/light_flicker_off");
|
|
}
|
|
|
|
// 随机等待时间,模拟接触不良效果
|
|
float waitTime = UnityEngine.Random.Range(cutTextFlickerInterval * 0.5f, cutTextFlickerInterval * 1.5f);
|
|
yield return new WaitForSeconds(waitTime);
|
|
}
|
|
}
|
|
|
|
public void UpdateCutProgress(float progress)
|
|
{
|
|
// 检查是否达到闪烁阈值
|
|
if (!isCutTextFlickering && progress >= cutTextFlickerThreshold)
|
|
{
|
|
StartCutTextFlicker();
|
|
}
|
|
}
|
|
|
|
// public void EnableCut()
|
|
// {
|
|
// // 打开或获取CutLine
|
|
// ShowCutLine();
|
|
// _cutLine.OnCuttingDown += OnCuttingDown;
|
|
// _cutting = true;
|
|
|
|
// EnumEventSystem.Global.Send(EventEnum.CutStart);
|
|
// }
|
|
|
|
// private void OnCut()
|
|
// {
|
|
// Vector2 mousePos = CommonUtil.GetMouseWorldPos(Input.mousePosition);
|
|
// _cutLine.DetectLineClick(mousePos, clickRadius);
|
|
// }
|
|
|
|
// private void OnCuttingDown()
|
|
// {
|
|
// _cutLine.OnCuttingDown -= OnCuttingDown;
|
|
// _cutting = false;
|
|
// // 模块脱落
|
|
// _cutLine.HideLine();
|
|
// _bodyModuleSystem.RemoveModule(this);
|
|
// ActionKit.Sequence()
|
|
// .Delay(0.8f)
|
|
// .DOTween(() => transform.DOMoveY(ConstRef.TweenEndY,
|
|
// (transform.position.y - ConstRef.TweenEndY) / (ConstRef.FallSpeed * 0.6f))
|
|
// .SetEase(Ease.InQuad))
|
|
// .Callback(() => DialogController.Instance?.StartDialogNode($"{data.moduleName}CutDown"))
|
|
// .Callback(() => EnumEventSystem.Global.Send(EventEnum.CutEnd))
|
|
// .Start(this, () => Destroy(gameObject));
|
|
// }
|
|
|
|
// private void ShowCutLine()
|
|
// {
|
|
// // 切割线不存在,就新建一个
|
|
// if (!_cutLine)
|
|
// {
|
|
// var prefab = ResourceKit.LoadAssetSync<GameObject>(ConstRef.CutLinePrefabName);
|
|
// var cutLineObj = Instantiate(prefab, transform);
|
|
// _cutLine = cutLineObj.GetComponent<CutLine>();
|
|
// }
|
|
|
|
// // 对切割线进行初始化
|
|
// var shapePoints = new List<Vector2>();
|
|
// _targetSpriteRenderer.sprite.GetPhysicsShape(0, shapePoints);
|
|
// if (shapePoints.Count <= 0)
|
|
// {
|
|
// Debug.LogWarning($"{name} has no shape");
|
|
// return;
|
|
// }
|
|
|
|
// // 本地坐标转世界坐标
|
|
// var shapePointsV3 = shapePoints
|
|
// .Select(item => _targetSpriteRenderer.transform.TransformPoint(item))
|
|
// .ToList();
|
|
|
|
// _cutLine.Init(shapePointsV3, clickRadius);
|
|
// }
|
|
|
|
#endregion
|
|
}
|
|
|
|
[Serializable]
|
|
public struct BodyModuleData
|
|
{
|
|
public const string PlugInNodeTemplate = "{0}PlugIn";
|
|
|
|
// ID
|
|
public string moduleName;
|
|
|
|
// 只读
|
|
[JsonIgnore] public string PlugInNodeName => string.Format(PlugInNodeTemplate, moduleName);
|
|
}
|
|
} |