Example #1
0
        public static RoundSound LoadRoundSound(XElement element, bool stream = false)
        {
            if (GameMain.SoundManager?.Disabled ?? true)
            {
                return(null);
            }

            string filename = element.GetAttributeString("file", "");

            if (string.IsNullOrEmpty(filename))
            {
                filename = element.GetAttributeString("sound", "");
            }

            if (string.IsNullOrEmpty(filename))
            {
                string errorMsg = "Error when loading round sound (" + element + ") - file path not set";
                DebugConsole.ThrowError(errorMsg);
                GameAnalyticsManager.AddErrorEventOnce("Submarine.LoadRoundSound:FilePathEmpty" + element.ToString(), GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg + "\n" + Environment.StackTrace);
                return(null);
            }

            filename = Path.GetFullPath(filename.CleanUpPath()).CleanUpPath();
            Sound existingSound = null;

            if (roundSounds == null)
            {
                roundSounds = new List <RoundSound>();
            }
            else
            {
                existingSound = roundSounds.Find(s => s.Filename == filename && s.Stream == stream)?.Sound;
            }

            if (existingSound == null)
            {
                try
                {
                    existingSound = GameMain.SoundManager.LoadSound(filename, stream);
                    if (existingSound == null)
                    {
                        return(null);
                    }
                }
                catch (System.IO.FileNotFoundException e)
                {
                    string errorMsg = "Failed to load sound file \"" + filename + "\".";
                    DebugConsole.ThrowError(errorMsg, e);
                    GameAnalyticsManager.AddErrorEventOnce("Submarine.LoadRoundSound:FileNotFound" + filename, GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg + "\n" + Environment.StackTrace);
                    return(null);
                }
            }

            RoundSound newSound = new RoundSound(element, existingSound);

            roundSounds.Add(newSound);
            return(newSound);
        }
Example #2
0
        public static RoundSound LoadRoundSound(XElement element, bool stream = false)
        {
            string filename = element.GetAttributeString("file", "");

            if (string.IsNullOrEmpty(filename))
            {
                filename = element.GetAttributeString("sound", "");
            }

            if (string.IsNullOrEmpty(filename))
            {
                DebugConsole.ThrowError("Error when loading round sound (" + element + ") - file path not set");
                return(null);
            }

            filename = Path.GetFullPath(filename);
            Sound existingSound = null;

            if (roundSounds == null)
            {
                roundSounds = new List <RoundSound>();
            }
            else
            {
                existingSound = roundSounds.Find(s => s.Filename == filename && s.Stream == stream)?.Sound;
            }

            if (existingSound == null)
            {
                try
                {
                    existingSound = GameMain.SoundManager.LoadSound(filename, stream);
                    if (existingSound == null)
                    {
                        return(null);
                    }
                }
                catch (FileNotFoundException e)
                {
                    DebugConsole.ThrowError("Failed to load sound file \"" + filename + "\".", e);
                    return(null);
                }
            }

            RoundSound newSound = new RoundSound(element, existingSound);

            roundSounds.Add(newSound);
            return(newSound);
        }
Example #3
0
        private static void RemoveRoundSound(RoundSound roundSound)
        {
            roundSound.Sound?.Dispose();
            if (roundSounds == null)
            {
                return;
            }

            if (roundSounds.Contains(roundSound))
            {
                roundSounds.Remove(roundSound);
            }
            foreach (RoundSound otherSound in roundSounds)
            {
                if (otherSound.Sound == roundSound.Sound)
                {
                    otherSound.Sound = null;
                }
            }
        }
        public static void ReloadRoundSound(RoundSound roundSound)
        {
            Sound existingSound = roundSounds?.Find(s => s.Filename == roundSound.Filename && s.Stream == roundSound.Stream && !s.Sound.Disposed)?.Sound;

            if (existingSound == null)
            {
                try
                {
                    existingSound = GameMain.SoundManager.LoadSound(roundSound.Filename, roundSound.Stream);
                }
                catch (System.IO.FileNotFoundException e)
                {
                    string errorMsg = "Failed to load sound file \"" + roundSound.Filename + "\".";
                    DebugConsole.ThrowError(errorMsg, e);
                    GameAnalyticsManager.AddErrorEventOnce("Submarine.LoadRoundSound:FileNotFound" + roundSound.Filename, GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg + "\n" + Environment.StackTrace.CleanupStackTrace());
                    return;
                }
            }
            roundSound.Sound = existingSound;
        }
Example #5
0
        partial void InitProjSpecific(XElement element)
        {
            if (element.Attribute("sound") != null)
            {
                DebugConsole.ThrowError("Error in attack (" + element + ") - sounds should be defined as child elements, not as attributes.");
                return;
            }

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "particleemitter":
                    particleEmitter = new ParticleEmitter(subElement);
                    break;

                case "sound":
                    sound = Submarine.LoadRoundSound(subElement);
                    break;
                }
            }
        }
Example #6
0
        partial void InitProjSpecific()
        {
            CurrentSwingAmount      = Prefab.SwingAmountRad;
            CurrentScaleOscillation = Prefab.ScaleOscillation;

            SwingTimer          = Rand.Range(0.0f, MathHelper.TwoPi);
            ScaleOscillateTimer = Rand.Range(0.0f, MathHelper.TwoPi);

            if (Prefab.ParticleEmitterPrefabs != null)
            {
                ParticleEmitters        = new ParticleEmitter[Prefab.ParticleEmitterPrefabs.Count];
                ParticleEmitterTriggers = new LevelTrigger[Prefab.ParticleEmitterPrefabs.Count];
                for (int i = 0; i < Prefab.ParticleEmitterPrefabs.Count; i++)
                {
                    ParticleEmitters[i]        = new ParticleEmitter(Prefab.ParticleEmitterPrefabs[i]);
                    ParticleEmitterTriggers[i] = Prefab.ParticleEmitterTriggerIndex[i] > -1 ?
                                                 Triggers[Prefab.ParticleEmitterTriggerIndex[i]] : null;
                }
            }

            if (Prefab.LightSourceParams != null)
            {
                LightSources        = new LightSource[Prefab.LightSourceParams.Count];
                LightSourceTriggers = new LevelTrigger[Prefab.LightSourceParams.Count];
                for (int i = 0; i < Prefab.LightSourceParams.Count; i++)
                {
                    LightSources[i] = new LightSource(Prefab.LightSourceParams[i])
                    {
                        Position     = new Vector2(Position.X, Position.Y),
                        IsBackground = true
                    };
                    LightSourceTriggers[i] = Prefab.LightSourceTriggerIndex[i] > -1 ?
                                             Triggers[Prefab.LightSourceTriggerIndex[i]] : null;
                }
            }

            Sounds        = new RoundSound[Prefab.Sounds.Count];
            SoundChannels = new SoundChannel[Prefab.Sounds.Count];
            SoundTriggers = new LevelTrigger[Prefab.Sounds.Count];
            for (int i = 0; i < Prefab.Sounds.Count; i++)
            {
                Sounds[i]        = Submarine.LoadRoundSound(Prefab.Sounds[i].SoundElement, false);
                SoundTriggers[i] = Prefab.Sounds[i].TriggerIndex > -1 ? Triggers[Prefab.Sounds[i].TriggerIndex] : null;
            }

            int j = 0;

            foreach (XElement subElement in Prefab.Config.Elements())
            {
                if (subElement.Name.ToString().ToLowerInvariant() != "deformablesprite")
                {
                    continue;
                }
                foreach (XElement animationElement in subElement.Elements())
                {
                    var newDeformation = SpriteDeformation.Load(animationElement, Prefab.Name);
                    if (newDeformation != null)
                    {
                        newDeformation.DeformationParams = Prefab.SpriteDeformations[j].DeformationParams;
                        spriteDeformations.Add(newDeformation);
                        j++;
                    }
                }
            }
        }
Example #7
0
        public void Update(float deltaTime)
        {
            if (ParticleEmitters != null)
            {
                for (int i = 0; i < ParticleEmitters.Length; i++)
                {
                    if (ParticleEmitterTriggers[i] != null && !ParticleEmitterTriggers[i].IsTriggered)
                    {
                        continue;
                    }
                    Vector2 emitterPos = LocalToWorld(Prefab.EmitterPositions[i]);
                    ParticleEmitters[i].Emit(deltaTime, emitterPos, hullGuess: null,
                                             angle: ParticleEmitters[i].Prefab.CopyEntityAngle ? Rotation : 0.0f);
                }
            }

            CurrentRotation = Rotation;
            if (ActivePrefab.SwingFrequency > 0.0f)
            {
                SwingTimer += deltaTime * ActivePrefab.SwingFrequency;
                SwingTimer  = SwingTimer % MathHelper.TwoPi;
                //lerp the swing amount to the correct value to prevent it from abruptly changing to a different value
                //when a trigger changes the swing amoung
                CurrentSwingAmount = MathHelper.Lerp(CurrentSwingAmount, ActivePrefab.SwingAmountRad, deltaTime * 10.0f);

                if (ActivePrefab.SwingAmountRad > 0.0f)
                {
                    CurrentRotation += (float)Math.Sin(SwingTimer) * CurrentSwingAmount;
                }
            }

            CurrentScale = Vector2.One * Scale;
            if (ActivePrefab.ScaleOscillationFrequency > 0.0f)
            {
                ScaleOscillateTimer    += deltaTime * ActivePrefab.ScaleOscillationFrequency;
                ScaleOscillateTimer     = ScaleOscillateTimer % MathHelper.TwoPi;
                CurrentScaleOscillation = Vector2.Lerp(CurrentScaleOscillation, ActivePrefab.ScaleOscillation, deltaTime * 10.0f);

                float sin = (float)Math.Sin(ScaleOscillateTimer);
                CurrentScale *= new Vector2(
                    1.0f + sin * CurrentScaleOscillation.X,
                    1.0f + sin * CurrentScaleOscillation.Y);
            }

            if (LightSources != null)
            {
                for (int i = 0; i < LightSources.Length; i++)
                {
                    if (LightSourceTriggers[i] != null)
                    {
                        LightSources[i].Enabled = LightSourceTriggers[i].IsTriggered;
                    }
                    LightSources[i].Rotation    = -CurrentRotation;
                    LightSources[i].SpriteScale = CurrentScale;
                }
            }

            if (spriteDeformations.Count > 0)
            {
                UpdateDeformations(deltaTime);
            }

            for (int i = 0; i < Sounds.Length; i++)
            {
                if (SoundTriggers[i] == null || SoundTriggers[i].IsTriggered)
                {
                    RoundSound roundSound = Sounds[i];
                    Vector2    soundPos   = LocalToWorld(new Vector2(Prefab.Sounds[i].Position.X, Prefab.Sounds[i].Position.Y));
                    if (Vector2.DistanceSquared(new Vector2(GameMain.SoundManager.ListenerPosition.X, GameMain.SoundManager.ListenerPosition.Y), soundPos) <
                        roundSound.Range * roundSound.Range)
                    {
                        if (SoundChannels[i] == null || !SoundChannels[i].IsPlaying)
                        {
                            SoundChannels[i] = roundSound.Sound.Play(roundSound.Volume, roundSound.Range, soundPos);
                        }
                        SoundChannels[i].Position = new Vector3(soundPos.X, soundPos.Y, 0.0f);
                    }
                }
                else if (SoundChannels[i] != null && SoundChannels[i].IsPlaying)
                {
                    SoundChannels[i].Dispose();
                    SoundChannels[i] = null;
                }
            }
        }
Example #8
0
        partial void InitProjSpecific()
        {
            Sprite?.EnsureLazyLoaded();
            Prefab.DeformableSprite?.EnsureLazyLoaded();

            CurrentSwingAmount      = Prefab.SwingAmountRad;
            CurrentScaleOscillation = Prefab.ScaleOscillation;

            SwingTimer          = Rand.Range(0.0f, MathHelper.TwoPi);
            ScaleOscillateTimer = Rand.Range(0.0f, MathHelper.TwoPi);

            if (Prefab.ParticleEmitterPrefabs != null)
            {
                ParticleEmitters        = new ParticleEmitter[Prefab.ParticleEmitterPrefabs.Count];
                ParticleEmitterTriggers = new LevelTrigger[Prefab.ParticleEmitterPrefabs.Count];
                for (int i = 0; i < Prefab.ParticleEmitterPrefabs.Count; i++)
                {
                    ParticleEmitters[i]        = new ParticleEmitter(Prefab.ParticleEmitterPrefabs[i]);
                    ParticleEmitterTriggers[i] = Prefab.ParticleEmitterTriggerIndex[i] > -1 ?
                                                 Triggers[Prefab.ParticleEmitterTriggerIndex[i]] : null;
                }
            }

            if (Prefab.LightSourceParams != null && Prefab.LightSourceParams.Count > 0)
            {
                LightSources        = new LightSource[Prefab.LightSourceParams.Count];
                LightSourceTriggers = new LevelTrigger[Prefab.LightSourceParams.Count];
                for (int i = 0; i < Prefab.LightSourceParams.Count; i++)
                {
                    LightSources[i] = new LightSource(Prefab.LightSourceParams[i])
                    {
                        Position     = new Vector2(Position.X, Position.Y),
                        IsBackground = true
                    };
                    LightSourceTriggers[i] = Prefab.LightSourceTriggerIndex[i] > -1 ?
                                             Triggers[Prefab.LightSourceTriggerIndex[i]] : null;
                }
            }

            Sounds        = new RoundSound[Prefab.Sounds.Count];
            SoundChannels = new SoundChannel[Prefab.Sounds.Count];
            SoundTriggers = new LevelTrigger[Prefab.Sounds.Count];
            for (int i = 0; i < Prefab.Sounds.Count; i++)
            {
                Sounds[i]        = Submarine.LoadRoundSound(Prefab.Sounds[i].SoundElement, false);
                SoundTriggers[i] = Prefab.Sounds[i].TriggerIndex > -1 ? Triggers[Prefab.Sounds[i].TriggerIndex] : null;
            }

            int j = 0;

            foreach (XElement subElement in Prefab.Config.Elements())
            {
                if (!subElement.Name.ToString().Equals("deformablesprite", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                foreach (XElement animationElement in subElement.Elements())
                {
                    var newDeformation = SpriteDeformation.Load(animationElement, Prefab.Name);
                    if (newDeformation != null)
                    {
                        newDeformation.Params = Prefab.SpriteDeformations[j].Params;
                        spriteDeformations.Add(newDeformation);
                        j++;
                    }
                }
            }

            VisibleOnSonar = Prefab.SonarDisruption > 0.0f || Prefab.OverrideProperties.Any(p => p != null && p.SonarDisruption > 0.0f) ||
                             (Triggers != null && Triggers.Any(t => !MathUtils.NearlyEqual(t.Force, Vector2.Zero) && t.ForceMode != LevelTrigger.TriggerForceMode.LimitVelocity || !string.IsNullOrWhiteSpace(t.InfectIdentifier)));
            if (VisibleOnSonar && Triggers.Any())
            {
                SonarRadius = Triggers.Select(t => t.ColliderRadius * 1.5f).Max();
            }
        }
Example #9
0
 public CharacterSound(CharacterParams.SoundParams soundParams)
 {
     Params     = soundParams;
     roundSound = Submarine.LoadRoundSound(soundParams.Element);
 }
Example #10
0
 public CharacterSound(XElement element)
 {
     roundSound = Submarine.LoadRoundSound(element);
     Enum.TryParse(element.GetAttributeString("state", "Idle"), true, out Type);
     Enum.TryParse(element.GetAttributeString("gender", "None"), true, out Gender);
 }