Advertisement
jargon

// SparkedJs (Beta 2) :: "Game/Input Handler.js"

Nov 2nd, 2024 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SparkedJs (Beta 2) :: "Game/Input Handler.js"
  2.  
  3. function haveCommonEntry(arr1, arr2) {
  4.    
  5.     let tolerance = 8;
  6.    
  7.     if ( arr1.length < tolerance && arr2.length < tolerance ) {
  8.         return arr1.some(item => arr2.includes(item));
  9.     } else {
  10.         const set1 = new Set(arr1); return arr2.some(v => set1.has(v));
  11.         return set1;
  12.     }
  13. }
  14.  
  15. function handleInput(e) {
  16.    
  17.     let previous = player;
  18.  
  19.     let keys = {};
  20.    
  21.     let key = e.key;
  22.    
  23.     if (key.length === 1) {
  24.         key = key.toUpperCase();
  25.     }
  26.    
  27.     if ( haveCommonEntry( [key], ["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"] ) ) {
  28.        
  29.         if ( keys[key] === undefined ){
  30.             keys[key] = 0;
  31.         }
  32.         keys[key]++;
  33.        
  34.         e.preventDefault();
  35.     }
  36.    
  37.     if ( haveCommonEntry( [key], ["W","Escape","Q","I"] ) ) {
  38.        
  39.         if ( keys[key] === undefined ){
  40.             keys[key] = 0;
  41.         }
  42.         keys[key]++;
  43.        
  44.         e.preventDefault();
  45.     }
  46.    
  47.     if ( haveCommonEntry( [key], ["ArrowUp"] ) ) player.y--;
  48.     if ( haveCommonEntry( [key], ["ArrowDown"] ) ) player.y++;
  49.     if ( haveCommonEntry( [key], ["ArrowLeft"] ) ) player.x--;
  50.     if ( haveCommonEntry( [key], ["ArrowRight"] ) ) player.x++;
  51.    
  52.     if ( player.y < 0 ) player.y = 0;
  53.    
  54.     if ( player.x < 0 ) player.x = 0;
  55.        
  56.     if ( player.y >= Maps[player.map].raw.length ) player.y = Maps[player.map].raw.length - 1;
  57.    
  58.     if ( player.x >= Maps[player.map].raw[player.y].length ) player.x = Maps[player.map].raw[player.y].length - 1;
  59.  
  60.     if ( collisionDebug ) console.log(
  61.         `Collision: `
  62.         + `${typeof Maps[player.map].raw[player.y][player.x]}:`
  63.         + ` ${Maps[player.map].raw[player.y][player.x]}`
  64.         + ` @ (${player.x}, ${player.y}) in "${Maps[player.map].about.title}"`
  65.     );
  66.    
  67.     if ( `${Maps[player.map].raw[player.y][player.x]}` !== 0 ) {
  68.    
  69.         if ( collisionDebug ) console.log(
  70.             `Collision Detect @ ( ${player.x}, ${player.y} )`
  71.         );
  72.        
  73.         if ( collisionDebug ) console.log(
  74.             `Collision Reset @ ( ${previous.x}, ${previous.y} )`
  75.         );
  76.        
  77.         player.x = previous.x;
  78.         player.y = previous.y;
  79.        
  80.     }
  81.    
  82.     if ( haveCommonEntry( [key], ["W"] ) ) {
  83.        
  84.         player.map++;
  85.         player.map %= Maps.length;
  86.     }
  87.    
  88.     if ( haveCommonEntry( [key], ["ArrowUp","ArrowDown","ArrowLeft","ArrowRight","W"] ) ) {
  89.        
  90.         let log = ``;
  91.        
  92.         log += `Player:` ;
  93.         log += ` "${Sprites[player.sprite].about.title}"`;
  94.         log += ` Map:`;
  95.         log += ` "${Maps[player.map].about.title}"`;
  96.         log += ` ${player.map + 1}/${Maps.length}`;
  97.         log += ` "${Maps[player.map].raw[player.y][player.x]}"`;
  98.         log += ` @`;
  99.         log += ` (`;
  100.         log += ` ${player.x}, ${player.y}`;
  101.         log += ` )`;
  102.         log += ` in`
  103.         log += ` ${Maps[player.map].xspan}`
  104.         log += ` x`
  105.         log += ` ${Maps[player.map].yspan}`;
  106.    
  107.         if ( collisionDebug ) console.log(log);
  108.     }
  109.    
  110.     if ( haveCommonEntry( [key], ["Q","Escape"] ) ) {
  111.        
  112.         document.removeEventListener(`keyup`, handleInput);
  113.         clearInterval(intervalId);
  114.         window.location = `http://${window.location.hostname}/`;               
  115.     }
  116.  
  117.     if ( haveCommonEntry( [key], ["I"] ) ) inventory = (inventory === 2 ? 3 : 2);
  118.    
  119.     return keys;
  120.    
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement