コード例 #1
0
        public override void OnStart(StartState state)
        {
            if (state == StartState.Editor || state == StartState.None)
            {
                return;
            }

            string partParentName = part.name + "_" + this.moduleName;

            audioParent = part.gameObject.GetChild(partParentName);
            if (audioParent == null)
            {
                audioParent = new GameObject(partParentName);
                audioParent.transform.rotation = part.transform.rotation;
                audioParent.transform.position = part.transform.position;
                audioParent.transform.parent   = part.transform;
            }

            var configNode = AudioUtility.GetConfigNode(part.partInfo.name, this.moduleName);

            SoundLayers = AudioUtility.CreateSoundLayerGroup(configNode.GetNodes("SOUNDLAYER"));

            if (part.isLaunchClamp())
            {
                fxGroup     = part.findFxGroup("activate");
                isDecoupler = true;
            }

            if (part.GetComponent <ModuleDecouplerBase>())
            {
                fxGroup     = part.findFxGroup("decouple");
                isDecoupler = true;
            }

            if (fxGroup != null)
            {
                if (SoundLayers.Where(x => x.name == fxGroup.name).Count() > 0)
                {
                    var soundLayer = SoundLayers.Find(x => x.name == fxGroup.name);
                    if (soundLayer.audioClips != null)
                    {
                        var clip = GameDatabase.Instance.GetAudioClip(soundLayer.audioClips[0]);
                        if (clip != null)
                        {
                            fxGroup.sfx   = clip;
                            fxGroup.audio = AudioUtility.CreateOneShotSource(
                                audioParent,
                                soundLayer.volume * HighLogic.CurrentGame.Parameters.CustomParams <Settings>().ShipVolume,
                                soundLayer.pitch,
                                soundLayer.maxDistance,
                                soundLayer.spread);

                            Sources.Add(soundLayer.name, fxGroup.audio);
                        }
                    }
                }
            }

            GameEvents.onGameUnpause.Add(onGameUnpause);
            GameEvents.onDockingComplete.Add(onDock);
            GameEvents.onPartUndockComplete.Add(onUnDock);
        }
コード例 #2
0
        public override void OnUpdate()
        {
            if (audioParent == null || !HighLogic.LoadedSceneIsFlight || gamePaused || !initialized)
            {
                return;
            }

            foreach (var engineModule in engineModules)
            {
                string engineID       = engineModule.engineID;
                bool   engineIgnited  = engineModule.EngineIgnited;
                bool   engineFlameout = engineModule.flameout;

                float control = engineModule.GetCurrentThrust() / engineModule.maxThrust;

                if (SoundLayerGroups.ContainsKey(engineID))
                {
                    float finalControl = control;

                    foreach (var soundLayer in SoundLayerGroups[engineID])
                    {
                        string sourceLayerName = engineID + "_" + soundLayer.name;

                        if (!spools.ContainsKey(sourceLayerName))
                        {
                            spools.Add(sourceLayerName, 0);
                        }

                        if (soundLayer.spool)
                        {
                            if (engineModule.flameout)
                            {
                                spools[sourceLayerName] = Mathf.MoveTowards(spools[sourceLayerName], 0, Mathf.Max(0.1f, engineModule.currentThrottle));
                            }
                            else
                            {
                                float idle = engineModule.EngineIgnited ? soundLayer.spoolIdle : 0;
                                spools[sourceLayerName] = Mathf.MoveTowards(spools[sourceLayerName], Mathf.Max(idle, engineModule.currentThrottle), soundLayer.spoolSpeed * TimeWarp.deltaTime);
                                finalControl            = spools[sourceLayerName];
                            }
                        }
                        else
                        {
                            //fix for audiosource clicks
                            spools[sourceLayerName] = Mathf.MoveTowards(spools[sourceLayerName], control, Mathf.Max(0.1f, control));
                            finalControl            = spools[sourceLayerName];
                        }

                        //For Looped sounds cleanup
                        if (finalControl < float.Epsilon)
                        {
                            if (Sources.ContainsKey(sourceLayerName))
                            {
                                Sources[sourceLayerName].Stop();
                            }
                            continue;
                        }

                        AudioSource source;
                        if (!Sources.ContainsKey(sourceLayerName))
                        {
                            source = AudioUtility.CreateSource(audioParent, soundLayer);
                            Sources.Add(sourceLayerName, source);
                        }
                        else
                        {
                            source = Sources[sourceLayerName];
                        }

                        source.volume = soundLayer.volume.Value(finalControl) * volume * HighLogic.CurrentGame.Parameters.CustomParams <Settings>().ShipVolume;
                        source.pitch  = soundLayer.pitch.Value(finalControl);

                        AudioUtility.PlayAtChannel(source, soundLayer.channel, soundLayer.loop, soundLayer.loopAtRandom);
                    }
                }

                foreach (var soundLayer in SoundLayerGroups)
                {
                    switch (soundLayer.Key)
                    {
                    case "Engage":
                        if (engineIgnited && !ignites[engineID])
                        {
                            ignites[engineID] = true;
                        }
                        else
                        {
                            ignites[engineID] = engineIgnited;
                            continue;
                        }
                        break;

                    case "Disengage":
                        if (!engineIgnited && ignites[engineID])
                        {
                            ignites[engineID] = false;
                        }
                        else
                        {
                            ignites[engineID] = engineIgnited;
                            continue;
                        }
                        break;

                    case "Flameout":
                        if (engineFlameout && !flameouts[engineID])
                        {
                            flameouts[engineID] = true;
                        }
                        else
                        {
                            flameouts[engineID] = engineFlameout;
                            continue;
                        }
                        break;

                    default:
                        continue;
                    }

                    var oneShotLayers = soundLayer.Value;
                    foreach (var oneShotLayer in oneShotLayers)
                    {
                        if (oneShotLayer.audioClips != null)
                        {
                            var    clip             = GameDatabase.Instance.GetAudioClip(oneShotLayer.audioClips[0]);
                            string oneShotLayerName = soundLayer.Key + "_" + oneShotLayer.name;

                            AudioSource source;

                            if (Sources.ContainsKey(oneShotLayerName))
                            {
                                source = Sources[oneShotLayerName];
                            }
                            else
                            {
                                source = AudioUtility.CreateOneShotSource(audioParent, 1, oneShotLayer.pitch, oneShotLayer.maxDistance, oneShotLayer.spread);
                                Sources.Add(oneShotLayerName, source);
                            }

                            float finalVolume = oneShotLayer.volume * volume * HighLogic.CurrentGame.Parameters.CustomParams <Settings>().ShipVolume;
                            AudioUtility.PlayAtChannel(source, oneShotLayer.channel, false, false, true, finalVolume, clip);
                        }
                    }
                }
            }

            if (Sources.Count > 0)
            {
                var sourceKeys = Sources.Keys.ToList();
                foreach (var source in sourceKeys)
                {
                    if (!Sources[source].isPlaying)
                    {
                        UnityEngine.Object.Destroy(Sources[source]);
                        Sources.Remove(source);
                    }
                }
            }
        }
コード例 #3
0
        void PlaySounds(CollisionType collisionType, float control, CollidingObject collidingObjectType = CollidingObject.Dirt, bool oneshot = false)
        {
            foreach (var soundLayer in SoundLayerGroups[collisionType])
            {
                float finalVolume = soundLayer.volume.Value(control) * soundLayer.massToVolume.Value((float)part.physicsMass);
                float finalPitch  = soundLayer.pitch.Value(control) * soundLayer.massToPitch.Value((float)part.physicsMass);

                var layerMaskName = soundLayer.data.ToLower();
                if (layerMaskName != "")
                {
                    switch (collidingObjectType)
                    {
                    case CollidingObject.Vessel:
                        if (!layerMaskName.Contains("vessel"))
                        {
                            finalVolume = 0;
                        }
                        break;

                    case CollidingObject.Concrete:
                        if (!layerMaskName.Contains("concrete"))
                        {
                            finalVolume = 0;
                        }
                        break;

                    case CollidingObject.Dirt:
                        if (!layerMaskName.Contains("dirt"))
                        {
                            finalVolume = 0;
                        }
                        break;
                    }
                }

                if (finalVolume > float.Epsilon)
                {
                    if (!Sources.ContainsKey(soundLayer.name))
                    {
                        if (oneshot)
                        {
                            Sources.Add(soundLayer.name, AudioUtility.CreateOneShotSource(part.gameObject, 1, 1, soundLayer.maxDistance, soundLayer.spread));
                        }
                        else
                        {
                            Sources.Add(soundLayer.name, AudioUtility.CreateSource(part.gameObject, soundLayer));
                        }
                    }

                    var source = Sources[soundLayer.name];

                    if (source == null)
                    {
                        return;
                    }

                    finalVolume *= HighLogic.CurrentGame.Parameters.CustomParams <Settings>().EffectsVolume;
                    source.pitch = finalPitch;

                    if (oneshot)
                    {
                        var audioClips = soundLayer.audioClips;
                        if (audioClips == null)
                        {
                            continue;
                        }

                        int index = 0;
                        if (audioClips.Length > 1)
                        {
                            index = UnityEngine.Random.Range(0, audioClips.Length);
                        }

                        var clip = GameDatabase.Instance.GetAudioClip(audioClips[index]);

                        source.volume = 1;
                        finalVolume  *= UnityEngine.Random.Range(0.9f, 1.0f);
                        AudioUtility.PlayAtChannel(source, soundLayer.channel, false, false, true, finalVolume, clip);
                    }
                    else
                    {
                        source.volume = finalVolume;
                        AudioUtility.PlayAtChannel(source, soundLayer.channel, soundLayer.loop, soundLayer.loopAtRandom);
                    }
                }
                else
                {
                    if (Sources.ContainsKey(soundLayer.name) && Sources[soundLayer.name].isPlaying)
                    {
                        Sources[soundLayer.name].Stop();
                    }
                }
            }
        }
コード例 #4
0
        public override void OnUpdate()
        {
            if (!initialized || !moduleWheel || !moduleWheel.Wheel || !audioParent || gamePaused)
            {
                return;
            }

            bool  running          = false;
            bool  motorEnabled     = false;
            float driveOutput      = 0;
            float wheelSpeed       = moduleWheel.Wheel.WheelRadius * moduleWheel.Wheel.wheelCollider.angularVelocity;
            float slipDisplacement = Mathf.Abs(GetSlipDisplacement(wheelSpeed));

            WheelHit hit;

            //ISSUES: it wont detect Vessels :(
            CollidingObject colObjectType = CollidingObject.Dirt;

            if (moduleWheel.Wheel.wheelCollider.GetGroundHit(out hit))
            {
                colObjectType = AudioUtility.GetCollidingType(hit.collider.gameObject);
            }

            if (moduleMotor)
            {
                running      = moduleMotor.state == ModuleWheelMotor.MotorState.Running;
                motorEnabled = moduleMotor.motorEnabled;
                driveOutput  = moduleMotor.driveOutput;
            }

            bool isRetracted = false;

            if (moduleDeploy)
            {
                isRetracted = moduleDeploy.stateString == "Retracted";
            }

            foreach (var soundLayerGroup in SoundLayerGroups)
            {
                string soundLayerKey = soundLayerGroup.Key;
                float  control       = 0;
                float  masterVolume  = HighLogic.CurrentGame.Parameters.CustomParams <Settings>().ShipVolume;

                if (!isRetracted)
                {
                    switch (soundLayerGroup.Key)
                    {
                    case "Torque":
                        control = running ? driveOutput / 100 : 0;
                        break;

                    case "Speed":
                        control = motorEnabled ? Mathf.Abs(wheelSpeed) : 0;
                        break;

                    case "Ground":
                        control      = moduleWheel.isGrounded ? Mathf.Abs(wheelSpeed) : 0;
                        masterVolume = HighLogic.CurrentGame.Parameters.CustomParams <Settings>().EffectsVolume;
                        break;

                    case "Slip":
                        control      = moduleWheel.isGrounded ? slipDisplacement : 0;
                        masterVolume = HighLogic.CurrentGame.Parameters.CustomParams <Settings>().EffectsVolume;
                        break;

                    default:
                        continue;
                    }
                }

                foreach (var soundLayer in soundLayerGroup.Value)
                {
                    float finalControl = control;

                    if (soundLayerKey == "Ground" || soundLayerKey == "Slip")
                    {
                        string layerMaskName = soundLayer.data;
                        if (layerMaskName != "")
                        {
                            switch (colObjectType)
                            {
                            case CollidingObject.Vessel:
                                if (!layerMaskName.Contains("vessel"))
                                {
                                    finalControl = 0;
                                }
                                break;

                            case CollidingObject.Concrete:
                                if (!layerMaskName.Contains("concrete"))
                                {
                                    finalControl = 0;
                                }
                                break;

                            case CollidingObject.Dirt:
                                if (!layerMaskName.Contains("dirt"))
                                {
                                    finalControl = 0;
                                }
                                break;
                            }
                        }
                    }

                    if (!spools.ContainsKey(soundLayer.name))
                    {
                        spools.Add(soundLayer.name, 0);
                    }

                    if (soundLayer.spool)
                    {
                        spools[soundLayer.name] = Mathf.MoveTowards(spools[soundLayer.name], finalControl, soundLayer.spoolSpeed * TimeWarp.deltaTime);
                        finalControl            = spools[soundLayer.name];
                    }
                    else
                    {
                        //fix for audiosource clicks
                        spools[soundLayer.name] = Mathf.MoveTowards(spools[soundLayer.name], control, Mathf.Max(0.1f, control));
                        finalControl            = spools[soundLayer.name];
                    }

                    if (finalControl < 0.01f)
                    {
                        if (Sources.ContainsKey(soundLayer.name))
                        {
                            UnityEngine.Object.Destroy(Sources[soundLayer.name]);
                            Sources.Remove(soundLayer.name);
                        }
                        continue;
                    }

                    AudioSource source;
                    if (Sources.ContainsKey(soundLayer.name))
                    {
                        source = Sources[soundLayer.name];
                    }
                    else
                    {
                        source = AudioUtility.CreateSource(audioParent, soundLayer);
                        Sources.Add(soundLayer.name, source);
                    }

                    source.volume = soundLayer.volume.Value(finalControl) * volume * masterVolume;
                    source.pitch  = soundLayer.pitch.Value(finalControl);

                    AudioUtility.PlayAtChannel(source, soundLayer.channel, soundLayer.loop, soundLayer.loopAtRandom);
                }
            }

            if (Sources.Count > 0)
            {
                var sourceKeys = Sources.Keys.ToList();
                foreach (var source in sourceKeys)
                {
                    if (!Sources[source].isPlaying)
                    {
                        UnityEngine.Object.Destroy(Sources[source]);
                        Sources.Remove(source);
                    }
                }
            }
        }
コード例 #5
0
        void LateUpdate()
        {
            if (vessel.loaded && !initialized && !ignoreVessel)
            {
                Initialize();
                return;
            }

            if (vessel == null | !vessel.loaded || vessel.isEVA || !HighLogic.LoadedSceneIsFlight || !initialized || ignoreVessel || gamePause)
            {
                return;
            }

            //calculate forces
            Acceleration     = (float)vessel.geeForce * 9.81f;
            Jerk             = Mathf.Abs(pastAcceleration - Acceleration) / Time.fixedDeltaTime;
            pastAcceleration = Acceleration;

            if (timeOut != 60)
            {
                timeOut++;
                return;
            }

            TotalMass = vessel.GetTotalMass();
            DryMass   = vessel.Parts.Sum(x => x.prefabMass);

            var excludedPart = vessel.Parts.Find(x => x.Modules.Contains("ModuleAsteroid"));

            if (excludedPart != null)
            {
                TotalMass -= excludedPart.mass;
                DryMass   -= excludedPart.prefabMass;
            }

            float controlDampener = Mathf.Lerp(0.2f, 0.1f, DryMass / TotalMass);

            foreach (var soundLayer in SoundLayers)
            {
                if (!controllers.ContainsKey(soundLayer.name))
                {
                    controllers.Add(soundLayer.name, 0);
                }
                float controller = GetController(soundLayer.data);
                float control    = Mathf.MoveTowards(controllers[soundLayer.name], controller, Mathf.Max(1, Mathf.Abs(controllers[soundLayer.name] - controller)) * TimeWarp.deltaTime);
                controllers[soundLayer.name] = control;

                float finalVolume = soundLayer.volume.Value(control) * soundLayer.massToVolume.Value(TotalMass);
                float finalPitch  = soundLayer.pitch.Value(control) * soundLayer.massToPitch.Value(TotalMass);

                bool skip = (soundLayer.channel == FXChannel.ShipInternal && vessel != FlightGlobals.ActiveVessel);

                if (finalVolume < float.Epsilon || float.IsNaN(control) || float.IsInfinity(control) || skip)
                {
                    if (Sources.ContainsKey(soundLayer.name))
                    {
                        UnityEngine.Object.Destroy(Sources[soundLayer.name]);
                        Sources.Remove(soundLayer.name);
                    }
                    continue;
                }

                AudioSource source;
                if (Sources.ContainsKey(soundLayer.name))
                {
                    source = Sources[soundLayer.name];
                }
                else
                {
                    source = AudioUtility.CreateSource(vessel.gameObject, soundLayer);
                    Sources.Add(soundLayer.name, source);
                }

                source.volume = finalVolume * HighLogic.CurrentGame.Parameters.CustomParams <Settings>().EffectsVolume;
                source.pitch  = finalPitch;

                AudioUtility.PlayAtChannel(source, soundLayer.channel, soundLayer.loop, soundLayer.loopAtRandom);
            }
        }