Comparing version 1.1.0 to 1.2.0
@@ -45,7 +45,7 @@ /** Representation of a two-dimensional vector. */ | ||
/** Returns whether the passed in button is currently pressed. */ | ||
isPressed(button = MouseButton.Left) { | ||
isPressed(/* istanbul ignore next */ button = MouseButton.Left) { | ||
return this.pressedButtons.has(button); | ||
} | ||
/** Returns whether the passed in mouse button was pressed. */ | ||
wasPressed(button = MouseButton.Left) { | ||
wasPressed(/* istanbul ignore next */ button = MouseButton.Left) { | ||
if (this.queuedButtons.has(button)) { | ||
@@ -83,4 +83,10 @@ this.queuedButtons.delete(button); | ||
const arrowKeyTemplates = { | ||
arrows: ['ArrowUp', 'ArrowLeft', 'ArrowDown', 'ArrowRight'], | ||
wasd: ['W', 'A', 'S', 'D'], | ||
}; | ||
class Keyboard { | ||
constructor({ doc = document } = {}) { | ||
constructor( | ||
/* istanbul ignore next */ | ||
{ doc = document } = {}) { | ||
this.pressedKeys = new Set(); | ||
@@ -112,6 +118,29 @@ this.queuedKeys = new Set(); | ||
} | ||
getMovementVector(arrowKeys) { | ||
if (typeof arrowKeys === 'string') { | ||
arrowKeys = arrowKeys.toLowerCase(); | ||
if (arrowKeys in arrowKeyTemplates) { | ||
arrowKeys = arrowKeyTemplates[arrowKeys]; | ||
} | ||
else { | ||
throw new Error(`Arrow key template "${arrowKeys}" not found!`); | ||
} | ||
} | ||
const vector = new Vector2(); | ||
if (this.isPressed(arrowKeys[0])) | ||
vector.y -= 1; | ||
if (this.isPressed(arrowKeys[1])) | ||
vector.x -= 1; | ||
if (this.isPressed(arrowKeys[2])) | ||
vector.y += 1; | ||
if (this.isPressed(arrowKeys[3])) | ||
vector.x += 1; | ||
return vector; | ||
} | ||
} | ||
class Gamepad { | ||
constructor({ win = window, nav = navigator } = {}) { | ||
constructor( | ||
/* istanbul ignore next */ | ||
{ win = window, nav = navigator } = {}) { | ||
this.pressedButtons = new Set(); | ||
@@ -121,2 +150,3 @@ this.window = win; | ||
this.window.addEventListener('gamepadconnected', ({ gamepad }) => { | ||
/* istanbul ignore else */ | ||
if (!this.isConnected()) | ||
@@ -126,3 +156,12 @@ this.gamepadIndex = gamepad.index; | ||
this.window.addEventListener('gamepaddisconnected', ({ gamepad }) => { | ||
this.gamepadIndex = undefined; | ||
/* istanbul ignore else */ | ||
if (this.gamepadIndex === gamepad.index) { | ||
const gamepads = this.navigator.getGamepads(); | ||
if (gamepads.length) { | ||
this.gamepadIndex = this.navigator.getGamepads()[0].index; | ||
} | ||
else { | ||
this.gamepadIndex = undefined; | ||
} | ||
} | ||
}); | ||
@@ -144,2 +183,3 @@ } | ||
wasPressed(button) { | ||
/* istanbul ignore else */ | ||
if (this.isConnected()) { | ||
@@ -146,0 +186,0 @@ if (this.gamepad.buttons[button].pressed) { |
@@ -50,7 +50,7 @@ (function (global, factory) { | ||
/** Returns whether the passed in button is currently pressed. */ | ||
isPressed(button = exports.MouseButton.Left) { | ||
isPressed(/* istanbul ignore next */ button = exports.MouseButton.Left) { | ||
return this.pressedButtons.has(button); | ||
} | ||
/** Returns whether the passed in mouse button was pressed. */ | ||
wasPressed(button = exports.MouseButton.Left) { | ||
wasPressed(/* istanbul ignore next */ button = exports.MouseButton.Left) { | ||
if (this.queuedButtons.has(button)) { | ||
@@ -88,4 +88,10 @@ this.queuedButtons.delete(button); | ||
const arrowKeyTemplates = { | ||
arrows: ['ArrowUp', 'ArrowLeft', 'ArrowDown', 'ArrowRight'], | ||
wasd: ['W', 'A', 'S', 'D'], | ||
}; | ||
class Keyboard { | ||
constructor({ doc = document } = {}) { | ||
constructor( | ||
/* istanbul ignore next */ | ||
{ doc = document } = {}) { | ||
this.pressedKeys = new Set(); | ||
@@ -117,6 +123,29 @@ this.queuedKeys = new Set(); | ||
} | ||
getMovementVector(arrowKeys) { | ||
if (typeof arrowKeys === 'string') { | ||
arrowKeys = arrowKeys.toLowerCase(); | ||
if (arrowKeys in arrowKeyTemplates) { | ||
arrowKeys = arrowKeyTemplates[arrowKeys]; | ||
} | ||
else { | ||
throw new Error(`Arrow key template "${arrowKeys}" not found!`); | ||
} | ||
} | ||
const vector = new Vector2(); | ||
if (this.isPressed(arrowKeys[0])) | ||
vector.y -= 1; | ||
if (this.isPressed(arrowKeys[1])) | ||
vector.x -= 1; | ||
if (this.isPressed(arrowKeys[2])) | ||
vector.y += 1; | ||
if (this.isPressed(arrowKeys[3])) | ||
vector.x += 1; | ||
return vector; | ||
} | ||
} | ||
class Gamepad { | ||
constructor({ win = window, nav = navigator } = {}) { | ||
constructor( | ||
/* istanbul ignore next */ | ||
{ win = window, nav = navigator } = {}) { | ||
this.pressedButtons = new Set(); | ||
@@ -126,2 +155,3 @@ this.window = win; | ||
this.window.addEventListener('gamepadconnected', ({ gamepad }) => { | ||
/* istanbul ignore else */ | ||
if (!this.isConnected()) | ||
@@ -131,3 +161,12 @@ this.gamepadIndex = gamepad.index; | ||
this.window.addEventListener('gamepaddisconnected', ({ gamepad }) => { | ||
this.gamepadIndex = undefined; | ||
/* istanbul ignore else */ | ||
if (this.gamepadIndex === gamepad.index) { | ||
const gamepads = this.navigator.getGamepads(); | ||
if (gamepads.length) { | ||
this.gamepadIndex = this.navigator.getGamepads()[0].index; | ||
} | ||
else { | ||
this.gamepadIndex = undefined; | ||
} | ||
} | ||
}); | ||
@@ -149,2 +188,3 @@ } | ||
wasPressed(button) { | ||
/* istanbul ignore else */ | ||
if (this.isConnected()) { | ||
@@ -151,0 +191,0 @@ if (this.gamepad.buttons[button].pressed) { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.Contro={})}(this,function(e){"use strict";class t{constructor(e=0,t=0){this.x=e,this.y=t}}!function(e){e[e.Left=0]="Left",e[e.Middle=1]="Middle",e[e.Right=2]="Right"}(e.MouseButton||(e.MouseButton={}));e.Mouse=class{constructor({canvas:e,doc:s=document}){this.pointerLocked=!1,this.pointerMovement=new t,this.pressedButtons=new Set,this.queuedButtons=new Set,this.scrollDistance=0,this.canvas=e,this.document=s;let n=this.canvas.addEventListener.bind(this.canvas);n("mousedown",e=>{this.pressedButtons.add(e.button),this.queuedButtons.add(e.button)}),n("mouseup",e=>{this.pressedButtons.delete(e.button),this.queuedButtons.delete(e.button)}),n("mousemove",e=>{this.pointerMovement.x+=e.movementX,this.pointerMovement.y+=e.movementY}),n("wheel",e=>{const t=e.deltaY;this.scrollDistance+=t}),n=this.document.addEventListener.bind(this.document)}isPressed(t=e.MouseButton.Left){return this.pressedButtons.has(t)}wasPressed(t=e.MouseButton.Left){return!!this.queuedButtons.has(t)&&(this.queuedButtons.delete(t),!0)}getPointerMovement(){const e=this.pointerMovement;return this.pointerMovement=new t(0,0),e}getScrollDistance(){const e=this.scrollDistance;return this.scrollDistance=0,e}lockPointer(){this.canvas.requestPointerLock()}unlockPointer(){this.document.exitPointerLock()}isPointerLocked(){return this.document.pointerLockElement===this.canvas}},e.Keyboard=class{constructor({doc:e=document}={}){this.pressedKeys=new Set,this.queuedKeys=new Set,this.document=e,this.document.addEventListener("keydown",e=>{const t=e.key.toLowerCase();this.pressedKeys.add(t),this.queuedKeys.add(t)}),this.document.addEventListener("keyup",e=>{const t=e.key.toLowerCase();this.pressedKeys.delete(t),this.queuedKeys.delete(t)})}isPressed(e){return e=e.toLowerCase(),this.pressedKeys.has(e)}wasPressed(e){return e=e.toLowerCase(),!!this.queuedKeys.has(e)&&(this.queuedKeys.delete(e),!0)}},e.Gamepad=class{constructor({win:e=window,nav:t=navigator}={}){this.pressedButtons=new Set,this.window=e,this.navigator=t,this.window.addEventListener("gamepadconnected",({gamepad:e})=>{this.isConnected()||(this.gamepadIndex=e.index)}),this.window.addEventListener("gamepaddisconnected",({gamepad:e})=>{this.gamepadIndex=void 0})}isConnected(){return void 0!==this.gamepadIndex&&this.gamepad.connected}get gamepad(){return this.navigator.getGamepads()[this.gamepadIndex]}isPressed(e){return this.isConnected()&&this.gamepad.buttons[e].pressed}wasPressed(e){if(this.isConnected())if(this.gamepad.buttons[e].pressed){if(!this.pressedButtons.has(e))return this.pressedButtons.add(e),!0}else this.pressedButtons.delete(e);return!1}},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.Contro={})}(this,function(e){"use strict";class t{constructor(e=0,t=0){this.x=e,this.y=t}}!function(e){e[e.Left=0]="Left",e[e.Middle=1]="Middle",e[e.Right=2]="Right"}(e.MouseButton||(e.MouseButton={}));const s={arrows:["ArrowUp","ArrowLeft","ArrowDown","ArrowRight"],wasd:["W","A","S","D"]};e.Mouse=class{constructor({canvas:e,doc:s=document}){this.pointerLocked=!1,this.pointerMovement=new t,this.pressedButtons=new Set,this.queuedButtons=new Set,this.scrollDistance=0,this.canvas=e,this.document=s;let n=this.canvas.addEventListener.bind(this.canvas);n("mousedown",e=>{this.pressedButtons.add(e.button),this.queuedButtons.add(e.button)}),n("mouseup",e=>{this.pressedButtons.delete(e.button),this.queuedButtons.delete(e.button)}),n("mousemove",e=>{this.pointerMovement.x+=e.movementX,this.pointerMovement.y+=e.movementY}),n("wheel",e=>{const t=e.deltaY;this.scrollDistance+=t}),n=this.document.addEventListener.bind(this.document)}isPressed(t=e.MouseButton.Left){return this.pressedButtons.has(t)}wasPressed(t=e.MouseButton.Left){return!!this.queuedButtons.has(t)&&(this.queuedButtons.delete(t),!0)}getPointerMovement(){const e=this.pointerMovement;return this.pointerMovement=new t(0,0),e}getScrollDistance(){const e=this.scrollDistance;return this.scrollDistance=0,e}lockPointer(){this.canvas.requestPointerLock()}unlockPointer(){this.document.exitPointerLock()}isPointerLocked(){return this.document.pointerLockElement===this.canvas}},e.Keyboard=class{constructor({doc:e=document}={}){this.pressedKeys=new Set,this.queuedKeys=new Set,this.document=e,this.document.addEventListener("keydown",e=>{const t=e.key.toLowerCase();this.pressedKeys.add(t),this.queuedKeys.add(t)}),this.document.addEventListener("keyup",e=>{const t=e.key.toLowerCase();this.pressedKeys.delete(t),this.queuedKeys.delete(t)})}isPressed(e){return e=e.toLowerCase(),this.pressedKeys.has(e)}wasPressed(e){return e=e.toLowerCase(),!!this.queuedKeys.has(e)&&(this.queuedKeys.delete(e),!0)}getMovementVector(e){if("string"==typeof e){if(!((e=e.toLowerCase())in s))throw new Error(`Arrow key template "${e}" not found!`);e=s[e]}const n=new t;return this.isPressed(e[0])&&(n.y-=1),this.isPressed(e[1])&&(n.x-=1),this.isPressed(e[2])&&(n.y+=1),this.isPressed(e[3])&&(n.x+=1),n}},e.Gamepad=class{constructor({win:e=window,nav:t=navigator}={}){this.pressedButtons=new Set,this.window=e,this.navigator=t,this.window.addEventListener("gamepadconnected",({gamepad:e})=>{this.isConnected()||(this.gamepadIndex=e.index)}),this.window.addEventListener("gamepaddisconnected",({gamepad:e})=>{this.gamepadIndex===e.index&&(this.navigator.getGamepads().length?this.gamepadIndex=this.navigator.getGamepads()[0].index:this.gamepadIndex=void 0)})}isConnected(){return void 0!==this.gamepadIndex&&this.gamepad.connected}get gamepad(){return this.navigator.getGamepads()[this.gamepadIndex]}isPressed(e){return this.isConnected()&&this.gamepad.buttons[e].pressed}wasPressed(e){if(this.isConnected())if(this.gamepad.buttons[e].pressed){if(!this.pressedButtons.has(e))return this.pressedButtons.add(e),!0}else this.pressedButtons.delete(e);return!1}},Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "contro", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Game controls done right.", | ||
@@ -13,3 +13,5 @@ "main": "dist/contro.esm.js", | ||
"scripts": { | ||
"lint": "tslint -p .", | ||
"lint": "npm run lint-ts && npm run lint-md", | ||
"lint-ts": "tslint -p .", | ||
"lint-md": "remark .", | ||
"test": "nyc mocha src/**/*.spec.ts --require ts-node/register --watch-extensions ts", | ||
@@ -45,12 +47,17 @@ "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", | ||
"nyc": "^11.4.1", | ||
"remark-cli": "^4.0.0", | ||
"remark-lint": "^6.0.1", | ||
"remark-lint-no-empty-url": "^1.0.2", | ||
"remark-preset-lint-markdown-style-guide": "^2.1.1", | ||
"remark-preset-lint-recommended": "^3.0.1", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.53.0", | ||
"rollup-plugin-typescript": "^0.8.1", | ||
"semantic-release": "^11.0.2", | ||
"ts-node": "^4.1.0", | ||
"tslint": "^5.8.0", | ||
"typescript": "^2.6.2", | ||
"uglify-es": "^3.2.2", | ||
"semantic-release": "^11.0.2" | ||
"uglify-es": "^3.2.2" | ||
}, | ||
"dependencies": {} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23071
401
20