Skip to content
0.2.0-dev Download ZIP
122 lines ·4.7 KB Raw
1/**************************************************************************/
2/* test_grid_map.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "../grid_map.h"
34
35#include "scene/main/scene_tree.h"
36#include "scene/main/window.h"
37#include "scene/resources/3d/primitive_meshes.h"
38#include "tests/test_macros.h"
39
40namespace TestGridMap {
41
42TEST_CASE("[SceneTree][GridMap][MeshLibrary] Octant querying") {
43 Ref<MeshLibrary> mesh_library;
44 mesh_library.instantiate();
45
46 int item1 = 0;
47 int item2 = 1;
48
49 {
50 Vector<int> item_list = mesh_library->get_item_list();
51 CHECK(item_list.size() == 0);
52 }
53
54 mesh_library->create_item(item1);
55 mesh_library->create_item(item2);
56
57 int last_unused_item_id = mesh_library->get_last_unused_item_id();
58 CHECK(last_unused_item_id == 2);
59
60 mesh_library->set_item_name(item1, "Item1");
61 mesh_library->set_item_name(item2, "Item2");
62
63 Ref<BoxMesh> box_mesh;
64 box_mesh.instantiate();
65 box_mesh->set_size(Vector3(1.0, 1.0, 1.0));
66
67 mesh_library->set_item_mesh(item1, box_mesh);
68 mesh_library->set_item_mesh(item2, box_mesh);
69
70 {
71 Vector<int> item_list = mesh_library->get_item_list();
72 CHECK(item_list.size() == 2);
73 CHECK(item_list[0] == item1);
74 CHECK(item_list[1] == item2);
75 }
76
77 GridMap *grid_map = memnew(GridMap);
78 grid_map->set_mesh_library(mesh_library);
79
80 SceneTree::get_singleton()->get_root()->add_child(grid_map);
81
82 grid_map->set_octant_size(8);
83 grid_map->set_cell_size(Vector3(1.0, 1.0, 1.0));
84
85 grid_map->set_cell_item(Vector3i(0, 0, 0), -1);
86
87 CHECK(grid_map->get_used_cells().size() == 0);
88 CHECK(grid_map->get_used_octants().size() == 0);
89
90 grid_map->set_cell_item(Vector3i(0, 0, 0), item1);
91 grid_map->set_cell_item(Vector3i(7, 7, 7), item2);
92
93 CHECK(grid_map->get_used_cells().size() == 2);
94 CHECK(grid_map->get_used_octants().size() == 1);
95 CHECK(grid_map->get_used_octants()[0] == Vector3i(0, 0, 0));
96
97 grid_map->set_cell_item(Vector3i(8, 8, 8), item2);
98
99 CHECK(grid_map->get_used_cells().size() == 3);
100 CHECK(grid_map->get_used_octants().size() == 2);
101 CHECK(grid_map->get_used_octants()[0] == Vector3i(0, 0, 0));
102 CHECK(grid_map->get_used_octants()[1] == Vector3i(1, 1, 1));
103
104 AABB bounds;
105 bounds.position = Vector3(0.0, 0.0, 0.0);
106 bounds.size = Vector3(8.0, 8.0, 8.0);
107
108 CHECK(grid_map->get_octants_in_bounds(bounds).size() == 1);
109 CHECK(grid_map->get_used_octants_in_bounds(bounds).size() == 1);
110 CHECK(grid_map->get_octants_in_bounds(bounds)[0] == Vector3i(0, 0, 0));
111
112 // Edge margin is -CMP_EPSILON.
113 bounds.size = Vector3(8.001, 8.001, 8.001);
114 CHECK(grid_map->get_octants_in_bounds(bounds).size() == 8);
115 CHECK(grid_map->get_used_octants_in_bounds(bounds).size() == 2);
116
117 SceneTree::get_singleton()->get_root()->remove_child(grid_map);
118
119 memdelete(grid_map);
120}
121
122} // namespace TestGridMap