PixelChipCode

Fireflies following Lissajous curves for GameMaker (GML)

Oct 14th, 2024 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.32 KB | Source Code | 0 0
  1. // Firefly by @PixelChipCode
  2. // This code controls the movement and glow behavior of a firefly in GameMaker.
  3. // The firefly follows a Lissajous curve, which creates smooth, looping movement patterns
  4. // based on sine waves for both the x and y positions. Random values are assigned
  5. // to the amplitude, frequency, and phase shift of the curve to make each firefly's movement unique.
  6. // Additionally, the firefly exhibits a soft, glowing effect that fades in and out in cycles.
  7. // This glow is controlled by a timer, and each firefly's glow intensity and speed of fading
  8. // are randomized for a more natural and organic feel.
  9.  
  10. // Create event
  11.  
  12. // Store the origin position based on the spawn location (mouse)
  13. origin_x = x;  // Set the initial x position to mouse_x when spawned
  14. origin_y = y;  // Set the initial y position to mouse_y when spawned
  15.  
  16. // Initialize Lissajous curve variables with random values
  17. amplitude_x = irandom_range(80, 150);
  18. amplitude_y = irandom_range(40, 100);
  19. frequency_x = irandom_range(2, 4);
  20. frequency_y = irandom_range(1, 3);
  21. phase_shift = random_range(0, 2 * pi);
  22. speed = random_range(0.001, 0.003);
  23. time = 0;
  24.  
  25. // Glow variables
  26. glow_timer = 0;                           // Timer for the glow effect
  27. glow_duration = irandom_range(60, 120);    // Random duration of the glow cycle (in frames)
  28. glow_intensity = 0;                       // Intensity of the glow (0 = off, 1 = full glow)
  29. glow_speed = random_range(0.01, 0.03);    // How fast the glow fades in/out
  30.  
  31. color = random_range(40,60);
  32.  
  33. // Step event
  34.  
  35. // Update time and calculate position based on the Lissajous curve
  36. time += speed;
  37.  
  38. // Set x and y relative to the original spawn location (origin_x, origin_y)
  39. x = origin_x + amplitude_x * sin(frequency_x * time + phase_shift);
  40. y = origin_y + amplitude_y * sin(frequency_y * time);
  41.  
  42. // Handle glow effect
  43. glow_timer += 1;  // Increment the glow timer
  44.  
  45. if (glow_timer < glow_duration / 2) {
  46.     // Fade in glow (first half of the glow cycle)
  47.     glow_intensity = min(glow_intensity + glow_speed, 1);
  48. } else {
  49.     // Fade out glow (second half of the glow cycle)
  50.     glow_intensity = max(glow_intensity - glow_speed, 0);
  51. }
  52.  
  53. // Reset timer when the full glow cycle is complete
  54. if (glow_timer >= glow_duration) {
  55.     glow_timer = 0;
  56.     glow_duration = irandom_range(60, 120);  // Set a new random duration for the next cycle
  57. }
  58.  
  59. // Draw event
  60.  
  61. // Set additive blending for the glow effect
  62. gpu_set_blendmode(bm_add);
  63.  
  64. // Set the glow color to a more yellowy-green hue
  65. var glow_color = make_color_hsv(color, 255, 255);  // Shifted hue to 65 for a more yellowy tone
  66. draw_set_color(glow_color);
  67. draw_set_alpha(glow_intensity);  // Use the glow intensity to control the alpha
  68.  
  69. // Draw the glowing firefly as a circle
  70. draw_circle(x, y,  2 * glow_intensity, false);  // The size increases slightly with glow intensity
  71.  
  72. // Reset blend mode to normal
  73. gpu_set_blendmode(bm_normal);
  74.  
  75. // Reset color and alpha to default
  76. draw_set_color(c_white);
  77. draw_set_alpha(1);
  78.  
  79.  
  80. /////obj_firefly_spawner
  81.  
  82. //step event
  83. if(mouse_check_button(mb_left)) {
  84.     var firefly = instance_create_layer(mouse_x, mouse_y, "Instances", obj_firefly);
  85.     // Set the origin manually, if needed
  86.     firefly.origin_x = mouse_x;
  87.     firefly.origin_y = mouse_y;
  88. }
  89.  
  90. if(keyboard_check_pressed(ord("R"))){
  91.     game_restart();
  92. }
  93.  
Tags: gameMaker
Advertisement
Add Comment
Please, Sign In to add comment