-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90a2f71
commit 4addbdc
Showing
13 changed files
with
57,808 additions
and
27,918 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<base data-ice="baseUrl" href="../../"> | ||
<title data-ice="title">src/layer.mjs | Parallax</title> | ||
<link type="text/css" rel="stylesheet" href="css/style.css"> | ||
<link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> | ||
<script src="script/prettify/prettify.js"></script> | ||
<script src="script/manual.js"></script> | ||
<meta name="description" content="A parallax module that will enable effortless integration of depth effects into your game."><meta property="twitter:card" content="summary"><meta property="twitter:title" content="Parallax"><meta property="twitter:description" content="A parallax module that will enable effortless integration of depth effects into your game."><meta property="twitter:image" content="./evitcastudio_round_branding.png"></head> | ||
<body class="layout-container" data-ice="rootContainer"> | ||
|
||
<header> | ||
<a href="./" style="display: flex; align-items: center;"><img src="./image/brand_logo.png" style="width:34px;"></a> | ||
|
||
<a href="identifiers.html">Reference</a> | ||
<a href="source.html">Source</a> | ||
|
||
<div class="search-box"> | ||
<span> | ||
<img src="./image/search.png"> | ||
<span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> | ||
</span> | ||
<ul class="search-result"></ul> | ||
</div> | ||
<a style="position:relative; top:3px;" href="https://github.com/EvitcaStudio/Parallax"><img width="20px" src="./image/github.png"></a></header> | ||
|
||
<nav class="navigation" data-ice="nav"><div> | ||
<ul> | ||
|
||
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/layer.mjs~Layer.html">Layer</a></span></span></li> | ||
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/parallax.mjs~ParallaxSingleton.html">ParallaxSingleton</a></span></span></li> | ||
<li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Parallax">Parallax</a></span></span></li> | ||
</ul> | ||
</div> | ||
</nav> | ||
|
||
<div class="content" data-ice="content"><h1 data-ice="title">src/layer.mjs</h1> | ||
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import { Parallax } from './parallax.mjs'; | ||
|
||
export class Layer { | ||
/** | ||
* The configuration object of this layer. | ||
* @private | ||
* @type {Object} | ||
* @param {number} horizontalSpeed - The horizontal speed of the layer. | ||
* @param {number} verticalSpeed - The vertical speed of the layer. | ||
* @param {number} plane - The plane this parallax layer will occupy. | ||
* @param {Diob[] | MapObject[]} backgrounds - An array of instances that will serve as the background. These are automatically toggled to repeat. | ||
* @param {Set} instances - A set of instances currently on the layer. | ||
*/ | ||
config = { | ||
// Move the instance with the camera if the parallax is set to 0 | ||
horizontalSpeed: 0, | ||
verticalSpeed: 0, | ||
plane: 1, | ||
backgrounds: new Set(), | ||
instances: new Set() | ||
} | ||
/** | ||
* Creates a new Parallax layer with the supplied configuration. | ||
* When creating a layer, all instances and backgrounds should already be on the map. | ||
* @param {Object} pConfig - The configuration of the parallax layer. | ||
* @prop {number} pConfig.horizontalSpeed - The horizontal speed of the layer. | ||
* @prop {number} pConfig.verticalSpeed - The vertical speed of the layer. | ||
* @prop {number} pConfig.plane - The plane this layer will occupy. | ||
* @param {Diob[] | MapObject[]} pConfig.backgrounds - An array of instances that will serve as the background. These are automatically toggled to repeat. | ||
* @prop {Diob[] | MapObject[]} pConfig.instances - The instances that will be added to the layer. | ||
*/ | ||
constructor(pConfig) { | ||
this.updateConfigSpeed(pConfig, true); | ||
|
||
if (!typeof pConfig.plane === 'number') { | ||
Parallax.logger.prefix('Parallax-Module').warn('Expected a number for "pConfig.plane", but received:', typeof pConfig.plane, '\n Default plane of "1" used.'); | ||
} else { | ||
this.config.plane = pConfig.plane; | ||
} | ||
|
||
const instanceConfig = { | ||
x: this.config.horizontalSpeed, | ||
y: this.config.verticalSpeed | ||
} | ||
|
||
if (Array.isArray(pConfig.instances)) { | ||
pConfig.instances.forEach(pInstance => { | ||
pInstance.plane = this.config.plane; | ||
this.add(pInstance, instanceConfig) | ||
}); | ||
} | ||
|
||
if (Array.isArray(pConfig.backgrounds)) { | ||
// Backgrounds automatically loop | ||
instanceConfig.loop = true; | ||
pConfig.backgrounds.forEach(pInstance => { | ||
pInstance.plane = this.config.plane; | ||
this.add(pInstance, instanceConfig) | ||
}); | ||
} | ||
} | ||
/** | ||
* Updates the configuration speed of the layer. | ||
* @param {Object} pConfig - The speed configuration of the parallax layer. | ||
* @prop {number} pConfig.horizontalSpeed - The horizontal speed of the layer. | ||
* @prop {number} pConfig.verticalSpeed - The vertical speed of the layer. | ||
* @param {boolean} pUpdateLayerConfigOnly - If only to update the layer config and not the instance config. | ||
*/ | ||
updateConfigSpeed(pConfig, pUpdateLayerConfigOnly) { | ||
this.updateHorizontalSpeed(pConfig.horizontalSpeed, pUpdateLayerConfigOnly); | ||
this.updateVerticalSpeed(pConfig.verticalSpeed, pUpdateLayerConfigOnly); | ||
} | ||
/** | ||
* Updates the horizontal speed of this layer. | ||
* @param {number} pHorizontalSpeed - The new horizontal speed. | ||
* @param {boolean} pUpdateLayerConfigOnly - If only to update the layer config and not the instance config. | ||
*/ | ||
updateHorizontalSpeed(pHorizontalSpeed, pUpdateLayerConfigOnly) { | ||
const inValidHorizontal = pHorizontalSpeed && typeof pHorizontalSpeed !== 'number'; | ||
|
||
if (inValidHorizontal) { | ||
Parallax.logger.prefix('Parallax-Module').warn('Expected a number for "pHorizontalSpeed", but received:', typeof pHorizontalSpeed); | ||
return | ||
} | ||
|
||
this.config.horizontalSpeed = pHorizontalSpeed; | ||
|
||
if (!pUpdateLayerConfigOnly) { | ||
const update = (pInstance) => { | ||
const parallaxInfo = Parallax.instanceWeakMap.get(pInstance); | ||
parallaxInfo.x = pVerticalSpeed; | ||
} | ||
|
||
this.instances.forEach(pInstance => { | ||
update(pInstance); | ||
}); | ||
this.backgrounds.forEach(pInstance => { | ||
update(pInstance); | ||
}); | ||
} | ||
} | ||
/** | ||
* Updates the vertical speed of the layer. | ||
* @param {number} pVerticalSpeed - The new vertical speed. | ||
* @param {boolean} pUpdateLayerConfigOnly - If only to update the layer config and not the instance config. | ||
*/ | ||
updateVerticalSpeed(pVerticalSpeed, pUpdateLayerConfigOnly) { | ||
const inValidVertical = pVerticalSpeed && typeof pVerticalSpeed !== 'number'; | ||
|
||
if (inValidVertical) { | ||
Parallax.logger.prefix('Parallax-Module').warn('Expected a number for "pVerticalSpeed", but received:', typeof pVerticalSpeed); | ||
return | ||
} | ||
|
||
this.config.verticalSpeed = pVerticalSpeed; | ||
|
||
if (!pUpdateLayerConfigOnly) { | ||
const update = (pInstance) => { | ||
const parallaxInfo = Parallax.instanceWeakMap.get(pInstance); | ||
parallaxInfo.y = pVerticalSpeed; | ||
} | ||
|
||
this.instances.forEach(pInstance => { | ||
update(pInstance); | ||
}); | ||
this.backgrounds.forEach(pInstance => { | ||
update(pInstance); | ||
}); | ||
} | ||
} | ||
/** | ||
* Adds the instance to the parallax layer. | ||
* When using this API the instance should already be on the map. | ||
* @param {Diob} pInstance - The instance to add to the layer. | ||
* @param {Object} pConfig - The personal config of this instance. Akin to the parallax info passed via the `Parallax.add` API. | ||
* @prop {number} pConfig.x - The horizontal speed of this instance. (This will be ignored and the layer's speed will be used.) | ||
* @prop {number} pConfig.y - The vertical speed of this instance. (This will be ignored and the layer's speed will be used.) | ||
* @prop {boolean} pConfig.loop - Whether this instance will be treated as a background and loop seamlessly. | ||
*/ | ||
add(pInstance, pConfig) { | ||
if (this.config.instances.has(pInstance)) return; | ||
this.config.instances.add(pInstance); | ||
const config = pConfig | ||
? pConfig | ||
: { | ||
x: this.config.horizontalSpeed, | ||
y: this.config.verticalSpeed | ||
} | ||
Parallax.add(pInstance, config); | ||
} | ||
/** | ||
* Removes the instance from the parallax layer. | ||
* @param {Diob} pInstance - The instance to remove from the layer. | ||
*/ | ||
remove(pInstance) { | ||
this.config.instances.delete(pInstance); | ||
Parallax.remove(pInstance); | ||
} | ||
}</code></pre> | ||
|
||
</div> | ||
|
||
<footer class="footer"> | ||
Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.1.0)</span><img src="./image/esdoc-logo-mini-black.png"></a> | ||
</footer> | ||
|
||
<script src="script/search_index.js"></script> | ||
<script src="script/search.js"></script> | ||
<script src="script/pretty-print.js"></script> | ||
<script src="script/inherited-summary.js"></script> | ||
<script src="script/test-summary.js"></script> | ||
<script src="script/inner-link.js"></script> | ||
<script src="script/patch-for-local.js"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.