2222using Unity . Mathematics ;
2323using UnityEngine ;
2424using UnityEngine . InputSystem ;
25- using UnityEngine . Serialization ;
26- using VisualPinball . Engine . Common ;
27- using VisualPinball . Engine . Game ;
28- using VisualPinball . Engine . Game . Engines ;
25+ using UnityEngine . Serialization ;
26+ using VisualPinball . Engine . Common ;
27+ using VisualPinball . Engine . Game ;
28+ using VisualPinball . Engine . Game . Engines ;
29+ using VisualPinball . Unity . Simulation ;
2930using Color = VisualPinball . Engine . Math . Color ;
3031using Logger = NLog . Logger ;
3132
@@ -107,11 +108,12 @@ public void UnpackReferences(byte[] data, Transform root, PackagedRefs refs, Pac
107108 private const float SlowMotionMax = 0.1f ;
108109 private const float TimeLapseMax = 2.5f ;
109110
110- private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
111+ private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
111112 private TableComponent _tableComponent ;
112113 private PlayfieldComponent _playfieldComponent ;
113- private PhysicsEngine _physicsEngine ;
114- private CancellationTokenSource _gamelogicEngineInitCts ;
114+ private PhysicsEngine _physicsEngine ;
115+ private SimulationThreadComponent _simulationThreadComponent ;
116+ private CancellationTokenSource _gamelogicEngineInitCts ;
115117
116118 private PlayfieldComponent PlayfieldComponent {
117119 get {
@@ -122,22 +124,39 @@ private PlayfieldComponent PlayfieldComponent {
122124 }
123125 }
124126
125- private PhysicsEngine PhysicsEngine {
127+ private PhysicsEngine PhysicsEngine {
126128 get {
127129 if ( _physicsEngine == null ) {
128130 _physicsEngine = GetComponentInChildren < PhysicsEngine > ( ) ;
129131 }
130132 return _physicsEngine ;
131133 }
132- }
134+ }
135+
136+ private SimulationThreadComponent SimulationThreadComponent {
137+ get {
138+ if ( _simulationThreadComponent == null ) {
139+ _simulationThreadComponent = GetComponent < SimulationThreadComponent > ( ) ;
140+ }
141+ return _simulationThreadComponent ;
142+ }
143+ }
133144
134145 #region Access
135146
136147 internal IApiSwitch Switch ( ISwitchDeviceComponent component , string switchItem ) => component != null ? _switchPlayer . Switch ( component , switchItem ) : null ;
137148 public IApiCoil Coil ( ICoilDeviceComponent component , string coilItem ) => component != null ? _coilPlayer . Coil ( component , coilItem ) : null ;
138149 public IApiLamp Lamp ( ILampDeviceComponent component ) => component != null ? _lampPlayer . Lamp ( component ) : null ;
139150 public IApiWireDeviceDest WireDevice ( IWireableComponent c ) => _wirePlayer . WireDevice ( c ) ;
140- internal void HandleWireSwitchChange ( WireDestConfig wireConfig , bool isEnabled ) => _wirePlayer . HandleSwitchChange ( wireConfig , isEnabled ) ;
151+ internal void HandleWireSwitchChange ( WireDestConfig wireConfig , bool isEnabled ) => _wirePlayer . HandleSwitchChange ( wireConfig , isEnabled ) ;
152+
153+ internal void DispatchSwitch ( string switchId , bool isClosed )
154+ {
155+ if ( SimulationThreadComponent != null && SimulationThreadComponent . EnqueueSwitchFromMainThread ( switchId , isClosed ) ) {
156+ return ;
157+ }
158+ GamelogicEngine ? . Switch ( switchId , isClosed ) ;
159+ }
141160
142161 public Dictionary < string , IApiSwitchStatus > SwitchStatuses => _switchPlayer . SwitchStatuses ;
143162 public Dictionary < string , bool > CoilStatuses => _coilPlayer . CoilStatuses ;
@@ -170,7 +189,7 @@ private void Awake()
170189 GamelogicEngine = engineComponent ;
171190 _lampPlayer . Awake ( this , _tableComponent , GamelogicEngine ) ;
172191 _coilPlayer . Awake ( this , _tableComponent , GamelogicEngine , _lampPlayer , _wirePlayer ) ;
173- _switchPlayer . Awake ( _tableComponent , GamelogicEngine , _inputManager ) ;
192+ _switchPlayer . Awake ( this , _tableComponent , GamelogicEngine , _inputManager ) ;
174193 _wirePlayer . Awake ( _tableComponent , _inputManager , _switchPlayer , this , PhysicsEngine ) ;
175194 _displayPlayer . Awake ( GamelogicEngine ) ;
176195 }
@@ -297,15 +316,15 @@ public void Register<TApi>(TApi api, MonoBehaviour component) where TApi : IApi
297316 }
298317 }
299318
300- private void RegisterCollider ( int itemId , IApiColliderGenerator apiColl )
301- {
302- if ( ! apiColl . IsColliderAvailable ) {
303- return ;
304- }
305- _colliderGenerators . Add ( apiColl ) ;
306- if ( apiColl is IApiHittable apiHittable ) {
307- _hittables [ itemId ] = apiHittable ;
308- }
319+ private void RegisterCollider ( int itemId , IApiColliderGenerator apiColl )
320+ {
321+ if ( ! apiColl . IsColliderAvailable ) {
322+ return ;
323+ }
324+ _colliderGenerators . Add ( apiColl ) ;
325+ if ( apiColl is IApiHittable apiHittable ) {
326+ _hittables [ itemId ] = apiHittable ;
327+ }
309328
310329 if ( apiColl is IApiCollidable apiCollidable ) {
311330 _collidables [ itemId ] = apiCollidable ;
@@ -319,20 +338,19 @@ private void RegisterCollider(int itemId, IApiColliderGenerator apiColl)
319338 public void ScheduleAction ( int timeMs , Action action ) => PhysicsEngine . ScheduleAction ( timeMs , action ) ;
320339 public void ScheduleAction ( uint timeMs , Action action ) => PhysicsEngine . ScheduleAction ( timeMs , action ) ;
321340
322- public void OnEvent ( in EventData eventData )
323- {
324- Debug . Log ( eventData ) ;
325- switch ( eventData . EventId ) {
326- case EventId . HitEventsHit :
327- if ( ! _hittables . ContainsKey ( eventData . ItemId ) ) {
328- Debug . LogError ( $ "Cannot find { eventData . ItemId } in hittables.") ;
329- }
330- _hittables [ eventData . ItemId ] . OnHit ( eventData . BallId ) ;
331- break ;
332-
333- case EventId . HitEventsUnhit :
334- _hittables [ eventData . ItemId ] . OnHit ( eventData . BallId , true ) ;
335- break ;
341+ public void OnEvent ( in EventData eventData )
342+ {
343+ switch ( eventData . EventId ) {
344+ case EventId . HitEventsHit :
345+ if ( ! _hittables . ContainsKey ( eventData . ItemId ) ) {
346+ Debug . LogError ( $ "Cannot find { eventData . ItemId } in hittables.") ;
347+ }
348+ _hittables [ eventData . ItemId ] . OnHit ( eventData . BallId ) ;
349+ break ;
350+
351+ case EventId . HitEventsUnhit :
352+ _hittables [ eventData . ItemId ] . OnHit ( eventData . BallId , true ) ;
353+ break ;
336354
337355 case EventId . LimitEventsBos :
338356 _rotatables [ eventData . ItemId ] . OnRotate ( eventData . FloatParam , false ) ;
0 commit comments