Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SparkedJs (Beta 2) :: "Game/Input Handler.js"
- function haveCommonEntry(arr1, arr2) {
- let tolerance = 8;
- if ( arr1.length < tolerance && arr2.length < tolerance ) {
- return arr1.some(item => arr2.includes(item));
- } else {
- const set1 = new Set(arr1); return arr2.some(v => set1.has(v));
- return set1;
- }
- }
- function handleInput(e) {
- let previous = player;
- let keys = {};
- let key = e.key;
- if (key.length === 1) {
- key = key.toUpperCase();
- }
- if ( haveCommonEntry( [key], ["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"] ) ) {
- if ( keys[key] === undefined ){
- keys[key] = 0;
- }
- keys[key]++;
- e.preventDefault();
- }
- if ( haveCommonEntry( [key], ["W","Escape","Q","I"] ) ) {
- if ( keys[key] === undefined ){
- keys[key] = 0;
- }
- keys[key]++;
- e.preventDefault();
- }
- if ( haveCommonEntry( [key], ["ArrowUp"] ) ) player.y--;
- if ( haveCommonEntry( [key], ["ArrowDown"] ) ) player.y++;
- if ( haveCommonEntry( [key], ["ArrowLeft"] ) ) player.x--;
- if ( haveCommonEntry( [key], ["ArrowRight"] ) ) player.x++;
- if ( player.y < 0 ) player.y = 0;
- if ( player.x < 0 ) player.x = 0;
- if ( player.y >= Maps[player.map].raw.length ) player.y = Maps[player.map].raw.length - 1;
- if ( player.x >= Maps[player.map].raw[player.y].length ) player.x = Maps[player.map].raw[player.y].length - 1;
- if ( collisionDebug ) console.log(
- `Collision: `
- + `${typeof Maps[player.map].raw[player.y][player.x]}:`
- + ` ${Maps[player.map].raw[player.y][player.x]}`
- + ` @ (${player.x}, ${player.y}) in "${Maps[player.map].about.title}"`
- );
- if ( `${Maps[player.map].raw[player.y][player.x]}` !== 0 ) {
- if ( collisionDebug ) console.log(
- `Collision Detect @ ( ${player.x}, ${player.y} )`
- );
- if ( collisionDebug ) console.log(
- `Collision Reset @ ( ${previous.x}, ${previous.y} )`
- );
- player.x = previous.x;
- player.y = previous.y;
- }
- if ( haveCommonEntry( [key], ["W"] ) ) {
- player.map++;
- player.map %= Maps.length;
- }
- if ( haveCommonEntry( [key], ["ArrowUp","ArrowDown","ArrowLeft","ArrowRight","W"] ) ) {
- let log = ``;
- log += `Player:` ;
- log += ` "${Sprites[player.sprite].about.title}"`;
- log += ` Map:`;
- log += ` "${Maps[player.map].about.title}"`;
- log += ` ${player.map + 1}/${Maps.length}`;
- log += ` "${Maps[player.map].raw[player.y][player.x]}"`;
- log += ` @`;
- log += ` (`;
- log += ` ${player.x}, ${player.y}`;
- log += ` )`;
- log += ` in`
- log += ` ${Maps[player.map].xspan}`
- log += ` x`
- log += ` ${Maps[player.map].yspan}`;
- if ( collisionDebug ) console.log(log);
- }
- if ( haveCommonEntry( [key], ["Q","Escape"] ) ) {
- document.removeEventListener(`keyup`, handleInput);
- clearInterval(intervalId);
- window.location = `http://${window.location.hostname}/`;
- }
- if ( haveCommonEntry( [key], ["I"] ) ) inventory = (inventory === 2 ? 3 : 2);
- return keys;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement