<commit_before>/**
    * The EntityFactory creates Entities through Lua config scripts
    *
    * Author: <NAME>
    * Date: 8/9/2013
    * File: EntityFactory.cpp
**/

#include "EntityFactory.h"
#include <lua.hpp>
#include "Entity.h"
#include "IListener.h"

/**
 * @brief EntityFactory::Register allows the use of lambda functions to register component types so that entities can be constructed
 * @param componentType the type of component (same string referenced in Lua!)
 * @param constructor the constructor of the component of componentType
 * @return true if the constructor is registered, false otherwise
 */
bool EntityFactory::Register(const char *componentType, std::function<IComponent*()> constructor)
{
    ConstructorMap::iterator it = _ConstructorMap.find(componentType);

    if(it != _ConstructorMap.end())
    {
        return true;
    }

    _ConstructorMap[componentType] = constructor;
    return true;
}

/**
 * @brief EntityFactory::Create creates an entity from a Lua script
 * @param entity the file from which to load the entity
 */
void EntityFactory::Create(const char* entity)
{
    Entity* e = new Entity();
    IComponent* c = nullptr;
    lua_State* L = luaL_newstate();
    luaL_dofile(L, entity);
    lua_getglobal(L, "Entity");
    lua_pushnil(L);

    while(lua_next(L, 1) != 0)
    {
        c = _ConstructorMap[lua_tostring(L, -2)]();
        c->Load(L);
        e->AttachComponent(c);
        lua_pop(L, 1);
    }
    lua_close(L);

    AddEntityMessage msg;
    msg.entity = e;
    Emit<AddEntityMessage>(msg);
}<commit_msg>More work on #6. All that's left is to actually fill the Load methods for components.<commit_after>/**
    * The EntityFactory creates Entities through Lua config scripts
    *
    * Author: <NAME>
    * Date: 8/9/2013
    * File: EntityFactory.cpp
**/

#include "EntityFactory.h"
#include <lua.hpp>
#include "Entity.h"
#include "IListener.h"
#include "ScriptableBehavior.h"

/**
 * @brief EntityFactory::Register allows the use of lambda functions to register component types so that entities can be constructed
 * @param componentType the type of component (same string referenced in Lua!)
 * @param constructor the constructor of the component of componentType
 * @return true if the constructor is registered, false otherwise
 */
bool EntityFactory::Register(const char *componentType, std::function<IComponent*()> constructor)
{
    ConstructorMap::iterator it = _ConstructorMap.find(componentType);

    if(it != _ConstructorMap.end())
    {
        return true;
    }

    _ConstructorMap[componentType] = constructor;
    return true;
}

/**
 * @brief EntityFactory::Create creates an entity from a Lua script
 * @param entity the file from which to load the entity
 */
void EntityFactory::Create(const char* entity)
{
    Entity* e = new Entity();
    IComponent* c = nullptr;
    lua_State* L = luaL_newstate();
    luaL_dofile(L, entity);
    lua_getglobal(L, "Components");
    lua_pushnil(L);

    while(lua_next(L, 1) != 0)
    {
        c = _ConstructorMap[lua_tostring(L, -2)]();
        c->Load(L);
        e->AttachComponent(c);
        lua_pop(L, 1);
    }

    lua_settop(L, 0);
    lua_getglobal(L, "Behaviors");
    lua_pushnil(L);
    ScriptableBehavior* b = nullptr;

    while(lua_next(L, 1) != 0)
    {
        b = new ScriptableBehavior(lua_tostring(L, -2), lua_tostring(L, -1));
        e->AttachBehavior(b);
    }

    lua_close(L);

    AddEntityMessage msg;
    msg.entity = e;
    Emit<AddEntityMessage>(msg);
}
{'fraction_non_alphanumeric': 0.1188034188034188, 'fraction_numerical': 0.007407407407407408, 'mean_word_length': 3.5012820512820513, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2436574', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1073, 'n_words': 317}