Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // anagram shift effect in GameMaker by oliver @pixelchipcode
- // Create Event
- // First set up our variables
- anagrams = [
- "astronomers",
- "no more stars"
- ];
- current_anagram = 0;
- next_anagram = 1;
- transition_time = 1.5;
- transition_progress = 0;
- is_transitioning = false;
- letters = ds_list_create();
- // Initialize starting positions
- function init_letters() {
- ds_list_clear(letters);
- var spacing = 20;
- var current_word = anagrams[current_anagram];
- for (var i = 0; i < string_length(current_word); i++) {
- var letter = {
- char: string_char_at(current_word, i + 1),
- current_x: x + (i * spacing),
- target_x: x + (i * spacing),
- start_x: x + (i * spacing),
- alpha: 1,
- start_alpha: 1,
- target_alpha: 1
- };
- ds_list_add(letters, letter);
- }
- }
- // Function to find best target position for each letter
- function assign_target_positions() {
- var next_word = anagrams[next_anagram];
- var spacing = 20;
- var used_indices = array_create(string_length(next_word), false);
- // Update start positions for all existing letters
- for (var i = 0; i < ds_list_size(letters); i++) {
- var letter = letters[|i];
- letter.start_x = letter.current_x;
- letter.start_alpha = letter.alpha;
- }
- // First pass: match exact characters
- for (var i = 0; i < ds_list_size(letters); i++) {
- var letter = letters[|i];
- var found = false;
- for (var j = 0; j < string_length(next_word); j++) {
- if (!used_indices[j] && string_char_at(next_word, j + 1) == letter.char) {
- letter.target_x = x + (j * spacing);
- letter.target_alpha = 1;
- used_indices[j] = true;
- found = true;
- break;
- }
- }
- // If no match found, letter will fade out
- if (!found) {
- letter.target_alpha = 0;
- }
- }
- // Create new letters for unmatched characters in target word
- for (var i = 0; i < string_length(next_word); i++) {
- if (!used_indices[i]) {
- var new_letter = {
- char: string_char_at(next_word, i + 1),
- current_x: x + (i * spacing) + (random_range(-50, 50)),
- start_x: x + (i * spacing) + (random_range(-50, 50)),
- target_x: x + (i * spacing),
- alpha: 0,
- start_alpha: 0,
- target_alpha: 1
- };
- ds_list_add(letters, new_letter);
- }
- }
- }
- // New easing function (can be placed in Create event with other functions)
- function EaseInOutCubic(time, start, dest, duration) {
- var change = dest - start; // Calculate the total change in value
- time /= duration * 0.5;
- if (time < 1) {
- return change * 0.5 * power(time, 3) + start;
- }
- return change * 0.5 * (power(time - 2, 3) + 2) + start;
- }
- // Initialize the first word
- init_letters();
- // Step Event
- if (mouse_check_button_pressed(mb_left)) {
- is_transitioning = true;
- transition_progress = 0;
- assign_target_positions(); // Calculate targets based on current next_anagram
- current_anagram = next_anagram; // Then update indices after
- next_anagram = (next_anagram + 1) % array_length(anagrams);
- }
- if (is_transitioning) {
- transition_progress += delta_time / 1000000 / transition_time;
- // Update all letter positions and alphas
- for (var i = 0; i < ds_list_size(letters); i++) {
- var letter = letters[|i];
- // Use new easing function for both position and alpha
- letter.current_x = EaseInOutCubic(transition_progress, letter.start_x, letter.target_x, 1);
- letter.alpha = EaseInOutCubic(transition_progress, letter.start_alpha, letter.target_alpha, 1);
- }
- if (transition_progress >= 1) {
- is_transitioning = false;
- // Clean up any fully faded out letters
- var i = 0;
- while (i < ds_list_size(letters)) {
- if (letters[|i].alpha <= 0) {
- ds_list_delete(letters, i);
- } else {
- i++;
- }
- }
- }
- }
- // Draw Event
- draw_set_color(c_white);
- draw_set_font(Font2)
- for (var i = 0; i < ds_list_size(letters); i++) {
- var letter = letters[|i];
- draw_set_alpha(letter.alpha);
- draw_text(letter.current_x, y, letter.char);
- }
- draw_set_alpha(1);
Advertisement
Add Comment
Please, Sign In to add comment