2024-06-30 19:19:43 +03:00

131 lines
3.2 KiB
C++

#include "api_lua.hpp"
#include "../../LevelController.hpp"
#include "../../../world/Level.hpp"
#include "../../../objects/Player.hpp"
#include "../../../objects/Entities.hpp"
#include "../../../physics/Hitbox.hpp"
#include "../../../window/Camera.hpp"
#include "../../../frontend/hud.hpp"
#include "../../../content/Content.hpp"
#include <optional>
namespace scripting {
extern Hud* hud;
}
using namespace scripting;
static std::optional<Entity> get_entity(lua::State* L, int idx) {
auto id = lua::tointeger(L, idx);
auto level = controller->getLevel();
return level->entities->get(id);
}
static int l_exists(lua::State* L) {
return lua::pushboolean(L, get_entity(L, 1).has_value());
}
static int l_spawn(lua::State* L) {
auto level = controller->getLevel();
auto defname = lua::tostring(L, 1);
auto& def = content->entities.require(defname);
auto pos = lua::tovec3(L, 2);
level->entities->spawn(def, pos);
return 1;
}
static int l_despawn(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
entity->destroy();
}
return 0;
}
static int l_get_pos(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushvec3_arr(L, entity->getTransform().pos);
}
return 0;
}
static int l_set_pos(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
entity->getTransform().setPos(lua::tovec3(L, 2));
}
return 0;
}
static int l_get_vel(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushvec3_arr(L, entity->getRigidbody().hitbox.velocity);
}
return 0;
}
static int l_set_vel(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
entity->getRigidbody().hitbox.velocity = lua::tovec3(L, 2);
}
return 0;
}
static int l_get_rot(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushmat4(L, entity->getTransform().rot);
}
return 0;
}
static int l_set_rot(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
entity->getTransform().setRot(lua::tomat4(L, 2));
}
return 0;
}
static int l_is_enabled(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
lua::pushboolean(L, entity->getRigidbody().enabled);
}
return 0;
}
static int l_set_enabled(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
entity->getRigidbody().enabled = lua::toboolean(L, 2);
}
return 0;
}
static int l_get_size(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushvec3(L, entity->getRigidbody().hitbox.halfsize * 2.0f);
}
return 0;
}
const luaL_Reg entitylib [] = {
{"exists", lua::wrap<l_exists>},
{"spawn", lua::wrap<l_spawn>},
{"despawn", lua::wrap<l_despawn>},
{NULL, NULL}
};
const luaL_Reg transformlib [] = {
{"get_pos", lua::wrap<l_get_pos>},
{"set_pos", lua::wrap<l_set_pos>},
{"get_rot", lua::wrap<l_get_rot>},
{"set_rot", lua::wrap<l_set_rot>},
{NULL, NULL}
};
const luaL_Reg rigidbodylib [] = {
{"is_enabled", lua::wrap<l_is_enabled>},
{"set_enabled", lua::wrap<l_set_enabled>},
{"get_vel", lua::wrap<l_get_vel>},
{"set_vel", lua::wrap<l_set_vel>},
{"get_size", lua::wrap<l_get_size>},
{NULL, NULL}
};