From 19a2ec3d04b0eaad0e9a8378ee6eb35e8c999dea Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Wed, 16 Feb 2022 13:04:31 +0800 Subject: [PATCH] Add GridMap.get_used_cells_by_item --- modules/gridmap/doc_classes/GridMap.xml | 7 +++++++ modules/gridmap/grid_map.cpp | 13 +++++++++++++ modules/gridmap/grid_map.h | 1 + 3 files changed, 21 insertions(+) diff --git a/modules/gridmap/doc_classes/GridMap.xml b/modules/gridmap/doc_classes/GridMap.xml index 2ea9a78970b..049d3726718 100644 --- a/modules/gridmap/doc_classes/GridMap.xml +++ b/modules/gridmap/doc_classes/GridMap.xml @@ -79,6 +79,13 @@ Returns an array of [Vector3] with the non-empty cell coordinates in the grid map. + + + + + Returns an array of all cells with the given item index specified in [code]item[/code]. + + diff --git a/modules/gridmap/grid_map.cpp b/modules/gridmap/grid_map.cpp index 6df7835855d..4b72c71a5ab 100644 --- a/modules/gridmap/grid_map.cpp +++ b/modules/gridmap/grid_map.cpp @@ -878,6 +878,7 @@ void GridMap::_bind_methods() { ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear); ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells); + ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item); ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes); ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes); @@ -950,6 +951,18 @@ Array GridMap::get_used_cells() const { return a; } +Array GridMap::get_used_cells_by_item(int p_item) const { + Array a; + for (const KeyValue &E : cell_map) { + if (E.value.item == p_item) { + Vector3 p(E.key.x, E.key.y, E.key.z); + a.push_back(p); + } + } + + return a; +} + Array GridMap::get_meshes() const { if (mesh_library.is_null()) { return Array(); diff --git a/modules/gridmap/grid_map.h b/modules/gridmap/grid_map.h index 6cdc3b178df..83d5af1324b 100644 --- a/modules/gridmap/grid_map.h +++ b/modules/gridmap/grid_map.h @@ -266,6 +266,7 @@ public: float get_cell_scale() const; Array get_used_cells() const; + Array get_used_cells_by_item(int p_item) const; Array get_meshes() const;