LCOV - code coverage report
Current view: top level - src - api_object.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 77 0
Test Date: 2026-04-03 02:26:39 Functions: 0.0 % 11 0

            Line data    Source code
       1              : /**
       2              :  * @file api_object.cpp
       3              :  * @brief Krig Game Engine Lua Scripting API - Game Object
       4              :  *
       5              :  * Game object API functionality.
       6              :  */
       7              : #include "api.h"
       8              : #include "api_object.h"
       9              : #include "SpriteGameObject.h"
      10              : 
      11              : #if DOXYGEN_ONLY
      12              : /**
      13              :  * Set the end rotation (by axis) for the specified game object's interpolation.
      14              :  * @param Vector4 - start rotation.
      15              :  * @param float - start interpolation value.
      16              :  * @param Vector4 - end rotation.
      17              :  * @param float - end interpolation value.
      18              :  * @return n/a
      19              :  */
      20              : void setup_interpolation(Vector4, float, Vector4, float);
      21              : #endif
      22            0 : static int setup_interpolation(lua_State *L) {
      23            0 :   luaL_checktype(L, 1, LUA_TTABLE);
      24            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
      25              : 
      26            0 :   Quaternion tq1 = loadQuaternion(L, 2);
      27            0 :   float tv1 = lua_tonumber(L, 3);
      28            0 :   Quaternion tq2 = loadQuaternion(L, 4);
      29            0 :   float tv2 = lua_tonumber(L, 5);
      30              : 
      31            0 :   object->setRInterpStart(tq1);
      32            0 :   object->setRInterpEnd(tq2);
      33            0 :   object->setValInterpBegin(tv1);
      34            0 :   object->setValInterpEnd(tv2);
      35              : 
      36              :   return 0;
      37            0 : }
      38              : 
      39              : #if DOXYGEN_ONLY
      40              : /**
      41              :  * Set current value of the interpolation variable.
      42              :  * @param float value.
      43              :  * @return n/a
      44              :  */
      45              : void set_interpolation_value(float);
      46              : #endif
      47            0 : static int update_interpolation_value(lua_State *L) {
      48            0 :   luaL_checktype(L, 1, LUA_TTABLE);
      49            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
      50            0 :   object->setValInterpCurrent(lua_tonumber(L, 2));
      51            0 :   return 0;
      52              : }
      53              : 
      54              : #if DOXYGEN_ONLY
      55              : /**
      56              :  * Suspend running the game object's script until the suspend time has elapsed.
      57              :  * @param float
      58              :  * @return n/a
      59              :  */
      60              : void suspend(float);
      61              : #endif
      62            0 : static int suspend(lua_State *L) {
      63            0 :   luaL_checktype(L, 1, LUA_TTABLE);
      64            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
      65            0 :   object->setSuspendTime(lua_tonumber(L, 2));
      66            0 :   return 0;
      67              : }
      68              : 
      69              : #if DOXYGEN_ONLY
      70              : /**
      71              :  * Specify the model to render for the game object.
      72              :  * @string file containing the model to load.
      73              :  * @return n/a
      74              :  */
      75              : void set_model(string);
      76              : #endif
      77            0 : static int set_model(lua_State *L) {
      78            0 :   luaL_checktype(L, 1, LUA_TTABLE);
      79            0 :   ModelGameObject *object = static_cast<ModelGameObject*>(loadObject(L, 1));
      80              : 
      81            0 :   const char *s     = lua_tostring(L, 2);
      82            0 :   std::string model = std::string(s);
      83              : 
      84            0 :   object->load(model);
      85              :   return 0;
      86            0 : }
      87              : 
      88              : #if DOXYGEN_ONLY
      89              : /**
      90              :  * Specify the texture to render for the sprite.
      91              :  * @string file containing the texture to load (in png format).
      92              :  * @return n/a
      93              :  */
      94              : void set_texture(string);
      95              : #endif
      96            0 : static int set_texture(lua_State *L) {
      97            0 :   luaL_checktype(L, 1, LUA_TTABLE);
      98            0 :   SpriteGameObject *object = static_cast<SpriteGameObject*>(loadObject(L, 1));
      99              : 
     100            0 :   const char *s       = lua_tostring(L, 2);
     101            0 :   std::string texture = std::string(s);
     102              : 
     103            0 :   object->setTexture(texture);
     104              :   return 0;
     105            0 : }
     106              : 
     107              : #if DOXYGEN_ONLY
     108              : /**
     109              :  * Set lua script for game object.
     110              :  * @param string lua script file name.
     111              :  * @return n/a
     112              :  */
     113              : void set_script(string);
     114              : #endif
     115            0 : static int set_script(lua_State *L) {
     116            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     117            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
     118              : 
     119            0 :   if (object != NULL) {
     120            0 :     const char *s      = lua_tostring(L, 2);
     121            0 :     std::string script = std::string(s);
     122              : 
     123            0 :     object->unloadScript();
     124            0 :     object->setScript(script);
     125            0 :     object->loadScript(script, L);
     126            0 :   }
     127              : 
     128            0 :   return 0;
     129            0 : }
     130              : 
     131              : #if DOXYGEN_ONLY
     132              : /**
     133              :  * Add a particle system to the game object.
     134              :  * @param int - number representing the particle system to load.
     135              :  * @return n/a
     136              :  */
     137              : void add_particle_system(GameObjectReference, int);
     138              : #endif
     139            0 : static int add_particle_system(lua_State *L) {
     140            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     141            0 :   Object *object = static_cast<Object*>(loadObject(L, 1));
     142              : 
     143            0 :   object->setParticleSystem((int)lua_tonumber(L, 2));
     144              : 
     145            0 :   return 0;
     146              : }
     147              : 
     148              : #if DOXYGEN_ONLY
     149              : /**
     150              :  * Rotate the game object such that it appears to be resting on the terrain.
     151              :  * Additionally, the specified rotation will be used as the base rotation of
     152              :  * the game object.
     153              :  * @param x rotation around x axis
     154              :  * @param y rotation around y axis
     155              :  * @param z rotation around z axis
     156              :  * @return n/a
     157              :  */
     158              : void orient_on_terrain(float, float, float);
     159              : #endif
     160            0 : static int orient_on_terrain(lua_State *L) {
     161            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     162            0 :   Object *object = loadObject(L, 1);
     163              : 
     164            0 :   Quaternion rotation;
     165            0 :   rotation.buildFromEuler(lua_tonumber(L,2),lua_tonumber(L,3),lua_tonumber(L,4));
     166              : 
     167            0 :   object->orientOnTerrain(g_KRIG_ENGINE.getCurrentLevel()->getTerrain(), rotation);
     168              : 
     169              :   return 0;
     170            0 : }
     171              : 
     172              : #if DOXYGEN_ONLY
     173              : /**
     174              :  * Determine the height of the terrain beneath the game object and
     175              :  * set its height to match the terrain's height at this location. The offset
     176              :  * is used to adjust the object's height along the y axis.
     177              :  * @param float - height offset
     178              :  * @return n/a
     179              :  */
     180              : void set_height_from_terrain(GameObjectReference, float);
     181              : #endif
     182            0 : static int set_height_from_terrain(lua_State *L) {
     183            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     184            0 :   ModelGameObject *object = static_cast<ModelGameObject*>(loadObject(L, 1));
     185              : 
     186            0 :   float offset = lua_tonumber(L, 2);
     187              : 
     188            0 :   object->setHeightFromTerrain(g_KRIG_ENGINE.getCurrentLevel()->getTerrain(), offset);
     189              : 
     190            0 :   return 0;
     191              : }
     192              : 
     193              : #if DOXYGEN_ONLY
     194              : /**
     195              :  * Load properties for game object.
     196              :  * @return GameObject
     197              :  */
     198              : GameObject load();
     199              : #endif
     200            0 : static int load(lua_State *L) {
     201            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     202            0 :   Object *object = loadObject(L, 1);
     203              : 
     204            0 :   returnObject(L, object);
     205            0 :   object->buildLuaObjectTable(L);
     206              : 
     207            0 :   return 1;
     208              : }
     209              : 
     210              : #if DOXYGEN_ONLY
     211              : /**
     212              :  * Save game object properties.
     213              :  * @return n/a
     214              :  */
     215              : void save();
     216              : #endif
     217            0 : static int save(lua_State *L) {
     218            0 :   luaL_checktype(L, 1, LUA_TTABLE);
     219            0 :   Object *object = loadObject(L, 1);
     220              : 
     221            0 :   object->transferLuaObjectTable(L);
     222              : 
     223            0 :   return 0;
     224              : }
     225              : 
     226              : const luaL_Reg krigObjectLib[] = {
     227              :   {"add_particle_system", add_particle_system},
     228              :   {"load", load},
     229              :   {"orient_on_terrain", orient_on_terrain},
     230              :   {"save", save},
     231              :   {"set_height_from_terrain", set_height_from_terrain},
     232              :   {"set_model", set_model},
     233              :   {"set_texture", set_texture},
     234              :   {"set_script", set_script},
     235              :   {"setup_interpolation", setup_interpolation},
     236              :   {"suspend", suspend},
     237              :   {"update_interpolation_value", update_interpolation_value},
     238              :   {NULL, NULL}
     239              : };
        

Generated by: LCOV version 2.4-0