1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-10 13:00:37 +00:00

Update GridMap to use Vector3i instead of three ints

This commit is contained in:
Aaron Franke
2020-06-29 04:52:01 -04:00
parent f9c2f35dd9
commit 8a333cdf9f
4 changed files with 85 additions and 73 deletions

View File

@@ -244,26 +244,26 @@ bool GridMap::get_center_z() const {
return center_z; return center_z;
} }
void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) { void GridMap::set_cell_item(const Vector3i &p_position, int p_item, int p_rot) {
if (baked_meshes.size() && !recreating_octants) { if (baked_meshes.size() && !recreating_octants) {
//if you set a cell item, baked meshes go good bye //if you set a cell item, baked meshes go good bye
clear_baked_meshes(); clear_baked_meshes();
_recreate_octant_data(); _recreate_octant_data();
} }
ERR_FAIL_INDEX(ABS(p_x), 1 << 20); ERR_FAIL_INDEX(ABS(p_position.x), 1 << 20);
ERR_FAIL_INDEX(ABS(p_y), 1 << 20); ERR_FAIL_INDEX(ABS(p_position.y), 1 << 20);
ERR_FAIL_INDEX(ABS(p_z), 1 << 20); ERR_FAIL_INDEX(ABS(p_position.z), 1 << 20);
IndexKey key; IndexKey key;
key.x = p_x; key.x = p_position.x;
key.y = p_y; key.y = p_position.y;
key.z = p_z; key.z = p_position.z;
OctantKey ok; OctantKey ok;
ok.x = p_x / octant_size; ok.x = p_position.x / octant_size;
ok.y = p_y / octant_size; ok.y = p_position.y / octant_size;
ok.z = p_z / octant_size; ok.z = p_position.z / octant_size;
if (p_item < 0) { if (p_item < 0) {
//erase //erase
@@ -318,15 +318,15 @@ void GridMap::set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot) {
cell_map[key] = c; cell_map[key] = c;
} }
int GridMap::get_cell_item(int p_x, int p_y, int p_z) const { int GridMap::get_cell_item(const Vector3i &p_position) const {
ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, INVALID_CELL_ITEM); ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, INVALID_CELL_ITEM);
ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, INVALID_CELL_ITEM); ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, INVALID_CELL_ITEM);
ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, INVALID_CELL_ITEM); ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, INVALID_CELL_ITEM);
IndexKey key; IndexKey key;
key.x = p_x; key.x = p_position.x;
key.y = p_y; key.y = p_position.y;
key.z = p_z; key.z = p_position.z;
if (!cell_map.has(key)) { if (!cell_map.has(key)) {
return INVALID_CELL_ITEM; return INVALID_CELL_ITEM;
@@ -334,15 +334,15 @@ int GridMap::get_cell_item(int p_x, int p_y, int p_z) const {
return cell_map[key].item; return cell_map[key].item;
} }
int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const { int GridMap::get_cell_item_orientation(const Vector3i &p_position) const {
ERR_FAIL_INDEX_V(ABS(p_x), 1 << 20, -1); ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, -1);
ERR_FAIL_INDEX_V(ABS(p_y), 1 << 20, -1); ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, -1);
ERR_FAIL_INDEX_V(ABS(p_z), 1 << 20, -1); ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, -1);
IndexKey key; IndexKey key;
key.x = p_x; key.x = p_position.x;
key.y = p_y; key.y = p_position.y;
key.z = p_z; key.z = p_position.z;
if (!cell_map.has(key)) { if (!cell_map.has(key)) {
return -1; return -1;
@@ -350,20 +350,20 @@ int GridMap::get_cell_item_orientation(int p_x, int p_y, int p_z) const {
return cell_map[key].rot; return cell_map[key].rot;
} }
Vector3 GridMap::world_to_map(const Vector3 &p_world_pos) const { Vector3i GridMap::world_to_map(const Vector3 &p_world_position) const {
Vector3 map_pos = p_world_pos / cell_size; Vector3 map_position = p_world_position / cell_size;
map_pos.x = floor(map_pos.x); map_position.x = floor(map_position.x);
map_pos.y = floor(map_pos.y); map_position.y = floor(map_position.y);
map_pos.z = floor(map_pos.z); map_position.z = floor(map_position.z);
return map_pos; return Vector3i(map_position);
} }
Vector3 GridMap::map_to_world(int p_x, int p_y, int p_z) const { Vector3 GridMap::map_to_world(const Vector3i &p_map_position) const {
Vector3 offset = _get_offset(); Vector3 offset = _get_offset();
Vector3 world_pos( Vector3 world_pos(
p_x * cell_size.x + offset.x, p_map_position.x * cell_size.x + offset.x,
p_y * cell_size.y + offset.y, p_map_position.y * cell_size.y + offset.y,
p_z * cell_size.z + offset.z); p_map_position.z * cell_size.z + offset.z);
return world_pos; return world_pos;
} }
@@ -725,7 +725,7 @@ void GridMap::_recreate_octant_data() {
Map<IndexKey, Cell> cell_copy = cell_map; Map<IndexKey, Cell> cell_copy = cell_map;
_clear_internal(); _clear_internal();
for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) { for (Map<IndexKey, Cell>::Element *E = cell_copy.front(); E; E = E->next()) {
set_cell_item(E->key().x, E->key().y, E->key().z, E->get().item, E->get().rot); set_cell_item(Vector3i(E->key()), E->get().item, E->get().rot);
} }
recreating_octants = false; recreating_octants = false;
} }
@@ -799,12 +799,12 @@ void GridMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size); ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size); ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
ClassDB::bind_method(D_METHOD("set_cell_item", "x", "y", "z", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0)); ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_cell_item", "x", "y", "z"), &GridMap::get_cell_item); ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &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("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
ClassDB::bind_method(D_METHOD("world_to_map", "pos"), &GridMap::world_to_map); ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
ClassDB::bind_method(D_METHOD("map_to_world", "x", "y", "z"), &GridMap::map_to_world); ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);
ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback); ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed); ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);

View File

@@ -59,6 +59,15 @@ class GridMap : public Node3D {
return key < p_key.key; return key < p_key.key;
} }
_FORCE_INLINE_ operator Vector3i() const {
return Vector3i(x, y, z);
}
IndexKey(Vector3i p_vector) {
x = (int16_t)p_vector.x;
y = (int16_t)p_vector.y;
z = (int16_t)p_vector.z;
}
IndexKey() { key = 0; } IndexKey() { key = 0; }
}; };
@@ -234,12 +243,12 @@ public:
void set_center_z(bool p_enable); void set_center_z(bool p_enable);
bool get_center_z() const; bool get_center_z() const;
void set_cell_item(int p_x, int p_y, int p_z, int p_item, int p_rot = 0); void set_cell_item(const Vector3i &p_position, int p_item, int p_rot = 0);
int get_cell_item(int p_x, int p_y, int p_z) const; int get_cell_item(const Vector3i &p_position) const;
int get_cell_item_orientation(int p_x, int p_y, int p_z) const; int get_cell_item_orientation(const Vector3i &p_position) const;
Vector3 world_to_map(const Vector3 &p_world_pos) const; Vector3i world_to_map(const Vector3 &p_world_position) const;
Vector3 map_to_world(int p_x, int p_y, int p_z) const; Vector3 map_to_world(const Vector3i &p_map_position) 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_clip(bool p_enabled, bool p_clip_above = true, int p_floor = 0, Vector3::Axis p_axis = Vector3::AXIS_X);

View File

@@ -291,15 +291,15 @@ void GridMapEditor::_update_selection_transform() {
} else { } else {
Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1)); Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
scale[edit_axis] = 1.0; scale[edit_axis] = 1.0;
Vector3 pos = selection.begin; Vector3 position = selection.begin;
pos[edit_axis] = edit_floor[edit_axis]; position[edit_axis] = edit_floor[edit_axis];
scale *= node->get_cell_size(); scale *= node->get_cell_size();
pos *= node->get_cell_size(); position *= node->get_cell_size();
Transform xf2; Transform xf2;
xf2.basis.scale(scale); xf2.basis.scale(scale);
xf2.origin = pos; xf2.origin = position;
RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2); RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2);
} }
@@ -414,11 +414,11 @@ bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, b
} }
if (input_action == INPUT_PASTE) { if (input_action == INPUT_PASTE) {
paste_indicator.current = Vector3(cell[0], cell[1], cell[2]); paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]);
_update_paste_indicator(); _update_paste_indicator();
} else if (input_action == INPUT_SELECT) { } else if (input_action == INPUT_SELECT) {
selection.current = Vector3(cell[0], cell[1], cell[2]); selection.current = Vector3i(cell[0], cell[1], cell[2]);
if (p_click) { if (p_click) {
selection.click = selection.current; selection.click = selection.current;
} }
@@ -427,7 +427,7 @@ bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, b
return true; return true;
} else if (input_action == INPUT_PICK) { } else if (input_action == INPUT_PICK) {
int item = node->get_cell_item(cell[0], cell[1], cell[2]); int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
if (item >= 0) { if (item >= 0) {
selected_palette = item; selected_palette = item;
mesh_library_palette->set_current(item); mesh_library_palette->set_current(item);
@@ -438,23 +438,23 @@ bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, b
} }
if (input_action == INPUT_PAINT) { if (input_action == INPUT_PAINT) {
SetItem si; SetItem si;
si.pos = Vector3(cell[0], cell[1], cell[2]); si.position = Vector3i(cell[0], cell[1], cell[2]);
si.new_value = selected_palette; si.new_value = selected_palette;
si.new_orientation = cursor_rot; si.new_orientation = cursor_rot;
si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]); si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]); si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
set_items.push_back(si); set_items.push_back(si);
node->set_cell_item(cell[0], cell[1], cell[2], selected_palette, cursor_rot); node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot);
return true; return true;
} else if (input_action == INPUT_ERASE) { } else if (input_action == INPUT_ERASE) {
SetItem si; SetItem si;
si.pos = Vector3(cell[0], cell[1], cell[2]); si.position = Vector3i(cell[0], cell[1], cell[2]);
si.new_value = -1; si.new_value = -1;
si.new_orientation = 0; si.new_orientation = 0;
si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]); si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]); si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
set_items.push_back(si); set_items.push_back(si);
node->set_cell_item(cell[0], cell[1], cell[2], -1); node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1);
return true; return true;
} }
@@ -470,8 +470,9 @@ void GridMapEditor::_delete_selection() {
for (int i = selection.begin.x; i <= selection.end.x; i++) { for (int i = selection.begin.x; i <= selection.end.x; i++) {
for (int j = selection.begin.y; j <= selection.end.y; j++) { for (int j = selection.begin.y; j <= selection.end.y; j++) {
for (int k = selection.begin.z; k <= selection.end.z; k++) { for (int k = selection.begin.z; k <= selection.end.z; k++) {
undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM); Vector3i selected = Vector3i(i, j, k);
undo_redo->add_undo_method(node, "set_cell_item", i, j, k, node->get_cell_item(i, j, k), node->get_cell_item_orientation(i, j, k)); undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM);
undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
} }
} }
} }
@@ -489,8 +490,9 @@ void GridMapEditor::_fill_selection() {
for (int i = selection.begin.x; i <= selection.end.x; i++) { for (int i = selection.begin.x; i <= selection.end.x; i++) {
for (int j = selection.begin.y; j <= selection.end.y; j++) { for (int j = selection.begin.y; j <= selection.end.y; j++) {
for (int k = selection.begin.z; k <= selection.end.z; k++) { for (int k = selection.begin.z; k <= selection.end.z; k++) {
undo_redo->add_do_method(node, "set_cell_item", i, j, k, selected_palette, cursor_rot); Vector3i selected = Vector3i(i, j, k);
undo_redo->add_undo_method(node, "set_cell_item", i, j, k, node->get_cell_item(i, j, k), node->get_cell_item_orientation(i, j, k)); undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot);
undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
} }
} }
} }
@@ -515,7 +517,8 @@ void GridMapEditor::_set_clipboard_data() {
for (int i = selection.begin.x; i <= selection.end.x; i++) { for (int i = selection.begin.x; i <= selection.end.x; i++) {
for (int j = selection.begin.y; j <= selection.end.y; j++) { for (int j = selection.begin.y; j <= selection.end.y; j++) {
for (int k = selection.begin.z; k <= selection.end.z; k++) { for (int k = selection.begin.z; k <= selection.end.z; k++) {
int itm = node->get_cell_item(i, j, k); Vector3i selected = Vector3i(i, j, k);
int itm = node->get_cell_item(selected);
if (itm == GridMap::INVALID_CELL_ITEM) { if (itm == GridMap::INVALID_CELL_ITEM) {
continue; continue;
} }
@@ -524,8 +527,8 @@ void GridMapEditor::_set_clipboard_data() {
ClipboardItem item; ClipboardItem item;
item.cell_item = itm; item.cell_item = itm;
item.grid_offset = Vector3(i, j, k) - selection.begin; item.grid_offset = Vector3(selected) - selection.begin;
item.orientation = node->get_cell_item_orientation(i, j, k); item.orientation = node->get_cell_item_orientation(selected);
item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario()); item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
clipboard_items.push_back(item); clipboard_items.push_back(item);
@@ -583,14 +586,14 @@ void GridMapEditor::_do_paste() {
for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) { for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
ClipboardItem &item = E->get(); ClipboardItem &item = E->get();
Vector3 pos = rot.xform(item.grid_offset) + paste_indicator.begin + ofs; Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
Basis orm; Basis orm;
orm.set_orthogonal_index(item.orientation); orm.set_orthogonal_index(item.orientation);
orm = rot * orm; orm = rot * orm;
undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, item.cell_item, orm.get_orthogonal_index()); undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, orm.get_orthogonal_index());
undo_redo->add_undo_method(node, "set_cell_item", pos.x, pos.y, pos.z, node->get_cell_item(pos.x, pos.y, pos.z), node->get_cell_item_orientation(pos.x, pos.y, pos.z)); undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position));
} }
if (reselect) { if (reselect) {
@@ -667,11 +670,11 @@ bool GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<In
undo_redo->create_action(TTR("GridMap Paint")); undo_redo->create_action(TTR("GridMap Paint"));
for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) { for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
const SetItem &si = E->get(); const SetItem &si = E->get();
undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation); undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
} }
for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) { for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
const SetItem &si = E->get(); const SetItem &si = E->get();
undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation); undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
} }
undo_redo->commit_action(); undo_redo->commit_action();

View File

@@ -84,7 +84,7 @@ class GridMapEditor : public VBoxContainer {
Label *spin_box_label; Label *spin_box_label;
struct SetItem { struct SetItem {
Vector3 pos; Vector3i position;
int new_value; int new_value;
int new_orientation; int new_orientation;
int old_value; int old_value;