Fix for stop playing engine sound when scenario is going to disable

This commit is contained in:
PlayeRom 2022-08-18 19:17:26 +02:00
parent 1982df9f16
commit 5aa5f731a6
2 changed files with 53 additions and 18 deletions

View File

@ -43,11 +43,6 @@
<property>/addons/by-id/org.flightgear.addons.Aerotow/addon-devel/ai-model</property>
<value type="string">Cub</value>
</binding>
<binding>
<command>property-assign</command>
<property>/addons/by-id/org.flightgear.addons.Aerotow/addon-devel/sound/enable</property>
<value type="bool">true</value>
</binding>
</item>
<item>
<label>Call for Robin DR400 aircraft</label>
@ -56,11 +51,6 @@
<property>/addons/by-id/org.flightgear.addons.Aerotow/addon-devel/ai-model</property>
<value type="string">DR400</value>
</binding>
<binding>
<command>property-assign</command>
<property>/addons/by-id/org.flightgear.addons.Aerotow/addon-devel/sound/enable</property>
<value type="bool">true</value>
</binding>
</item>
<item>
<label>Call for Cessna 182 aircraft</label>
@ -69,11 +59,6 @@
<property>/addons/by-id/org.flightgear.addons.Aerotow/addon-devel/ai-model</property>
<value type="string">c182</value>
</binding>
<binding>
<command>property-assign</command>
<property>/addons/by-id/org.flightgear.addons.Aerotow/addon-devel/sound/enable</property>
<value type="bool">true</value>
</binding>
</item>
<item>
<label>Disable tow aircraft</label>
@ -84,7 +69,14 @@
</binding>
<binding>
<command>nasal</command>
<script><![CDATA[aerotow.stopAerotow();]]></script>
<script><![CDATA[
# Run stopAerotow() with a delay to ensure that the engine sound turns off
var timer = maketimer(1, func () {
aerotow.stopAerotow();
});
timer.singleShot = 1;
timer.start();
]]></script>
</binding>
</item>
<item>

View File

@ -41,7 +41,7 @@ var g_towListeners = [];
var init = func () {
# Listener for ai-model property triggered when the user select a tow aircraft from add-on menu
append(g_towListeners, setlistener(ADDON_NODE_PATH ~ "/addon-devel/ai-model", func () {
startAerotow();
restartAerotow();
}));
append(g_towListeners, setlistener("/sim/presets/longitude-deg", func () {
@ -104,6 +104,44 @@ var uninit = func () {
}
}
#
# Function for restart AI scenario with delay when the sound has to stop.
#
# Return 1 on successful, otherwise 0.
#
var restartAerotow = func () {
messages.displayOk("Aerotow in the way");
# Stop playing engine sound
setprop(ADDON_NODE_PATH ~ "/addon-devel/sound/enable", 0);
# Wait a second for the engine sound to turn off
var timer = maketimer(1, func () {
unloadScenario();
});
timer.singleShot = 1;
timer.start();
}
#
# Unload scenario and start a new one
#
var unloadScenario = func () {
if (g_isScenarioLoaded) {
var args = props.Node.new({ "name": SCENARIO_ID });
if (fgcommand("unload-scenario", args)) {
g_isScenarioLoaded = 0;
}
}
# Start aerotow with delay to avoid duplicate engine sound playing
var timer = maketimer(1, func () {
startAerotow();
});
timer.singleShot = 1;
timer.start();
}
#
# Main function to prepare AI scenario and run it.
#
@ -111,8 +149,11 @@ var uninit = func () {
#
var startAerotow = func () {
var args = props.Node.new({ "name": SCENARIO_ID });
if (g_isScenarioLoaded) {
fgcommand("unload-scenario", args);
if (fgcommand("unload-scenario", args)) {
g_isScenarioLoaded = 0;
}
}
generateScenarioXml();
@ -124,6 +165,8 @@ var startAerotow = func () {
if (fgcommand("load-scenario", args)) {
g_isScenarioLoaded = 1;
messages.displayOk("Let's fly!");
setprop(ADDON_NODE_PATH ~ "/addon-devel/sound/enable", 1);
return 1;
}