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