p5.raycaster
Version:
a simple p5js library for semi 3d rendering with ray casting
191 lines (163 loc) • 5.93 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: KeyboardControl.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: KeyboardControl.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>class KeyboardControl {
static defaultKeyMap = {
forward: {
keys: ["w"],
},
backward: {
keys: ["s"],
},
goLeft: {
keys: ["a"],
},
goRight: {
keys: ["d"],
},
turnLeft: {
keys: ["q"],
},
turnRight: {
keys: ["e"],
},
tiltUp: {
keys: ["z"],
},
tiltDown: {
keys: ["x"],
},
moveDoor: {
keys: [" "],
}
}
/**
*
* @param {object} [keyMap = KeyboardControl.defaultKeyMap]
*/
constructor(keyMap = KeyboardControl.defaultKeyMap){
this.keyMap = keyMap;
for (const key in this.keyMap) {
if (Object.hasOwnProperty.call(this.keyMap, key)) {
if (!keyMap[key].initValue) {
this[key] = false;
} else {
this[key] = keyMap[key].initValue;
}
}
}
this.regControl();
}
regControl(){
document.addEventListener("keydown", this.keyDown.bind(this));
document.addEventListener("keyup", this.keyUp.bind(this));
}
removeControl(){
document.removeEventListener("keydown", this.keyDown.bind(this));
document.removeEventListener("keyup", this.keyUp.bind(this));
}
/**
*
* @param {KeyboardEvent} ev
*/
keyDown(ev) {
ev.preventDefault();
for (const key in this.keyMap) {
if (Object.hasOwnProperty.call(this.keyMap, key)) {
const keyList = this.keyMap[key].keys;
if (keyList.includes(ev.key)) {
const toggle = this.keyMap[key].toggle;
const initV = this.keyMap[key].initValue;
if (toggle && toggle === "down") {
this[key] = !this[key];
} else if (!toggle) {
this[key] = initV !== undefined ? !initV : true;
}
}
}
}
}
/**
*
* @param {KeyboardEvent} ev
*/
keyUp(ev) {
for (const key in this.keyMap) {
if (Object.hasOwnProperty.call(this.keyMap, key)) {
const keyList = this.keyMap[key].keys;
if (keyList.includes(ev.key)) {
const toggle = this.keyMap[key].toggle;
const initV = this.keyMap[key].initValue;
if (toggle && toggle === "up") {
this[key] = !this[key];
} else if (!toggle) {
this[key] = initV !== undefined ? initV : false;
}
}
}
}
}
/**
* this function will not copy the object
*
* @param {object} keyMap
*/
loadKeyMap(keyMap) {
this.keyMap = keyMap;
}
/**
* add a new keyboard control item
* @param {string} name
* @param {Array} keyList
* @param {undefined} [toggle=undefined] when the value should be toggled, "down" or "up", undefined means it is not a toggle value
* @param {undefined} [initValue=undefined] the default value when no keyboard input
*/
addItem(name, keyList, toggle = undefined, initValue = undefined) {
this.keyMap[name] = {
keys: keyList,
toggle: toggle,
initValue: initValue
}
this[name] = false;
}
/**
* delete a keyboard control item
* @param {string} name
*/
removeItem(name) {
if (this.keyMap.hasOwnProperty(name)) {
delete this.keyMap[name];
delete this.name;
}
}
}
export default KeyboardControl;</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Camera.html">Camera</a></li><li><a href="KeyboardControl.html">KeyboardControl</a></li><li><a href="MouseControl.html">MouseControl</a></li><li><a href="PointerLockControl.html">PointerLockControl</a></li><li><a href="Sprite.html">Sprite</a></li><li><a href="TransparentWall.html">TransparentWall</a></li><li><a href="Util.html">Util</a></li><li><a href="World.html">World</a></li></ul><h3>Global</h3><ul><li><a href="global.html#createCamera">createCamera</a></li><li><a href="global.html#createSkyBox">createSkyBox</a></li><li><a href="global.html#createSprite">createSprite</a></li><li><a href="global.html#createTextureMap">createTextureMap</a></li><li><a href="global.html#createWorld">createWorld</a></li><li><a href="global.html#initKeyboardControl">initKeyboardControl</a></li><li><a href="global.html#initMouseControl">initMouseControl</a></li><li><a href="global.html#initPointerLockControl">initPointerLockControl</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Feb 01 2024 12:26:09 GMT+0000 (Greenwich Mean Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>