Skip to content

Commit 4a9a840

Browse files
authored
Merge pull request #66 from OpenCommissioning/54-un_driveisactive
fix(Drive): Drive State Color
2 parents 23c1bb0 + c133736 commit 4a9a840

File tree

8 files changed

+135
-94
lines changed

8 files changed

+135
-94
lines changed

Runtime/Scripts/Common/ActorSpeedColor.cs renamed to Runtime/Scripts/Common/DriveStateColor.cs

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
using System;
12
using System.Collections.Generic;
23
using OC.Components;
34
using UnityEngine;
45

56
namespace 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
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using OC.Components;
3+
using UnityEngine;
4+
5+
namespace OC
6+
{
7+
[Serializable]
8+
public class DriveStateObserver
9+
{
10+
public IPropertyReadOnly<Drive.DriveState> State => _state;
11+
public IPropertyReadOnly<bool> IsActive => _isActive;
12+
13+
public float Delay
14+
{
15+
get => _delay;
16+
set => _delay = value;
17+
}
18+
19+
[SerializeField]
20+
private Property<Drive.DriveState> _state = new(Drive.DriveState.Idle);
21+
[SerializeField]
22+
private Property<bool> _isActive = new(false);
23+
[SerializeField]
24+
private float _delay = 0.2f;
25+
26+
private float _delta;
27+
private float _lastValue;
28+
private float _timer;
29+
30+
public void Update(float value, float deltaTime)
31+
{
32+
_delta = value - _lastValue;
33+
34+
if (Mathf.Abs(_delta) < Utils.TOLERANCE)
35+
{
36+
if (_timer > 0)
37+
{
38+
_timer -= deltaTime;
39+
}
40+
else
41+
{
42+
_timer = 0;
43+
SetDelta(0);
44+
}
45+
}
46+
else
47+
{
48+
_lastValue = value;
49+
_timer = _delay;
50+
SetDelta(_delta);
51+
}
52+
}
53+
54+
public void SetDelta(float delta)
55+
{
56+
_state.Value = delta switch
57+
{
58+
> 0 => Drive.DriveState.IsRunningPositive,
59+
< 0 => Drive.DriveState.IsRunningNegative,
60+
_ => Drive.DriveState.Idle
61+
};
62+
63+
_isActive.Value = _state.Value != Drive.DriveState.Idle;
64+
}
65+
}
66+
}

Runtime/Scripts/Common/DriveStateObserver.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripts/Components/Drive.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@ public abstract class Drive : Actor, IDeviceMetadata, ICustomInspector, IInterac
1515
public Link Link => _link;
1616
public int MetadataAssetLength => 1;
1717

18-
public IProperty<bool> IsActive => _isActive;
19-
20-
[SerializeField]
21-
protected Property<bool> _isActive = new (false);
18+
public IPropertyReadOnly<bool> IsActive => _stateObserver.IsActive;
19+
public IPropertyReadOnly<DriveState> State => _stateObserver.State;
2220

2321
public UnityEvent<bool> OnActiveChanged;
2422

2523
[SerializeField]
2624
protected Link _link;
2725
[SerializeField]
2826
protected ConnectorDataFloat _connectorData;
27+
[HideInInspector]
28+
[SerializeField]
29+
protected DriveStateObserver _stateObserver = new ();
2930

3031
private void Start()
3132
{
3233
_link.Initialize(this);
3334
_connectorData = new ConnectorDataFloat(_link);
34-
_isActive.OnValueChanged += value => OnActiveChanged?.Invoke(value);
35+
_stateObserver.IsActive.OnValueChanged += value => OnActiveChanged?.Invoke(value);
3536
}
3637

3738
protected void Reset()
@@ -84,5 +85,12 @@ private IEnumerator InitializeCoroutine(float value)
8485
yield return new WaitForSeconds(1);
8586
_connectorData.Status.SetBit(7, false);
8687
}
88+
89+
public enum DriveState
90+
{
91+
Idle,
92+
IsRunningNegative,
93+
IsRunningPositive
94+
}
8795
}
8896
}
Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections;
21
using OC.Communication;
32
using UnityEngine;
43

@@ -11,19 +10,9 @@ namespace OC.Components
1110
public class DrivePosition : Drive
1211
{
1312
public IProperty<float> Speed => _speed;
14-
public IPropertyReadOnly<float> Delta => _delta;
1513

1614
[SerializeField]
1715
private Property<float> _speed = new (100);
18-
[SerializeField]
19-
private Property<float> _delta = new (0);
20-
21-
private IEnumerator _isActiveCoroutine;
22-
23-
private void Awake()
24-
{
25-
_isActiveCoroutine = IsActiveCoroutine();
26-
}
2716

2817
protected override void GetLinkData()
2918
{
@@ -34,30 +23,20 @@ protected override void Operation(float deltaTime)
3423
{
3524
if (_speed.Value > Utils.TOLERANCE_HALF)
3625
{
37-
_delta.Value = _target.Value - _value.Value;
3826
_value.Value = Mathf.MoveTowards(_value.Value, _target.Value, _speed.Value * deltaTime);
39-
_isActive.Value = Mathf.Abs(_target.Value - _value.Value) > Utils.TOLERANCE_HALF;
4027
}
4128
else
4229
{
43-
_delta.Value = _target.Value - _value.Value;
4430
_value.Value = _target.Value;
45-
StopCoroutine(_isActiveCoroutine);
46-
StartCoroutine(_isActiveCoroutine);
4731
}
48-
}
49-
50-
private IEnumerator IsActiveCoroutine()
51-
{
52-
_isActive.Value = true;
53-
yield return new WaitForSeconds(.1f);
54-
_isActive.Value = false;
32+
33+
_stateObserver.Update(_value.Value, deltaTime);
5534
}
5635

5736
protected override void SetLinkData()
5837
{
5938
_connectorData.StatusData = _value.Value;
60-
_connectorData.Status.SetBit(6, _isActive);
39+
_connectorData.Status.SetBit(6, _stateObserver.IsActive.Value);
6140
}
6241
}
6342
}

Runtime/Scripts/Components/DriveSpeed.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ protected override void GetLinkData()
2121
protected override void Operation(float deltaTime)
2222
{
2323
_value.Value = Mathf.MoveTowards(_value.Value, _target, _acceleration * deltaTime);
24-
_isActive.Value = Mathf.Abs(_value) > Utils.TOLERANCE_HALF;
24+
_stateObserver.SetDelta(_value.Value);
2525
}
2626

2727
protected override void SetLinkData()
2828
{
2929
_connectorData.StatusData = _value.Value;
30-
_connectorData.Status.SetBit(6, _isActive);
30+
_connectorData.Status.SetBit(6, _stateObserver.IsActive.Value);
3131
}
3232
}
3333
}

Samples/Demo/0.1 Devices.unity

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,6 @@ MonoBehaviour:
548548
_value: 0
549549
_value:
550550
_value: 0
551-
_isActive:
552-
_value: 0
553551
OnActiveChanged:
554552
m_PersistentCalls:
555553
m_Calls: []
@@ -569,6 +567,12 @@ MonoBehaviour:
569567
Status: 0
570568
ControlData: 0
571569
StatusData: 0
570+
_stateObserver:
571+
_state:
572+
_value: 0
573+
_isActive:
574+
_value: 0
575+
_delay: 0
572576
_acceleration:
573577
_value: 100
574578
--- !u!1 &186187010
@@ -2301,8 +2305,6 @@ MonoBehaviour:
23012305
_value: 0
23022306
_value:
23032307
_value: 0
2304-
_isActive:
2305-
_value: 0
23062308
OnActiveChanged:
23072309
m_PersistentCalls:
23082310
m_Calls: []
@@ -2322,6 +2324,12 @@ MonoBehaviour:
23222324
Status: 0
23232325
ControlData: 0
23242326
StatusData: 0
2327+
_stateObserver:
2328+
_state:
2329+
_value: 0
2330+
_isActive:
2331+
_value: 0
2332+
_delay: 0
23252333
_acceleration:
23262334
_value: 100
23272335
_speed:
@@ -4981,8 +4989,6 @@ MonoBehaviour:
49814989
_value: 0
49824990
_value:
49834991
_value: 0
4984-
_isActive:
4985-
_value: 0
49864992
OnActiveChanged:
49874993
m_PersistentCalls:
49884994
m_Calls: []
@@ -5002,10 +5008,14 @@ MonoBehaviour:
50025008
Status: 0
50035009
ControlData: 0
50045010
StatusData: 0
5011+
_stateObserver:
5012+
_state:
5013+
_value: 0
5014+
_isActive:
5015+
_value: 0
5016+
_delay: 0
50055017
_speed:
50065018
_value: 100
5007-
_delta:
5008-
_value: 0
50095019
--- !u!1 &1585958997
50105020
GameObject:
50115021
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)