Skip to content

Commit b24784f

Browse files
committed
Added attributes to fill level object changes in FillUnitExtension specialization
1 parent c8536d3 commit b24784f

2 files changed

Lines changed: 54 additions & 35 deletions

File tree

docs/FILLUNIT_EXTENSION.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ Trigger object changes based on the fill level percentage.
4242
| Name | Type | Required | Default | Description |
4343
|-----------|-------|----|-----------|------------------------------|
4444
| threshold | float | No | ```0.9```| Defines at which fillUnit fill level percentage the object changes |
45-
45+
| thresholdIsGreater | boolean | No | ```true``` | If true the object changes are activated above threshold, if not then below threshold |
46+
| requiresPoweredOn | boolean | No | ```true``` | |
47+
| requiresTurnedOn | boolean | No | ```false``` | |
4648

4749
### Example
4850
```xml

scripts/specializations/FillUnitExtension.lua

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
---@field soundRequiresPoweredOn boolean
77
---@field sample? table
88
---@field fillLevelObjectChangeThreshold number
9+
---@field fillLevelObjectChangeThresholdIsGreater number
10+
---@field fillLevelObjectChangeRequiresTurnedOn boolean
11+
---@field fillLevelObjectChangeRequiresPoweredOn boolean
912
---@field fillLevelObjectChanges? table
1013

1114
---@class FillUnitExtension_spec
@@ -31,13 +34,16 @@ function FillUnitExtension.initSpecialization()
3134
schema:register(XMLValueType.INT, basePath .. '#fillUnitIndex')
3235

3336
SoundManager.registerSampleXMLPaths(schema, basePath, "fillLevelSound")
34-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresTurnedOn', '', false)
35-
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresPoweredOn', '', true)
3637
schema:register(XMLValueType.FLOAT, basePath .. '.fillLevelSound#threshold', '', 0.9)
3738
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#thresholdIsGreater', '', true)
39+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresTurnedOn', '', false)
40+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelSound#requiresPoweredOn', '', true)
3841

39-
schema:register(XMLValueType.FLOAT, basePath .. '.fillLevelObjectChanges#threshold', 'Defines at which fillUnit fill level percentage the object changes', 0.9)
4042
ObjectChangeUtil.registerObjectChangeXMLPaths(schema, basePath .. ".fillLevelObjectChanges")
43+
schema:register(XMLValueType.FLOAT, basePath .. '.fillLevelObjectChanges#threshold', 'Defines at which fillUnit fill level percentage the object changes', 0.9)
44+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#thresholdIsGreater', '', true)
45+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#requiresTurnedOn', '', false)
46+
schema:register(XMLValueType.BOOL, basePath .. '.fillLevelObjectChanges#requiresPoweredOn', '', true)
4147

4248
schema:setXMLSpecializationType()
4349
end
@@ -68,11 +74,13 @@ function FillUnitExtension:onLoad()
6874
return
6975
end
7076

71-
entry.soundRequiresPoweredOn = xmlFile:getValue(key .. '.fillLevelSound#requiresPoweredOn', true)
72-
entry.soundRequiresTurnedOn = xmlFile:getValue(key .. '.fillLevelSound#requiresTurnedOn', false)
73-
entry.soundThreshold = xmlFile:getValue(key .. '.fillLevelSound#threshold', 0.9)
74-
entry.soundThresholdIsGreater = xmlFile:getValue(key .. '.fillLevelSound#thresholdIsGreater', true)
75-
entry.sample = g_soundManager:loadSampleFromXML(xmlFile, key, "fillLevelSound", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
77+
if self.isClient then
78+
entry.soundRequiresPoweredOn = xmlFile:getValue(key .. '.fillLevelSound#requiresPoweredOn', true)
79+
entry.soundRequiresTurnedOn = xmlFile:getValue(key .. '.fillLevelSound#requiresTurnedOn', false)
80+
entry.soundThreshold = xmlFile:getValue(key .. '.fillLevelSound#threshold', 0.9)
81+
entry.soundThresholdIsGreater = xmlFile:getValue(key .. '.fillLevelSound#thresholdIsGreater', true)
82+
entry.sample = g_soundManager:loadSampleFromXML(xmlFile, key, "fillLevelSound", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
83+
end
7684

7785
entry.fillLevelObjectChanges = {}
7886
ObjectChangeUtil.loadObjectChangeFromXML(xmlFile, key .. '.fillLevelObjectChanges', entry.fillLevelObjectChanges, self.components, self)
@@ -81,6 +89,9 @@ function FillUnitExtension:onLoad()
8189
entry.fillLevelObjectChanges = nil
8290
else
8391
entry.fillLevelObjectChangeThreshold = xmlFile:getValue(key .. '.fillLevelObjectChanges#threshold', 0.9)
92+
entry.fillLevelObjectChangeThresholdIsGreater = xmlFile:getValue(key .. '.fillLevelObjectChanges#thresholdIsGreater', true)
93+
entry.fillLevelObjectChangeRequiresPoweredOn = xmlFile:getValue(key .. '.fillLevelObjectChanges#requiresPoweredOn', true)
94+
entry.fillLevelObjectChangeRequiresTurnedOn = xmlFile:getValue(key .. '.fillLevelObjectChanges#requiresTurnedOn', false)
8495
ObjectChangeUtil.setObjectChanges(entry.fillLevelObjectChanges, false, self, self.setMovingToolDirty)
8596
end
8697

@@ -102,40 +113,46 @@ function FillUnitExtension:onUpdateTick(dt)
102113
local spec = self[FillUnitExtension.SPEC_NAME]
103114

104115
for _, entry in ipairs(spec.fillUnitEntries) do
105-
if entry.fillLevelObjectChanges ~= nil then
106-
local fillLevelPct = self:getFillUnitFillLevelPercentage(entry.fillUnitIndex) or 0
107-
ObjectChangeUtil.setObjectChanges(entry.fillLevelObjectChanges, fillLevelPct > entry.fillLevelObjectChangeThreshold, self, self.setMovingToolDirty)
116+
local fillLevelPct = self:getFillUnitFillLevelPercentage(entry.fillUnitIndex) or 0
117+
local isPowered = self:getIsPowered()
118+
---@type boolean?
119+
local isTurnedOn
120+
121+
if self.getIsTurnedOn ~= nil then
122+
isTurnedOn = self:getIsTurnedOn()
108123
end
109124

110-
FillUnitExtension.updateFillLevelSound(self, entry)
125+
if entry.fillLevelObjectChanges ~= nil then
126+
local isActive = (entry.fillLevelObjectChangeThresholdIsGreater and fillLevelPct > entry.fillLevelObjectChangeThreshold) or (not entry.fillLevelObjectChangeThresholdIsGreater and fillLevelPct < entry.fillLevelObjectChangeThreshold)
111127

112-
if self.isClient then
113-
if entry.sample ~= nil and g_soundManager:getIsSamplePlaying(entry.sample) then
114-
self:raiseActive()
128+
if entry.fillLevelObjectChangeRequiresTurnedOn and isTurnedOn == false then
129+
isActive = false
130+
elseif entry.fillLevelObjectChangeRequiresPoweredOn and not isPowered then
131+
isActive = false
115132
end
133+
134+
ObjectChangeUtil.setObjectChanges(entry.fillLevelObjectChanges, isActive, self, self.setMovingToolDirty)
116135
end
117-
end
118-
end
119136

120-
---@param entry FillUnitEntry
121-
function FillUnitExtension:updateFillLevelSound(entry)
122-
if not self.isClient or not entry.sample then
123-
return
124-
end
137+
if self.isClient and entry.sample ~= nil then
138+
local playSample = (entry.soundThresholdIsGreater and fillLevelPct > entry.soundThreshold) or (not entry.soundThresholdIsGreater and fillLevelPct < entry.soundThreshold)
139+
local isPlaying = g_soundManager:getIsSamplePlaying(entry.sample)
125140

126-
local fillLevelPct = self:getFillUnitFillLevelPercentage(entry.fillUnitIndex) or 0
127-
local playSample = (entry.soundThresholdIsGreater and fillLevelPct > entry.soundThreshold) or (not entry.soundThresholdIsGreater and fillLevelPct < entry.soundThreshold)
128-
local isPlaying = g_soundManager:getIsSamplePlaying(entry.sample)
141+
if entry.soundRequiresTurnedOn and isTurnedOn == false then
142+
playSample = false
143+
elseif entry.soundRequiresPoweredOn and not isPowered then
144+
playSample = false
145+
end
129146

130-
if entry.soundRequiresTurnedOn and self.getIsTurnedOn ~= nil and not self:getIsTurnedOn() then
131-
playSample = false
132-
elseif entry.soundRequiresPoweredOn and not self:getIsPowered() then
133-
playSample = false
134-
end
147+
if playSample and not isPlaying then
148+
g_soundManager:playSample(entry.sample)
149+
elseif not playSample and isPlaying then
150+
g_soundManager:stopSample(entry.sample)
151+
end
135152

136-
if playSample and not isPlaying then
137-
g_soundManager:playSample(entry.sample)
138-
elseif not playSample and isPlaying then
139-
g_soundManager:stopSample(entry.sample)
153+
if g_soundManager:getIsSamplePlaying(entry.sample) then
154+
self:raiseActive()
155+
end
156+
end
140157
end
141158
end

0 commit comments

Comments
 (0)