ST_engine  0.3-ALPHA
level.cpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #include <game_manager/level/level.hpp>
11 #include <fstream>
12 #include <sstream>
13 #include <ST_util/string_util.hpp>
14 
20 ST::level::level(const std::string &lvl_name, message_bus *msg_bus) {
21  name = lvl_name;
22  gMessage_bus = msg_bus;
23 }
24 
30 int8_t ST::level::load() {
31  if (load_input_conf() != 0) {
32  return -1;
33  }
34  //Register all the keys this level uses with the input manager.
35  for (const auto &i: actions_buttons) {
36  for (const auto &key: i.second) {
37  if (key != ST::key::UNKNOWN) {
38  gMessage_bus->send_msg(new message(REGISTER_KEY, static_cast<uint8_t>(key), nullptr));
39  }
40  }
41  }
42  std::string temp = "game/levels/" + name + "/assets.list";
43  gMessage_bus->send_msg(new message(LOAD_LIST, make_data(temp)));
44  return 0;
45 }
46 
51  std::string temp = "game/levels/" + name + "/assets.list";
52  gMessage_bus->send_msg(new message(UNLOAD_LIST, make_data(temp)));
53  for (const auto &i: actions_buttons) {
54  for (const auto &key: i.second) {
55  if (key != ST::key::UNKNOWN) {
56  gMessage_bus->send_msg(new message(UNREGISTER_KEY, static_cast<uint8_t>(key), nullptr));
57  }
58  }
59  }
60  actions_buttons.clear();
61  gMessage_bus->send_msg(new message(LOAD_LIST, make_data(temp)));
62  load_input_conf();
63  for (const auto &i: actions_buttons) {
64  for (const auto &key: i.second) {
65  if (key != ST::key::UNKNOWN) {
66  gMessage_bus->send_msg(new message(REGISTER_KEY, static_cast<uint8_t>(key), nullptr));
67  }
68  }
69  }
70 }
71 
76 std::string ST::level::get_name() const {
77  return name;
78 }
79 
85  //unload inputConf
86  actions_buttons.clear();
87 
88  //unload entities.
89  entities.clear();
90 
91  //unload text objects
92  text_objects.clear();
93 
94  //unload lights
95  lights.clear();
96 }
97 
103  for (const auto &i: actions_buttons) {
104  for (const auto &key: i.second) {
105  if (key != ST::key::UNKNOWN) {
106  gMessage_bus->send_msg(new message(UNREGISTER_KEY, static_cast<uint8_t>(key), nullptr));
107  }
108  }
109  }
110  //unload assets
111  std::string temp = "game/levels/" + name + "/assets.list";
112  gMessage_bus->send_msg(new message(UNLOAD_LIST, make_data(temp)));
113 
114  //unload inputConf
115  actions_buttons.clear();
116 
117  //unload entities
118  entities.clear();
119 
120  //unload text objects
121  text_objects.clear();
122 
123  //unload lights
124  lights.clear();
125 }
126 
133  std::ifstream file;
134  std::string temp = "game/levels/" + name + "/inputConf.cfg";
135  file.open(temp.c_str());
136  if (file.is_open()) {
137  gMessage_bus->send_msg(new message(LOG_INFO, make_data<std::string>("Loading " + temp)));
138  std::string action;
139  std::string button;
140  int actionRead = 0;
141  while (!file.eof()) {
142  auto a = static_cast<char>(file.get());
143  //Ignore comments
144  if (a == '#') {
145  while (a != '\n') {
146  a = static_cast<char>(file.get());;
147  }
148  } else if (a == '=') {
149  actionRead = 1;
150  } else if (actionRead == 0 && a == '\n') { //ignore empty lines
151  continue;
152  } else if (actionRead == 0) {
153  action += a;
154  } else if (a == '\n' || file.eof()) {
155  std::istringstream buf(button);
156  std::istream_iterator<std::string> beg(buf), end;
157  std::vector<std::string> tokens(beg, end);
158  actions_buttons.emplace(ST::hash_string(action), std::vector<ST::key>());
159  std::vector<ST::key> *buttons_for_action = &actions_buttons.at(ST::hash_string(action));
160  for (const auto &token: tokens) {
161  buttons_for_action->emplace_back(key_index(token));
162  }
163  actionRead = 0;
164  action.clear();
165  button.clear();
166  } else {
167  button += a;
168  }
169  }
170  file.close();
171  } else {
172  gMessage_bus->send_msg(new message(LOG_ERROR, make_data<std::string>("File " + temp + " not found")));
173  return -1;
174  }
175  return 0;
176 }
177 
183 ST::key ST::level::key_index(const std::string &arg) {
184  key index = key::UNKNOWN;
185  if (arg == "left") {
186  index = key::LEFT;
187  } else if (arg == "right") {
188  index = key::RIGHT;
189  } else if (arg == "up") {
190  index = key::UP;
191  } else if (arg == "down") {
192  index = key::DOWN;
193  } else if (arg == "a") {
194  index = key::A;
195  } else if (arg == "b") {
196  index = key::B;
197  } else if (arg == "c") {
198  index = key::C;
199  } else if (arg == "d") {
200  index = key::D;
201  } else if (arg == "e") {
202  index = key::E;
203  } else if (arg == "f") {
204  index = key::F;
205  } else if (arg == "g") {
206  index = key::G;
207  } else if (arg == "h") {
208  index = key::H;
209  } else if (arg == "i") {
210  index = key::I;
211  } else if (arg == "j") {
212  index = key::J;
213  } else if (arg == "k") {
214  index = key::K;
215  } else if (arg == "l") {
216  index = key::L;
217  } else if (arg == "m") {
218  index = key::M;
219  } else if (arg == "n") {
220  index = key::N;
221  } else if (arg == "o") {
222  index = key::O;
223  } else if (arg == "p") {
224  index = key::P;
225  } else if (arg == "q") {
226  index = key::Q;
227  } else if (arg == "r") {
228  index = key::R;
229  } else if (arg == "s") {
230  index = key::S;
231  } else if (arg == "t") {
232  index = key::T;
233  } else if (arg == "u") {
234  index = key::U;
235  } else if (arg == "v") {
236  index = key::V;
237  } else if (arg == "w") {
238  index = key::W;
239  } else if (arg == "x") {
240  index = key::X;
241  } else if (arg == "y") {
242  index = key::Y;
243  } else if (arg == "z") {
244  index = key::Z;
245  } else if (arg == "1") {
246  index = key::ONE;
247  } else if (arg == "2") {
248  index = key::TWO;
249  } else if (arg == "3") {
250  index = key::THREE;
251  } else if (arg == "4") {
252  index = key::FOUR;
253  } else if (arg == "5") {
254  index = key::FIVE;
255  } else if (arg == "6") {
256  index = key::SIX;
257  } else if (arg == "7") {
258  index = key::SEVEN;
259  } else if (arg == "8") {
260  index = key::EIGHT;
261  } else if (arg == "9") {
262  index = key::NINE;
263  } else if (arg == "0") {
264  index = key::ZERO;
265  } else if (arg == "escape") {
266  index = key::ESCAPE;
267  } else if (arg == "enter") {
268  index = key::ENTER;
269  } else if (arg == "spacebar") {
270  index = key::SPACEBAR;
271  } else if (arg == "tilde") {
272  index = key::TILDE;
273  } else if (arg == "lshift") {
274  index = key::LSHIFT;
275  } else if (arg == "backspace") {
276  index = key::BACKSPACE;
277  } else if (arg == "backslash") {
278  index = key::BACKSLASH;
279  } else if (arg == "capslock") {
280  index = key::CAPSLOCK;
281  } else if (arg == "comma") {
282  index = key::COMMA;
283  } else if (arg == "equals") {
284  index = key::EQUALS;
285  } else if (arg == "lalt") {
286  index = key::LALT;
287  } else if (arg == "lctrl") {
288  index = key::LCTRL;
289  } else if (arg == "lbracket") {
290  index = key::LBRACKET;
291  } else if (arg == "rbracket") {
292  index = key::RBRACKET;
293  } else if (arg == "minus") {
294  index = key::MINUS;
295  } else if (arg == "delete") {
296  index = key::DELETE;
297  } else if (arg == "ralt") {
298  index = key::RALT;
299  } else if (arg == "rctrl") {
300  index = key::RCTRL;
301  } else if (arg == "semicolon") {
302  index = key::SEMICOLON;
303  } else if (arg == "slash") {
304  index = key::SLASH;
305  } else if (arg == "tab") {
306  index = key::TAB;
307  } else if (arg == "mouseLeft") {
308  index = key::MOUSELEFT;
309  } else if (arg == "mouseMiddle") {
310  index = key::MOUSEMIDDLE;
311  } else if (arg == "mouseRight") {
312  index = key::MOUSERIGHT;
313  } else if (arg == "controllerA") {
314  index = key::CONTROLLER_BUTTON_A;
315  } else if (arg == "controllerB") {
316  index = key::CONTROLLER_BUTTON_B;
317  } else if (arg == "controllerX") {
318  index = key::CONTROLLER_BUTTON_X;
319  } else if (arg == "controllerY") {
320  index = key::CONTROLLER_BUTTON_Y;
321  } else if (arg == "controllerSelect") {
322  index = key::CONTROLLER_BUTTON_SELECT;
323  } else if (arg == "controllerStart") {
324  index = key::CONTROLLER_BUTTON_START;
325  } else if (arg == "controllerLeftStick") {
326  index = key::CONTROLLER_BUTTON_LEFTSTICK;
327  } else if (arg == "controllerRightStick") {
328  index = key::CONTROLLER_BUTTON_RIGHTSTICK;
329  } else if (arg == "controllerLeftShoulder") {
330  index = key::CONTROLLER_BUTTON_LEFTSHOULDER;
331  } else if (arg == "controllerRightShoulder") {
332  index = key::CONTROLLER_BUTTON_RIGHTSHOULDER;
333  } else if (arg == "controllerDpadUp") {
334  index = key::CONTROLLER_BUTTON_DPAD_UP;
335  } else if (arg == "controllerDpadDown") {
336  index = key::CONTROLLER_BUTTON_DPAD_DOWN;
337  } else if (arg == "controllerDpadLeft") {
338  index = key::CONTROLLER_BUTTON_DPAD_LEFT;
339  } else if (arg == "controllerDpadRight") {
340  index = key::CONTROLLER_BUTTON_DPAD_RIGHT;
341  } else if (arg == "controllerRightTrigger") {
342  index = key::CONTROLLER_BUTTON_RIGHT_TRIGGER;
343  } else if (arg == "controllerLeftTrigger") {
344  index = key::CONTROLLER_BUTTON_LEFT_TRIGGER;
345  }
346  return index;
347 }
std::string get_name() const
Definition: level.cpp:76
int8_t load_input_conf()
Definition: level.cpp:132
void unload()
Definition: level.cpp:102
level(const std::string &, message_bus *)
Definition: level.cpp:20
int8_t load()
Definition: level.cpp:30
~level()
Definition: level.cpp:84
void reload()
Definition: level.cpp:50
The central messaging system of the engine. All subsystem make extensive use of it.
Definition: message_bus.hpp:29
A message object passed around in the message bus. Holds anything created with make_data<>().
Definition: message.hpp:21