PixelChipCode

sprite based kaleidoscope in #GameMaker. uses mirrored surfaces/object pooling

Dec 9th, 2024 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.91 KB | Source Code | 0 0
  1. // Create Event
  2. surface_mirror = -1;
  3. pool_size = 150; // Maximum number of particles
  4. active_particles = 0;
  5. particle_sprite = spr_sprite; //your sprite here
  6.  
  7. // Animation speed controls
  8. min_speed = 0.3;  // Minimum animation speed
  9. max_speed = 0.5;  // Maximum animation speed
  10.  
  11. // Initialize arrays for object pooling
  12. particle_active = array_create(pool_size, false);
  13. particle_x = array_create(pool_size, 0);
  14. particle_y = array_create(pool_size, 0);
  15. particle_scale = array_create(pool_size, 0);
  16. particle_angle = array_create(pool_size, 0);
  17. particle_frame = array_create(pool_size, 0);
  18. particle_speed = array_create(pool_size, 0);
  19.  
  20.  
  21.  
  22. // Clean up Event
  23. if (surface_exists(surface_mirror)) {
  24.     surface_free(surface_mirror);
  25. }
  26.  
  27. // Step Event
  28. if (!surface_exists(surface_mirror)) {
  29.     surface_mirror = surface_create(room_width, room_height);
  30. }
  31.  
  32. // Spawn particles
  33. if (mouse_check_button(mb_left)) {
  34.     repeat(4) {
  35.         // Find inactive particle slot
  36.         var slot = -1;
  37.         for (var i = 0; i < pool_size; i++) {
  38.             if (!particle_active[i]) {
  39.                 slot = i;
  40.                 break;
  41.             }
  42.         }
  43.        
  44.         if (slot != -1) {
  45.             particle_active[slot] = true;
  46.             particle_x[slot] = mouse_x;
  47.             particle_y[slot] = mouse_y;
  48.             particle_scale[slot] = random_range(0.5, 0.7);
  49.             particle_angle[slot] = random(360);
  50.             particle_frame[slot] = 0;
  51.             particle_speed[slot] = random_range(min_speed, max_speed);  // Using the new speed range
  52.             active_particles++;
  53.         }
  54.     }
  55. }
  56.  
  57. // Update particles
  58. for (var i = 0; i < pool_size; i++) {
  59.     if (particle_active[i]) {
  60.         // Update animation frame
  61.         particle_frame[i] += particle_speed[i];
  62.        
  63.         // Check if animation is complete (17 frames in my animation but change it to however many yours has)
  64.         if (particle_frame[i] >= 17) {
  65.             particle_active[i] = false;
  66.             active_particles--;
  67.             particle_frame[i] = 0;
  68.         }
  69.     }
  70. }
  71.  
  72. // Draw Event
  73. surface_set_target(surface_mirror);
  74. draw_clear_alpha(c_black, 0);
  75.  
  76. // Draw all active particles
  77. for (var i = 0; i < pool_size; i++) {
  78.     if (particle_active[i]) {
  79.         draw_sprite_ext(
  80.             particle_sprite,
  81.             floor(particle_frame[i]),
  82.             particle_x[i],
  83.             particle_y[i],
  84.             particle_scale[i],
  85.             particle_scale[i],
  86.             particle_angle[i],
  87.             c_white,
  88.             1
  89.         );
  90.     }
  91. }
  92.  
  93. surface_reset_target();
  94.  
  95. // Draw the mirrored surfaces
  96. draw_surface_ext(surface_mirror, room_width, 0, -1, 1, 0, c_white, 1);
  97. draw_surface_ext(surface_mirror, 0, room_height, 1, -1, 0, c_white, 1);
  98. draw_surface_ext(surface_mirror, room_width, room_height, -1, -1, 0, c_white, 1);
  99. draw_surface(surface_mirror, 0, 0);
  100.  
  101. //draw_text(10, 10, string(active_particles));
Advertisement
Add Comment
Please, Sign In to add comment