/*
 * World.h
 *
 *  Created on: 2009-07-10
 *      Author: wasyl
 */

#ifndef WORLD_H_
#define WORLD_H_

#include "Entity.h"
#include <set>
#include <map>
#include <vector>

typedef std::vector<Entity*> Entities;

/*********************************************/

class EntityCompare
{
private:
	int id;
public:
	EntityCompare(int id) : id(id) {}
	bool operator()(Entity *a, Entity *b) { return a->order(id) < b->order(id); }
};

/*********************************************/

class World
{
private:
	std::map<int, std::vector<Entity*> > mIndices;
	std::vector<Entity*> mToAdd, mToRemove;

public:
	/**
	 * Adds entity.
	 */
	void add(Entity *e);

	/**
	 * Removes entity.
	 */
	void remove(Entity *e);

	/**
	 * Refreshes indices, makes adds() and removes() actually
	 * do their job.
	 */
	void reindex();

	std::vector<Entity*> &index(int id) { return mIndices[id]; };
	Entity* entity(int id) { return (index(id).size()) ? index(id).front() : NULL; }
};

#endif /* WORLD_H_ */
