Path: blob/master/modules/navigation_3d/nav_obstacle_3d.cpp
10277 views
/**************************************************************************/1/* nav_obstacle_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "nav_obstacle_3d.h"3132#include "nav_agent_3d.h"33#include "nav_map_3d.h"3435void NavObstacle3D::set_agent(NavAgent3D *p_agent) {36if (agent == p_agent) {37return;38}3940agent = p_agent;4142internal_update_agent();4344request_sync();45}4647void NavObstacle3D::set_avoidance_enabled(bool p_enabled) {48if (avoidance_enabled == p_enabled) {49return;50}5152avoidance_enabled = p_enabled;53obstacle_dirty = true;5455internal_update_agent();5657request_sync();58}5960void NavObstacle3D::set_use_3d_avoidance(bool p_enabled) {61if (use_3d_avoidance == p_enabled) {62return;63}6465use_3d_avoidance = p_enabled;66obstacle_dirty = true;6768if (agent) {69agent->set_use_3d_avoidance(use_3d_avoidance);70}7172request_sync();73}7475void NavObstacle3D::set_map(NavMap3D *p_map) {76if (map == p_map) {77return;78}7980cancel_sync_request();8182if (map) {83map->remove_obstacle(this);84if (agent) {85agent->set_map(nullptr);86}87}8889map = p_map;90obstacle_dirty = true;9192if (map) {93map->add_obstacle(this);94internal_update_agent();9596request_sync();97}98}99100void NavObstacle3D::set_position(const Vector3 p_position) {101if (position == p_position) {102return;103}104105position = p_position;106obstacle_dirty = true;107108if (agent) {109agent->set_position(position);110}111112request_sync();113}114115void NavObstacle3D::set_radius(real_t p_radius) {116if (radius == p_radius) {117return;118}119120radius = p_radius;121122if (agent) {123agent->set_radius(radius);124}125}126127void NavObstacle3D::set_height(const real_t p_height) {128if (height == p_height) {129return;130}131132height = p_height;133obstacle_dirty = true;134135if (agent) {136agent->set_height(height);137}138139request_sync();140}141142void NavObstacle3D::set_velocity(const Vector3 p_velocity) {143velocity = p_velocity;144145if (agent) {146agent->set_velocity(velocity);147}148}149150void NavObstacle3D::set_vertices(const Vector<Vector3> &p_vertices) {151if (vertices == p_vertices) {152return;153}154155vertices = p_vertices;156obstacle_dirty = true;157158request_sync();159}160161bool NavObstacle3D::is_map_changed() {162if (map) {163bool is_changed = map->get_iteration_id() != last_map_iteration_id;164last_map_iteration_id = map->get_iteration_id();165return is_changed;166} else {167return false;168}169}170171void NavObstacle3D::set_avoidance_layers(uint32_t p_layers) {172if (avoidance_layers == p_layers) {173return;174}175176avoidance_layers = p_layers;177obstacle_dirty = true;178179if (agent) {180agent->set_avoidance_layers(avoidance_layers);181}182183request_sync();184}185186bool NavObstacle3D::is_dirty() const {187return obstacle_dirty;188}189190void NavObstacle3D::sync() {191obstacle_dirty = false;192}193194void NavObstacle3D::internal_update_agent() {195if (agent) {196agent->set_neighbor_distance(0.0);197agent->set_max_neighbors(0.0);198agent->set_time_horizon_agents(0.0);199agent->set_time_horizon_obstacles(0.0);200agent->set_avoidance_mask(0.0);201agent->set_neighbor_distance(0.0);202agent->set_avoidance_priority(1.0);203agent->set_map(map);204agent->set_paused(paused);205agent->set_radius(radius);206agent->set_height(height);207agent->set_position(position);208agent->set_avoidance_layers(avoidance_layers);209agent->set_avoidance_enabled(avoidance_enabled);210agent->set_use_3d_avoidance(use_3d_avoidance);211}212}213214void NavObstacle3D::set_paused(bool p_paused) {215if (paused == p_paused) {216return;217}218219paused = p_paused;220221if (map) {222if (paused) {223map->remove_obstacle(this);224} else {225map->add_obstacle(this);226}227}228internal_update_agent();229}230231bool NavObstacle3D::get_paused() const {232return paused;233}234235void NavObstacle3D::request_sync() {236if (map && !sync_dirty_request_list_element.in_list()) {237map->add_obstacle_sync_dirty_request(&sync_dirty_request_list_element);238}239}240241void NavObstacle3D::cancel_sync_request() {242if (map && sync_dirty_request_list_element.in_list()) {243map->remove_obstacle_sync_dirty_request(&sync_dirty_request_list_element);244}245}246247NavObstacle3D::NavObstacle3D() :248sync_dirty_request_list_element(this) {249}250251NavObstacle3D::~NavObstacle3D() {252cancel_sync_request();253}254255256