Advertisement
jargon

Story05Js :: "canvasPageLogic.js"

Jul 6th, 2024 (edited)
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function canvasCheckPage() {
  2.     const [px, py, pz] = cursorState(layers.gui);
  3.  
  4.     if (typeof book === 'undefined') {
  5.         book = `"Puzzlum's Palace" (No.1)`;
  6.     }
  7.  
  8.     if (typeof page === 'undefined') {
  9.         page = `"Puzzlum's Palace" (No.1)`;
  10.     }
  11.  
  12.     return true;
  13. }
  14.  
  15. // List of keys to capture
  16. var keysToCapture = [];
  17.  
  18. // Object to hold the state of pressed keys
  19. var keysPressed = {};
  20.  
  21. // Event listener for keydown to update the keysPressed object
  22. document.addEventListener('keydown', function(event) {
  23.     if (keysToCapture.includes(event.code)) {
  24.         keysPressed[event.code] = true;
  25.     }
  26. });
  27.  
  28. // Event listener for keyup to update the keysPressed object
  29. document.addEventListener('keyup', function(event) {
  30.     if (keysToCapture.includes(event.code)) {
  31.         keysPressed[event.code] = false;
  32.     }
  33. });
  34.  
  35. // Function to handle key captures and page rendering
  36. function canvasRenderPage() {
  37.    
  38.     keysToCapture = [];
  39.  
  40.     if (typeof history === 'undefined') {
  41.         history = {};
  42.     }
  43.  
  44.     if (typeof history[book] === 'undefined') {
  45.         history[book] = {};
  46.     }
  47.  
  48.     if (typeof history[book][page] === 'undefined') {
  49.         history[book][page] = 0;
  50.     }
  51.  
  52.     if (history[book][page] === 0) {
  53.         history[book][page] = 1;
  54.     }
  55.  
  56.     const width = backContext.width;
  57.     const height = backContext.height;
  58.     const defaultRowspan = 12;
  59.     let rowspan = defaultRowspan;
  60.     let row = 0;
  61.     let xOffset = 0;
  62.     let yOffset = 0;
  63.    
  64.     /*
  65.     var imageStack = [];
  66.    
  67.     for ( let index = 1; index <= books[book][page]['header']['count']; index++ )
  68.     {
  69.         imageStack.push ( `/assets`+books[book][page]['header'][index]['illustration'] );
  70.     }
  71.    
  72.     var stack = loadImageStack(imageStack);
  73.    
  74.     for ( let index = 0; index < stack.length; index++ )
  75.     {
  76.         backContext.drawImage(stack[index],stack[index].naturalWidth,stack[index].naturalHeight);
  77.     }
  78.     */
  79.    
  80.     function incrementRow() {
  81.         yOffset += (row + 1) * rowspan;
  82.         row = 0;
  83.     }
  84.  
  85.     function renderText(text, font = '12px Arial', color = '#000000') {
  86.         wrapText(xOffset, yOffset + row * rowspan, width, rowspan, font, color, text);
  87.         row++;
  88.     }
  89.  
  90.     incrementRow();
  91.     renderText(book.split(`"`)[1], `${rowspan}px Arial`);
  92.  
  93.    incrementRow();
  94.    const captionCount = parseInt(books[book][page]['caption']['count']);
  95.    for (let caption = 1; caption <= captionCount; caption++) {
  96.        let text = books[book][page]['caption'][caption]['caption'];
  97.        if (text) {
  98.            renderText(text, `${rowspan}px Small Fonts`);
  99.        }
  100.    }
  101.  
  102.    incrementRow();
  103.    const entityCount = parseInt(books[book][page]['entity']['count']);
  104.    for (let entity = 1; entity <= entityCount; entity++) {
  105.        let { name, sprite, text, mood } = books[book][page]['entity'][entity];
  106.        if (text) {
  107.            renderText(`${mood} ${name}: ${text}`, '12px Small Fonts');
  108.        }
  109.    }
  110.  
  111.    incrementRow();
  112.    const optionCount = parseInt(books[book][page]['option']['count']);
  113.    for (let option = 1; option <= optionCount; option++) {
  114.        
  115.         keysToCapture[option] = {};
  116.  
  117.        let { key, dest, preq, caption, condition } = books[book][page]['option'][option];
  118.        let approval = false;
  119.  
  120.        switch (ucwords(condition)) {
  121.            case "Contrary":
  122.                if (!history[book][page] || history[book][preq] === 0) {
  123.                    approval = true;
  124.                }
  125.                break;
  126.            case "Mandatory":
  127.                if (history[book][page] && history[book][preq] > 0) {
  128.                    approval = true;
  129.                }
  130.                break;
  131.            case "Elective":
  132.                approval = true;
  133.                break;
  134.        }
  135.  
  136.        if (approval) {
  137.            
  138.             keysToCapture[option] = { key, dest };
  139.            renderText(`${key}: ${caption}`, '12px Small Fonts');
  140.        }
  141.    }
  142.  
  143.    incrementRow();
  144.    const visitedPagesCount = Object.values(history).reduce((acc, value) => acc + (value > 0 ? 1 : 0), 0);
  145.    renderText(`Pages visited: ${visitedPagesCount}`, '12px Small Fonts');
  146.  
  147.    canvasPageKeys(keysToCapture);
  148.    return true;
  149. }
  150.  
  151. // Function to handle the pressed keys and page navigation
  152. function canvasPageKeys(keysToCapture) {
  153.    for (let option = 1; option < keysToCapture.length - 1; option++) {
  154.        let key = keysToCapture[option]['key'];
  155.        switch (key) {
  156.            case 'SP':
  157.            case 'SPC':
  158.            case 'SPACE':
  159.                key = 'Space';
  160.                break;
  161.            case 'CR':
  162.            case 'ENTER':
  163.                key = 'Enter';
  164.                break;
  165.            case 'ESC':
  166.            case 'ESCAPE':
  167.                key = 'Escape';
  168.                break;
  169.            case 'TAB':
  170.            case 'HTAB':
  171.                key = 'Tab';
  172.                break;
  173.            case 'DEL':
  174.            case 'DELETE':
  175.                key = 'Delete';
  176.                break;
  177.            case 'BS':
  178.            case 'BKSP':
  179.                key = 'Backspace';
  180.                break;
  181.        }
  182.        
  183.         page = keysToCapture[option]['dest'];
  184.    }
  185. }
  186.  
  187. // Set up the interval to check key presses every 100 milliseconds
  188. setInterval(canvasRenderPage, 100);
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement