-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityFactory.cpp
124 lines (102 loc) · 3.02 KB
/
EntityFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* The EntityFactory creates Entities through Lua config scripts
*
* Author: Skylar Payne
* Date: 8/9/2013
* File: EntityFactory.cpp
**/
#include "EntityFactory.h"
#include <lua.hpp>
#include "Entity.h"
#include "IListener.h"
#include "ScriptableBehavior.h"
#include "Logger.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, float x, float y)
{
lua_State* L = luaL_newstate();
if(luaL_dofile(L, entity))
{
g_Logger << lua_error(L) << lua_tostring(L, -1) << "\n";
g_Logger.flush();
lua_pop(L, 1);
return;
}
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_setglobal(L, "y");
lua_setglobal(L, "x");
Entity* e = new Entity();
IComponent* c = nullptr;
lua_settop(L, 0);
lua_getglobal(L, "Components");
if(lua_isnil(L, -1) == 0)
{
lua_pushnil(L);
while(lua_next(L, 1) != 0)
{
const char* temp = lua_tostring(L, -2);
if(strcmp(temp, "Tag") == 0)
{
e->SetTag(lua_tostring(L, -1));
lua_pop(L, 1);
continue;
}
else if(_ConstructorMap.find(temp) == _ConstructorMap.end())
{
g_Logger << temp << " component type not found. Continuing\n";
lua_pop(L, 1);
continue;
}
c = _ConstructorMap[temp]();
c->Load(L);
lua_settop(L, 3);
e->AttachComponent(c);
lua_pop(L, 1);
}
}
lua_settop(L, 0);
lua_getglobal(L, "Behaviors");
if(lua_isnil(L, -1) == 0)
{
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_pop(L, 1);
}
}
lua_close(L);
AddEntityMessage msg;
msg.entity = e;
Emit<AddEntityMessage>(msg);
}
/**
* @brief EntityFactory::OnMessage calls the create function with data held in msg
* @param msg position and file from which to load entity.
*/
void EntityFactory::OnMessage(CreateEntityMessage& msg)
{
Create(msg.script, msg.position.x, msg.position.y);
}