add entity.despawn(...)
This commit is contained in:
@@ -40,6 +40,7 @@ void LevelController::update(float delta, bool input, bool pause) {
|
|||||||
level->objects.end()
|
level->objects.end()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
level->entities->clean();
|
||||||
if (!pause) {
|
if (!pause) {
|
||||||
// update all objects that needed
|
// update all objects that needed
|
||||||
for (const auto& obj : level->objects) {
|
for (const auto& obj : level->objects) {
|
||||||
|
|||||||
@@ -31,6 +31,13 @@ static int l_spawn(lua::State* L) {
|
|||||||
return lua::pushinteger(L, id);
|
return lua::pushinteger(L, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
static int l_get_pos(lua::State* L) {
|
||||||
if (auto entity = get_entity(L, 1)) {
|
if (auto entity = get_entity(L, 1)) {
|
||||||
return lua::pushvec3_arr(L, entity->getTransform().pos);
|
return lua::pushvec3_arr(L, entity->getTransform().pos);
|
||||||
@@ -87,8 +94,16 @@ static int l_set_enabled(lua::State* L) {
|
|||||||
return 0;
|
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 [] = {
|
const luaL_Reg entitylib [] = {
|
||||||
{"spawn", lua::wrap<l_spawn>},
|
{"spawn", lua::wrap<l_spawn>},
|
||||||
|
{"despawn", lua::wrap<l_despawn>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -105,5 +120,6 @@ const luaL_Reg rigidbodylib [] = {
|
|||||||
{"set_enabled", lua::wrap<l_set_enabled>},
|
{"set_enabled", lua::wrap<l_set_enabled>},
|
||||||
{"get_vel", lua::wrap<l_get_vel>},
|
{"get_vel", lua::wrap<l_get_vel>},
|
||||||
{"set_vel", lua::wrap<l_set_vel>},
|
{"set_vel", lua::wrap<l_set_vel>},
|
||||||
|
{"get_size", lua::wrap<l_get_size>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -33,6 +33,16 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) {
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entities::clean() {
|
||||||
|
for (auto it = entities.begin(); it != entities.end(); ++it) {
|
||||||
|
if (registry.valid(it->second)) {
|
||||||
|
++it;
|
||||||
|
} else {
|
||||||
|
it = entities.erase(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Entities::updatePhysics(float delta){
|
void Entities::updatePhysics(float delta){
|
||||||
auto view = registry.view<Transform, Rigidbody>();
|
auto view = registry.view<Transform, Rigidbody>();
|
||||||
auto physics = level->physics.get();
|
auto physics = level->physics.get();
|
||||||
|
|||||||
@@ -36,11 +36,16 @@ class Rig;
|
|||||||
struct EntityDef;
|
struct EntityDef;
|
||||||
|
|
||||||
class Entity {
|
class Entity {
|
||||||
|
entityid_t id;
|
||||||
entt::registry& registry;
|
entt::registry& registry;
|
||||||
const entt::entity entity;
|
const entt::entity entity;
|
||||||
public:
|
public:
|
||||||
Entity(entt::registry& registry, const entt::entity entity)
|
Entity(entityid_t id, entt::registry& registry, const entt::entity entity)
|
||||||
: registry(registry), entity(entity) {}
|
: id(id), registry(registry), entity(entity) {}
|
||||||
|
|
||||||
|
entityid_t getID() const {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
bool isValid() const {
|
bool isValid() const {
|
||||||
return registry.valid(entity);
|
return registry.valid(entity);
|
||||||
@@ -57,6 +62,10 @@ public:
|
|||||||
entityid_t getUID() const {
|
entityid_t getUID() const {
|
||||||
return registry.get<EntityId>(entity).uid;
|
return registry.get<EntityId>(entity).uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void destroy() {
|
||||||
|
registry.destroy(entity);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Entities {
|
class Entities {
|
||||||
@@ -66,6 +75,7 @@ class Entities {
|
|||||||
entityid_t nextID = 1;
|
entityid_t nextID = 1;
|
||||||
public:
|
public:
|
||||||
Entities(Level* level);
|
Entities(Level* level);
|
||||||
|
void clean();
|
||||||
void updatePhysics(float delta);
|
void updatePhysics(float delta);
|
||||||
|
|
||||||
void renderDebug(LineBatch& batch);
|
void renderDebug(LineBatch& batch);
|
||||||
@@ -76,7 +86,7 @@ public:
|
|||||||
std::optional<Entity> get(entityid_t id) {
|
std::optional<Entity> get(entityid_t id) {
|
||||||
const auto& found = entities.find(id);
|
const auto& found = entities.find(id);
|
||||||
if (found != entities.end()) {
|
if (found != entities.end()) {
|
||||||
return Entity(registry, found->second);
|
return Entity(id, registry, found->second);
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user