Advertisement
jargon

Story05Js :: "canvasPageLogic.js"

Jul 8th, 2024
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var pageviews = {};
  3. var keysPressed = {};
  4.  
  5. window.onkeydown=e=>{keysPressed[e.code] = "down";}
  6. window.onkeyup=e=>{keysPressed[e.code] = "up";}
  7.  
  8. function initializepageviews(pageviews, book, page) {
  9.     if (typeof pageviews === 'undefined') {
  10.         pageviews = {};
  11.     }
  12.  
  13.     if (typeof pageviews[book] === 'undefined') {
  14.         pageviews[book] = {};
  15.     }
  16.  
  17.     if (typeof pageviews[book][page] === 'undefined') {
  18.         pageviews[book][page] = 0;
  19.     }
  20.  
  21.     if (pageviews[book][page] === 0) {
  22.         pageviews[book][page] = 1;
  23.     }
  24.  
  25.     return pageviews;
  26. }
  27.  
  28. var defaultRowspan = 12;
  29.  
  30. canvasCheckPage ( );
  31.  
  32. function canvasCheckPage() {
  33.     const [px, py, pz] = cursorState(layers.gui);
  34.  
  35.     if (typeof book === 'undefined') {
  36.         // book = `"Puzzlum's Palace" (No.1)`;
  37.         if (books.length > 0) {
  38.             book = Object.keys(books[0])[0];
  39.         }
  40.     }
  41.  
  42.     if (typeof page === 'undefined') {
  43.         // page = `"Puzzlum's Palace" (No.1)`;
  44.         if (books[book].length > 0) {
  45.             page = Object.keys(books[book][0])[0];
  46.         }
  47.     }
  48.  
  49.     return true;
  50. }
  51.  
  52. // Function to handle key captures and page rendering
  53. function canvasRenderPage() {
  54.  
  55.     if ( book === page ) {
  56.         pageviews[book] = [];
  57.         pageviews[book][page] = 1;
  58.     }
  59.    
  60.     backContext.clearRect(0, 0, frontCanvas.width, frontCanvas.height);
  61.    
  62.     initializepageviews(pageviews, book, page);
  63.        
  64.     const width = backContext.width;
  65.     const height = backContext.height;
  66.     // const defaultRowspan = 12;
  67.     let rowspan = defaultRowspan;
  68.     let row = 0;
  69.     let xOffset = 0;
  70.     let yOffset = 0;
  71.        
  72.     function incrementRow() {
  73.         yOffset += (row + 1) * rowspan;
  74.         row = 0;
  75.     }
  76.  
  77.     function renderText(text, font = '12px Arial', color = '#000000') {
  78.         yOffset = wrapText(xOffset, yOffset + row * rowspan, backCanvas.width, rowspan * 2, font, color, text);
  79.         row++;
  80.     }
  81.    
  82.     // incrementRow();
  83.     // renderText(book.split(`"`)[1], `${rowspan}px Arial`);
  84.  
  85.     incrementRow();
  86.  
  87.     incrementRow();
  88.  
  89.     const captionCount = parseInt(books[book][page]['caption']['count']);
  90.     for (let caption = 1; caption <= captionCount; caption++) {
  91.         let text = books[book][page]['caption'][caption]['caption'];
  92.         if (text) {
  93.            
  94.             renderText(text, `${rowspan}px Small Fonts`);
  95.            
  96.             // wrapText(xOffset, yOffset, backCanvas.w, `${rowspan}`, `${rowspan}px Small Fonts`, `black`, text);
  97.            
  98.             incrementRow();
  99.  
  100.         }
  101.     }
  102.  
  103.     var illustration = loadImage( `/assets`+books[book][page]['header'][1]['illustration'] );
  104.    
  105.     backContext.drawImage( illustration, ( backCanvas.width - illustration.naturalWidth ) / 2, yOffset - rowspan );
  106.    
  107.     yOffset += illustration.height;
  108.    
  109.     incrementRow();
  110.     incrementRow();
  111.     incrementRow();
  112.    
  113.     const entityCount = parseInt(books[book][page]['entity']['count']);
  114.     for (let entity = 1; entity <= entityCount; entity++) {
  115.        
  116.         let { name, sprite, text, mood } = books[book][page]['entity'][entity];
  117.        
  118.         if (text) {
  119.             renderText(`${mood} ${name}: ${text}`, '12px Small Fonts');
  120.         }
  121.     }
  122.  
  123.     incrementRow();
  124.     incrementRow();
  125.    
  126.     triggerKeys = {};
  127.    
  128.     const optionCount = parseInt(books[book][page]['option']['count']);
  129.    
  130.     for (let option = 1; option <= optionCount; option++) {
  131.        
  132.         let key = books[book][page]['option'][option]['key'];
  133.         let dest = books[book][page]['option'][option]['dest'];
  134.         let preq = books[book][page]['option'][option]['preq'];
  135.         let caption = books[book][page]['option'][option]['caption'];
  136.         let condition = books[book][page]['option'][option]['condition'];
  137.        
  138.         console.log(key);
  139.        
  140.         var approval = false;
  141.  
  142.         switch (ucwords(condition)) {
  143.         case "Contrary":
  144.             if (!pageviews[book][preq] === 0) {
  145.                 approval = true;
  146.             }
  147.             break;
  148.         case "Mandatory":
  149.             if (pageviews[book][preq] > 0) {
  150.                 approval = true;
  151.             }
  152.             break;
  153.         case "Elective":
  154.             approval = true;
  155.             break;
  156.         }
  157.  
  158.         if (approval) {
  159.            
  160.             renderText(`${key}: ${caption}`, '12px Small Fonts');
  161.            
  162.             var match = key.match(/\(([a-z])\)[a-z]*/);
  163.        
  164.             if ( match !== null ) {
  165.                 key = `Key` + ucase ( match[1] );
  166.             }
  167.            
  168.             if ( keysPressed[key] === "up" ) {
  169.                 console.log ( key );
  170.                 pageviews[book][dest]++;
  171.                 page = dest;
  172.             }
  173.         }
  174.     }
  175.  
  176.     incrementRow();
  177.     incrementRow();
  178.        
  179.     var visitedPagesCount = visitedSum ( book, page );
  180.  
  181.     var totalPagesCount = visitedCount ( book, page );
  182.    
  183.     var efficiency = `${Math.round( 100 * visitedPagesCount / totalPagesCount )}%`;
  184.    
  185.     renderText(`Pages visited: ${visitedPagesCount} of ${totalPagesCount} ( ${efficiency} efficiency )`, '12px Small Fonts');
  186.  
  187.     frontContext.clearRect(0, 0, frontCanvas.width, frontCanvas.height);
  188.     frontContext.drawImage( backCanvas, 0, 0 );
  189.    
  190.     return true;
  191. }
  192.  
  193. // Set up the interval to check key presses every 100 milliseconds
  194. setInterval(canvasRenderPage, 100);
  195.  
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement