-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentitymanager.cpp
More file actions
195 lines (167 loc) · 5.54 KB
/
entitymanager.cpp
File metadata and controls
195 lines (167 loc) · 5.54 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "entitymanager.h"
#include <iostream>
#include <vector>
#include <string>
#include "raylib.h"
#include "raymath.h"
#ifdef _WIN32
#include <lua.hpp>
#else
#include <luajit-2.1/lua.hpp>
#endif
#include "entitydefinition.h"
extern std::vector<Entity> entities_scene;
extern int next_entity_id;
extern lua_State* L;
extern Camera camera;
void entity_add_lua_table_int(lua_State* Ls, int push, std::string lua_var){
lua_pushinteger(Ls,push);
lua_setfield(Ls,-2,lua_var.c_str());
}
void entity_add_lua_table_float(lua_State* Ls, float push, std::string lua_var){
lua_pushnumber(Ls,push);
lua_setfield(Ls,-2,lua_var.c_str());
}
void entity_add_lua_table_bool(lua_State* Ls, bool push, std::string lua_var){
lua_pushboolean(Ls,push);
lua_setfield(Ls,-2,lua_var.c_str());
}
void entity_add_lua_table_string(lua_State* Ls, std::string push, std::string lua_var){
lua_pushstring(Ls,push.c_str());
lua_setfield(Ls,-2,lua_var.c_str());
}
void entity_get_lua_table_int(lua_State* Ls, int& ret, std::string lua_var){
lua_getfield(Ls,-1,lua_var.c_str());
ret = lua_tointeger(Ls,-1);
lua_pop(Ls,1);
}
void entity_get_lua_table_float(lua_State* Ls, float& ret, std::string lua_var){
lua_getfield(Ls,-1,lua_var.c_str());
ret = lua_tonumber(Ls,-1);
lua_pop(Ls,1);
}
void entity_get_lua_table_bool(lua_State* Ls, bool& ret, std::string lua_var){
lua_getfield(Ls,-1,lua_var.c_str());
ret = lua_toboolean(Ls,-1);
lua_pop(Ls,1);
}
void entity_get_lua_table_string(lua_State* Ls, std::string& ret, std::string lua_var){
lua_getfield(Ls,-1,lua_var.c_str());
ret = std::string(lua_tostring(Ls,-1));
lua_pop(Ls,1);
}
void entity_set_table(lua_State* Ls, Entity e){
entity_add_lua_table_float(Ls,e.position.x,"x");
entity_add_lua_table_float(Ls,e.position.y,"y");
entity_add_lua_table_float(Ls,e.position.z,"z");
entity_add_lua_table_float(Ls,e.culling_radius,"culling_radius");
entity_add_lua_table_bool(Ls,e.persistent,"is_persistent");
}
void entity_get_table(lua_State* Ls, Entity& e){
entity_get_lua_table_float(Ls,e.position.x,"x");
entity_get_lua_table_float(Ls,e.position.y,"y");
entity_get_lua_table_float(Ls,e.position.z,"z");
entity_get_lua_table_float(Ls,e.culling_radius,"culling_radius");
entity_get_lua_table_bool(Ls,e.persistent,"is_persistent");
}
int CreateEntity(std::string entity_lua, Vector3 position){
Entity new_entity;
next_entity_id ++;
new_entity.id = next_entity_id;
new_entity.position = position;
new_entity.script = entity_lua;
lua_newtable(L);
entity_set_table(L,new_entity);
lua_newtable(L);
lua_getglobal(L,"_G");
lua_setfield(L,-2,"__index");
lua_setmetatable(L,-2);
lua_setfield(L, LUA_REGISTRYINDEX, std::to_string(next_entity_id).c_str());
luaL_loadfile(L,entity_lua.c_str());
lua_getfield(L,LUA_REGISTRYINDEX, std::to_string(next_entity_id).c_str());
lua_setfenv(L,-2);
int result = lua_pcall(L,0,0,0);
if(result != LUA_OK){
printf("[ART] Error loading %s: %s\n",entity_lua.c_str(), lua_tostring(L,-1));
lua_pop(L,1);
return -1;
}
lua_getfield(L,LUA_REGISTRYINDEX, std::to_string(new_entity.id).c_str());
lua_getfield(L,-1,"init");
if(lua_isfunction(L,-1)){
lua_pushvalue(L,-2);
lua_pcall(L,1,0,0);
entity_get_table(L,new_entity);
}else{
lua_pop(L,1);
}
lua_pop(L,1);
entities_scene.push_back(new_entity);
return next_entity_id;
}
bool DeleteEntity(int id){
for(auto it = entities_scene.begin(); it != entities_scene.end();it++){
if(it->id == id){
lua_pushnil(L);
lua_setfield(L,LUA_REGISTRYINDEX, std::to_string(id).c_str());
entities_scene.erase(it);
return true;
}
}
return false;
}
void UpdateEntities(float dt){
for(auto& e : entities_scene){
lua_getfield(L,LUA_REGISTRYINDEX, std::to_string(e.id).c_str());
entity_set_table(L,e);
lua_getfield(L,-1,"update");
if(lua_isfunction(L,-1)){
lua_pushvalue(L,-2);
lua_pushnumber(L,dt);
int result = lua_pcall(L,2,0,0);
if(result != LUA_OK){
printf("[ART] Error on update %d: %s\n",e.id,lua_tostring(L,-1));
lua_pop(L,1);
}
}else{
lua_pop(L,1);
}
entity_get_table(L,e);
lua_pop(L,1);
}
}
void DrawEntities(){
for(auto& e : entities_scene){
float dist = Vector3Distance(camera.position,e.position);
if(dist > e.culling_radius && e.culling_radius > 0.0) continue;
lua_getfield(L,LUA_REGISTRYINDEX, std::to_string(e.id).c_str());
lua_getfield(L,-1,"draw");
if(lua_isfunction(L,-1)){
lua_pushvalue(L,-2);
int result = lua_pcall(L,1,0,0);
if(result != LUA_OK){
printf("[ART] Error on draw %d: %s\n",e.id,lua_tostring(L,-1));
lua_pop(L,1);
}
}else{
lua_pop(L,1);
}
lua_pop(L,1);
}
}
void DrawScreenEntities(){
for(auto& e : entities_scene){
lua_getfield(L,LUA_REGISTRYINDEX, std::to_string(e.id).c_str());
lua_getfield(L,-1,"draw_screen");
if(lua_isfunction(L,-1)){
int result = lua_pcall(L,0,0,0);
if(result != LUA_OK){
printf("[ART] Error on draw screen %d: %s\n",e.id,lua_tostring(L,-1));
lua_pop(L,1);
}
}else{
lua_pop(L,1);
}
lua_pop(L,1);
}
}