diff options
Diffstat (limited to 'modules/gridmap')
-rw-r--r-- | modules/gridmap/doc_classes/GridMap.xml | 52 | ||||
-rw-r--r-- | modules/gridmap/grid_map.cpp | 48 | ||||
-rw-r--r-- | modules/gridmap/grid_map.h | 7 |
3 files changed, 104 insertions, 3 deletions
diff --git a/modules/gridmap/doc_classes/GridMap.xml b/modules/gridmap/doc_classes/GridMap.xml index 3676570ec1..5b0fe56f25 100644 --- a/modules/gridmap/doc_classes/GridMap.xml +++ b/modules/gridmap/doc_classes/GridMap.xml @@ -1,8 +1,13 @@ <?xml version="1.0" encoding="UTF-8" ?> <class name="GridMap" inherits="Spatial" category="Core" version="3.0.alpha.custom_build"> <brief_description> + Node for 3D tile-based maps. </brief_description> <description> + GridMap lets you place meshes on a grid interactively. It works both from the editor and can help you create in-game level editors. + GridMaps use a [MeshLibrary] which contain a list of tiles: meshes with materials plus optional collisions and extra elements. + A GridMap contains a collection of cells. Each grid cell refers to a [MeshLibrary] item. All cells in the map have the same dimensions. + A GridMap is split into a sparse collection of octants for efficient rendering and physics processing. Every octant has the same dimensions and can contain several cells. </description> <tutorials> </tutorials> @@ -13,6 +18,7 @@ <return type="void"> </return> <description> + Clear all cells. </description> </method> <method name="get_cell_item" qualifiers="const"> @@ -25,6 +31,7 @@ <argument index="2" name="z" type="int"> </argument> <description> + The [MeshLibrary] item index located at the grid-based X, Y and Z coordinates. If the cell is empty, [INVALID_CELL_ITEM] will be returned. </description> </method> <method name="get_cell_item_orientation" qualifiers="const"> @@ -37,48 +44,75 @@ <argument index="2" name="z" type="int"> </argument> <description> + The orientation of the cell at the grid-based X, Y and Z coordinates. -1 is retuned if the cell is empty. </description> </method> <method name="get_cell_size" qualifiers="const"> <return type="Vector3"> </return> <description> + The dimensions of the grid's cells. </description> </method> <method name="get_center_x" qualifiers="const"> <return type="bool"> </return> <description> + Returns whether or not grid items are centered on the X axis. </description> </method> <method name="get_center_y" qualifiers="const"> <return type="bool"> </return> <description> + Returns whether or not grid items are centered on the Y axis. </description> </method> <method name="get_center_z" qualifiers="const"> <return type="bool"> </return> <description> + Returns whether or not grid items are centered on the Z axis. </description> </method> <method name="get_meshes"> <return type="Array"> </return> <description> + Array of [Transform] and [Mesh] references corresponding to the non empty cells in the grid. The transforms are specified in world space. </description> </method> <method name="get_octant_size" qualifiers="const"> <return type="int"> </return> <description> + The size of each octant measured in number of cells. This applies to all three axis. </description> </method> <method name="get_theme" qualifiers="const"> <return type="MeshLibrary"> </return> <description> + The assigned [MeshLibrary]. + </description> + </method> + <method name="get_used_cells" qualifiers="const"> + <return type="Array"> + </return> + <description> + Array of [Vector3] with the non empty cell coordinates in the grid map. + </description> + </method> + <method name="map_to_world" qualifiers="const"> + <return type="Vector3"> + </return> + <argument index="0" name="x" type="int"> + </argument> + <argument index="1" name="y" type="int"> + </argument> + <argument index="2" name="z" type="int"> + </argument> + <description> </description> </method> <method name="resource_changed"> @@ -103,6 +137,9 @@ <argument index="4" name="orientation" type="int" default="0"> </argument> <description> + Set the mesh index for the cell referenced by its grid-based X, Y and Z coordinates. + A negative item index will clear the cell. + Optionally, the item's orientation can be passed. </description> </method> <method name="set_cell_size"> @@ -111,6 +148,7 @@ <argument index="0" name="size" type="Vector3"> </argument> <description> + Sets the height, width and depth of the grid's cells. </description> </method> <method name="set_center_x"> @@ -119,6 +157,7 @@ <argument index="0" name="enable" type="bool"> </argument> <description> + Set grid items to be centered on the X axis. By default it is enabled. </description> </method> <method name="set_center_y"> @@ -127,6 +166,7 @@ <argument index="0" name="enable" type="bool"> </argument> <description> + Set grid items to be centered on the Y axis. By default it is enabled. </description> </method> <method name="set_center_z"> @@ -135,6 +175,7 @@ <argument index="0" name="enable" type="bool"> </argument> <description> + Set grid items to be centered on the Z axis. By default it is enabled. </description> </method> <method name="set_clip"> @@ -157,6 +198,7 @@ <argument index="0" name="size" type="int"> </argument> <description> + Sets the size for each octant measured in number of cells. This applies to all three axis. </description> </method> <method name="set_theme"> @@ -165,11 +207,21 @@ <argument index="0" name="theme" type="MeshLibrary"> </argument> <description> + Sets the collection of meshes for the map. + </description> + </method> + <method name="world_to_map" qualifiers="const"> + <return type="Vector3"> + </return> + <argument index="0" name="pos" type="Vector3"> + </argument> + <description> </description> </method> </methods> <constants> <constant name="INVALID_CELL_ITEM" value="-1" enum=""> + Invalid cell item that can be used in [method set_cell_item] to clear cells (or represent an empty cell in [method get_cell_item]). </constant> </constants> </class> diff --git a/modules/gridmap/grid_map.cpp b/modules/gridmap/grid_map.cpp index 4f7545a11d..cb14a5ee9c 100644 --- a/modules/gridmap/grid_map.cpp +++ b/modules/gridmap/grid_map.cpp @@ -333,6 +333,23 @@ int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const { return cell_map[key].rot; } +Vector3 GridMap::world_to_map(const Vector3 &p_world_pos) const { + Vector3 map_pos = p_world_pos / cell_size; + map_pos.x = floor(map_pos.x); + map_pos.y = floor(map_pos.y); + map_pos.z = floor(map_pos.z); + return map_pos; +} + +Vector3 GridMap::map_to_world(int p_x, int p_y, int p_z) const { + Vector3 offset = _get_offset(); + Vector3 world_pos( + p_x * cell_size.x + offset.x, + p_y * cell_size.y + offset.y, + p_z * cell_size.z + offset.z); + return world_pos; +} + void GridMap::_octant_transform(const OctantKey &p_key) { ERR_FAIL_COND(!octant_map.has(p_key)); @@ -407,7 +424,7 @@ bool GridMap::_octant_update(const OctantKey &p_key) { //print_line("OCTANT, CELLS: "+itos(ii.cells.size())); Vector3 cellpos = Vector3(E->get().x, E->get().y, E->get().z); - Vector3 ofs(cell_size.x * 0.5 * int(center_x), cell_size.y * 0.5 * int(center_y), cell_size.z * 0.5 * int(center_z)); + Vector3 ofs = _get_offset(); Transform xform; @@ -754,6 +771,9 @@ void GridMap::_bind_methods() { ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item); ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "x", "y", "z"), &GridMap::get_cell_item_orientation); + ClassDB::bind_method(D_METHOD("world_to_map", "pos"), &GridMap::world_to_map); + ClassDB::bind_method(D_METHOD("map_to_world", "x", "y", "z"), &GridMap::map_to_world); + //ClassDB::bind_method(D_METHOD("_recreate_octants"),&GridMap::_recreate_octants); ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback); ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed); @@ -769,6 +789,8 @@ 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_meshes"), &GridMap::get_meshes); BIND_CONSTANT(INVALID_CELL_ITEM); @@ -799,7 +821,7 @@ void GridMap::set_clip(bool p_enabled, bool p_clip_above, int p_floor, Vector3:: void GridMap::set_cell_scale(float p_scale) { cell_scale = p_scale; - _queue_octants_dirty(); + _recreate_octant_data(); } float GridMap::get_cell_scale() const { @@ -807,12 +829,25 @@ float GridMap::get_cell_scale() const { return cell_scale; } +Array GridMap::get_used_cells() const { + + Array a; + a.resize(cell_map.size()); + int i = 0; + for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) { + Vector3 p(E->key().x, E->key().y, E->key().z); + a[i++] = p; + } + + return a; +} + Array GridMap::get_meshes() { if (theme.is_null()) return Array(); - Vector3 ofs(cell_size.x * 0.5 * int(center_x), cell_size.y * 0.5 * int(center_y), cell_size.z * 0.5 * int(center_z)); + Vector3 ofs = _get_offset(); Array meshes; for (Map<IndexKey, Cell>::Element *E = cell_map.front(); E; E = E->next()) { @@ -842,6 +877,13 @@ Array GridMap::get_meshes() { return meshes; } +Vector3 GridMap::_get_offset() const { + return Vector3( + cell_size.x * 0.5 * int(center_x), + cell_size.y * 0.5 * int(center_y), + cell_size.z * 0.5 * int(center_z)); +} + GridMap::GridMap() { cell_size = Vector3(2, 2, 2); diff --git a/modules/gridmap/grid_map.h b/modules/gridmap/grid_map.h index eb1b215696..5bfdf1dac3 100644 --- a/modules/gridmap/grid_map.h +++ b/modules/gridmap/grid_map.h @@ -184,6 +184,8 @@ class GridMap : public Spatial { void _clear_internal(); + Vector3 _get_offset() const; + protected: bool _set(const StringName &p_name, const Variant &p_value); bool _get(const StringName &p_name, Variant &r_ret) const; @@ -218,11 +220,16 @@ public: int get_cell_item(int p_x, int p_y, int p_z) const; int get_cell_item_orientation(int p_x, int p_y, int p_z) const; + Vector3 world_to_map(const Vector3 &p_pos) const; + Vector3 map_to_world(int p_x, int p_y, int p_z) const; + void set_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X); void set_cell_scale(float p_scale); float get_cell_scale() const; + Array get_used_cells() const; + Array get_meshes(); void clear(); |