ersatzben

TOMBOLA SEQUENCER V1.0 JS CODE

Oct 22nd, 2024 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. // TOMBOLA SEQUENCER V1.0
  2. // (c) Ben Johnson 2024
  3. // Licensed with CC BY 4.0 - reuse, remix, whatever, but pls credit me!
  4. // Functionality to add: params visibility; respawning
  5.  
  6. autowatch = 1;
  7. outlets = 2;
  8. mgraphics.init();
  9. mgraphics.relative_coords = 1;
  10. mgraphics.autofill = 0;
  11.  
  12. var window_width = box.rect[2] - box.rect[0];
  13. var window_height = box.rect[3] - box.rect[1];
  14.  
  15. var config = {
  16. sides: 5,
  17. numBalls: 5,
  18. gravity: -0.00005,
  19. spinspeed: 0.01
  20. };
  21.  
  22. var rotationAngle = 0;
  23. var refreshRate = 60;
  24.  
  25. var balls = [];
  26. var edgeHitTimes = [];
  27. for (var i = 0; i < config.sides; i++) {
  28. edgeHitTimes.push(0);
  29. }
  30. var vertices = [];
  31.  
  32. // New: Collision report cooldown
  33. var collisionCooldowns = {};
  34. var cooldownTime = 500; // milliseconds
  35.  
  36. function createBall() {
  37. var r = Math.random() * 0.3;
  38. var angle = Math.random() * 2 * Math.PI;
  39. return {
  40. position: [r * Math.cos(angle), r * Math.sin(angle)],
  41. velocity: [Math.random() * 0.006 - 0.003, Math.random() * 0.006 - 0.003],
  42. radius: 0.01,
  43. color: [Math.random() * 0.5 + 0.5, Math.random() * 0.5 + 0.5, Math.random() * 0.5 + 0.5, 1]
  44. };
  45. }
  46.  
  47. for (var i = 0; i < config.numBalls; i++) {
  48. balls.push(createBall());
  49. }
  50.  
  51. var rotateTask = new Task(updateScene, this);
  52. rotateTask.interval = 1000 / refreshRate;
  53. rotateTask.repeat();
  54.  
  55. function paint() {
  56. with (mgraphics) {
  57. set_source_rgba(0.2, 0.2, 0.2, 1);
  58. paint();
  59. drawShapeOutline();
  60. drawBalls();
  61. }
  62. }
  63.  
  64. function drawShapeOutline() {
  65. var angleStep = (2 * Math.PI) / config.sides;
  66. var radius = 0.8;
  67. var cosA = Math.cos(rotationAngle);
  68. var sinA = Math.sin(rotationAngle);
  69.  
  70. vertices = [];
  71. for (var i = 0; i < config.sides; i++) {
  72. var angle = i * angleStep;
  73. var x = radius * Math.cos(angle);
  74. var y = radius * Math.sin(angle);
  75. vertices.push([x * cosA - y * sinA, x * sinA + y * cosA]);
  76. }
  77.  
  78. for (var i = 0; i < config.sides; i++) {
  79. var p1 = vertices[i];
  80. var p2 = vertices[(i + 1) % config.sides];
  81.  
  82. if (!p1 || !p2) {
  83. post("Error: Undefined vertex at index " + i + "\n");
  84. continue;
  85. }
  86.  
  87. var timeSinceHit = Date.now() - edgeHitTimes[i];
  88. var hitIntensity = Math.max(0, 1 - timeSinceHit / 500);
  89.  
  90. mgraphics.set_source_rgba(0.8 + 0.2 * hitIntensity, 0.8 * (1 - hitIntensity), 0.8 * (1 - hitIntensity), 1);
  91.  
  92. var lineWidth = 0.005 + 0.05 * hitIntensity;
  93. mgraphics.set_line_width(lineWidth);
  94.  
  95. mgraphics.move_to(p1[0], p1[1]);
  96. mgraphics.line_to(p2[0], p2[1]);
  97. mgraphics.stroke();
  98. }
  99. }
  100.  
  101. function drawBalls() {
  102. for (var i = 0; i < balls.length; i++) {
  103. var ball = balls[i];
  104. mgraphics.set_source_rgba(ball.color[0], ball.color[1], ball.color[2], ball.color[3]);
  105. mgraphics.ellipse(ball.position[0] - ball.radius, ball.position[1] - ball.radius, ball.radius * 2, ball.radius * 2);
  106. mgraphics.fill();
  107. }
  108. }
  109.  
  110. function updateScene() {
  111. rotationAngle = (rotationAngle + (2 * Math.PI * config.spinspeed) / refreshRate) % (2 * Math.PI);
  112.  
  113. for (var i = 0; i < balls.length; i++) {
  114. var ball = balls[i];
  115. ball.velocity[1] += config.gravity;
  116. ball.position[0] += ball.velocity[0];
  117. ball.position[1] += ball.velocity[1];
  118.  
  119. checkEdgeCollisions(ball, i);
  120. }
  121.  
  122. mgraphics.redraw();
  123. }
  124.  
  125. function checkEdgeCollisions(ball, ballIndex) {
  126. if (vertices.length !== config.sides) {
  127. post("Error: Mismatch between config.sides and vertices.length\n");
  128. return;
  129. }
  130.  
  131. for (var i = 0; i < config.sides; i++) {
  132. var p1 = vertices[i];
  133. var p2 = vertices[(i + 1) % config.sides];
  134.  
  135. if (!p1 || !p2) {
  136. post("Error: Undefined vertex at index " + i + " or " + ((i + 1) % config.sides) + "\n");
  137. continue;
  138. }
  139.  
  140. var edge = [p2[0] - p1[0], p2[1] - p1[1]];
  141. var edgeLength = Math.sqrt(edge[0] * edge[0] + edge[1] * edge[1]);
  142. var edgeUnit = [edge[0] / edgeLength, edge[1] / edgeLength];
  143. var normal = [-edgeUnit[1], edgeUnit[0]];
  144.  
  145. var relativePosition = [ball.position[0] - p1[0], ball.position[1] - p1[1]];
  146. var distanceToEdge = Math.abs(relativePosition[0] * normal[0] + relativePosition[1] * normal[1]);
  147.  
  148. if (distanceToEdge < ball.radius) {
  149. var dotProduct = relativePosition[0] * edgeUnit[0] + relativePosition[1] * edgeUnit[1];
  150. if (dotProduct >= 0 && dotProduct <= edgeLength) {
  151. edgeHitTimes[i] = Date.now();
  152.  
  153. // Check cooldown before reporting collision
  154. var cooldownKey = ballIndex + "-" + i;
  155. var currentTime = Date.now();
  156. if (!collisionCooldowns[cooldownKey] || currentTime - collisionCooldowns[cooldownKey] > cooldownTime) {
  157. // post("Ball " + ballIndex + " hit edge between vertices " + i + " and " + ((i + 1) % config.sides) + "\n");
  158. outlet(0, ballIndex);
  159. outlet(1, i);
  160. collisionCooldowns[cooldownKey] = currentTime;
  161. }
  162.  
  163. var velocityDotNormal = ball.velocity[0] * normal[0] + ball.velocity[1] * normal[1];
  164. ball.velocity[0] -= 2 * velocityDotNormal * normal[0];
  165. ball.velocity[1] -= 2 * velocityDotNormal * normal[1];
  166.  
  167. var penetrationDepth = ball.radius - distanceToEdge;
  168. ball.position[0] += normal[0] * penetrationDepth;
  169. ball.position[1] += normal[1] * penetrationDepth;
  170.  
  171. if (Math.random() < 0.05) {
  172. var boostFactor = 1.1 + Math.random() * 0.2;
  173. ball.velocity[0] *= boostFactor;
  174. ball.velocity[1] *= boostFactor;
  175. } else {
  176. ball.velocity[0] *= 0.99;
  177. ball.velocity[1] *= 0.99;
  178. }
  179. }
  180. }
  181. }
  182. }
  183.  
  184. function forcesize(w,h) {
  185. if (w != h) {
  186. w = h;
  187. box.size(w,h);
  188. }
  189. }
  190. forcesize.local = 1;
  191.  
  192. function onresize(w,h) {
  193. forcesize(w,h);
  194. window_width = box.rect[2] - box.rect[0];
  195. window_height = box.rect[3] - box.rect[1];
  196. mgraphics.redraw();
  197. }
  198. onresize.local = 1;
Tags: JSUI
Advertisement
Add Comment
Please, Sign In to add comment