1
1
using JetBrains . Annotations ;
2
+ using tech . gyoku . FDMi . core ;
2
3
using UdonSharp ;
3
4
using UnityEngine ;
4
5
using URC ;
6
+ using VRC . SDKBase ;
5
7
6
8
namespace VRChatAerospaceUniversity . V320 . Avionics . Communication . AudioSource {
7
- [ UdonBehaviourSyncMode ( BehaviourSyncMode . None ) ]
9
+ [ UdonBehaviourSyncMode ( BehaviourSyncMode . Manual ) ]
8
10
[ AircraftLifecycleReceiver ]
9
11
public class UdonRadioCommunicationAudioSource : UdonSharpBehaviour {
10
12
public float defaultFrequency = 118.0f ;
@@ -15,29 +17,68 @@ public float Frequency {
15
17
set => _transceiver . _SetFrequency ( value ) ;
16
18
}
17
19
20
+ [ UdonSynced ] [ SerializeField ] [ HideInInspector ] private bool _receive ;
18
21
[ PublicAPI ]
19
22
public bool Receive {
20
- get => _transceiver . _GetReceive ( ) ;
21
- set => _transceiver . _SetReceive ( value ) ;
23
+ get => _receive ;
24
+ set {
25
+ TakeOwnership ( ) ;
26
+
27
+ _receive = value ;
28
+ _UpdateReceiveTransmit ( ) ;
29
+
30
+ RequestSerialization ( ) ;
31
+ }
22
32
}
23
33
34
+ [ UdonSynced ] [ SerializeField ] [ HideInInspector ] private bool _transmit ;
24
35
[ PublicAPI ]
25
36
public bool Transmit {
26
- get => _transceiver . _GetTransmit ( ) ;
27
- set => _transceiver . _SetTransmit ( value ) ;
37
+ get => _transmit ;
38
+ set {
39
+ TakeOwnership ( ) ;
40
+
41
+ _transmit = value ;
42
+ _UpdateReceiveTransmit ( ) ;
43
+
44
+ RequestSerialization ( ) ;
45
+ }
28
46
}
29
47
30
48
[ SerializeField ] private Transceiver _transceiver ;
49
+ [ SerializeField ] private FDMiBool _isPowered ;
50
+
51
+ public void _UpdateReceiveTransmit ( ) {
52
+ var isPowered = ! _isPowered || _isPowered . Data ;
53
+
54
+ _transceiver . _SetTransmit ( isPowered && _transmit ) ;
55
+ _transceiver . _SetReceive ( isPowered && _receive ) ;
56
+ }
57
+
58
+ [ PublicAPI ]
59
+ private void _OnPowerStateChanged ( ) => _UpdateReceiveTransmit ( ) ;
60
+ public override void OnDeserialization ( ) => _UpdateReceiveTransmit ( ) ;
31
61
32
62
[ PublicAPI ]
33
63
public void _OnInit ( ) {
64
+ if ( _isPowered ) {
65
+ _isPowered . subscribe ( this , nameof ( _UpdateReceiveTransmit ) ) ;
66
+ }
67
+
34
68
// Delay 4s due to
35
69
// https://github.com/esnya/UdonRadioCommunications/blob/v5.0.0/Packages/com.nekometer.esnya.udon-radio-communications/Scripts/UdonRadioCommunication.cs#L63
36
70
SendCustomEventDelayedSeconds ( nameof ( _SetDefaultFrequency ) , 4 ) ;
71
+ _UpdateReceiveTransmit ( ) ;
37
72
}
38
73
39
74
public void _SetDefaultFrequency ( ) {
40
75
_transceiver . Frequency = defaultFrequency ;
41
76
}
77
+
78
+ private void TakeOwnership ( ) {
79
+ if ( Networking . IsOwner ( gameObject ) ) return ;
80
+
81
+ Networking . SetOwner ( Networking . LocalPlayer , gameObject ) ;
82
+ }
42
83
}
43
84
}
0 commit comments