package ei.game.player;

import java.util.ArrayList;

public class PlayerHandler {
	// The instance of this class
	private static PlayerHandler instance;
	// The player list
	private ArrayList<Player> players;
	
	/**
	 * Creates a PlayerHandler
	 *
	 */
	public PlayerHandler(){
		players = new ArrayList<Player>();
	}
	
	/**
	 * Add a player to the handler
	 * 
	 * @param p The player to add to the handler
	 * @return true if added else false
	 */
	public boolean addPlayer(Player p){
		if(!players.contains(p)){
			players.add(p);
			return true;
		}
		return false;
	}
	
	/**
	 * Removes a player from the handler
	 * @param p The player to remove
	 * @return true if succesful else false
	 */
	public boolean removePlayer(Player p){
		if(players.contains(p)){
			players.remove(p);
			return true;
		}
		return false;
	}
	
	public void update(){
		for(int i=0; i<players.size() ;i++){
			players.get(i).update();
		}
	}
	
	/**
	 * Returns the instance of this class
	 * 
	 * @return The instance
	 */
	public static PlayerHandler getInstance(){
		if(instance == null){
			instance = new PlayerHandler();
		}
		return instance;
	}
}
