BulbDynamicOccluder.gml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /// @param renderer
  2. function BulbDynamicOccluder(_renderer) constructor
  3. {
  4. visible = true;
  5. x = 0;
  6. y = 0;
  7. xscale = 1.0;
  8. yscale = 1.0;
  9. angle = 0.0;
  10. vertexArray = [];
  11. __radius = 0;
  12. __destroyed = false;
  13. static Destroy = function()
  14. {
  15. __destroyed = true;
  16. }
  17. static AddEdge = function(_x1, _y1, _x2, _y2)
  18. {
  19. if (__destroyed) return;
  20. //Choose the longest axis of the sprite as the radius
  21. //We apply x/y scaling in the __IsOnScreen() function
  22. __radius = max(__radius, sqrt(max(_x1*_x1 + _y1*_y1, _x2*_x2 + _y2*_y2)));
  23. array_push(vertexArray, _x1, _y1, _x2, _y2, _y2-_y1, _x1-_x2);
  24. }
  25. static AddCircle = function(_radius, _x = 0, _y = 0, _edges = 24)
  26. {
  27. if (__destroyed) return;
  28. __radius = max(__radius, _radius);
  29. var _angle = 0;
  30. var _angleStep = 360 / _edges;
  31. var _x2 = _x + lengthdir_x(_radius, _angle);
  32. var _y2 = _y + lengthdir_y(_radius, _angle);
  33. repeat(_edges)
  34. {
  35. _angle -= _angleStep;
  36. var _x1 = _x2;
  37. var _y1 = _y2;
  38. _x2 = _x + lengthdir_x(_radius, _angle);
  39. _y2 = _y + lengthdir_y(_radius, _angle);
  40. array_push(vertexArray, _x1, _y1, _x2, _y2, _y2-_y1, _x1-_x2);
  41. }
  42. }
  43. static ClearEdges = function(_x1, _y1, _x2, _y2)
  44. {
  45. if (__destroyed) return;
  46. __radius = 0;
  47. array_resize(vertexArray, 0);
  48. }
  49. static AddToRenderer = function(_renderer)
  50. {
  51. if (__destroyed) return;
  52. array_push(_renderer.__dynamicOccludersArray, weak_ref_create(self));
  53. }
  54. static RemoveFromRenderer = function(_renderer)
  55. {
  56. var _array = _renderer.__dynamicOccludersArray;
  57. var _i = array_length(_array) - 1;
  58. repeat(array_length(_array))
  59. {
  60. var _weak = _array[_i];
  61. if (weak_ref_alive(_weak))
  62. {
  63. if (_weak.ref == self) array_delete(_array, _i, 1);
  64. }
  65. else
  66. {
  67. array_delete(_array, _i, 1);
  68. }
  69. --_i;
  70. }
  71. }
  72. static __IsOnScreen = function(_cameraL, _cameraT, _cameraR, _cameraB)
  73. {
  74. var _radius = __radius*max(xscale, yscale);
  75. return (!__destroyed && visible && __BulbRectInRect(x - _radius, y - _radius, x + _radius, y + _radius, _cameraL, _cameraT, _cameraR, _cameraB));
  76. }
  77. if (_renderer != undefined) AddToRenderer(_renderer);
  78. }