feat(): 完成控制台的2中发射信号的模式切换, 优化基本按钮和拉杆的表现

This commit is contained in:
2025-10-22 19:38:22 +08:00
parent 5198a80318
commit 7903e7fdbe
3 changed files with 71 additions and 12 deletions

View File

@@ -9,7 +9,7 @@ namespace Script.Gameplay.Facility
public class ButtonInteractController : BaseFacilityController public class ButtonInteractController : BaseFacilityController
{ {
[SerializeField] private float signalDuration = 1.0f; // 信号持续时间(秒) [SerializeField] private float signalDuration = 1.0f; // 信号持续时间(秒)
private bool IsPressing = true; private bool isPressing = false;
public override string GetInteractPrompt() public override string GetInteractPrompt()
{ {
@@ -18,21 +18,23 @@ namespace Script.Gameplay.Facility
public override void Interact(GameObject interactor) public override void Interact(GameObject interactor)
{ {
if (!IsPressing) return; if (isPressing) return;
StartCoroutine(SendSignalCoroutine()); StartCoroutine(SendSignalCoroutine());
Debug.Log("Button pressed"); //Debug.Log("Button pressed");
} }
private IEnumerator SendSignalCoroutine() private IEnumerator SendSignalCoroutine()
{ {
SendSignal(true, this.gameObject); SendSignal(true, this.gameObject);
IsPressing = false; isPressing = true;
// 按钮压下的动画或效果可以在这里添加 // 按钮压下的动画或效果可以在这里添加
transform.localScale = new Vector3(0.3f, 0.3f, 0.1f);
yield return new WaitForSeconds(signalDuration); yield return new WaitForSeconds(signalDuration);
SendSignal(false, this.gameObject); SendSignal(false, this.gameObject);
IsPressing = true; isPressing = false;
// 按钮弹起的动画或效果可以在这里添加 // 按钮弹起的动画或效果可以在这里添加
transform.localScale = new Vector3(0.3f, 0.3f, 0.15f);
} }
} }
} }

View File

@@ -1,14 +1,25 @@
using System.Collections.Generic; using System.Collections.Generic;
using Script.Gameplay.Connect; using Script.Gameplay.Connect;
using System.Collections;
using Script.Gameplay.Interface; using Script.Gameplay.Interface;
using UnityEngine; using UnityEngine;
namespace Script.Gameplay.Facility namespace Script.Gameplay.Facility
{ {
public enum SendSignalMode
{
Toggle, // 切换true/false交替发送
//Hold, // 按住, 触发发送true/false, 结束不发送信号
Pulse // 脉冲, 发送true持续一段时间后发送false
}
public class ConsoleController : BaseFacilityController public class ConsoleController : BaseFacilityController
{ {
[SerializeField] private SendSignalMode sendSignalMode = SendSignalMode.Pulse;
[SerializeField] private List<MonoBehaviour> controlTarget; [SerializeField] private List<MonoBehaviour> controlTarget;
[SerializeField] private bool IsBeActive = false; [SerializeField] private bool IsBeActive = false;
[SerializeField] private float signalDuration = 1.0f; // 信号持续时间(秒)
private bool isPressing = false;
private List<ISignalReceiver> controlTargetSignalReceiver = new List<ISignalReceiver>(); private List<ISignalReceiver> controlTargetSignalReceiver = new List<ISignalReceiver>();
private bool lastSendSignal = false; private bool lastSendSignal = false;
@@ -22,7 +33,7 @@ namespace Script.Gameplay.Facility
} }
else else
{ {
if(target != null) if (target != null)
Debug.Log(target.name + " is not a supported control target"); Debug.Log(target.name + " is not a supported control target");
} }
} }
@@ -35,6 +46,17 @@ namespace Script.Gameplay.Facility
} }
public override void Interact(GameObject interactor) public override void Interact(GameObject interactor)
{
if(sendSignalMode== SendSignalMode.Toggle)
{
SendToggleSignal();
}
else if(sendSignalMode== SendSignalMode.Pulse)
{
SendPulseSignal();
}
}
private void SendToggleSignal()
{ {
if (IsBeActive && controlTargetSignalReceiver != null) if (IsBeActive && controlTargetSignalReceiver != null)
{ {
@@ -43,8 +65,33 @@ namespace Script.Gameplay.Facility
{ {
receiver.OnSignalReceived(signal, this.gameObject); receiver.OnSignalReceived(signal, this.gameObject);
} }
lastSendSignal = signal; lastSendSignal = signal;
} }
} }
private void SendPulseSignal()
{
if (IsBeActive)
{
if(isPressing) return;
foreach (var receiver in controlTargetSignalReceiver)
{
StartCoroutine(SendSignalCoroutine(receiver));
}
}
}
private IEnumerator SendSignalCoroutine(ISignalReceiver receiver)
{
receiver.OnSignalReceived(true, this.gameObject);
isPressing = true;
// 按钮压下的动画或效果可以在这里添加
yield return new WaitForSeconds(signalDuration);
receiver.OnSignalReceived(false, this.gameObject);
isPressing = false;
// 按钮弹起的动画或效果可以在这里添加
}
} }
} }

View File

@@ -20,6 +20,16 @@ namespace Script.Gameplay.Facility
isPulled = !isPulled; isPulled = !isPulled;
SendSignal(isPulled, this.gameObject); SendSignal(isPulled, this.gameObject);
// 可选:拉杆动画 // 可选:拉杆动画
if (isPulled)
{
// 旋转拉杆到下拉位置
transform.rotation = Quaternion.Euler(0f, 0f, 45f);
}
else
{
// 旋转拉杆回到初始位置
transform.rotation = Quaternion.Euler(0f, 0f, 0f);
}
//Debug.Log(isPulled ? "Lever pulled down" : "Lever reset"); //Debug.Log(isPulled ? "Lever pulled down" : "Lever reset");
} }