Path: blob/master/modules/navigation_2d/nav_obstacle_2d.cpp
10277 views
/**************************************************************************/1/* nav_obstacle_2d.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_2d.h"3132#include "nav_agent_2d.h"33#include "nav_map_2d.h"3435void NavObstacle2D::set_agent(NavAgent2D *p_agent) {36if (agent == p_agent) {37return;38}3940agent = p_agent;4142internal_update_agent();4344request_sync();45}4647void NavObstacle2D::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 NavObstacle2D::set_map(NavMap2D *p_map) {61if (map == p_map) {62return;63}6465cancel_sync_request();6667if (map) {68map->remove_obstacle(this);69if (agent) {70agent->set_map(nullptr);71}72}7374map = p_map;75obstacle_dirty = true;7677if (map) {78map->add_obstacle(this);79internal_update_agent();8081request_sync();82}83}8485void NavObstacle2D::set_position(const Vector2 &p_position) {86if (position == p_position) {87return;88}8990position = p_position;91obstacle_dirty = true;9293if (agent) {94agent->set_position(position);95}9697request_sync();98}99100void NavObstacle2D::set_radius(real_t p_radius) {101if (radius == p_radius) {102return;103}104105radius = p_radius;106107if (agent) {108agent->set_radius(radius);109}110}111112void NavObstacle2D::set_velocity(const Vector2 &p_velocity) {113velocity = p_velocity;114115if (agent) {116agent->set_velocity(velocity);117}118}119120void NavObstacle2D::set_vertices(const Vector<Vector2> &p_vertices) {121if (vertices == p_vertices) {122return;123}124125vertices = p_vertices;126obstacle_dirty = true;127128request_sync();129}130131bool NavObstacle2D::is_map_changed() {132if (map) {133bool is_changed = map->get_iteration_id() != last_map_iteration_id;134last_map_iteration_id = map->get_iteration_id();135return is_changed;136} else {137return false;138}139}140141void NavObstacle2D::set_avoidance_layers(uint32_t p_layers) {142if (avoidance_layers == p_layers) {143return;144}145146avoidance_layers = p_layers;147obstacle_dirty = true;148149if (agent) {150agent->set_avoidance_layers(avoidance_layers);151}152153request_sync();154}155156bool NavObstacle2D::is_dirty() const {157return obstacle_dirty;158}159160void NavObstacle2D::sync() {161obstacle_dirty = false;162}163164void NavObstacle2D::internal_update_agent() {165if (agent) {166agent->set_neighbor_distance(0.0);167agent->set_max_neighbors(0.0);168agent->set_time_horizon_agents(0.0);169agent->set_time_horizon_obstacles(0.0);170agent->set_avoidance_mask(0.0);171agent->set_neighbor_distance(0.0);172agent->set_avoidance_priority(1.0);173agent->set_map(map);174agent->set_paused(paused);175agent->set_radius(radius);176agent->set_position(position);177agent->set_avoidance_layers(avoidance_layers);178agent->set_avoidance_enabled(avoidance_enabled);179}180}181182void NavObstacle2D::set_paused(bool p_paused) {183if (paused == p_paused) {184return;185}186187paused = p_paused;188189if (map) {190if (paused) {191map->remove_obstacle(this);192} else {193map->add_obstacle(this);194}195}196internal_update_agent();197}198199bool NavObstacle2D::get_paused() const {200return paused;201}202203void NavObstacle2D::request_sync() {204if (map && !sync_dirty_request_list_element.in_list()) {205map->add_obstacle_sync_dirty_request(&sync_dirty_request_list_element);206}207}208209void NavObstacle2D::cancel_sync_request() {210if (map && sync_dirty_request_list_element.in_list()) {211map->remove_obstacle_sync_dirty_request(&sync_dirty_request_list_element);212}213}214215NavObstacle2D::NavObstacle2D() :216sync_dirty_request_list_element(this) {217}218219NavObstacle2D::~NavObstacle2D() {220cancel_sync_request();221}222223224