1+ using System ;
12using System . Collections . Generic ;
23using OC . Components ;
34using UnityEngine ;
45
56namespace OC
67{
7- public class ActorSpeedColor : MonoBehaviour
8+ public class DriveStateColor : MonoBehaviour
89 {
910 [ Header ( "Parameter" ) ]
1011 [ SerializeField ]
11- private Actor _actor ;
12+ private Drive _drive ;
1213 [ SerializeField ]
1314 private Color _colorForward = Color . green ;
1415 [ SerializeField ]
@@ -26,34 +27,33 @@ public class ActorSpeedColor : MonoBehaviour
2627 private void OnEnable ( )
2728 {
2829 GetPropertyBlock ( ) ;
29- if ( _actor == null ) return ;
30- if ( _actor is DriveSpeed ) _actor . Value . OnValueChanged += OnSpeedChanged ;
31- if ( _actor is DrivePosition drivePosition ) drivePosition . Delta . OnValueChanged += OnSpeedChanged ;
30+ if ( _drive == null ) return ;
31+ _drive . State . OnValueChanged += OnDriveStateChanged ;
3232 }
3333
34- private void OnDisable ( )
34+ private void OnDriveStateChanged ( Drive . DriveState state )
3535 {
36- if ( _actor == null ) return ;
37- if ( _actor is DriveSpeed ) _actor . Value . OnValueChanged -= OnSpeedChanged ;
38- if ( _actor is DrivePosition drivePosition ) drivePosition . Delta . OnValueChanged -= OnSpeedChanged ;
39- }
40-
41- private void OnSpeedChanged ( float value )
42- {
43- if ( Math . FastApproximately ( value , 0 ) )
36+ switch ( state )
4437 {
45- SetColor ( false , false ) ;
46- }
47- else switch ( value )
48- {
49- case > 0 :
50- SetColor ( true , ! _invertDirection ) ;
38+ case Drive . DriveState . Idle :
39+ SetProperties ( _initProperty ) ;
40+ break ;
41+ case Drive . DriveState . IsRunningNegative :
42+ SetProperties ( _backwardProperty ) ;
5143 break ;
52- case < 0 :
53- SetColor ( true , _invertDirection ) ;
44+ case Drive . DriveState . IsRunningPositive :
45+ SetProperties ( _forwardProperty ) ;
5446 break ;
47+ default :
48+ throw new ArgumentOutOfRangeException ( nameof ( state ) , state , null ) ;
5549 }
5650 }
51+
52+ private void OnDisable ( )
53+ {
54+ if ( _drive == null ) return ;
55+ _drive . State . OnValueChanged -= OnDriveStateChanged ;
56+ }
5757
5858 private void GetPropertyBlock ( )
5959 {
@@ -64,9 +64,10 @@ private void GetPropertyBlock()
6464
6565 foreach ( var item in _renderers )
6666 {
67- var initBlocks = new MaterialPropertyBlock [ item . materials . Length ] ;
68- var forwardBlocks = new MaterialPropertyBlock [ item . materials . Length ] ;
69- var backwardBlocks = new MaterialPropertyBlock [ item . materials . Length ] ;
67+ var materials = item . materials ;
68+ var initBlocks = new MaterialPropertyBlock [ materials . Length ] ;
69+ var forwardBlocks = new MaterialPropertyBlock [ materials . Length ] ;
70+ var backwardBlocks = new MaterialPropertyBlock [ materials . Length ] ;
7071
7172 for ( var i = 0 ; i < item . materials . Length ; i ++ )
7273 {
@@ -89,39 +90,13 @@ private void GetPropertyBlock()
8990 }
9091 }
9192
92- private void SetColor ( bool enable , bool forwardDirection )
93+ private void SetProperties ( Dictionary < Renderer , MaterialPropertyBlock [ ] > propertyBlocksMap )
9394 {
94- if ( enable )
95- {
96- if ( forwardDirection )
97- {
98- foreach ( var item in _forwardProperty )
99- {
100- for ( var i = 0 ; i < item . Key . materials . Length ; i ++ )
101- {
102- item . Key . SetPropertyBlock ( item . Value [ i ] , i ) ;
103- }
104- }
105- }
106- else
107- {
108- foreach ( var item in _backwardProperty )
109- {
110- for ( var i = 0 ; i < item . Key . materials . Length ; i ++ )
111- {
112- item . Key . SetPropertyBlock ( item . Value [ i ] , i ) ;
113- }
114- }
115- }
116- }
117- else
95+ foreach ( var item in propertyBlocksMap )
11896 {
119- foreach ( var item in _initProperty )
97+ for ( var i = 0 ; i < item . Key . materials . Length ; i ++ )
12098 {
121- for ( var i = 0 ; i < item . Key . materials . Length ; i ++ )
122- {
123- item . Key . SetPropertyBlock ( item . Value [ i ] , i ) ;
124- }
99+ item . Key . SetPropertyBlock ( item . Value [ i ] , i ) ;
125100 }
126101 }
127102 }
0 commit comments