package main.worlds;

import ea.Knoten;
import main.DoorTile;
import main.entities.player.Player;
import main.maps.ImageMap;
import main.maps.Map;

/**
 * Hier werden alle Maps gespeichert.
 */
public abstract class World extends Knoten {

	public static final int SCALE_FACTOR = 4; // Der Basis Zoomfaktor
	public static final int SCALE = SCALE_FACTOR * 16; // Eine Gameunit ist so viele pixel lang

	private Map currentMap; // Die Map die aktuell angezeigt werden soll.

	private Player player;

	protected static Map[] maps;

	public World(int startMap) {
		if (maps == null) {
			maps = new ImageMap[8];
			for (int i = 0; i < maps.length; i++) {
				maps[i] = new ImageMap("/res/images/maps/map" + i + "a.png");
			}
		}
		init(maps[startMap]);
	}

	public World(Map startMap) {
		init(startMap);
	}

	private void init(Map startMap) {
		currentMap = startMap;
		player = new Player();
		add(currentMap);
	}

	public Map getCurrentMap() {
		return currentMap;
	}

	public Player getPlayer() {
		return player;
	}

	public void changeMap(DoorTile door) {
		currentMap.stop();
		currentMap.entfernen(player.actionFigur);
		entfernen(currentMap);
		currentMap = door.getConnectedDoor().getMap();
		add(currentMap);
		currentMap.add(player.actionFigur);
		currentMap.start();
		player.setPos(door.getConnectedDoor().getPosX() + 0.5f, door.getConnectedDoor().getPosY() + 0.5f);
		door.getConnectedDoor().waitForLeave();
	}

	public void start() {
		currentMap.addLivingEntity(player);
		currentMap.start();
	}
}
