Scripts/engine_sound.js |
"use strict";
this.name = "engine_sound";
this.author = "Norby";
this.copyright = "2015 Norby";
this.description= "The tone of engine sound follow the speed of your ship.";
this.licence = "CC BY-NC-SA 4.0";
//internal properties, should not touch
this.$Last = 1; //last played sound
this.$Pause = false; //no sound in pause
this.$S = []; //sound objects storing sound files
this.$SS = null; //soundsource
//worldscript events
this.startUp = function() {
for(var i=1; i<26; i++) {
this.$S[i] = Sound.load("bgs-m_ambi_engine1-"+i+".ogg");
}
this.$SS = new SoundSource;
this.$SS.sound = this.$S[1];
this.$SS.loop = true;
this.$T = new Timer(this, this.$Play, 0, 0.25);
this.$T2 = new Timer(this, this.$Play, 0.125, 0.25);
}
this.startUpComplete = function() {
var b = worldScripts["BGS-M"];
// if( b ) b.ambientSounds = false; //turn off BGS engine sound but chatter too
}
this.gamePaused = this.shipDockedWithStation = this.shipDied = this.shipWillEnterWitchspace = function() {
this.$SS.volume = 0;
this.$Pause = true;
}
this.gameResumed = this.shipWillLaunchFromStation = this.shipExitedWitchspace = function() {
this.$Pause = false;
}
//engine sound methods
this.$Play = function() {
var p = player.ship;
if( !p || !p.isValid || p.docked || p.speed < 1 || p.torusEngaged
|| p.speed > 8 * p.maxSpeed + 1
|| !this.$SS || this.$Pause ) {
this.$SS.volume = 0;
return;
}
var s = p.speed;
if( !p.injectorsEngaged && s > p.maxSpeed ) s = p.maxSpeed;
var l = Math.ceil( s/20 );
if( !this.$S[ l ] ) l = 25; //speed over 500
this.$SS.volume = 0.2 + l / 250; //from 0.2 to 0.3
if( this.$Last != l ) {
this.$Last = l;
this.$SS.sound = this.$S[ l ];
this.$SS.play();
}
} |