Пример #1
0
        public static bool PlayContactSound(long entityId, MyStringId strID, Vector3D position, MyStringHash materialA, MyStringHash materialB, float volume = 1, Func <bool> canHear = null, Func <bool> shouldPlay2D = null)
        {
            ProfilerShort.Begin("GetCue");

            MySoundPair cue = MyMaterialPropertiesHelper.Static.GetCollisionCue(strID, materialA, materialB);

            if (!cue.SoundId.IsNull && MyAudio.Static.SourceIsCloseEnoughToPlaySound(position, cue.SoundId))
            {
                MyEntity3DSoundEmitter emitter = MyAudioComponent.TryGetSoundEmitter();
                if (emitter == null)
                {
                    ProfilerShort.End();
                    return(false);
                }
                ProfilerShort.BeginNextBlock("Emitter lambdas");
                MyAudioComponent.ContactSoundsPool.TryAdd(entityId, 0);
                emitter.StoppedPlaying += (e) =>
                {
                    byte val;
                    MyAudioComponent.ContactSoundsPool.TryRemove(entityId, out val);
                };
                if (MySession.Static.Settings.RealisticSound && MyFakes.ENABLE_NEW_SOUNDS)
                {
                    Action <MyEntity3DSoundEmitter> remove = null;
                    remove = (e) =>
                    {
                        emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.CanHear].Remove(canHear);
                        emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.ShouldPlay2D].Remove(shouldPlay2D);
                        emitter.StoppedPlaying -= remove;
                    };
                    emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.CanHear].Add(canHear);
                    emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.ShouldPlay2D].Add(shouldPlay2D);
                    emitter.StoppedPlaying += remove;
                }
                ProfilerShort.BeginNextBlock("PlaySound");
                emitter.SetPosition(position);
                emitter.PlaySound(cue, true);

                if (emitter.Sound != null)
                {
                    if (volume != 0)
                    {
                        emitter.Sound.SetVolume(volume);
                    }
                }

                ProfilerShort.End();
                return(true);
            }

            ProfilerShort.End();
            return(false);
        }
Пример #2
0
        public static void PlayDestructionSound(MySlimBlock b)
        {
            MyPhysicalMaterialDefinition def = null;

            if (b.FatBlock is MyCompoundCubeBlock)
            {
                var compound = b.FatBlock as MyCompoundCubeBlock;
                if (compound.GetBlocksCount() > 0)
                {
                    def = compound.GetBlocks()[0].BlockDefinition.PhysicalMaterial;
                }
            }
            else if (b.FatBlock is MyFracturedBlock)
            {
                MyCubeBlockDefinition bDef;
                if (MyDefinitionManager.Static.TryGetDefinition <MyCubeBlockDefinition>((b.FatBlock as MyFracturedBlock).OriginalBlocks[0], out bDef))
                {
                    def = bDef.PhysicalMaterial;
                }
            }
            else
            {
                def = b.BlockDefinition.PhysicalMaterial;
            }

            if (def == null)
            {
                return;
            }

            MySoundPair destructionCue;

            if (def.GeneralSounds.TryGetValue(m_destructionSound, out destructionCue) && !destructionCue.SoundId.IsNull)
            {
                var emmiter = MyAudioComponent.TryGetSoundEmitter();
                if (emmiter == null)
                {
                    return;
                }
                Vector3D pos;
                b.ComputeWorldCenter(out pos);
                emmiter.SetPosition(pos);
                emmiter.PlaySound(destructionCue);
            }
        }
Пример #3
0
        public static void PlayContactSound(long entityId, Vector3D position, MyStringHash materialA, MyStringHash materialB, float volume = 1, Func <bool> canHear = null, Func <bool> shouldPlay2D = null)
        {
            MySoundPair cue = MyMaterialSoundsHelper.Static.GetCollisionCue(m_startCue, materialA, materialB);

            if (!cue.SoundId.IsNull)
            {
                MyEntity3DSoundEmitter emitter = MyAudioComponent.TryGetSoundEmitter();
                if (emitter == null)
                {
                    ProfilerShort.End();
                    return;
                }

                MyAudioComponent.ContactSoundsPool.TryAdd(entityId, 0);
                emitter.StoppedPlaying += (e) =>
                {
                    byte val;
                    MyAudioComponent.ContactSoundsPool.TryRemove(entityId, out val);
                };
                if (MySession.Static.Settings.RealisticSound && MyFakes.ENABLE_NEW_SOUNDS)
                {
                    Action <MyEntity3DSoundEmitter> remove = null;
                    remove = (e) =>
                    {
                        emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.CanHear].Remove(canHear);
                        emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.ShouldPlay2D].Remove(shouldPlay2D);
                        emitter.StoppedPlaying -= remove;
                    };
                    emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.CanHear].Add(canHear);
                    emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.ShouldPlay2D].Add(shouldPlay2D);
                    emitter.StoppedPlaying += remove;
                }

                emitter.SetPosition(position);
                emitter.PlaySound(cue, true);

                if (emitter.Sound != null)
                {
                    if (volume != 0)
                    {
                        emitter.Sound.SetVolume(volume);
                    }
                }
            }
        }
Пример #4
0
        public static void PlayDestructionSound(MyFracturedPiece fp)
        {
            var bDef = MyDefinitionManager.Static.GetCubeBlockDefinition(fp.OriginalBlocks[0]);

            if (bDef == null)
            {
                return;
            }
            MyPhysicalMaterialDefinition def = bDef.PhysicalMaterial;

            MySoundPair destructionCue;

            if (def.GeneralSounds.TryGetValue(m_destructionSound, out destructionCue) && !destructionCue.SoundId.IsNull)
            {
                var emmiter = MyAudioComponent.TryGetSoundEmitter();
                if (emmiter == null)
                {
                    return;
                }
                Vector3D pos = fp.PositionComp.GetPosition();
                emmiter.SetPosition(pos);
                emmiter.PlaySound(destructionCue);
            }
        }
Пример #5
0
        public static bool PlayContactSound(long entityId, MyStringId strID, Vector3D position, MyStringHash materialA, MyStringHash materialB, float volume = 1, Func <bool> canHear = null, Func <bool> shouldPlay2D = null, MyEntity surfaceEntity = null, float separatingVelocity = 0f)
        {
            ProfilerShort.Begin("GetCue");

            MyEntity firstEntity = null;

            MyEntities.TryGetEntityById(entityId, out firstEntity);
            if (firstEntity == null)
            {
                ProfilerShort.End();
                return(false);
            }

            MySoundPair cue = (firstEntity.Physics != null && firstEntity.Physics.IsStatic == false) ?
                              MyMaterialPropertiesHelper.Static.GetCollisionCueWithMass(strID, materialA, materialB, ref volume, firstEntity.Physics.Mass, separatingVelocity) :
                              MyMaterialPropertiesHelper.Static.GetCollisionCue(strID, materialA, materialB);

            if (separatingVelocity > 0f && separatingVelocity < 0.5f)
            {
                return(false);
            }

            if (!cue.SoundId.IsNull && MyAudio.Static.SourceIsCloseEnoughToPlaySound(position, cue.SoundId))
            {
                MyEntity3DSoundEmitter emitter = MyAudioComponent.TryGetSoundEmitter();
                if (emitter == null)
                {
                    ProfilerShort.End();
                    return(false);
                }
                ProfilerShort.BeginNextBlock("Emitter lambdas");
                MyAudioComponent.ContactSoundsPool.TryAdd(entityId, 0);
                emitter.StoppedPlaying += (e) =>
                {
                    byte val;
                    MyAudioComponent.ContactSoundsPool.TryRemove(entityId, out val);
                };
                if (MySession.Static.Settings.RealisticSound && MyFakes.ENABLE_NEW_SOUNDS)
                {
                    Action <MyEntity3DSoundEmitter> remove = null;
                    remove = (e) =>
                    {
                        emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.CanHear].Remove(canHear);
                        emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.ShouldPlay2D].Remove(shouldPlay2D);
                        emitter.StoppedPlaying -= remove;
                    };
                    emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.CanHear].Add(canHear);
                    emitter.EmitterMethods[MyEntity3DSoundEmitter.MethodsEnum.ShouldPlay2D].Add(shouldPlay2D);
                    emitter.StoppedPlaying += remove;
                }
                ProfilerShort.BeginNextBlock("PlaySound");
                if (surfaceEntity != null)
                {
                    emitter.Entity = surfaceEntity;
                }
                else
                {
                    emitter.Entity = firstEntity;
                }
                emitter.SetPosition(position);

                //GR: Changed stopPrevious argument to false due to bugs with explosion sound. May revision to the future
                emitter.PlaySound(cue, false);

                if (emitter.Sound != null && volume != 0)
                {
                    emitter.Sound.SetVolume(volume);
                }

                ProfilerShort.End();
                return(true);
            }

            ProfilerShort.End();
            return(false);
        }