ebabel

Library of reusable game modules to build HTML5 games.

Usage no npm install needed!

<script type="module">
  import ebabel from 'https://cdn.skypack.dev/ebabel';
</script>

README

ebabel

Build Status

Library of re-useable game modules to build web browser HTML5 games.

Install

npm install --save ebabel

Generate this README.md document

npm run readme

Contribute to ebabel game modules

First of all, thank you! Please see the contributing guidelines so we can start working together effectively.

audio(input) ⇒ Object

ebabelAudio Play music or positional sound.

Kind: global function
Returns: Object - Multiple objects and properties: camera, url, listener, sound, and audioLoader.

Param Type Description
input Object Object with properties to specify theme music parameters.
input.THREE Object Library THREE.js
input.camera Object THREE.js camera instance.
input.volume number Loudness of the music, ranges from 0 to 1.
input.url string Path to the theme music file.
input.distance number If this is set we are using positional audio. Distance at which the sound is audible.
input.name string Unique name to identify the theme music and listener in THREE.js camera children.
input.loop boolean Play theme music in a loop. False by default.
input.autostart boolean Stars playing the music as soon as it loads. False by default.

Constants

mockEG

EG Mock EG (eBabel Games) for unit testing purposes.

mockTHREE

mockTHREE Mock THREE.js for unit testing purposes.

mockTHREEx

mockTHREEx Mock THREEx.js for unit testing purposes.

Functions

hexStringToInt(input)number

hexStringToInt Convert string hex to integer.

mockEG

EG Mock EG (eBabel Games) for unit testing purposes.

Kind: global constant

mockTHREE

mockTHREE Mock THREE.js for unit testing purposes.

Kind: global constant

mockTHREEx

mockTHREEx Mock THREEx.js for unit testing purposes.

Kind: global constant

hexStringToInt(input) ⇒ number

hexStringToInt Convert string hex to integer.

Kind: global function
Returns: number - Integer conversion.

Param Type Description
input string Input hexadecimal color in string format, i.e. #ff0099

light(input) ⇒ Object

light Setup a THREE.js PointLight and add it to the scene.

Kind: global function
Returns: Object - THREE.PointLight instance.

Param Type Description
input Object Parameters and dependencies.
input.THREE Object Core library of THREE.js
input.scene Object Scene object where the light will be added.
input.color hex Dominant color of the light in hexadecima format. Defaults to pure white 0xffffff.
input.position Array 3D coordinates of where the light should be placed. Defaults to [1, 250, 1].
input.name String Name of the light. Defaults to 'main-light'.

textSprite(input) ⇒ Object

ebabelTextSprite Create a 2D text sprite that can be added to any THREE.js mesh.

Kind: global function
Returns: Object - THREE.js sprite that can be added to a mesh.

Param Type Description
input Object Collection of input properties.
input.THREE Object Library THREE.js object.
input.canvas Object DOM canvas, i.e. document.createElement('canvas')
input.text string String of text to display above a sprite.

Functions

updatePlayerPositionRotation(camera, dataStore)

updatePlayerPositionRotation Update the position and rotation of the current player camera.

keyboardControls(dataStore)

keyboardControls Update the position and rotation of the current player camera based on keyboard keys pressed up or down.

updatePlayerPositionRotation(camera, dataStore)

updatePlayerPositionRotation Update the position and rotation of the current player camera.

Kind: global function

Param Type Description
camera Object THREE.js camera.
dataStore Object Central store of state data.

keyboardControls(dataStore)

keyboardControls Update the position and rotation of the current player camera based on keyboard keys pressed up or down.

Kind: global function

Param Type Description
dataStore Object Central store of state data.

Functions

skybox(input)Object

Skybox Setup a skybox mesh and add it to the scene.

userData(size, position)

userData Setup skybox default userData.

skybox(input) ⇒ Object

Skybox Setup a skybox mesh and add it to the scene.

Kind: global function
Returns: Object - Skybox mesh.

Param Type Description
input Object Parameters and dependencies.
input.THREE Object Core library of THREE.js
input.scene Object Scene object where the skybox will be added.
input.directions Array Array of image paths for all skybox sides, by directions.
input.size Number Size of the skybox. Defaults to 10000.
input.position Array Position of the skybox as 3D coordinates. Defaults to [0, 0, 0].

userData(size, position)

userData Setup skybox default userData.

Kind: global function

Param Type Description
size Number Length of each side of the skybox cube.
position Array Coordinates where the skybox is to be positioned: Array of 3 numbers.

Functions

countArrayElements(input)

countArrayElements Returns an object that counts how many times each element is present in a given array.

deepCopy(input)Object

deepCopy Make a deep copy of an object, i.e. breaks the pointers, so that updating the copy will not change the original.

dice()number

dice Throw 1 die.

distance(i, j)number

distance Calculate the distance in 3D space from point "i" x, y, and z coordinates to point "j" x, y, and z coordinates.

guid()string

guid Returns a globally unique id string following the standard guid format.

Ido()

Ido Returns a a string that commemorates how many days since Ido died.

positive(input)number

positive Computes the positive rounded up value of the input.

preventXss(input)string

preventXss Processes an input string to prevent Cross Site Scripting injection attacks (XSS). Returns a safe version of that input.

randomList(input)object

randomList Returns the subset of a list of x elements from a larger source list.

randomPosOrNeg(max)number

randomPosOrNeg Calculates a positive or negative random number.

randomPosition(size)array

randomPosition Calculate new coordinates in a 3D space randomly to generate a new position.

random(max, min)number

random Returns a random integer number from 1 to 100 or from min to max (min is non-inclusive).

reducedDistance(i, j, r)array

reducedDistance Calculate new coordinates in a 3D space for point "i" in order to get closer to point "j" reducing the distance between the two points by "r" amount.

trait()number

trait Throw 3 dices.

countArrayElements(input)

countArrayElements Returns an object that counts how many times each element is present in a given array.

Kind: global function

Param Type Description
input array Array where element occurences need to be counted.

deepCopy(input) ⇒ Object

deepCopy Make a deep copy of an object, i.e. breaks the pointers, so that updating the copy will not change the original.

Kind: global function
Returns: Object - Copy of the input onject.

Param Type Description
input Object Original object that needs to be copied.

dice() ⇒ number

dice Throw 1 die.

Kind: global function
Returns: number - Return a random integer from 1 to 6.

distance(i, j) ⇒ number

distance Calculate the distance in 3D space from point "i" x, y, and z coordinates to point "j" x, y, and z coordinates.

Kind: global function
Returns: number - Distance between i and j.

Param Type Description
i array Array start position in 3D space e.g. [-10, 5.1, 3]
j array Array end position in 3D space e.g. [0, 15.1, -7]

guid() ⇒ string

guid Returns a globally unique id string following the standard guid format.

Kind: global function
Returns: string - Guid of 36 characters, including hyphens.

Ido()

Ido Returns a a string that commemorates how many days since Ido died.

Kind: global function

positive(input) ⇒ number

positive Computes the positive rounded up value of the input.

Kind: global function
Returns: number - Positive value of the input number.

Param Type Description
input number Number to be converted into positive.

preventXss(input) ⇒ string

preventXss Processes an input string to prevent Cross Site Scripting injection attacks (XSS). Returns a safe version of that input.

Kind: global function
Returns: string - Cleaned string that shouldn't run any javascript code if displayed on a webpage.

Param Type Description
input string The user input string that needs to be sanitized, to remove the possibility of injecting script of other html tags.

randomList(input) ⇒ object

randomList Returns the subset of a list of x elements from a larger source list.

Kind: global function
Returns: object - 2 arrays in an object: list and rest.

Param Type Description
input object Input properties: source and max.
input.source array The source property is the array from which a max number of elements will be randomly selected.
input.max number Number of elements that will be randomly selected from input.source

randomPosOrNeg(max) ⇒ number

randomPosOrNeg Calculates a positive or negative random number.

Kind: global function
Returns: number - Positive or negative number randomly generated.

Param Type Description
max number Maximum value that can be generated.

randomPosition(size) ⇒ array

randomPosition Calculate new coordinates in a 3D space randomly to generate a new position.

Kind: global function
Returns: array - Array of generated position coordinates.

Param Type Description
size array Array of 3 numbers for the width, height and depth, respectively.

random(max, min) ⇒ number

random Returns a random integer number from 1 to 100 or from min to max (min is non-inclusive).

Kind: global function
Returns: number - Integer between min and max (inclusively)

Param Type Description
max number the maximum to be returned, should be an integer
min number the minimum to be returned, should be an integer

reducedDistance(i, j, r) ⇒ array

reducedDistance Calculate new coordinates in a 3D space for point "i" in order to get closer to point "j" reducing the distance between the two points by "r" amount.

Kind: global function
Returns: array - Array of coordinates the i position should get to in order to get closer to j.

Param Type Description
i array Array of 3 numbers for the coordinates x, y, z of the source point.
j array Array of 3 numbers for the coordicates x, y, z of the target point.
r number Number by which the distance between i and j needs to be reduced.

trait() ⇒ number

trait Throw 3 dices.

Kind: global function
Returns: number - Return a random integer from 3 to 18.

boundingBox(THREE, mesh)

boundingBox Calculate a 3D bounding box for a THREE.js mesh.

Kind: global function

Param Type Description
THREE object Core library of THREE.js
mesh string 3D THREE.js object for a mesh that needs a bounding box calculated.