Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define an object that maps each character to its Morse code representation
- const morseCode = {
- 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
- 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
- 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
- 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
- 'Y': '-.--', 'Z': '--..', '0': '-----', '1': '.----', '2': '..---',
- '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
- '8': '---..', '9': '----.',
- '.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--',
- '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...',
- ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-',
- '"': '.-..-.', '$': '...-..-', '@': '.--.-.', ' ': '/'
- };
- // Function to translate a message to Morse code
- function translateToMorse(message, callback) {
- // Convert the message to uppercase and translate each character to Morse code
- let morse = message.toUpperCase().split('').map(char => morseCode[char] || '').join(' ');
- let index = 0;
- // Recursive function to update the Morse code character by character
- function updateMorse() {
- if (index < morse.length) {
- let char = morse[index];
- callback(char);
- index++;
- setTimeout(updateMorse, 1200 / 13); // Delay between each character at 13 WPM
- } else {
- callback(' ');
- setEndVariable(); // Call the function to set the END variable
- }
- }
- updateMorse();
- }
- let player = GetPlayer();
- let MESSAGE = player.GetVar('MESSAGE');
- let MORSE = '';
- let DOT_DASH = 'BLANK';
- let END = false;
- // Function to update the MORSE and DOT_DASH variables
- function updateMorseAndDotDash(morseChar) {
- MORSE = MORSE + morseChar; // Append the new character to the existing Morse code
- player.SetVar('MORSE', MORSE);
- DOT_DASH = morseChar === '.' ? 'DOT' : morseChar === '-' ? 'DASH' : 'BLANK';
- player.SetVar('DOT_DASH', DOT_DASH);
- }
- // Function to set the END variable to true
- function setEndVariable() {
- END = true;
- player.SetVar('END', END);
- }
- // Start translating the message to Morse code when the MESSAGE variable changes
- // The DOT_DASH output variable is used to trigger dot and dash sound effects
- // as the code translates the text to Morse code
- translateToMorse(MESSAGE, updateMorseAndDotDash);
- // List of variables needed in Storyline for this code to work correctly:
- // 1. MESSAGE (input): A text variable that holds the message to be translated to Morse code.
- // 2. MORSE (output): A text variable that stores the translated Morse code.
- // 3. DOT_DASH (output): A text variable that indicates whether the current Morse code character is a dot, dash, or blank.
- // 4. END (output): A true/false variable that indicates when the translation is complete.
Advertisement
Add Comment
Please, Sign In to add comment