1515// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
1717using System ;
18+ using System . Linq ;
19+ using NLog ;
1820using Unity . VisualScripting ;
19- using UnityEngine ;
2021
2122namespace VisualPinball . Unity . VisualScripting
2223{
@@ -25,6 +26,9 @@ namespace VisualPinball.Unity.VisualScripting
2526 [ UnitCategory ( "Visual Pinball/Variables" ) ]
2627 public class ChangePlayerStateUnit : GleUnit
2728 {
29+ [ Serialize , Inspectable , UnitHeaderInspectable ( "Next Player" ) ]
30+ public bool NextPlayer { get ; set ; }
31+
2832 [ DoNotSerialize ]
2933 [ PortLabelHidden ]
3034 public ControlInput InputTrigger ;
@@ -37,14 +41,18 @@ public class ChangePlayerStateUnit : GleUnit
3741 [ PortLabel ( "Player ID" ) ]
3842 public ValueInput PlayerId { get ; private set ; }
3943
44+ private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
45+
4046 protected override void Definition ( )
4147 {
42- PlayerId = ValueInput ( nameof ( PlayerId ) , 0 ) ;
43-
4448 InputTrigger = ControlInput ( nameof ( InputTrigger ) , Process ) ;
4549 OutputTrigger = ControlOutput ( nameof ( OutputTrigger ) ) ;
4650
47- Requirement ( PlayerId , InputTrigger ) ;
51+ if ( ! NextPlayer ) {
52+ PlayerId = ValueInput ( nameof ( PlayerId ) , 0 ) ;
53+ Requirement ( PlayerId , InputTrigger ) ;
54+ }
55+
4856 Succession ( InputTrigger , OutputTrigger ) ;
4957 }
5058
@@ -54,7 +62,24 @@ private ControlOutput Process(Flow flow)
5462 throw new InvalidOperationException ( "Cannot retrieve GLE from unit." ) ;
5563 }
5664
57- VsGle . SetCurrentPlayer ( flow . GetValue < int > ( PlayerId ) ) ;
65+ int playerId ;
66+ if ( NextPlayer ) {
67+ var lastPlayer = VsGle . PlayerStates . Keys . Max ( ) ;
68+ if ( VsGle . CurrentPlayerState . Id == lastPlayer ) {
69+ playerId = VsGle . PlayerStates . Keys . Min ( ) ;
70+
71+ } else if ( VsGle . PlayerStates . ContainsKey ( VsGle . CurrentPlayerState . Id + 1 ) ) {
72+ playerId = VsGle . CurrentPlayerState . Id + 1 ;
73+
74+ } else {
75+ Logger . Warn ( $ "Non-existent next player { VsGle . CurrentPlayerState . Id + 1 } .") ;
76+ playerId = VsGle . CurrentPlayerState . Id ;
77+ }
78+ } else {
79+ playerId = flow . GetValue < int > ( PlayerId ) ;
80+ }
81+
82+ VsGle . SetCurrentPlayer ( playerId ) ;
5883
5984 return OutputTrigger ;
6085 }
0 commit comments