74 lines
2.5 KiB
C#
74 lines
2.5 KiB
C#
using UnityEngine;
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class MapControlBinder : MonoBehaviour
|
|
{
|
|
[SerializeField] private MapController mapController;
|
|
[SerializeField] private NetworkPhysicalWheelInteractable horizontalWheel;
|
|
[SerializeField] private NetworkPhysicalWheelInteractable verticalWheel;
|
|
[SerializeField] private NetworkPhysicalLeverInteractable fireLever;
|
|
[SerializeField] private NetworkButtonInteractable scanButton;
|
|
|
|
private bool initialValuesApplied;
|
|
|
|
private void Reset() => mapController = GetComponent<MapController>();
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (horizontalWheel != null)
|
|
horizontalWheel.ValueChanged += HandleHorizontalChanged;
|
|
if (verticalWheel != null)
|
|
verticalWheel.ValueChanged += HandleVerticalChanged;
|
|
if (fireLever != null)
|
|
fireLever.StateChanged += HandleFireChanged;
|
|
if (scanButton != null)
|
|
scanButton.Pressed += HandleScanPressed;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (horizontalWheel != null)
|
|
horizontalWheel.ValueChanged -= HandleHorizontalChanged;
|
|
if (verticalWheel != null)
|
|
verticalWheel.ValueChanged -= HandleVerticalChanged;
|
|
if (fireLever != null)
|
|
fireLever.StateChanged -= HandleFireChanged;
|
|
if (scanButton != null)
|
|
scanButton.Pressed -= HandleScanPressed;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (initialValuesApplied || mapController == null || !mapController.IsSpawned ||
|
|
horizontalWheel == null || verticalWheel == null ||
|
|
!horizontalWheel.IsSpawned || !verticalWheel.IsSpawned)
|
|
{
|
|
return;
|
|
}
|
|
|
|
initialValuesApplied = true;
|
|
HandleHorizontalChanged(horizontalWheel.NormalizedValue);
|
|
HandleVerticalChanged(verticalWheel.NormalizedValue);
|
|
}
|
|
|
|
private void HandleHorizontalChanged(float value) =>
|
|
ApplyOnServer(() => mapController.SetHorizontalNormalized(value));
|
|
|
|
private void HandleVerticalChanged(float value) =>
|
|
ApplyOnServer(() => mapController.SetVerticalNormalized(value));
|
|
|
|
private void HandleFireChanged(bool active)
|
|
{
|
|
if (active)
|
|
ApplyOnServer(mapController.RequestPseudoFire);
|
|
}
|
|
|
|
private void HandleScanPressed() => ApplyOnServer(mapController.RequestRadarScan);
|
|
|
|
private void ApplyOnServer(System.Action action)
|
|
{
|
|
if (mapController != null && mapController.IsSpawned && mapController.IsServer)
|
|
action?.Invoke();
|
|
}
|
|
}
|