separation_ray_shape_2d.cpp 4.9 KB

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