Esempio n. 1
0
        public static void PartCollisionSound(SceneObjectPart part, List <CollisionForSoundInfo> collidersinfolist)
        {
            if (part.CollisionSoundType < 0)
            {
                return;
            }

            if (collidersinfolist.Count == 0 || part == null)
            {
                return;
            }

            if (part.VolumeDetectActive || (part.Flags & PrimFlags.Physics) == 0)
            {
                return;
            }

            SceneObjectGroup sog = part.ParentGroup;

            if (sog == null || sog.IsDeleted || sog.inTransit)
            {
                return;
            }

            if (sog.CollisionSoundThrottled(part.CollisionSoundType))
            {
                return;
            }

            float volume = part.CollisionSoundVolume;

            UUID soundID = part.CollisionSound;

            bool HaveSound = false;

            switch (part.CollisionSoundType)
            {
            case 0:     // default sounds
                volume = 1.0f;
                break;

            case 1:     // selected sound
                if (soundID == part.invalidCollisionSoundUUID)
                {
                    return;
                }
                HaveSound = true;
                break;

            case 2:     // default sounds with volume set by script
            default:
                break;
            }

            if (volume == 0.0f)
            {
                return;
            }

            bool doneownsound = false;

            int thisMaterial = (int)part.Material;

            if (thisMaterial >= MaxMaterials)
            {
                thisMaterial = 3;
            }
            int thisMatScaled = thisMaterial * MaxMaterials;

            CollisionForSoundInfo colInfo;
            uint id;

            for (int i = 0; i < collidersinfolist.Count; i++)
            {
                colInfo = collidersinfolist[i];

                id = colInfo.colliderID;
                if (id == 0) // terrain collision
                {
                    if (!doneownsound)
                    {
                        if (!HaveSound)
                        {
                            float vol = Math.Abs(colInfo.relativeVel);
                            if (vol < 0.2f)
                            {
                                continue;
                            }

                            vol *= vol * .0625f; // 4m/s == full volume
                            if (vol > 1.0f)
                            {
                                vol = 1.0f;
                            }

                            soundID = m_TerrainPart[thisMaterial];
                            volume *= vol;
                        }
                        part.SendCollisionSound(soundID, volume, colInfo.position);
                        doneownsound = true;
                    }
                    continue;
                }

                SceneObjectPart otherPart = sog.Scene.GetSceneObjectPart(id);
                if (otherPart != null)
                {
                    SceneObjectGroup othersog = otherPart.ParentGroup;
                    if (othersog == null || othersog.IsDeleted || othersog.inTransit)
                    {
                        continue;
                    }

                    int otherType = otherPart.CollisionSoundType;
                    if (otherType < 0 || otherPart.VolumeDetectActive)
                    {
                        continue;
                    }

                    if (!HaveSound)
                    {
                        if (othersog.CollisionSoundThrottled(otherType))
                        {
                            continue;
                        }

                        if (otherType == 1)
                        {
                            soundID = otherPart.CollisionSound;
                            volume  = otherPart.CollisionSoundVolume;
                            if (volume == 0.0f)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (otherType == 2)
                            {
                                volume = otherPart.CollisionSoundVolume;
                                if (volume == 0.0f)
                                {
                                    continue;
                                }
                            }

                            float vol = Math.Abs(colInfo.relativeVel);
                            if (vol < 0.2f)
                            {
                                continue;
                            }

                            vol *= vol * .0625f; // 4m/s == full volume
                            if (vol > 1.0f)
                            {
                                vol = 1.0f;
                            }

                            int otherMaterial = (int)otherPart.Material;
                            if (otherMaterial >= MaxMaterials)
                            {
                                otherMaterial = 3;
                            }

                            soundID = m_PartPart[thisMatScaled + otherMaterial];
                            volume *= vol;
                        }
                    }

                    if (doneownsound)
                    {
                        otherPart.SendCollisionSound(soundID, volume, colInfo.position);
                    }
                    else
                    {
                        part.SendCollisionSound(soundID, volume, colInfo.position);
                        doneownsound = true;
                    }
                }
            }
        }