-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthMonitor.cs
More file actions
169 lines (138 loc) · 5.47 KB
/
DepthMonitor.cs
File metadata and controls
169 lines (138 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using Timberborn.Automation;
using Timberborn.BaseComponentSystem;
using Timberborn.BlockSystem;
using Timberborn.DuplicationSystem;
using Timberborn.EntitySystem;
using Timberborn.Persistence;
using Timberborn.WaterSystem;
using Timberborn.WorldPersistence;
using UnityEngine;
namespace Calloatti.DepthMonitor
{
public class DepthMonitor : BaseComponent, IAwakableComponent, IInitializableEntity, IStartableComponent, IPersistentEntity, IDuplicable<DepthMonitor>, IDuplicable, ISamplingTransmitter, ITransmitter
{
private static readonly ComponentKey DepthMonitorKey = new ComponentKey("DepthMonitor");
private static readonly PropertyKey<float> ThresholdOnKey = new PropertyKey<float>("ThresholdOn");
private static readonly PropertyKey<float> ThresholdOffKey = new PropertyKey<float>("ThresholdOff");
private static readonly PropertyKey<bool> CurrentlyActiveKey = new PropertyKey<bool>("CurrentlyActive");
// Default offsets if placed fresh
private static readonly float DefaultDepthOffsetOn = 0.2f;
private static readonly float DefaultDepthOffsetOff = 0.8f;
private readonly IThreadSafeWaterMap _threadSafeWaterMap;
private Automator _automator;
private BlockObject _blockObject;
private DepthMonitorSpec _depthMonitorSpec;
private float? _rawThresholdOn;
private float? _rawThresholdOff;
private bool _currentlyActive;
private float _sampledWaterHeight;
private int _sampledFloor;
public Vector3Int SensorCoordinates { get; private set; }
public int MinThreshold => _sampledFloor;
public float MaxThreshold => (float)SensorCoordinates.z + _depthMonitorSpec.SensorHeightOffset;
public float ThresholdOn => Mathf.Clamp(_rawThresholdOn ?? 0f, MinThreshold, MaxThreshold);
public float ThresholdOnFromFloor => ThresholdOn - (float)_sampledFloor;
public float ThresholdOff => Mathf.Clamp(_rawThresholdOff ?? 0f, MinThreshold, MaxThreshold);
public float ThresholdOffFromFloor => ThresholdOff - (float)_sampledFloor;
public float Depth => Mathf.Clamp(_sampledWaterHeight, MinThreshold, MaxThreshold);
public float DepthFromFloor => Mathf.Clamp(_sampledWaterHeight - (float)_sampledFloor, 0f, MaxThreshold - (float)MinThreshold);
public DepthMonitor(IThreadSafeWaterMap threadSafeWaterMap)
{
_threadSafeWaterMap = threadSafeWaterMap;
}
public void Awake()
{
_automator = GetComponent<Automator>();
_blockObject = GetComponent<BlockObject>();
_depthMonitorSpec = GetComponent<DepthMonitorSpec>();
}
public void InitializeEntity()
{
InitializeSensorCoordinates();
if (!_rawThresholdOn.HasValue)
{
_rawThresholdOn = (float)SensorCoordinates.z + DefaultDepthOffsetOn;
}
if (!_rawThresholdOff.HasValue)
{
_rawThresholdOff = (float)SensorCoordinates.z + DefaultDepthOffsetOff;
}
}
public void Start()
{
_automator?.SetState(_currentlyActive);
}
public void SetThresholdOn(float value)
{
if (!_rawThresholdOn.Equals(value))
{
_rawThresholdOn = value;
UpdateOutputState();
}
}
public void SetThresholdOff(float value)
{
if (!_rawThresholdOff.Equals(value))
{
_rawThresholdOff = value;
UpdateOutputState();
}
}
public void Save(IEntitySaver entitySaver)
{
IObjectSaver component = entitySaver.GetComponent(DepthMonitorKey);
if (_rawThresholdOn.HasValue) component.Set(ThresholdOnKey, _rawThresholdOn.Value);
if (_rawThresholdOff.HasValue) component.Set(ThresholdOffKey, _rawThresholdOff.Value);
component.Set(CurrentlyActiveKey, _currentlyActive);
}
public void Load(IEntityLoader entityLoader)
{
if (entityLoader.TryGetComponent(DepthMonitorKey, out var component))
{
if (component.Has(ThresholdOnKey)) _rawThresholdOn = component.Get(ThresholdOnKey);
if (component.Has(ThresholdOffKey)) _rawThresholdOff = component.Get(ThresholdOffKey);
if (component.Has(CurrentlyActiveKey)) _currentlyActive = component.Get(CurrentlyActiveKey);
}
}
public void DuplicateFrom(DepthMonitor source)
{
InitializeSensorCoordinates();
_rawThresholdOn = (float)GetCurrentFloor() + source.ThresholdOnFromFloor;
_rawThresholdOff = (float)GetCurrentFloor() + source.ThresholdOffFromFloor;
_currentlyActive = source._currentlyActive;
UpdateOutputState();
}
public void Sample()
{
_sampledWaterHeight = _threadSafeWaterMap.WaterHeightOrFloor(SensorCoordinates);
_sampledFloor = GetCurrentFloor();
UpdateOutputState();
}
private void InitializeSensorCoordinates()
{
SensorCoordinates = _blockObject.TransformCoordinates(_depthMonitorSpec.SensorCoordinates);
}
private void UpdateOutputState()
{
bool targetState = _currentlyActive;
// Turn ON if the water goes below or equals the low threshold
if (_sampledWaterHeight <= ThresholdOn) targetState = true;
// Turn OFF if the water goes above or equals the high threshold
if (_sampledWaterHeight >= ThresholdOff) targetState = false;
if (targetState != _currentlyActive)
{
_currentlyActive = targetState;
_automator?.SetState(_currentlyActive);
}
}
private int GetCurrentFloor()
{
if (!_threadSafeWaterMap.TryGetColumnFloor(SensorCoordinates, out var floor))
{
return SensorCoordinates.z;
}
return floor;
}
}
}