package main.maps;

import java.util.ArrayList;

import ea.Knoten;
import ea.Ticker;
import main.Main;
import main.Tile;
import main.entities.Entity;
import main.entities.LivingEntity;
import main.entities.player.Player;

/**
 * Auf der Map sind alle Entities, sowie die Tiles gespiechert.
 */
public abstract class Map extends Knoten implements Ticker {

	protected Tile[][] map; // Die Tiles der map in einem 2D Array.
	private ArrayList<Entity> entities;
	private Player player;
	
	public Map(int width, int height) {
		map = new Tile[width][height];

		entities = new ArrayList<>(100);
		
		Main.instance.manager.anmelden(this, 20);
	}
	
	/**
	 * Gibt das Tile-Objekt an der gegebenen Koordinate zurück. (nur wenn es existiert)
	 */
	public Tile getTile(int x, int y) {
		if (x >= 0 && x < map.length && y >= 0 && y < map[0].length) {
			return map[x][y];
		}
		return null;
	}
	
	@Override
	public void tick() {
		for (int i = 0; i < entities.size(); i++) {
			Entity e = entities.get(i);
			if (e instanceof LivingEntity) {
				if (e.isReadyToDelete()) {
					Main.instance.manager.abmelden(e);
					entities.remove(e);
				}
			}
		}
	}
	
	public int getWidth() {
		return map.length;
	}
	
	public int getHeight() {
		return map[0].length;
	}

	public ArrayList<Entity> getEntities() {
		return entities;
	}
	
	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}
}
