refactor: 'lua' namespace expansion
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#include "lua_commons.hpp"
|
||||
#include "api_lua.hpp"
|
||||
#include "../scripting.hpp"
|
||||
|
||||
#include "../../../world/Level.hpp"
|
||||
#include "../../../objects/Player.hpp"
|
||||
#include "../../../physics/Hitbox.hpp"
|
||||
@@ -9,20 +8,17 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
inline std::shared_ptr<Player> get_player(lua_State* L, int idx) {
|
||||
return scripting::level->getObject<Player>(lua_tointeger(L, idx));
|
||||
return level->getObject<Player>(lua::tointeger(L, idx));
|
||||
}
|
||||
|
||||
static int l_player_get_pos(lua_State* L) {
|
||||
auto player = get_player(L, 1);
|
||||
if (!player) {
|
||||
return 0;
|
||||
if (auto player = get_player(L, 1)) {
|
||||
return lua::pushvec3(L, player->hitbox->position);
|
||||
}
|
||||
glm::vec3 pos = player->hitbox->position;
|
||||
lua_pushnumber(L, pos.x);
|
||||
lua_pushnumber(L, pos.y);
|
||||
lua_pushnumber(L, pos.z);
|
||||
return 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_set_pos(lua_State* L) {
|
||||
@@ -30,23 +26,18 @@ static int l_player_set_pos(lua_State* L) {
|
||||
if (!player) {
|
||||
return 0;
|
||||
}
|
||||
auto x = lua_tonumber(L, 2);
|
||||
auto y = lua_tonumber(L, 3);
|
||||
auto z = lua_tonumber(L, 4);
|
||||
auto x = lua::tonumber(L, 2);
|
||||
auto y = lua::tonumber(L, 3);
|
||||
auto z = lua::tonumber(L, 4);
|
||||
player->hitbox->position = glm::vec3(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_get_vel(lua_State* L) {
|
||||
auto player = get_player(L, 1);
|
||||
if (!player) {
|
||||
return 0;
|
||||
if (auto player = get_player(L, 1)) {
|
||||
return lua::pushvec3(L, player->hitbox->velocity);
|
||||
}
|
||||
glm::vec3 vel = player->hitbox->velocity;
|
||||
lua_pushnumber(L, vel.x);
|
||||
lua_pushnumber(L, vel.y);
|
||||
lua_pushnumber(L, vel.z);
|
||||
return 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_set_vel(lua_State* L) {
|
||||
@@ -54,23 +45,18 @@ static int l_player_set_vel(lua_State* L) {
|
||||
if (!player) {
|
||||
return 0;
|
||||
}
|
||||
auto x = lua_tonumber(L, 2);
|
||||
auto y = lua_tonumber(L, 3);
|
||||
auto z = lua_tonumber(L, 4);
|
||||
auto x = lua::tonumber(L, 2);
|
||||
auto y = lua::tonumber(L, 3);
|
||||
auto z = lua::tonumber(L, 4);
|
||||
player->hitbox->velocity = glm::vec3(x, y, z);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_get_rot(lua_State* L) {
|
||||
auto player = get_player(L, 1);
|
||||
if (!player) {
|
||||
return 0;
|
||||
if (auto player = get_player(L, 1)) {
|
||||
return lua::pushvec3(L, player->cam);
|
||||
}
|
||||
const glm::vec3& rot = player->cam;
|
||||
lua_pushnumber(L, rot.x);
|
||||
lua_pushnumber(L, rot.y);
|
||||
lua_pushnumber(L, rot.z);
|
||||
return 3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_set_rot(lua_State* L) {
|
||||
@@ -80,11 +66,11 @@ static int l_player_set_rot(lua_State* L) {
|
||||
}
|
||||
glm::vec3& cam = player->cam;
|
||||
|
||||
lua_Number x = lua_tonumber(L, 2);
|
||||
lua_Number y = lua_tonumber(L, 3);
|
||||
lua_Number x = lua::tonumber(L, 2);
|
||||
lua_Number y = lua::tonumber(L, 3);
|
||||
lua_Number z = cam.z;
|
||||
if (lua_isnumber(L, 4)) {
|
||||
z = lua_tonumber(L, 4);
|
||||
z = lua::tonumber(L, 4);
|
||||
}
|
||||
cam.x = x;
|
||||
cam.y = y;
|
||||
@@ -97,37 +83,35 @@ static int l_player_get_inv(lua_State* L) {
|
||||
if (!player) {
|
||||
return 0;
|
||||
}
|
||||
lua_pushinteger(L, player->getInventory()->getId());
|
||||
lua_pushinteger(L, player->getChosenSlot());
|
||||
lua::pushinteger(L, player->getInventory()->getId());
|
||||
lua::pushinteger(L, player->getChosenSlot());
|
||||
return 2;
|
||||
}
|
||||
|
||||
static int l_player_is_flight(lua_State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
lua_pushboolean(L, player->isFlight());
|
||||
return 1;
|
||||
return lua::pushboolean(L, player->isFlight());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_set_flight(lua_State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
player->setFlight(lua_toboolean(L, 2));
|
||||
player->setFlight(lua::toboolean(L, 2));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_is_noclip(lua_State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
lua_pushboolean(L, player->isNoclip());
|
||||
return 1;
|
||||
return lua::pushboolean(L, player->isNoclip());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_player_set_noclip(lua_State* L) {
|
||||
if (auto player = get_player(L, 1)) {
|
||||
player->setNoclip(lua_toboolean(L, 2));
|
||||
player->setNoclip(lua::toboolean(L, 2));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -137,27 +121,23 @@ static int l_player_get_selected_block(lua_State* L) {
|
||||
if (player->selection.vox.id == BLOCK_VOID) {
|
||||
return 0;
|
||||
}
|
||||
const glm::ivec3 pos = player->selection.position;
|
||||
lua_pushinteger(L, pos.x);
|
||||
lua_pushinteger(L, pos.y);
|
||||
lua_pushinteger(L, pos.z);
|
||||
return 3;
|
||||
return lua::pushivec3(L, player->selection.position);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg playerlib [] = {
|
||||
{"get_pos", lua_wrap_errors<l_player_get_pos>},
|
||||
{"set_pos", lua_wrap_errors<l_player_set_pos>},
|
||||
{"get_vel", lua_wrap_errors<l_player_get_vel>},
|
||||
{"set_vel", lua_wrap_errors<l_player_set_vel>},
|
||||
{"get_rot", lua_wrap_errors<l_player_get_rot>},
|
||||
{"set_rot", lua_wrap_errors<l_player_set_rot>},
|
||||
{"get_inventory", lua_wrap_errors<l_player_get_inv>},
|
||||
{"is_flight", lua_wrap_errors<l_player_is_flight>},
|
||||
{"set_flight", lua_wrap_errors<l_player_set_flight>},
|
||||
{"is_noclip", lua_wrap_errors<l_player_is_noclip>},
|
||||
{"set_noclip", lua_wrap_errors<l_player_set_noclip>},
|
||||
{"get_selected_block", lua_wrap_errors<l_player_get_selected_block>},
|
||||
{"get_pos", lua::wrap<l_player_get_pos>},
|
||||
{"set_pos", lua::wrap<l_player_set_pos>},
|
||||
{"get_vel", lua::wrap<l_player_get_vel>},
|
||||
{"set_vel", lua::wrap<l_player_set_vel>},
|
||||
{"get_rot", lua::wrap<l_player_get_rot>},
|
||||
{"set_rot", lua::wrap<l_player_set_rot>},
|
||||
{"get_inventory", lua::wrap<l_player_get_inv>},
|
||||
{"is_flight", lua::wrap<l_player_is_flight>},
|
||||
{"set_flight", lua::wrap<l_player_set_flight>},
|
||||
{"is_noclip", lua::wrap<l_player_is_noclip>},
|
||||
{"set_noclip", lua::wrap<l_player_set_noclip>},
|
||||
{"get_selected_block", lua::wrap<l_player_get_selected_block>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user