fixed and optimized Area2/3D get_overlapping_bodies/areas

fixed a type and made it so area3d get_overlapping_x is similar to its counterpart func in area2d so that it uses TypedArray instead of Array and ERR_FAIL_COND_V_MSG instead of no message, also minimized array resize calls
This commit is contained in:
Void 2022-09-01 20:02:43 +01:00
parent 9142904c24
commit fdfe11d4b3
2 changed files with 22 additions and 23 deletions

View file

@ -426,36 +426,36 @@ bool Area2D::is_monitorable() const {
} }
TypedArray<Node2D> Area2D::get_overlapping_bodies() const { TypedArray<Node2D> Area2D::get_overlapping_bodies() const {
ERR_FAIL_COND_V_MSG(!monitoring, Array(), "Can't find overlapping bodies when monitoring is off.");
TypedArray<Node2D> ret; TypedArray<Node2D> ret;
ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping bodies when monitoring is off.");
ret.resize(body_map.size()); ret.resize(body_map.size());
int idx = 0; int idx = 0;
for (const KeyValue<ObjectID, BodyState> &E : body_map) { for (const KeyValue<ObjectID, BodyState> &E : body_map) {
Object *obj = ObjectDB::get_instance(E.key); Object *obj = ObjectDB::get_instance(E.key);
if (!obj) { if (obj) {
ret.resize(ret.size() - 1); //ops ret[idx] = obj;
} else { idx++;
ret[idx++] = obj;
} }
} }
ret.resize(idx);
return ret; return ret;
} }
TypedArray<Area2D> Area2D::get_overlapping_areas() const { TypedArray<Area2D> Area2D::get_overlapping_areas() const {
ERR_FAIL_COND_V_MSG(!monitoring, Array(), "Can't find overlapping bodies when monitoring is off.");
TypedArray<Area2D> ret; TypedArray<Area2D> ret;
ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping areas when monitoring is off.");
ret.resize(area_map.size()); ret.resize(area_map.size());
int idx = 0; int idx = 0;
for (const KeyValue<ObjectID, AreaState> &E : area_map) { for (const KeyValue<ObjectID, AreaState> &E : area_map) {
Object *obj = ObjectDB::get_instance(E.key); Object *obj = ObjectDB::get_instance(E.key);
if (!obj) { if (obj) {
ret.resize(ret.size() - 1); //ops ret[idx] = obj;
} else { idx++;
ret[idx++] = obj;
} }
} }
ret.resize(idx);
return ret; return ret;
} }

View file

@ -473,19 +473,19 @@ bool Area3D::is_monitoring() const {
} }
TypedArray<Node3D> Area3D::get_overlapping_bodies() const { TypedArray<Node3D> Area3D::get_overlapping_bodies() const {
ERR_FAIL_COND_V(!monitoring, Array()); TypedArray<Node3D> ret;
Array ret; ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping bodies when monitoring is off.");
ret.resize(body_map.size()); ret.resize(body_map.size());
int idx = 0; int idx = 0;
for (const KeyValue<ObjectID, BodyState> &E : body_map) { for (const KeyValue<ObjectID, BodyState> &E : body_map) {
Object *obj = ObjectDB::get_instance(E.key); Object *obj = ObjectDB::get_instance(E.key);
if (!obj) { if (obj) {
ret.resize(ret.size() - 1); //ops ret[idx] = obj;
} else { idx++;
ret[idx++] = obj;
} }
} }
ret.resize(idx);
return ret; return ret;
} }
@ -506,19 +506,18 @@ bool Area3D::is_monitorable() const {
} }
TypedArray<Area3D> Area3D::get_overlapping_areas() const { TypedArray<Area3D> Area3D::get_overlapping_areas() const {
ERR_FAIL_COND_V(!monitoring, Array()); TypedArray<Area3D> ret;
Array ret; ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping areas when monitoring is off.");
ret.resize(area_map.size()); ret.resize(area_map.size());
int idx = 0; int idx = 0;
for (const KeyValue<ObjectID, AreaState> &E : area_map) { for (const KeyValue<ObjectID, AreaState> &E : area_map) {
Object *obj = ObjectDB::get_instance(E.key); Object *obj = ObjectDB::get_instance(E.key);
if (!obj) { if (obj) {
ret.resize(ret.size() - 1); //ops ret[idx] = obj;
} else { idx++;
ret[idx++] = obj;
} }
} }
ret.resize(idx);
return ret; return ret;
} }