Line data Source code
1 : /**
2 : * @file api_level.cpp
3 : * @brief Krig Game Engine Lua Scripting API - Game Level
4 : *
5 : * Game level API functionality.
6 : */
7 : #include "api.h"
8 : #include "api_level.h"
9 : #include "SpriteGameObject.h"
10 :
11 : #if DOXYGEN_ONLY
12 : /**
13 : * Load the game level Lua script from the file specified.
14 : * @param string name of the game level Lua script to load.
15 : * @return n/a
16 : */
17 : void load(string);
18 : #endif
19 0 : static int load(lua_State *L) {
20 0 : const char *s = lua_tostring(L, 1);
21 0 : g_KRIG_ENGINE.loadLevel(s);
22 :
23 0 : return 0;
24 : }
25 :
26 : #if DOXYGEN_ONLY
27 : /**
28 : * Load the game level Lua script from the supplied string buffer.
29 : * @param string buffer containing a game level Lua script to load.
30 : * @return n/a
31 : */
32 : void load_from_buffer(string);
33 : #endif
34 0 : static int load_from_buffer(lua_State *L) {
35 0 : const char *s = lua_tostring(L, 1);
36 0 : g_KRIG_ENGINE.loadLevelFromBuffer(s);
37 :
38 0 : return 0;
39 : }
40 :
41 : #if DOXYGEN_ONLY
42 : /**
43 : * Pause execution of the game
44 : * @return n/a
45 : */
46 : void pause();
47 : #endif
48 0 : static int pause(lua_State *L) {
49 0 : g_KRIG_ENGINE.pause();
50 :
51 0 : return 0;
52 : }
53 :
54 : #if DOXYGEN_ONLY
55 : /**
56 : * Query the engine to determine the ID of the present level.
57 : * @return int the ID associated with the present game level.
58 : */
59 : int get_id();
60 : #endif
61 0 : static int get_id(lua_State *L) {
62 0 : lua_pushnumber(L, g_KRIG_ENGINE.getCurrentLevel()->getId());
63 0 : return 1;
64 : }
65 :
66 : #if DOXYGEN_ONLY
67 : /**
68 : * Set the ID associated with the present level.
69 : * @param level_id
70 : * @return n/a
71 : */
72 : int set_id(int);
73 : #endif
74 0 : static int set_id(lua_State *L) {
75 0 : g_KRIG_ENGINE.getCurrentLevel()->setId((int)lua_tonumber(L, 1));
76 0 : return 0;
77 : }
78 :
79 : #if DOXYGEN_ONLY
80 : /**
81 : * TODO: Undefined
82 : * @return n/a
83 : */
84 : void swap();
85 : #endif
86 0 : static int swap(lua_State *L) {
87 0 : g_KRIG_ENGINE.swapLevel();
88 0 : return 0;
89 : }
90 :
91 : #if DOXYGEN_ONLY
92 : /**
93 : * Query the engine to determine if the present level has been marked complete.
94 : * @return bool true if the level has been marked complete, false otherwise
95 : */
96 : bool get_complete();
97 : #endif
98 0 : static int get_complete(lua_State *L) {
99 0 : lua_pushnumber(L, g_KRIG_ENGINE.getCurrentLevel()->checkComplete());
100 0 : return 1;
101 : }
102 :
103 : #if DOXYGEN_ONLY
104 : /**
105 : * The currently running game level maintains a state about whether it has been
106 : * marked as "complete" or not. This method sets this state.
107 : * @param bool true - level is marked as complete, false - level is not complete
108 : * @return n/a
109 : */
110 : void set_complete(bool);
111 : #endif
112 0 : static int set_complete(lua_State *L) {
113 0 : g_KRIG_ENGINE.getCurrentLevel()->setComplete(lua_tonumber(L, 1));
114 0 : return 0;
115 : }
116 :
117 : #if DOXYGEN_ONLY
118 : /**
119 : * Playing song specified.
120 : * @param string name of file containing the song to play.
121 : * @param int repeat control (0 - do not repeat, 1 - repeat).
122 : * @return n/a
123 : */
124 : void play_music(string, int);
125 : #endif
126 0 : static int play_music(lua_State *L) {
127 0 : const char *s = lua_tostring(L, 1);
128 0 : int repeat = (int)lua_tonumber(L, 2);
129 0 : g_KRIG_ENGINE.getCurrentLevel()->setMusicPath(std::string(s));
130 0 : g_KRIG_ENGINE.getCurrentLevel()->getMusic()->StopSong();
131 0 : g_KRIG_ENGINE.getCurrentLevel()->getMusic()->PlaySong(s, repeat);
132 0 : return 0;
133 0 : }
134 :
135 : #if DOXYGEN_ONLY
136 : /**
137 : * Stop playing song (if playing). Song will restart when played.
138 : * @return n/a
139 : */
140 : void stop_music();
141 : #endif
142 0 : static int stop_music(lua_State *L) {
143 0 : g_KRIG_ENGINE.getCurrentLevel()->getMusic()->StopSong();
144 0 : return 0;
145 : }
146 :
147 : #if DOXYGEN_ONLY
148 : /**
149 : * Pause song (if playing). Song will resume where it was paused when played.
150 : * @return n/a
151 : */
152 : void pause_music();
153 : #endif
154 0 : static int pause_music(lua_State *L) {
155 0 : g_KRIG_ENGINE.getCurrentLevel()->getMusic()->PauseSong();
156 0 : return 0;
157 : }
158 :
159 : #if DOXYGEN_ONLY
160 : /**
161 : * Render a sky box around the level using the three colors supplied.
162 : * Color 1 is the top color; Color two is in the middle; Color 3 is the bottom color.
163 : * @param float first color, red attribute (0.0 - 1.0)
164 : * @param float first color, green attribute (0.0 - 1.0)
165 : * @param float first color, blue attribute (0.0 - 1.0)
166 : * @param float second color, red attribute (0.0 - 1.0)
167 : * @param float second color, green attribute (0.0 - 1.0)
168 : * @param float second color, blue attribute (0.0 - 1.0)
169 : * @param float third color, red attribute (0.0 - 1.0)
170 : * @param float third color, green attribute (0.0 - 1.0)
171 : * @param float third color, blue attribute (0.0 - 1.0)
172 : * @return n/a
173 : */
174 : void set_sky_box(float, float, float, float, float, float, float, float, float);
175 : #endif
176 0 : static int set_sky_box(lua_State *L) {
177 0 : int num = 1;
178 :
179 : float skyColors[3][3];
180 0 : for (int i = 0; i < 3; i++) {
181 0 : for (int j = 0; j < 3; j++) {
182 0 : skyColors[i][j] = lua_tonumber(L, num++);
183 0 : }
184 0 : }
185 :
186 0 : g_KRIG_ENGINE.getCurrentLevel()->setSkyBox(skyColors, 3, 3);
187 0 : return 0;
188 : }
189 :
190 : #if DOXYGEN_ONLY
191 : /**
192 : * Fetch the configured light direction.
193 : * @return Vector
194 : */
195 : Vector get_light_direction();
196 : #endif
197 0 : static int get_light_direction(lua_State *L) {
198 0 : returnVector(L, *g_KRIG_ENGINE.getCurrentLevel()->getLightDirection());
199 0 : return 1;
200 : }
201 :
202 : #if DOXYGEN_ONLY
203 : /**
204 : * Set the light direction in the game level's scene. This setting affects
205 : * shading on models and the terrain.
206 : * @param Vector array containing x, y, z coordinates of the directed light vector
207 : * @return n/a
208 : */
209 : void set_light_direction(Vector);
210 : #endif
211 : #if DOXYGEN_ONLY
212 : /**
213 : * Set the light direction in the game level's scene. This setting affects
214 : * shading on models and the terrain.
215 : * @param float x coordinate of the directed light vector
216 : * @param float y coordinate of the directed light vector
217 : * @param float z coordinate of the directed light vector
218 : * @return n/a
219 : */
220 : void set_light_direction(float, float, float);
221 : #endif
222 0 : static int set_light_direction(lua_State *L) {
223 0 : int index = 1;
224 0 : Vector t = loadVector(L, index);
225 0 : g_KRIG_ENGINE.getCurrentLevel()->setLightDirection(t.x, t.y, t.z);
226 0 : return 0;
227 : }
228 :
229 : #if DOXYGEN_ONLY
230 : /**
231 : * Set the specified level's terrain.
232 : * @param TerrainObjectReference
233 : * @param string terrain file to load.
234 : * @return n/a
235 : */
236 : void set_terrain(TerrainObjectReference, string);
237 : #endif
238 0 : static int set_terrain(lua_State *L) {
239 0 : luaL_checktype(L, 1, LUA_TTABLE);
240 0 : Terrain *terrain = static_cast<Terrain*>(loadObject(L, 1));
241 :
242 0 : const char *s = lua_tostring(L, 2);
243 :
244 0 : Vector *lightDirection = g_KRIG_ENGINE.getCurrentLevel()->getLightDirection();
245 0 : terrain->load(s, lightDirection);
246 0 : return 0;
247 : }
248 :
249 : #if DOXYGEN_ONLY
250 : /**
251 : * A game level can use up to 4 cameras, each with its own independent attached
252 : * script and settings. However, only one camera can be "active" at a time. This
253 : * method returns the ID of the active camera.
254 : * @return int ID of the active camera
255 : */
256 : int get_camera_id();
257 : #endif
258 0 : static int get_camera_id(lua_State *L) {
259 0 : lua_pushnumber(L, g_KRIG_ENGINE.getCurrentLevel()->getCamera()->id_);
260 0 : return 1;
261 : }
262 :
263 : #if DOXYGEN_ONLY
264 : /**
265 : * Query the engine for a game object of the specified type.
266 : * @param int - ID of object type to find.
267 : * @return Vertex2 - x, y coordinate of the mouse.
268 : */
269 : GameObjectReference find_object_of_type(int);
270 : #endif
271 0 : static int find_object_of_type(lua_State *L) {
272 0 : int type = (int)lua_tonumber(L, 1);
273 0 : ModelGameObject *temp = static_cast<ModelGameObject*>(
274 0 : g_KRIG_ENGINE.getCurrentLevel()->findEnemyOfType(type)
275 : );
276 :
277 0 : returnObject(L, temp);
278 0 : return 1;
279 : }
280 :
281 : #if DOXYGEN_ONLY
282 : /**
283 : * Add a game object to the current level.
284 : * @param string - path to lua script for new game object to load.
285 : * @param table - options table to be passed into the "on_load" method for the game object.
286 : * @return GameObjectReference
287 : */
288 : GameObjectReference add_object(string, options);
289 : #endif
290 0 : static int add_object(lua_State *L) {
291 0 : const char *s = lua_tostring(L, 1);
292 0 : std::string script = std::string(s);
293 :
294 0 : ModelGameObject *temp = static_cast<ModelGameObject*>(
295 0 : g_KRIG_ENGINE.getCurrentLevel()->addObject(script, L, TYPE_GAME_OBJECT)
296 : );
297 :
298 0 : returnObject(L, temp);
299 : return 1;
300 0 : }
301 :
302 : #if DOXYGEN_ONLY
303 : /**
304 : * Remove the specified object from the level.
305 : * @param GameObjectReference
306 : * @return n/a
307 : */
308 : void remove_object(GameObjectReference);
309 : #endif
310 0 : static int remove_object(lua_State *L) {
311 0 : luaL_checktype(L, 1, LUA_TTABLE);
312 0 : Object *object = static_cast<Object*>(loadObject(L, 1));
313 :
314 0 : object->setState(DEAD);
315 0 : return 0;
316 : }
317 :
318 : #if DOXYGEN_ONLY
319 : /**
320 : * Add a game object to the current level.
321 : * @param string - path to lua script for new text object to load.
322 : * @param string - text.
323 : * @param table - options table to be passed into the "on_load" method for the text object.
324 : * @return TextObjectReference
325 : */
326 : TextObjectReference add_text(string, string, options);
327 : #endif
328 0 : static int add_text(lua_State *L) {
329 0 : const char *s = lua_tostring(L, 1);
330 0 : std::string script = std::string(s);
331 :
332 0 : const char *t = lua_tostring(L, 2);
333 0 : std::string text = std::string(t);
334 :
335 0 : TextGameObject *temp = static_cast<TextGameObject*>(
336 0 : g_KRIG_ENGINE.getCurrentLevel()->addObject(script, L, TYPE_GAME_TEXT)
337 : );
338 0 : if (temp != NULL) {
339 0 : temp->text = text;
340 0 : }
341 :
342 0 : returnObject(L, temp);
343 : return 1;
344 0 : }
345 :
346 : #if DOXYGEN_ONLY
347 : /**
348 : * Add a 2D bitmapped game object to the current level.
349 : * @param string - path to lua script for new object to load.
350 : * @param table - options table to be passed into the "on_load" method for the object.
351 : * @return TextObjectReference
352 : */
353 : TextObjectReference add_sprite(string, options);
354 : #endif
355 0 : static int add_sprite(lua_State *L) {
356 0 : const char *s = lua_tostring(L, 1);
357 0 : std::string script = std::string(s);
358 :
359 0 : SpriteGameObject *temp = static_cast<SpriteGameObject*>(
360 0 : g_KRIG_ENGINE.getCurrentLevel()->addObject(script, L, TYPE_GAME_SPRITE)
361 : );
362 :
363 0 : returnObject(L, temp);
364 : return 1;
365 0 : }
366 :
367 : static const luaL_Reg krigLevelLib[] = {
368 : {"add_object", add_object},
369 : {"add_text", add_text},
370 : {"add_sprite", add_sprite},
371 : {"find_object_of_type", find_object_of_type},
372 : {"get_camera_id", get_camera_id},
373 : {"get_complete", get_complete},
374 : {"get_id", get_id},
375 : {"get_light_direction", get_light_direction},
376 : {"load", load},
377 : {"load_from_buffer", load_from_buffer},
378 : {"pause", pause},
379 : {"pause_music", pause_music},
380 : {"play_music", play_music},
381 : {"remove_object", remove_object},
382 : {"set_complete", set_complete},
383 : {"set_id", set_id},
384 : {"set_light_direction", set_light_direction},
385 : {"set_sky_box", set_sky_box},
386 : {"set_terrain", set_terrain},
387 : {"stop_music", stop_music},
388 : {"swap", swap},
389 : {NULL, NULL}
390 : };
391 :
392 9 : int luaopen_krigLevel (lua_State *L) {
393 9 : luaL_openlib(L, "krig.level", krigLevelLib, 0);
394 9 : return 1;
395 : }
|