PixelChipCode

Anagram shift effect GameMaker

Oct 25th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.55 KB | Source Code | 0 0
  1. // anagram shift effect in GameMaker by oliver @pixelchipcode
  2. // Create Event
  3. // First set up our variables
  4. anagrams = [
  5.     "astronomers",
  6.     "no more stars"
  7. ];
  8.  
  9. current_anagram = 0;
  10. next_anagram = 1;
  11. transition_time = 1.5;
  12. transition_progress = 0;
  13. is_transitioning = false;
  14. letters = ds_list_create();
  15.  
  16. // Initialize starting positions
  17. function init_letters() {
  18.     ds_list_clear(letters);
  19.     var spacing = 20;
  20.     var current_word = anagrams[current_anagram];
  21.    
  22.     for (var i = 0; i < string_length(current_word); i++) {
  23.         var letter = {
  24.             char: string_char_at(current_word, i + 1),
  25.             current_x: x + (i * spacing),
  26.             target_x: x + (i * spacing),
  27.             start_x: x + (i * spacing),    
  28.             alpha: 1,
  29.             start_alpha: 1,                
  30.             target_alpha: 1                
  31.         };
  32.         ds_list_add(letters, letter);
  33.     }
  34. }
  35.  
  36. // Function to find best target position for each letter
  37. function assign_target_positions() {
  38.     var next_word = anagrams[next_anagram];
  39.     var spacing = 20;
  40.     var used_indices = array_create(string_length(next_word), false);
  41.    
  42.     // Update start positions for all existing letters
  43.     for (var i = 0; i < ds_list_size(letters); i++) {
  44.         var letter = letters[|i];
  45.         letter.start_x = letter.current_x;
  46.         letter.start_alpha = letter.alpha;
  47.     }
  48.    
  49.     // First pass: match exact characters
  50.     for (var i = 0; i < ds_list_size(letters); i++) {
  51.         var letter = letters[|i];
  52.         var found = false;
  53.        
  54.         for (var j = 0; j < string_length(next_word); j++) {
  55.             if (!used_indices[j] && string_char_at(next_word, j + 1) == letter.char) {
  56.                 letter.target_x = x + (j * spacing);
  57.                 letter.target_alpha = 1;
  58.                 used_indices[j] = true;
  59.                 found = true;
  60.                 break;
  61.             }
  62.         }
  63.        
  64.         // If no match found, letter will fade out
  65.         if (!found) {
  66.             letter.target_alpha = 0;
  67.         }
  68.     }
  69.    
  70.     // Create new letters for unmatched characters in target word
  71.     for (var i = 0; i < string_length(next_word); i++) {
  72.         if (!used_indices[i]) {
  73.             var new_letter = {
  74.                 char: string_char_at(next_word, i + 1),
  75.                 current_x: x + (i * spacing) + (random_range(-50, 50)),
  76.                 start_x: x + (i * spacing) + (random_range(-50, 50)),  
  77.                 target_x: x + (i * spacing),
  78.                 alpha: 0,
  79.                 start_alpha: 0,    
  80.                 target_alpha: 1    
  81.             };
  82.             ds_list_add(letters, new_letter);
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. // New easing function (can be placed in Create event with other functions)
  89. function EaseInOutCubic(time, start, dest, duration) {
  90.     var change = dest - start;  // Calculate the total change in value
  91.     time /= duration * 0.5;
  92.    
  93.     if (time < 1) {
  94.         return change * 0.5 * power(time, 3) + start;
  95.     }
  96.    
  97.     return change * 0.5 * (power(time - 2, 3) + 2) + start;
  98. }
  99. // Initialize the first word
  100. init_letters();
  101.  
  102. // Step Event
  103. if (mouse_check_button_pressed(mb_left)) {
  104.     is_transitioning = true;
  105.     transition_progress = 0;
  106.     assign_target_positions();  // Calculate targets based on current next_anagram
  107.     current_anagram = next_anagram;  // Then update indices after
  108.     next_anagram = (next_anagram + 1) % array_length(anagrams);
  109. }
  110.  
  111. if (is_transitioning) {
  112.     transition_progress += delta_time / 1000000 / transition_time;
  113.    
  114.     // Update all letter positions and alphas
  115.     for (var i = 0; i < ds_list_size(letters); i++) {
  116.         var letter = letters[|i];
  117.         // Use new easing function for both position and alpha
  118.         letter.current_x = EaseInOutCubic(transition_progress, letter.start_x, letter.target_x, 1);
  119.         letter.alpha = EaseInOutCubic(transition_progress, letter.start_alpha, letter.target_alpha, 1);
  120.     }
  121.    
  122.     if (transition_progress >= 1) {
  123.         is_transitioning = false;
  124.        
  125.         // Clean up any fully faded out letters
  126.         var i = 0;
  127.         while (i < ds_list_size(letters)) {
  128.             if (letters[|i].alpha <= 0) {
  129.                 ds_list_delete(letters, i);
  130.             } else {
  131.                 i++;
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. // Draw Event
  138. draw_set_color(c_white);
  139. draw_set_font(Font2)
  140. for (var i = 0; i < ds_list_size(letters); i++) {
  141.     var letter = letters[|i];
  142.     draw_set_alpha(letter.alpha);
  143.     draw_text(letter.current_x, y, letter.char);
  144. }
  145. draw_set_alpha(1);
Tags: gameMaker
Advertisement
Add Comment
Please, Sign In to add comment