add Rigidbody.is_enabled, .set_enabled

This commit is contained in:
MihailRis 2024-06-27 08:23:01 +03:00
parent 1ca0761f49
commit 6d7f950410
3 changed files with 32 additions and 7 deletions

View File

@ -47,14 +47,14 @@ static int l_set_pos(lua::State* L) {
static int l_get_vel(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushvec3_arr(L, entity->getHitbox().velocity);
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->getHitbox().velocity = lua::tovec3(L, 2);
entity->getRigidbody().hitbox.velocity = lua::tovec3(L, 2);
}
return 0;
}
@ -73,6 +73,20 @@ static int l_set_rot(lua::State* L) {
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;
}
const luaL_Reg entitylib [] = {
{"spawn", lua::wrap<l_spawn>},
{NULL, NULL}
@ -87,6 +101,8 @@ const luaL_Reg entitylib [] = {
};
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>},
{NULL, NULL}

View File

@ -28,15 +28,19 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
auto id = nextID++;
registry.emplace<EntityId>(entity, static_cast<entityid_t>(id));
registry.emplace<Transform>(entity, pos, size/4.0f, glm::mat3(1.0f));
registry.emplace<Hitbox>(entity, pos, def.hitbox);
registry.emplace<Rigidbody>(entity, true, Hitbox {pos, def.hitbox});
entities[id] = entity;
return id;
}
void Entities::updatePhysics(float delta){
auto view = registry.view<Transform, Hitbox>();
auto view = registry.view<Transform, Rigidbody>();
auto physics = level->physics.get();
for (auto [entity, transform, hitbox] : view.each()) {
for (auto [entity, transform, rigidbody] : view.each()) {
if (!rigidbody.enabled) {
continue;
}
auto& hitbox = rigidbody.hitbox;
physics->step(
level->chunks.get(),
&hitbox,

View File

@ -22,6 +22,11 @@ struct Transform {
void refresh();
};
struct Rigidbody {
bool enabled = true;
Hitbox hitbox;
};
class Level;
class Assets;
class LineBatch;
@ -45,8 +50,8 @@ public:
return registry.get<Transform>(entity);
}
Hitbox& getHitbox() const {
return registry.get<Hitbox>(entity);
Rigidbody& getRigidbody() const {
return registry.get<Rigidbody>(entity);
}
entityid_t getUID() const {