// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include <map>
#include <string>

#include "entity.h"

#include "../lua/luavalue.h"

/** @brief Lua-controlled Entity. */
class ScriptEntity : public Entity {
	public:
	
		/** @brief Constructor.
		 * @param world The World that this belongs to.
		 * */
		ScriptEntity(World *world);
		
		/** @brief XML load constructor.
		 * @param world The World that this belongs to.
		 * @param xml An XML element to load from.
		 * */
		ScriptEntity(World *world, TiXmlElement *xml);
		
		virtual ~ScriptEntity(void);
		
		/** @brief Push the table that corresponds to this object onto
		 * the top of the Lua stack.
		 * */
		void pushTable(void) const;
	
		/** @brief Determine if this is a special Spawner entity. That
		 * is, subclassed off the Spawner class in Lua.
		 * @return true if it is a Spawner.
		 * */
		bool getIsSpawner(void) const;
		
		/** @brief Moves information from tableData and
		 * editorProperties into the Lua table. Mostly for
		 * initialization after both the Lua object and this object are
		 * already constructed.
		 * */
		void fillTableData(void);
		
		// Inherited and reimplemented from Entity...
		void update(void);
		void postUpdate(void);
		void setName(const std::string &name);
		void updateEditor(EditorUI *ui);
		void updateFromEditor(EditorUI *ui);
		void collided(Entity *otherEnt);
		void collidedWithWorld(void);
		void die(void);
		TiXmlElement *toXml(void) const;
		
		// TODO: Fix naming consistency.
		bool isSavable(void);
		bool getIsPermanent(void);
		
	protected:
	private:
		std::map<std::string, std::string> tableData;
		std::map<std::string, LuaValue> editorProperties;
};

/** @brief Set up the base class in Lua that all ScriptEntities are
 * based off of.
 * @param L The lua_State for the World.
 * */
void setupLuaBaseClass(lua_State *L);

/** @brief Get a ScriptEntity from its corresponding table in the Lua
 * stack.
 * @param L The lua_State for the World.
 * @param index The Lua stack index that the table is at.
 * */
ScriptEntity *getEntityFromTable(lua_State *L, int index);

/// Get whether or not there's even anything in a ScriptEntity table.
bool getEntityTableIsValid(lua_State *L, int index);

/** @brief Create a ScriptEntity and Lua table from an XML element.
 * @param world The World to put it in.
 * @param xml The XML element to load from.
 * */
void createScriptEntityWithXml(World *world, TiXmlElement *xml);

