package ei.engine.sound;

import java.awt.Rectangle;

import ei.engine.math.Vector3f;
import ei.engine.scene.Entity;

/**
 * A sound that can be played through OpenAL
 * 
 * @author Ziver Koc
 */
public class Sound extends Entity{
	private static int soundId = 1;
	/** The buffer containing the sound */
	private int buffer;
	private int sourceId;
	private int id;
	private Vector3f velocity;

	/**
	 * Create a new sound
	 * 
	 * @param name The name of the sound
	 * @param ref the path to the sound file
	 */
	public Sound(String name, String ref) {
		super(name);
		id = soundId;
		soundId++;
		velocity = new Vector3f();
		this.buffer = SoundLoader.getInstnace().loadSound(ref);		
	}

	/**
	 * Returns the unique id of the object
	 * 
	 * @return The id of the object
	 */
	public int getSoundId(){
		return id;
	}
	
	/**
	 * Returns the source this object uses
	 * 
	 * @return The id of the source
	 */
	public int getSourceId(){
		return sourceId;
	}
	
	/**
	 * Returns the buffer of this object
	 * 
	 * @return The buffer
	 */
	public int getBuffer(){
		return buffer;
	}
	
	/**
	 * Play this sound as a sound effect
	 * 
	 */
	public void play() {
		sourceId = SoundManager.getInstnace().playSound(this, false);
		update();
	}
	
	/**
	 * Loop this sound
	 * 
	 */
	public void loop() {
		SoundManager.getInstnace().playSound(this, true);
		update();
	}

	/**
	 * Updates the sound
	 */
	public void update() {
		//AL10.alSource(source.get(0), AL10.AL_POSITION, sourcePos);
		SoundManager.getInstnace().setSoundLocation(this, 
				getLocation().getX(),
				getLocation().getY(),
				getLocation().getZ());
		
	}	
	
	public Vector3f getVelocity(){
		return velocity;
	}
	
	/**
	 * Returns the bound of this class
	 */
	public Rectangle getBound() {
		return new Rectangle(
				(int)getLocation().getX(),
				(int)getLocation().getY(),
				10,	10);
	}

}
