import java.io.*;
import javax.sound.sampled.*;


public class ThreadSonAmbiance extends Thread{
	
	String snd=new String();
	
	AmbianceSonore player;
	
	private float vol=0;
	
	Monde monde;
	
	int r=0;
	
	InputStream stream;
	
	public ThreadSonAmbiance(float gain,Monde world){ // avec test
		vol=gain;
		monde=world;
	}
	
	
	public void run(){
		
		while(true){
			
			
			//"./sons/ambiance/Graveyard Wind.wav"
			//"./sons/ambiance/whispering.wav"
			//"./sons/ambiance/night.wav"
			//"./sons/ambiance/swamp.wav"
			
			if(monde.perso.getIndexTerrainZ()>0){
				
				r=(int)(Math.random()*100)+1;
				
				if(r<30){
					snd="./sons/ambiance/HAUNTING.wav";
				}
				
				else if(r>=30 && r<70){
					snd="./sons/ambiance/whispering.wav";
				}
				
				else if(r>=70){
					snd="./sons/weird.wav";
				}
					
			}
			else{
				
				
				
				if(!monde.getNuit()){
					if(!monde.pluie){
						snd="./sons/ambiance/Graveyard Wind.wav";
					}
					else{
						snd="./sons/ambiance/rain.wav";
					}
				}
				else{
					if(!monde.pluie){	
						snd="./sons/ambiance/night.wav";
					}
					else{
						snd="./sons/ambiance/rain.wav";
					}
				}	
				
			}
			
			//System.out.println("Ambiance son "+snd+" terrain "+monde.perso.getIndexTerrainZ()+" nuit "+monde.getNuit());
			
			
			try{
				player=new AmbianceSonore(snd,vol);
				stream=new ByteArrayInputStream(player.getSamples());
				player.play(stream);
				
			}
			catch(Exception e){}
			
		
			
			try { Thread.currentThread().sleep(100);}
			catch ( InterruptedException e ){};
			
		}
			
	}
		
}