Beispiel #1
0
 /// <summary>
 /// Begins playing the Music track.
 /// </summary>
 /// <param name="props">Player Properties</param>
 public virtual void BeginPlay(TrackedPlayerProperties props)
 {
     loading = true;
     musicEngine.LoadTrack(Location, (sound) => {
         if (sound != null)
         {
             sound.Start();
             if (!loading)
             {
                 sound.Stop(); sound.Dispose();
             }
             else
             {
                 Sound = sound;
             }
         }
         loading = false;
     });
 }
Beispiel #2
0
        /// <summary>
        /// Do we continue playing this track?
        /// </summary>
        /// <param name="dt">Delta time or Change in time</param>
        /// <param name="props">The properties of the current track.</param>
        /// <returns>Are we still playing or do we stop?</returns>
        public bool ContinuePlay(float dt, TrackedPlayerProperties props)
        {
            if (props.sunSlight > 3 || !ShouldPlayCaveMusic)
            {
                FadeOut(3);
                return(false);
            }

            if (activeUntilMs > 0 && capi.World.ElapsedMilliseconds >= activeUntilMs)
            {
                // Ok, time to stop. We play the current tracks until the end and stop
                bool active = IsActive;
                if (!active)
                {
                    activeUntilMs = 0;
                    foreach (MusicTrackPart part in Parts)
                    {
                        part.Sound?.Dispose();
                    }
                }
                return(active);
            }

            int quantityActive = 0;

            for (int i = 0; i < Parts.Length; i++)
            {
                quantityActive += (Parts[i].IsPlaying || Parts[i].Loading) ? 1: 0;
            }

            int beforePlaying = quantityActive;

            GameMath.Shuffle(rand, PartsShuffled);

            for (int i = 0; i < PartsShuffled.Length; i++)
            {
                MusicTrackPart part = PartsShuffled[i];
                if (part.Files.Length == 0)
                {
                    continue;
                }

                bool isPlaying  = part.IsPlaying;
                bool shouldPlay = part.Applicable(capi.World, props);

                // Part has recently ended
                if (!isPlaying && part.Sound != null)
                {
                    part.Sound.Dispose();
                    part.Sound = null;
                    continue;
                }

                // Part should be stopped
                if (isPlaying && !shouldPlay)
                {
                    if (!part.Sound.IsFadingOut)
                    {
                        part.Sound.FadeOut(3, (sound) => { part.Sound.Dispose(); part.Sound = null; });
                    }
                    continue;
                }


                bool shouldStart =
                    !isPlaying &&
                    shouldPlay &&
                    !part.Loading &&
                    quantityActive < maxSimultaenousTracks &&
                    (quantityActive == 0 || rand.NextDouble() < simultaenousTrackChance)
                ;

                if (shouldStart)
                {
                    AssetLocation location = part.Files[rand.Next(part.Files.Length)];
                    part.NowPlayingFile = location;
                    part.Loading        = true;
                    musicEngine.LoadTrack(location, (sound) => {
                        if (sound != null)
                        {
                            sound.Start();
                            part.Sound = sound;
                        }
                        part.Loading = false;
                    });

                    part.StartedMs = capi.World.ElapsedMilliseconds;
                    quantityActive++;
                }
            }

            return(true);
        }