基本齐全了
This commit is contained in:
@@ -14,17 +14,22 @@ public class ShockSystem : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
[SerializeField] private Transform shockDevice; // 电击器
|
||||
[SerializeField] private Transform leftShockDevice; // 左电击装置
|
||||
[SerializeField] private Transform rightShockDevice; // 右电击装置
|
||||
[SerializeField] private Transform operationPanel; // 操作面板
|
||||
[SerializeField] private SliderTransition powerSlider; // 力度滑块
|
||||
[SerializeField] private ProgressBarGridLinear progressBar; // 进度条
|
||||
[SerializeField] private Transform sliderBar; // 推杆的杆部分
|
||||
[SerializeField] private Transform lever; // 转动杆
|
||||
[SerializeField] private SpriteRenderer flashHeadSpriteRenderer; // 头部闪烁精灵渲染器
|
||||
[SerializeField] private SpriteRenderer flashBackgroundSpriteRenderer; // 背景闪烁精灵渲染器
|
||||
[SerializeField] private SpriteRenderer flashSpriteRenderer;
|
||||
[SerializeField] private List<Sprite> flashHeadSprites; // 头部闪烁精灵列表
|
||||
[SerializeField] private SpriteFlashSystem spriteFlashSystem; // Sprite闪烁系统
|
||||
[Header("Settings")]
|
||||
[SerializeField] private Vector3 shockDeviceTargetPos; // 电击器目标位置
|
||||
[SerializeField] private bool isSystemOn = false; // 系统开关状态
|
||||
[SerializeField] private Vector3 leftShockDeviceTargetPos; // 左电击装置目标位置
|
||||
[SerializeField] private Vector3 rightShockDeviceTargetPos; // 右电击装置目标位置
|
||||
[SerializeField] private Vector3 leftShockDeviceStartPos; // 左电击装置起始位置
|
||||
[SerializeField] private Vector3 rightShockDeviceStartPos; // 右电击装置起始位置
|
||||
[SerializeField] private Vector3 operationPanelTargetPos; // 操作面板目标位置
|
||||
[SerializeField] private float moveDuration = 1f; // 移动动画时长
|
||||
[SerializeField] private float shockDelay = 0.5f; // 释放电击延迟
|
||||
@@ -49,15 +54,24 @@ public class ShockSystem : MonoBehaviour
|
||||
[SerializeField] private float autoChargeSpeed = 5f; // 自动充能速度
|
||||
[SerializeField] private float autoChargeDuration = 1f; // 自动充能持续时间
|
||||
|
||||
[Header("Lever Settings")]
|
||||
[SerializeField] private float maxLeverAngle = 90f; // 最大转动角度
|
||||
[SerializeField] private float leverReturnSpeed = 300f; // 杆回弹速度
|
||||
[SerializeField] private float leverSmoothTime = 0.1f; // 杆转动平滑时间
|
||||
|
||||
private bool isCharging = false;
|
||||
private float currentProgress = 0f;
|
||||
private Slider baseSlider; // 基础Slider组件
|
||||
private float currentLeverAngle = 0f; // 当前杆的角度
|
||||
private float targetLeverAngle = 0f; // 目标杆的角度
|
||||
private float leverVelocity = 0f; // 用于平滑转动
|
||||
private bool isDraggingLever = false; // 是否正在拖动杆
|
||||
private Vector3 lastMousePosition; // 上一帧鼠标位置
|
||||
private Tweener progressTweener; // 用于存储进度条动画
|
||||
private Sequence flashSequence; // 用于存储闪烁动画序列
|
||||
private int currentFlashHeadIndex = 0; // 当前闪烁头部精灵索引
|
||||
private Tweener sliderReturnTweener; // 用于存储滑块回弹动画
|
||||
private float lastVibrateTime = 0f; // 上次震动时间
|
||||
private Vector3 deviceOriginalPos; // 电击器原始位置
|
||||
private Vector3 leftDeviceOriginalPos; // 左电击装置原始位置
|
||||
private Vector3 rightDeviceOriginalPos; // 右电击装置原始位置
|
||||
private Tweener deviceShakeTweener; // 电击器震动动画
|
||||
|
||||
// 公共访问器
|
||||
@@ -75,51 +89,102 @@ public class ShockSystem : MonoBehaviour
|
||||
private void Start()
|
||||
{
|
||||
// 保存电击器原始位置
|
||||
if (shockDevice != null)
|
||||
if (leftShockDevice != null)
|
||||
{
|
||||
deviceOriginalPos = shockDevice.localPosition;
|
||||
leftDeviceOriginalPos = leftShockDevice.localPosition;
|
||||
}
|
||||
if (rightShockDevice != null)
|
||||
{
|
||||
rightDeviceOriginalPos = rightShockDevice.localPosition;
|
||||
}
|
||||
|
||||
// 获取基础Slider组件
|
||||
baseSlider = powerSlider.GetComponent<Slider>();
|
||||
if (baseSlider != null)
|
||||
// 初始化杆的位置
|
||||
if (lever != null)
|
||||
{
|
||||
baseSlider.onValueChanged.AddListener(OnSliderValueChanged);
|
||||
lever.localRotation = Quaternion.Euler(0, 0, 0);
|
||||
}
|
||||
|
||||
// 根据系统状态初始化灯光
|
||||
SetSystemPower(isSystemOn);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleLeverInput();
|
||||
UpdateLeverRotation();
|
||||
UpdateCharging();
|
||||
}
|
||||
|
||||
private void HandleLeverInput()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
// 检查是否点击到杆
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
|
||||
|
||||
// 添加EventTrigger组件
|
||||
EventTrigger trigger = baseSlider.gameObject.GetComponent<EventTrigger>();
|
||||
if (trigger == null)
|
||||
if (hit.collider != null && hit.collider.transform == lever)
|
||||
{
|
||||
trigger = baseSlider.gameObject.AddComponent<EventTrigger>();
|
||||
isDraggingLever = true;
|
||||
lastMousePosition = Input.mousePosition;
|
||||
}
|
||||
|
||||
// 添加结束拖拽事件
|
||||
EventTrigger.Entry entry = new EventTrigger.Entry();
|
||||
entry.eventID = EventTriggerType.EndDrag;
|
||||
entry.callback.AddListener((data) => { OnSliderEndDrag(); });
|
||||
trigger.triggers.Add(entry);
|
||||
}
|
||||
|
||||
// 初始化闪烁精灵
|
||||
if (flashHeadSpriteRenderer != null)
|
||||
else if (Input.GetMouseButtonUp(0))
|
||||
{
|
||||
flashHeadSpriteRenderer.gameObject.SetActive(false);
|
||||
Color color = flashHeadSpriteRenderer.color;
|
||||
color.a = 0;
|
||||
flashHeadSpriteRenderer.color = color;
|
||||
isDraggingLever = false;
|
||||
// 停止充能
|
||||
if (isCharging)
|
||||
{
|
||||
isCharging = false;
|
||||
if (currentProgress >= progressBar.MaxValue)
|
||||
{
|
||||
DialogController.Instance.StartDialogNode(shockEffectNode);
|
||||
}
|
||||
ResetProgress();
|
||||
}
|
||||
}
|
||||
|
||||
if (flashBackgroundSpriteRenderer != null)
|
||||
if (isDraggingLever)
|
||||
{
|
||||
flashBackgroundSpriteRenderer.gameObject.SetActive(false);
|
||||
Color color = flashBackgroundSpriteRenderer.color;
|
||||
color.a = 0;
|
||||
color = Color.black;
|
||||
flashBackgroundSpriteRenderer.color = color;
|
||||
Vector3 mouseDelta = Input.mousePosition - lastMousePosition;
|
||||
float angleDelta = mouseDelta.x * 0.5f; // 调整灵敏度
|
||||
|
||||
targetLeverAngle = Mathf.Clamp(targetLeverAngle + angleDelta, 0f, maxLeverAngle);
|
||||
lastMousePosition = Input.mousePosition;
|
||||
|
||||
// 根据杆的角度计算进度
|
||||
float progressRatio = targetLeverAngle / maxLeverAngle;
|
||||
if (progressRatio > 0 && !isCharging)
|
||||
{
|
||||
isCharging = true;
|
||||
StartCharging();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 杆自动回弹
|
||||
targetLeverAngle = Mathf.MoveTowards(targetLeverAngle, 0f, leverReturnSpeed * Time.deltaTime);
|
||||
|
||||
// 如果杆已经回弹到接近0度,确保停止充能
|
||||
if (targetLeverAngle < 0.1f && isCharging)
|
||||
{
|
||||
isCharging = false;
|
||||
if (currentProgress >= progressBar.MaxValue)
|
||||
{
|
||||
DialogController.Instance.StartDialogNode(shockEffectNode);
|
||||
}
|
||||
ResetProgress();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//InitializeShock();
|
||||
private void UpdateLeverRotation()
|
||||
{
|
||||
if (lever != null)
|
||||
{
|
||||
currentLeverAngle = Mathf.SmoothDamp(currentLeverAngle, targetLeverAngle, ref leverVelocity, leverSmoothTime);
|
||||
lever.localRotation = Quaternion.Euler(0, 0, -currentLeverAngle);
|
||||
}
|
||||
}
|
||||
|
||||
// public void InitializeShock()
|
||||
@@ -137,91 +202,25 @@ public class ShockSystem : MonoBehaviour
|
||||
|
||||
public IEnumerator StartShock()
|
||||
{
|
||||
// 移动电击器和操作面板到指定位置
|
||||
var move1 = shockDevice.DOLocalMove(shockDeviceTargetPos, moveDuration);
|
||||
var move2 = operationPanel.DOLocalMove(operationPanelTargetPos, moveDuration);
|
||||
powerSlider.EnableSliderInteraction();
|
||||
// 移动左右电击装置到各自的目标位置
|
||||
var moveLeft = leftShockDevice.DOLocalMove(leftShockDeviceTargetPos, moveDuration);
|
||||
var moveRight = rightShockDevice.DOLocalMove(rightShockDeviceTargetPos, moveDuration);
|
||||
var movePanel = operationPanel.DOLocalMove(operationPanelTargetPos, moveDuration);
|
||||
|
||||
yield return move1.WaitForCompletion();
|
||||
yield return move2.WaitForCompletion();
|
||||
yield return moveLeft.WaitForCompletion();
|
||||
yield return moveRight.WaitForCompletion();
|
||||
yield return movePanel.WaitForCompletion();
|
||||
}
|
||||
|
||||
public IEnumerator StopShock()
|
||||
{
|
||||
var move1 = shockDevice.DOLocalMove(deviceOriginalPos, moveDuration);
|
||||
var move2 = operationPanel.DOLocalMove(operationPanelTargetPos, moveDuration);
|
||||
powerSlider.DisableSliderInteraction();
|
||||
var moveLeft = leftShockDevice.DOLocalMove(leftDeviceOriginalPos, moveDuration);
|
||||
var moveRight = rightShockDevice.DOLocalMove(rightDeviceOriginalPos, moveDuration);
|
||||
var movePanel = operationPanel.DOLocalMove(operationPanelTargetPos, moveDuration);
|
||||
|
||||
yield return move1.WaitForCompletion();
|
||||
yield return move2.WaitForCompletion();
|
||||
}
|
||||
|
||||
private void OnSliderValueChanged(float value)
|
||||
{
|
||||
if(value > 0)
|
||||
{
|
||||
sliderBar.localPosition = new Vector3(0, -89, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
sliderBar.localPosition = new Vector3(0, 80, 0);
|
||||
// 停止电击器震动
|
||||
if (deviceShakeTweener != null)
|
||||
{
|
||||
deviceShakeTweener.Kill();
|
||||
deviceShakeTweener = null;
|
||||
}
|
||||
}
|
||||
// 如果滑块值大于0,取消回弹动画
|
||||
if (value > 0 && sliderReturnTweener != null)
|
||||
{
|
||||
sliderReturnTweener.Kill();
|
||||
sliderReturnTweener = null;
|
||||
sliderBar.localPosition = new Vector3(0, -89, 0);
|
||||
}
|
||||
|
||||
if (value > 0)
|
||||
{
|
||||
if (!isCharging)
|
||||
{
|
||||
isCharging = true;
|
||||
StartCharging();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isCharging)
|
||||
{
|
||||
isCharging = false;
|
||||
// 只有在进度条满时才触发闪烁效果
|
||||
if (currentProgress >= progressBar.MaxValue)
|
||||
{
|
||||
// 触发 Yarn 节点来决定使用哪种效果
|
||||
DialogController.Instance.StartDialogNode(shockEffectNode);
|
||||
}
|
||||
// 重置进度条
|
||||
ResetProgress();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSliderEndDrag()
|
||||
{
|
||||
// 如果滑块值大于0,开始回弹动画
|
||||
if (baseSlider.value > 0)
|
||||
{
|
||||
if (sliderReturnTweener != null)
|
||||
{
|
||||
sliderReturnTweener.Kill();
|
||||
}
|
||||
|
||||
sliderReturnTweener = DOTween.To(
|
||||
() => baseSlider.value,
|
||||
x => baseSlider.value = x,
|
||||
0f,
|
||||
0.3f // 回弹动画持续时间
|
||||
).SetEase(Ease.OutQuad);
|
||||
}
|
||||
yield return moveLeft.WaitForCompletion();
|
||||
yield return moveRight.WaitForCompletion();
|
||||
yield return movePanel.WaitForCompletion();
|
||||
}
|
||||
|
||||
private void ResetProgress()
|
||||
@@ -245,13 +244,13 @@ public class ShockSystem : MonoBehaviour
|
||||
).SetEase(Ease.OutQuad);
|
||||
}
|
||||
|
||||
private void StartCharging()
|
||||
private void UpdateCharging()
|
||||
{
|
||||
if (!isCharging) return;
|
||||
|
||||
// 根据滑块值计算进度增加速度
|
||||
float currentValue = baseSlider.value;
|
||||
float speedMultiplier = 1f + (currentValue / maxSliderValue);
|
||||
// 根据杆的角度计算进度增加速度
|
||||
float progressRatio = currentLeverAngle / maxLeverAngle;
|
||||
float speedMultiplier = 1f + progressRatio;
|
||||
currentProgress += progressIncreaseSpeed * speedMultiplier * Time.deltaTime;
|
||||
|
||||
// 更新进度条
|
||||
@@ -259,15 +258,11 @@ public class ShockSystem : MonoBehaviour
|
||||
progressBar.CurrentValue = progressValue;
|
||||
|
||||
// 计算震动强度
|
||||
float progressRatio = Mathf.Min(currentProgress / progressBar.MaxValue, 1f);
|
||||
float currentVibrateStrength = Mathf.Lerp(minVibrateStrength, maxVibrateStrength, progressRatio);
|
||||
|
||||
// 检查是否需要震动
|
||||
if (Time.time - lastVibrateTime >= vibrateInterval)
|
||||
{
|
||||
// 相机震动
|
||||
//StartCoroutine(CameraKit.Instance.ShakeCamera(vibrateInterval, currentVibrateStrength, vibrate));
|
||||
|
||||
// 电击器震动
|
||||
if (shockDevice != null)
|
||||
{
|
||||
@@ -296,17 +291,31 @@ public class ShockSystem : MonoBehaviour
|
||||
// 检查是否达到最大值
|
||||
if (currentProgress >= progressBar.MaxValue)
|
||||
{
|
||||
// 继续充电和震动
|
||||
DOVirtual.DelayedCall(Time.deltaTime, StartCharging);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 继续充电
|
||||
DOVirtual.DelayedCall(Time.deltaTime, StartCharging);
|
||||
// 保持最大值
|
||||
currentProgress = progressBar.MaxValue;
|
||||
progressBar.CurrentValue = Mathf.FloorToInt(currentProgress);
|
||||
|
||||
// 触发释放效果
|
||||
DialogController.Instance.StartDialogNode(shockEffectNode);
|
||||
|
||||
// 停止充能
|
||||
isCharging = false;
|
||||
|
||||
// 强制杆回弹
|
||||
targetLeverAngle = 0f;
|
||||
|
||||
// 重置进度
|
||||
ResetProgress();
|
||||
}
|
||||
}
|
||||
|
||||
private void StartCharging()
|
||||
{
|
||||
if (!isCharging) return;
|
||||
// 重置上次震动时间
|
||||
lastVibrateTime = Time.time;
|
||||
}
|
||||
|
||||
public IEnumerator PlayFlashEffect()
|
||||
{
|
||||
if (flashSequence != null)
|
||||
@@ -391,8 +400,8 @@ public class ShockSystem : MonoBehaviour
|
||||
// 自动充能并释放
|
||||
public IEnumerator AutoChargeAndRelease(float chargeSpeed = 5f, float duration = 1f)
|
||||
{
|
||||
// 设置滑块值
|
||||
baseSlider.value = maxSliderValue;
|
||||
// 设置杆的角度
|
||||
targetLeverAngle = maxLeverAngle;
|
||||
|
||||
// 等待充能完成
|
||||
float startTime = Time.time;
|
||||
@@ -411,8 +420,8 @@ public class ShockSystem : MonoBehaviour
|
||||
// 触发 Yarn 节点来决定使用哪种效果
|
||||
DialogController.Instance.StartDialogNode(shockEffectNode);
|
||||
|
||||
// 重置滑块
|
||||
baseSlider.value = 0;
|
||||
// 重置杆的角度
|
||||
targetLeverAngle = 0f;
|
||||
ResetProgress();
|
||||
}
|
||||
|
||||
@@ -432,13 +441,43 @@ public class ShockSystem : MonoBehaviour
|
||||
{
|
||||
flashSequence.Kill();
|
||||
}
|
||||
if (sliderReturnTweener != null)
|
||||
{
|
||||
sliderReturnTweener.Kill();
|
||||
}
|
||||
if (deviceShakeTweener != null)
|
||||
{
|
||||
deviceShakeTweener.Kill();
|
||||
}
|
||||
}
|
||||
|
||||
// 设置系统电源状态
|
||||
public void SetSystemPower(bool isOn)
|
||||
{
|
||||
isSystemOn = isOn;
|
||||
|
||||
// 更新灯光状态
|
||||
if (isSystemOn)
|
||||
{
|
||||
if (flashHeadSpriteRenderer != null)
|
||||
{
|
||||
flashHeadSpriteRenderer.gameObject.SetActive(true);
|
||||
flashHeadSpriteRenderer.color = new Color(1, 1, 1, 1);
|
||||
}
|
||||
if (flashBackgroundSpriteRenderer != null)
|
||||
{
|
||||
flashBackgroundSpriteRenderer.gameObject.SetActive(true);
|
||||
flashBackgroundSpriteRenderer.color = new Color(0, 0, 0, 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (flashHeadSpriteRenderer != null)
|
||||
{
|
||||
flashHeadSpriteRenderer.gameObject.SetActive(false);
|
||||
flashHeadSpriteRenderer.color = new Color(1, 1, 1, 0);
|
||||
}
|
||||
if (flashBackgroundSpriteRenderer != null)
|
||||
{
|
||||
flashBackgroundSpriteRenderer.gameObject.SetActive(false);
|
||||
flashBackgroundSpriteRenderer.color = new Color(0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user