You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9 KiB

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System;
namespace Asteroids;
public static class AudioEn
{
public static float MasterVolume;
public static Sound[] Sounds;
private static List<SoundEffectInstance> SoundInits = new List<SoundEffectInstance>();
private static string[] music;
public static int cursong;
public static SoundEffectInstance muse;
public static SoundEffect nexmuse;
public static TimeSpan songtime;
public static TimeSpan playedtime;
private static KeyboardState old;
private static Game gam;
public static void Initialize(Game Gam)
{
gam = Gam;
music = Directory.GetFiles("Content/Music");
for(int i = 0; i< music.Length; i++)
{
music[i] = music[i].Remove(0, 8);
music[i] = music[i].Remove(music[i].Length-4, 4);
}
SoundEffect se = gam.Content.Load<SoundEffect>(music[cursong]);
muse = se.CreateInstance();
songtime = se.Duration;
muse.Play();
cursong++;
loadnex();
}
public static void playsound(int sound)
{
SoundInits.Add(Sounds[sound].effect.CreateInstance());
SoundInits[SoundInits.Count-1].Volume = Sounds[sound].volume * MasterVolume;
SoundInits[SoundInits.Count-1].Play();
}
public static void Update(GameTime gameTime)
{
musein();
SoundInits.RemoveAll(pred);
muse.Volume = Sounds[1].volume * MasterVolume;
if(muse.State == SoundState.Stopped)
{
playedtime = new TimeSpan();
muse = nexmuse.CreateInstance();
songtime = nexmuse.Duration;
loadnex();
muse.Play();
cursong++;
}
if(cursong == music.Length)
{
cursong = 0;
}
playedtime += gameTime.ElapsedGameTime;
}
private static void musein()
{
KeyboardState KEY = Keyboard.GetState();
if(KEY.IsKeyDown(Keys.Right) && !old.IsKeyDown(Keys.Right))
{
muse.Stop();
}
old = KEY;
}
private static void loadnex()
{
Task taskA = Task.Run( () => sus());
}
private static void sus()
{
nexmuse = gam.Content.Load<SoundEffect>(music[cursong]);
}
private static bool pred(SoundEffectInstance sei)
{
if(sei.State == SoundState.Playing)
{
return false;
}
return true;
}
}
public class Sound
{
public SoundEffect effect;
public float volume;
public string name;
public bool show;
public Sound(SoundEffect effect, float volume, string name, bool show)
{
this.effect = effect;
this.volume = volume;
this.name = name;
this.show = show;
}
}