2025-10-22 17:34:58 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Script.Gameplay.Connect;
|
2025-10-22 19:38:22 +08:00
|
|
|
|
using System.Collections;
|
2025-10-22 17:34:58 +08:00
|
|
|
|
using Script.Gameplay.Interface;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Script.Gameplay.Facility
|
|
|
|
|
|
{
|
2025-10-22 19:38:22 +08:00
|
|
|
|
public enum SendSignalMode
|
|
|
|
|
|
{
|
|
|
|
|
|
Toggle, // 切换,true/false交替发送
|
2025-10-29 12:01:33 +08:00
|
|
|
|
|
2025-10-22 19:38:22 +08:00
|
|
|
|
//Hold, // 按住, 触发发送true/false, 结束不发送信号
|
|
|
|
|
|
Pulse // 脉冲, 发送true,持续一段时间后发送false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
|
public class ConsoleController : BaseFacilityController
|
|
|
|
|
|
{
|
2025-10-22 19:38:22 +08:00
|
|
|
|
[SerializeField] private SendSignalMode sendSignalMode = SendSignalMode.Pulse;
|
2025-10-22 17:34:58 +08:00
|
|
|
|
[SerializeField] private List<MonoBehaviour> controlTarget;
|
|
|
|
|
|
[SerializeField] private bool IsBeActive = false;
|
2025-10-22 19:38:22 +08:00
|
|
|
|
[SerializeField] private float signalDuration = 1.0f; // 信号持续时间(秒)
|
2025-10-29 12:01:33 +08:00
|
|
|
|
|
|
|
|
|
|
[SerializeField] private GameObject pressButtonPrefab;
|
2025-10-22 19:38:22 +08:00
|
|
|
|
private bool isPressing = false;
|
2025-10-22 17:34:58 +08:00
|
|
|
|
private List<ISignalReceiver> controlTargetSignalReceiver = new List<ISignalReceiver>();
|
|
|
|
|
|
private bool lastSendSignal = false;
|
2025-10-22 19:38:22 +08:00
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var target in controlTarget)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (target is ISignalReceiver receiver)
|
|
|
|
|
|
{
|
|
|
|
|
|
controlTargetSignalReceiver.Add(receiver);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-10-22 19:38:22 +08:00
|
|
|
|
if (target != null)
|
2025-10-22 17:34:58 +08:00
|
|
|
|
Debug.Log(target.name + " is not a supported control target");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-22 19:38:22 +08:00
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
|
public override void OnSignalReceived(bool active, GameObject sender)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnSignalReceived(active, sender);
|
2025-10-30 13:16:16 +08:00
|
|
|
|
if (CurrentNeedSignalCount >= NeedSignalCount)
|
|
|
|
|
|
{
|
|
|
|
|
|
IsBeActive = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
IsBeActive = false;
|
|
|
|
|
|
}
|
2025-10-22 17:34:58 +08:00
|
|
|
|
}
|
2025-10-22 19:38:22 +08:00
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
|
public override void Interact(GameObject interactor)
|
2025-10-22 19:38:22 +08:00
|
|
|
|
{
|
2025-10-29 12:01:33 +08:00
|
|
|
|
if (!IsBeActive) return;
|
|
|
|
|
|
if (sendSignalMode == SendSignalMode.Toggle)
|
2025-10-22 19:38:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
SendToggleSignal();
|
|
|
|
|
|
}
|
2025-10-29 12:01:33 +08:00
|
|
|
|
else if (sendSignalMode == SendSignalMode.Pulse)
|
2025-10-22 19:38:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
SendPulseSignal();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-29 12:01:33 +08:00
|
|
|
|
|
|
|
|
|
|
private void UpdateAnimation()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (pressButtonPrefab != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isPressing)
|
|
|
|
|
|
{
|
|
|
|
|
|
pressButtonPrefab.transform.localPosition = new Vector3(0, 0f, 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
pressButtonPrefab.transform.localPosition = new Vector3(0, 0f, 0.75f);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-22 19:38:22 +08:00
|
|
|
|
private void SendToggleSignal()
|
2025-10-22 17:34:58 +08:00
|
|
|
|
{
|
2025-10-29 12:01:33 +08:00
|
|
|
|
if (controlTargetSignalReceiver != null)
|
2025-10-22 17:34:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
var signal = !lastSendSignal;
|
|
|
|
|
|
foreach (var receiver in controlTargetSignalReceiver)
|
2025-10-29 12:01:33 +08:00
|
|
|
|
{
|
2025-10-22 17:34:58 +08:00
|
|
|
|
receiver.OnSignalReceived(signal, this.gameObject);
|
|
|
|
|
|
}
|
2025-10-22 19:38:22 +08:00
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
|
lastSendSignal = signal;
|
2025-10-29 12:01:33 +08:00
|
|
|
|
isPressing = signal;
|
|
|
|
|
|
UpdateAnimation();
|
2025-10-22 17:34:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-29 12:01:33 +08:00
|
|
|
|
|
2025-10-22 19:38:22 +08:00
|
|
|
|
private void SendPulseSignal()
|
|
|
|
|
|
{
|
2025-10-29 12:01:33 +08:00
|
|
|
|
if (isPressing) return;
|
|
|
|
|
|
foreach (var receiver in controlTargetSignalReceiver)
|
2025-10-22 19:38:22 +08:00
|
|
|
|
{
|
2025-10-29 12:01:33 +08:00
|
|
|
|
StartCoroutine(SendSignalCoroutine(receiver));
|
2025-10-22 19:38:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-29 12:01:33 +08:00
|
|
|
|
|
2025-10-22 19:38:22 +08:00
|
|
|
|
private IEnumerator SendSignalCoroutine(ISignalReceiver receiver)
|
|
|
|
|
|
{
|
|
|
|
|
|
receiver.OnSignalReceived(true, this.gameObject);
|
|
|
|
|
|
isPressing = true;
|
|
|
|
|
|
// 按钮压下的动画或效果可以在这里添加
|
2025-10-29 12:01:33 +08:00
|
|
|
|
UpdateAnimation();
|
2025-10-22 19:38:22 +08:00
|
|
|
|
|
|
|
|
|
|
yield return new WaitForSeconds(signalDuration);
|
|
|
|
|
|
receiver.OnSignalReceived(false, this.gameObject);
|
|
|
|
|
|
isPressing = false;
|
|
|
|
|
|
// 按钮弹起的动画或效果可以在这里添加
|
2025-10-29 12:01:33 +08:00
|
|
|
|
UpdateAnimation();
|
2025-10-22 19:38:22 +08:00
|
|
|
|
}
|
2025-10-22 17:34:58 +08:00
|
|
|
|
}
|
2025-10-22 19:38:22 +08:00
|
|
|
|
}
|