2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-03-26 22:49:16 +01:00
|
|
|
/* proximity_group_3d.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "proximity_group_3d.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
void ProximityGroup3D::_clear_groups() {
|
2014-02-10 02:10:30 +01:00
|
|
|
Map<StringName, uint32_t>::Element *E;
|
|
|
|
|
|
|
|
{
|
|
|
|
const int size = 16;
|
|
|
|
StringName remove_list[size];
|
|
|
|
E = groups.front();
|
|
|
|
int num = 0;
|
|
|
|
while (E && num < size) {
|
|
|
|
if (E->get() != group_version) {
|
|
|
|
remove_list[num++] = E->key();
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
E = E->next();
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < num; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
groups.erase(remove_list[i]);
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (E) {
|
2020-12-11 14:26:50 +01:00
|
|
|
_clear_groups(); // call until we go through the whole list
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
void ProximityGroup3D::_update_groups() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (grid_radius == Vector3(0, 0, 0)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
++group_version;
|
|
|
|
|
|
|
|
Vector3 pos = get_global_transform().get_origin();
|
|
|
|
Vector3 vcell = pos / cell_size;
|
|
|
|
int cell[3] = { Math::fast_ftoi(vcell.x), Math::fast_ftoi(vcell.y), Math::fast_ftoi(vcell.z) };
|
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
_add_groups(cell, group_name, 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
_clear_groups();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
void ProximityGroup3D::_add_groups(int *p_cell, String p_base, int p_depth) {
|
2014-02-10 02:10:30 +01:00
|
|
|
p_base = p_base + "|";
|
|
|
|
if (grid_radius[p_depth] == 0) {
|
|
|
|
if (p_depth == 2) {
|
|
|
|
_new_group(p_base);
|
|
|
|
} else {
|
2020-12-11 14:26:50 +01:00
|
|
|
_add_groups(p_cell, p_base, p_depth + 1);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int start = p_cell[p_depth] - grid_radius[p_depth];
|
|
|
|
int end = p_cell[p_depth] + grid_radius[p_depth];
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = start; i <= end; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
String gname = p_base + itos(i);
|
|
|
|
if (p_depth == 2) {
|
|
|
|
_new_group(gname);
|
|
|
|
} else {
|
2020-12-11 14:26:50 +01:00
|
|
|
_add_groups(p_cell, gname, p_depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void ProximityGroup3D::_new_group(StringName p_name) {
|
2017-03-05 16:44:50 +01:00
|
|
|
const Map<StringName, uint32_t>::Element *E = groups.find(p_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!E) {
|
|
|
|
add_to_group(p_name);
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
groups[p_name] = group_version;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void ProximityGroup3D::_notification(int p_what) {
|
2017-08-11 21:10:05 +02:00
|
|
|
switch (p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE:
|
|
|
|
++group_version;
|
2020-12-11 14:26:50 +01:00
|
|
|
_clear_groups();
|
2017-03-05 16:44:50 +01:00
|
|
|
break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED:
|
2020-12-11 14:26:50 +01:00
|
|
|
_update_groups();
|
2017-03-05 16:44:50 +01:00
|
|
|
break;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
void ProximityGroup3D::broadcast(String p_method, Variant p_parameters) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Map<StringName, uint32_t>::Element *E;
|
|
|
|
E = groups.front();
|
|
|
|
while (E) {
|
2020-12-11 14:26:50 +01:00
|
|
|
get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFAULT, E->key(), "_proximity_group_broadcast", p_method, p_parameters);
|
2014-02-10 02:10:30 +01:00
|
|
|
E = E->next();
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
void ProximityGroup3D::_proximity_group_broadcast(String p_method, Variant p_parameters) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (dispatch_mode == MODE_PROXY) {
|
2021-07-27 06:26:13 +02:00
|
|
|
ERR_FAIL_COND(!is_inside_tree());
|
2020-12-11 14:26:50 +01:00
|
|
|
get_parent()->call(p_method, p_parameters);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("broadcast"), p_method, p_parameters);
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void ProximityGroup3D::set_group_name(const String &p_group_name) {
|
2018-01-11 23:35:12 +01:00
|
|
|
group_name = p_group_name;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2018-01-11 23:35:12 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
String ProximityGroup3D::get_group_name() const {
|
2018-01-11 23:35:12 +01:00
|
|
|
return group_name;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2018-01-11 23:35:12 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void ProximityGroup3D::set_dispatch_mode(DispatchMode p_mode) {
|
2018-01-11 23:35:12 +01:00
|
|
|
dispatch_mode = p_mode;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2018-01-11 23:35:12 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ProximityGroup3D::DispatchMode ProximityGroup3D::get_dispatch_mode() const {
|
2018-01-11 23:35:12 +01:00
|
|
|
return dispatch_mode;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void ProximityGroup3D::set_grid_radius(const Vector3 &p_radius) {
|
2014-02-10 02:10:30 +01:00
|
|
|
grid_radius = p_radius;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Vector3 ProximityGroup3D::get_grid_radius() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return grid_radius;
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void ProximityGroup3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_group_name", "name"), &ProximityGroup3D::set_group_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_group_name"), &ProximityGroup3D::get_group_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_dispatch_mode", "mode"), &ProximityGroup3D::set_dispatch_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_dispatch_mode"), &ProximityGroup3D::get_dispatch_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_grid_radius", "radius"), &ProximityGroup3D::set_grid_radius);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_grid_radius"), &ProximityGroup3D::get_grid_radius);
|
2020-12-11 14:26:50 +01:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("broadcast", "method", "parameters"), &ProximityGroup3D::broadcast);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("_proximity_group_broadcast", "method", "parameters"), &ProximityGroup3D::_proximity_group_broadcast);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "group_name"), "set_group_name", "get_group_name");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "dispatch_mode", PROPERTY_HINT_ENUM, "Proxy,Signal"), "set_dispatch_mode", "get_dispatch_mode");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "grid_radius"), "set_grid_radius", "get_grid_radius");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-12-11 14:26:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("broadcast", PropertyInfo(Variant::STRING, "method"), PropertyInfo(Variant::ARRAY, "parameters")));
|
2018-01-11 23:35:12 +01:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(MODE_PROXY);
|
|
|
|
BIND_ENUM_CONSTANT(MODE_SIGNAL);
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ProximityGroup3D::ProximityGroup3D() {
|
2017-01-13 00:35:46 +01:00
|
|
|
set_notify_transform(true);
|
2020-12-11 14:26:50 +01:00
|
|
|
}
|