1/**************************************************************************/
2/* separation_ray_shape_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 "separation_ray_shape_2d.h"
32
33#include "servers/physics_server_2d.h"
34#include "servers/rendering_server.h"
35
36void SeparationRayShape2D::_update_shape() {
37 Dictionary d;
38 d["length"] = length;
39 d["slide_on_slope"] = slide_on_slope;
40 PhysicsServer2D::get_singleton()->shape_set_data(get_rid(), d);
41 emit_changed();
42}
43
44void SeparationRayShape2D::draw(const RID &p_to_rid, const Color &p_color) {
45 const Vector2 target_position = Vector2(0, get_length());
46
47 const float max_arrow_size = 6;
48 const float line_width = 1.4;
49 bool no_line = target_position.length() < line_width;
50 float arrow_size = CLAMP(target_position.length() * 2 / 3, line_width, max_arrow_size);
51
52 if (no_line) {
53 arrow_size = target_position.length();
54 } else {
55 RS::get_singleton()->canvas_item_add_line(p_to_rid, Vector2(), target_position - target_position.normalized() * arrow_size, p_color, line_width);
56 }
57
58 Transform2D xf;
59 xf.rotate(target_position.angle());
60 xf.translate_local(Vector2(no_line ? 0 : target_position.length() - arrow_size, 0));
61
62 Vector<Vector2> pts = {
63 xf.xform(Vector2(arrow_size, 0)),
64 xf.xform(Vector2(0, 0.5 * arrow_size)),
65 xf.xform(Vector2(0, -0.5 * arrow_size))
66 };
67
68 Vector<Color> cols = { p_color, p_color, p_color };
69
70 RS::get_singleton()->canvas_item_add_primitive(p_to_rid, pts, cols, Vector<Point2>(), RID());
71}
72
73Rect2 SeparationRayShape2D::get_rect() const {
74 Rect2 rect;
75 rect.position = Vector2();
76 rect.expand_to(Vector2(0, length));
77 rect = rect.grow(Math_SQRT12 * 4);
78 return rect;
79}
80
81real_t SeparationRayShape2D::get_enclosing_radius() const {
82 return length;
83}
84
85void SeparationRayShape2D::_bind_methods() {
86 ClassDB::bind_method(D_METHOD("set_length", "length"), &SeparationRayShape2D::set_length);
87 ClassDB::bind_method(D_METHOD("get_length"), &SeparationRayShape2D::get_length);
88
89 ClassDB::bind_method(D_METHOD("set_slide_on_slope", "active"), &SeparationRayShape2D::set_slide_on_slope);
90 ClassDB::bind_method(D_METHOD("get_slide_on_slope"), &SeparationRayShape2D::get_slide_on_slope);
91
92 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "0.01,1024,0.01,or_greater,suffix:px"), "set_length", "get_length");
93 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "slide_on_slope"), "set_slide_on_slope", "get_slide_on_slope");
94}
95
96void SeparationRayShape2D::set_length(real_t p_length) {
97 length = p_length;
98 _update_shape();
99}
100
101real_t SeparationRayShape2D::get_length() const {
102 return length;
103}
104
105void SeparationRayShape2D::set_slide_on_slope(bool p_active) {
106 slide_on_slope = p_active;
107 _update_shape();
108}
109
110bool SeparationRayShape2D::get_slide_on_slope() const {
111 return slide_on_slope;
112}
113
114SeparationRayShape2D::SeparationRayShape2D() :
115 Shape2D(PhysicsServer2D::get_singleton()->separation_ray_shape_create()) {
116 _update_shape();
117}
118