Skip to content
0.2.0-dev Download ZIP
222 lines ·5.5 KB Raw
1/**************************************************************************/
2/* nav_obstacle_2d.cpp */
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#include "nav_obstacle_2d.h"
32
33#include "nav_agent_2d.h"
34#include "nav_map_2d.h"
35
36void NavObstacle2D::set_agent(NavAgent2D *p_agent) {
37 if (agent == p_agent) {
38 return;
39 }
40
41 agent = p_agent;
42
43 internal_update_agent();
44
45 request_sync();
46}
47
48void NavObstacle2D::set_avoidance_enabled(bool p_enabled) {
49 if (avoidance_enabled == p_enabled) {
50 return;
51 }
52
53 avoidance_enabled = p_enabled;
54 obstacle_dirty = true;
55
56 internal_update_agent();
57
58 request_sync();
59}
60
61void NavObstacle2D::set_map(NavMap2D *p_map) {
62 if (map == p_map) {
63 return;
64 }
65
66 cancel_sync_request();
67
68 if (map) {
69 map->remove_obstacle(this);
70 if (agent) {
71 agent->set_map(nullptr);
72 }
73 }
74
75 map = p_map;
76 obstacle_dirty = true;
77
78 if (map) {
79 map->add_obstacle(this);
80 internal_update_agent();
81
82 request_sync();
83 }
84}
85
86void NavObstacle2D::set_position(const Vector2 &p_position) {
87 if (position == p_position) {
88 return;
89 }
90
91 position = p_position;
92 obstacle_dirty = true;
93
94 if (agent) {
95 agent->set_position(position);
96 }
97
98 request_sync();
99}
100
101void NavObstacle2D::set_radius(real_t p_radius) {
102 if (radius == p_radius) {
103 return;
104 }
105
106 radius = p_radius;
107
108 if (agent) {
109 agent->set_radius(radius);
110 }
111}
112
113void NavObstacle2D::set_velocity(const Vector2 &p_velocity) {
114 velocity = p_velocity;
115
116 if (agent) {
117 agent->set_velocity(velocity);
118 }
119}
120
121void NavObstacle2D::set_vertices(const Vector<Vector2> &p_vertices) {
122 if (vertices == p_vertices) {
123 return;
124 }
125
126 vertices = p_vertices;
127 obstacle_dirty = true;
128
129 request_sync();
130}
131
132bool NavObstacle2D::is_map_changed() {
133 if (map) {
134 bool is_changed = map->get_iteration_id() != last_map_iteration_id;
135 last_map_iteration_id = map->get_iteration_id();
136 return is_changed;
137 } else {
138 return false;
139 }
140}
141
142void NavObstacle2D::set_avoidance_layers(uint32_t p_layers) {
143 if (avoidance_layers == p_layers) {
144 return;
145 }
146
147 avoidance_layers = p_layers;
148 obstacle_dirty = true;
149
150 if (agent) {
151 agent->set_avoidance_layers(avoidance_layers);
152 }
153
154 request_sync();
155}
156
157bool NavObstacle2D::is_dirty() const {
158 return obstacle_dirty;
159}
160
161void NavObstacle2D::sync() {
162 obstacle_dirty = false;
163}
164
165void NavObstacle2D::internal_update_agent() {
166 if (agent) {
167 agent->set_neighbor_distance(0.0);
168 agent->set_max_neighbors(0.0);
169 agent->set_time_horizon_agents(0.0);
170 agent->set_time_horizon_obstacles(0.0);
171 agent->set_avoidance_mask(0.0);
172 agent->set_neighbor_distance(0.0);
173 agent->set_avoidance_priority(1.0);
174 agent->set_map(map);
175 agent->set_paused(paused);
176 agent->set_radius(radius);
177 agent->set_position(position);
178 agent->set_avoidance_layers(avoidance_layers);
179 agent->set_avoidance_enabled(avoidance_enabled);
180 }
181}
182
183void NavObstacle2D::set_paused(bool p_paused) {
184 if (paused == p_paused) {
185 return;
186 }
187
188 paused = p_paused;
189
190 if (map) {
191 if (paused) {
192 map->remove_obstacle(this);
193 } else {
194 map->add_obstacle(this);
195 }
196 }
197 internal_update_agent();
198}
199
200bool NavObstacle2D::get_paused() const {
201 return paused;
202}
203
204void NavObstacle2D::request_sync() {
205 if (map && !sync_dirty_request_list_element.in_list()) {
206 map->add_obstacle_sync_dirty_request(&sync_dirty_request_list_element);
207 }
208}
209
210void NavObstacle2D::cancel_sync_request() {
211 if (map && sync_dirty_request_list_element.in_list()) {
212 map->remove_obstacle_sync_dirty_request(&sync_dirty_request_list_element);
213 }
214}
215
216NavObstacle2D::NavObstacle2D() :
217 sync_dirty_request_list_element(this) {
218}
219
220NavObstacle2D::~NavObstacle2D() {
221 cancel_sync_request();
222}