PixelChipCode

Gamemaker Creative coding 001 : Crystal Growth

Oct 1st, 2024 (edited)
158
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.34 KB | Source Code | 1 0
  1. By Oliver @PixelChipCode
  2.  
  3. Description
  4. This code creates a branching crystal growth effect in GameMaker. Clicking anywhere in the room starts a crystal that grows outward with occasional branching, creating a natural, organic visual effect. The crystals grow to a maximum length before stopping, and all growth is drawn on a persistent surface to maintain the visual effect. Pressing the "R" key will restart the room and clear the canvas.
  5.  
  6. Instructions for Setup:
  7.  
  8. Create two objects: obj_crystal and obj_spawner.
  9. Attach the following code to their respective events.
  10. Ensure the obj_spawner is placed in the room to manage the drawing surface and spawning.
  11.  
  12. //obj_crystal
  13.  
  14. //create event
  15. growth_speed = .5; // Speed of growth (slower for more control)
  16. branch_chance = 0.02; // Reduce the chance of branching to avoid excessive density
  17. growth_length = irandom_range(12, 50); // Randomize length to make each branch different
  18. current_length = 0;
  19. direction = irandom(360); // Start with a random direction
  20. turn_rate = random_range(-1, 1); // Slight random adjustment to make growth organic
  21.  
  22. //step event
  23. // Grow the crystal if it hasn't reached its maximum length
  24. if (current_length < growth_length) {
  25.     // Gradually adjust the growth direction for more natural branching
  26.     direction += random_range(-1, 1) + turn_rate;
  27.  
  28.     // Calculate growth direction
  29.     var dx = lengthdir_x(growth_speed, direction);
  30.     var dy = lengthdir_y(growth_speed, direction);
  31.     var prev_x = x;
  32.     var prev_y = y;
  33.  
  34.     x += dx;
  35.     y += dy;
  36.     current_length += growth_speed;
  37.  
  38.     // Draw the growth onto the surface
  39.     if (surface_exists(global.crystal_surface)) {
  40.         surface_set_target(global.crystal_surface);
  41.         draw_set_color(c_white);
  42.         draw_line_width(prev_x, prev_y, x, y, 1); // Use thin lines for a delicate look
  43.         surface_reset_target();
  44.     }
  45.  
  46.     // Occasionally create a new branch, but only if this branch is still growing
  47.     if (random(1) < branch_chance && current_length > 10) { // Only branch after some initial growth
  48.         var new_direction = direction + irandom_range(-30, 30); // Less variation for more controlled branching
  49.         var new_crystal = instance_create_layer(x, y, "Instances", obj_crystal);
  50.         new_crystal.direction = new_direction;
  51.     }
  52. }
  53.  
  54. //draw event
  55. if (surface_exists(global.crystal_surface)) {
  56.     draw_surface(global.crystal_surface, 0, 0);
  57. }
  58.  
  59. // obj_spawner
  60.  
  61. //create event
  62. // Create a surface for drawing the crystallization effect
  63. global.crystal_surface = -1;
  64.  
  65. //step event
  66. if(mouse_check_button(mb_left)){
  67. // If the surface exists, start drawing on it
  68. if (surface_exists(global.crystal_surface)) {
  69.     surface_set_target(global.crystal_surface);
  70.    
  71.     // Set draw color for crystals
  72.     draw_set_color(c_white);
  73.    
  74.     // Draw starting point
  75.     draw_circle(mouse_x, mouse_y, 2, false);
  76.    
  77.     // Reset the surface target to the application (main screen)
  78.     surface_reset_target();
  79.    
  80.     // Create an instance of obj_crystal at click point
  81.     instance_create_layer(mouse_x, mouse_y, "Instances", obj_crystal);
  82. }
  83.  
  84. }
  85.  
  86. if(keyboard_check_pressed(ord("R"))){
  87.     room_restart();
  88. }
  89.  
  90. //room start event
  91. if (surface_exists(global.crystal_surface)) {
  92.     surface_free(global.crystal_surface);
  93. }
  94. global.crystal_surface = surface_create(room_width, room_height);
  95.  
Advertisement
Add Comment
Please, Sign In to add comment