Giter VIP home page Giter VIP logo

re-5's Introduction

Introduction

Rect Engine 5 (RE-5) - is the object-oriented engine-framework made by Black Square Studios for 2D games development with JavaScript. Graphic engine in RE-5 is based on WebGL2 (browser adaptation of OpenGL3.0). Engine is suitable for small and big multiplayer games development as well. Engine has a flexible plugin system that expands its functionality. More lessons and guides to RE-5 you can find following this link.

Setting up the project

  • Install any local server or just use our small local server on Node.js
  • Create project directory on the server
  • Unpack this repository into that folder
  • When you just open it in browser you sould see the tinged square rotating around and the caption "Rect Engine 5"

WARNING! You can use any comfortable to you code editor, cause there's no native IDE for the engine yet.

Project structure

  • Engine (engine files)
    • Shaders (shaders)
      • fragment-shader.glsl (fragment shader)
      • vertex-shader.glsl (vertex shader)
    • collision.js (collision detection)
    • engine.js (engine logic, program interface)
    • renderer.js (graphics engine shall)
    • renderCore.js (graphics engine core)
    • matrix.js (matrix maths)
    • vert.js (object vertices)
  • Plugins (plugins)
  • Scene (scenes)
    • new (scene files)
      • end.js (runs when the scene ends)
      • init.js (runs after the scene is initialized)
      • start.js (runs every time when the scene starts)
  • Scripts (scripts)
    • assets.js (for setting up assets)
    • config.js (for setting up configurations and settings)
    • families.js (for families initializing)
    • sources.js (for source loading)
  • Sources (sources)
    • audio (audio files)
    • images (textures, tilemaps)
    • json (JSON files)
    • glsl (shaders)
  • index.html (runs engine.js and main.js)
  • main.js (engine initialization)

What's the project consist of?

In the main.js file there's the engine initializing, including all scripts from Scripts falder, creating scenes, scripts from the Scenes/scene name folder attaching to the scenes, switching to the certain scene.

In the config.js script there's all project and engine settings.

In the families.js script there's all families initializations

In the sources.js script there's all needed sources loading (textures, sounds, fonts...)

In the assets.js script there's all assets initializations.

You can add your own scripts in Scripts folder or in the folders of the scenes and include it wherever and whenever you want.

In the every folder of the scene there's 3 files:

  • init.js - the script runs only at ones after the scene is initialized, there you have to create cameras, layers, most of the game objects, game loop for the scene and initialize all needed event listeners.
  • start.js - the script runs every single time when the scene starts, here the game switches to the necessary camera, the engine settings change, the level starts, for example enemy takes their places on the level.
  • end.js - the script runs every single time when the scene ends, game engine settings turns back to normal, the game progress could be saved.

Methods and properties

Content

Initialization

new RectJS(callback[, sourceHOST[, engineSource[, pluginSource[, eventDetector]]]])

  • callback <function> - this function takes the game engine instance as an argument
  • sourceHOST <string> Default: "" - the realtive path from the index.html to main.js, engine and project folders.
  • engineSource <string> Default: "Engine/" - the path from the folder set up in sourceHOST to the engine folder.
  • pluginSource <string> Default: "Plugins/" - the path from the folder set up in sourceHOST to the plugins folder.
  • eventDetector <object> Default: null - DOM element on the page to which all mouse event listeners is bound. If equal null, it sets by default to RectJS.eventDetector.

Engine initialization

require(src[, type])

  • src <string> - relative path to the file
  • type <string> Default: "JS" - file type
    • "JS" - JavaScript. The method returns an arrow function from the script.
    • "JSON" - JSON file. The method returns JavaScript-object from the JSON file.
    • "TEXT" - Simple text. The method returns a text from the file as a string.

Loading an external script. Returns a function.

WARNING! All available scripts actually are the arrow functions:

(params) => {
	// code here
}

or the function taken in the brackets:

(function (params) {
	// code here
})

WARNING! If you want variable to be accessible form all of the scripts it need to be global. There's 3 ways to do that:

  • declare the variable missing the keyword ('const', 'let' or 'var') (recommended) variable = 1;
  • declare the variable as a property of the window window.variable = 1;
  • declare the variable beyound the "onload" event listener in the main.js script
let variable = 1;

window.addEventListener('load', e => {
...

RectJS.Script(src)

  • src <string> - path to the script relative to scripts folder (RectJS.scriptPath, Scripts/ by default)

Loading external script. Returns a function.

new RectJS.Scene(name[, initOnload[, id]])

  • name <string> - a name of the folder with scene files in the scenes folder (RectJS.scenePath, Scenes/ by default)
  • initOnload <boolean> Default: true
    • true - initialization script runs emmidiately after the scene is declared
    • false - initialization script runs right before the first scene start
  • id <string> Default: "scene_{scene number}" - scene indentifier

Scene initializer. Returns the scene instance.

!!!WARNING!!! To create a new scene you need to make a new folder with the name of the scene in Scenes/ directory and put 3 script files there:

  • init.js - runs after the scene was initialized
  • start.js - runs when the player go to the scene
  • end.js - runs when the player leave the scene

These files will load automaticaly.

The init.js script example:

(scene) => {
	
	// create a camera
	
	// create some layers
	
	// objects, loops, listeners and game logics
	
}

The start.js or end.js script example:

(scene, params) => {
	
	// some code
	
}

Scene.set([startParams[ ,endParams]])

  • startParams <object> - parameters passes to the start.js script of the scene
  • endParams <object> - parameters passes to the end.js script of a scene

Switching to the scene

Special methods

new RectJS.Vector2([x, y])

  • x <number> | <string> Default: 0 - x coordinate of the vector
  • y <number> | <string> Default: 0 - y coordinate of the vector

2D vector creation. Returns the Vector2 object.

Vector2.toString()

Converting the vector into a string. "v{X coordinate};{Y coordinate}"

new rjs.Vector2(1, 3).toString() = "v1;3";

Vector2.fromString(v)

  • v <string> - vector in the form of string

Converting vector from string into a Vector2 object.

Vector2.fromString("v1;3") = new rjs.Vector2(1; 3);

Vector2.len

Returns the length of the vector.

Vector2.angle

Returns the angle between source vector and X+ axis.

Vector2.add(v)

Vector2.add([x[, y]])

  • v <object> (<Vector2>) - vector
  • x <number> Default: 0 - x coordinate
  • y <number> Default: x - y coordinate

Returns the total of the vectors.

Vector2.sub(v)

Vector2.sub(x[, y])

  • v <object> (<Vector2>) - vector
  • x <number> Default: 0 - x coordinate
  • y <number> Default: x - y coordinate

Returns the difference of the vectors.

Vector2.mult(v)

Vector2.mult(x[, y])

  • v <object> (<Vector2>) - vector
  • x <number> Default: 0 - x coordinate
  • y <number> Default: x - y coordinate

Returns the product of the vectors.

Vector2.div(v)

Vector2.div(x[, y])

  • v <object> (<Vector2>) - vector
  • x <number> Default: 0 - x coordinate
  • y <number> Default: x - y coordinate

Returns the division of the vectors.

Vector2.dot(v)

Vector2.dot(x[, y])

  • v <object> (<Vector2>) - vector
  • x <number> Default: 0 - x coordinate
  • y <number> Default: x - y coordinate

Returns a dot product of the vectors.

Vector2.norm()

Returns the normalized vector.

Vector2.rot(a)

  • a <number> - angle in degrees

Returns the source vector rotated on a given angle.

Vector2.abs()

Returns the vector with positive values of x and y

vec2([x, y])

2D vector creation. Returns new RectJS.Vector2(x, y).

rgb(r, g, b)

  • r <number> - red channel (0-255)
  • g <number> - green channel (0-255)
  • b <number> - blue channel (0-255)

Returns the colorRGB object.

rgba(r, g, b[, a])

  • r <number> - red channel (0-255)
  • g <number> - green channel (0-255)
  • b <number> - blue channel (0-255)
  • a <number> Default: 255 - alpha channel (0-255)

Returns the colorRGBA object.

colorRGB.toString()

colorRGBA.toString()

Returns the color as a string.

const color = rgb(255, 255, 255);
console.log(color.toString());
// "rgb(255, 255, 255)"
const color = rgba(255, 255, 255, 255);
console.log(color.toString());
// "rgba(255, 255, 255, 255)"

colorRGBA.toStringCSS()

Returns the color converted to a string in CSS color type, where alpha channel is between 0 and 1.

const color = rgba(255, 255, 255, 255);
console.log(color.toStringCSS());
// "rgba(255, 255, 255, 1)"

count(object)

  • object <array> | <object> - an array or an object

Returns the amount of filled slots in the given object.

copy(object)

  • object <object> (<object> | <RectJS.Vector2>) - JavaScript-object

Returns the copy of a 1D JavaScript-object or RectJS.Vector2.

log()

THe reference to console.log().

RectJS.isObject(object)

  • object <object> - JavaScript-object

Returns true if the argument is a game object.

RectJS.checkSourceLoaded()

In loader mode RectJS.LOADER_MODE checks for sources that wasn't loaded yet. If they are - RectJS.sourceLoaded sets to false, otherwise - true.

Cameras and layers

new RectJS.Camera(options)

  • options <object> Default: new Object()
    • pos <object> (<RectJS.Vector2 | <RectJS.vec2>) Default: new RectJS.Vector2(0, 0) - a camera position on a scene
    • id <string> Default: "camera_{camera number}" - camera identifier

Returns the Camera object.

Camera.set()

Switching to the camera.

new RectJS.Layer(scene[, parallax[, scale[, id[, options]]]])

  • scene <object> (<RectJS.Scene>) - the layer scene
  • paralalx <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(100, 100) - percentage of the layer parallax
  • scale <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(1, 1) - the layer scaling
  • id <string> Default: "layer_{номер слоя}" - layer identifier
  • options <object> Default: new Object()
    • visible <boolean> Default: true - layer visibility

Returns a Layer object. WARNING! The RE-5 renderer draw all the layers in order of their initialization.

new RectJS.Group(layer[, id])

  • layer - the layer of the group
  • id Default: "group_{group number}"

Returns a rendering group. Rendering groups can help you to increase the performance by reducing unnecessary draw calls. If you have lost of different object on the layer you can add the objects with similar appearences to the render group, to optimize the rendering order. Pay attention to the following properties:

  • Textures
  • Shaders
  • Vertices
  • Colors
  • Color modes

!!!WARNING!!! If you move the object from one layer to another in runtime, make sure that you've removed it from all of the render groups. Mistakes like that can couse serious rendering bugs.

Group.isset(o)

  • o <object> (<RectJS.Sprite> | <RectJS.Polygon>) - a game object

Returns true if the game object is apart of the group, otherwise it returns false.

Group.add(o)

  • o <object> (<RectJS.Sprite> | <RectJS.Polygon>) - a game object

Adds an object to the group.

!!!WARNING!!! Use this method only if you want object to be rendered several times on a single frame. Make sure you know what are you doing.

Group.set(o)

  • o <object> (<RectJS.Sprite> | <RectJS.Polygon>) - a game object

Adds an object to the group and removes it from all of its current groups.

Group.remove(o)

  • o <object> (<RectJS.Sprite> | <RectJS.Polygon>) - a game object

Removes an object from the group.

Game objects

new RectJS.Polygon(options)

  • options <object>
    • pos <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - position on the scene
    • vertices <array> [<RectJS.Vector2>, ...] Default: new Array() - array of the vertices
    • scale <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(1, 1) - object scaling
    • angle <number> Default: 0 - angle (degrees)
    • origin <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - origin offset
    • points <array> [<RectJS.Vector2>, ...] Default: new Array() - bounding points
    • texture <object> (RectJS.Texture) Default: null - texture
    • color <arrat> (<rgb> | <rgba>) Default: rgb(255, 255, 255) - color
    • colors <array> [<rgb> | <rgba>, ...] Default: [...this.color] - Array with color of every single vertex. Works only if colorMode = "VERTEX"!
    • colorMode <string> Default: SINGLE - Color mode. Theres're 3 modes:
      • "SINGLE" - the object is entirely filled with one single color. Amount of objects with different colors doesn't affect on the performance.
      • "UNIFORM" - useful in case when there are a lot of objects with the same color, on a large scale increases the performance.
      • "VERTEX" - works almost like "SINGLE", but allows to color every single vertex (colors set in the colors property)
    • filters <array> [<rgb> | <rgba>, ...] Default: new Array() - array with color filters
    • opacity <number> Default: 100 - percentage of the opacity
    • render <boolean> Default: true - object visibility
    • layer <object> (<RectJS.Layer>) - object layer
    • id <string> Default: "object_{object number}" - object unique identifier
    • textOverlap <boolean> Default: false - texts overlapping
    • families <array> [<RectJS.Family>, ...] Default: new Array() - object families
    • program <object> (<RectJS.Program>) Default: RectJS.renderer.programs["DEFAULT"] - shader program (large amount of different shaders on the scene decreases the performane)
    • private <object> Default: new Object() - object with custom properties and methods.
      • init <function> Default: undefined - runs after the object created

Polygon object creation. Returns the game Object.

var object = new rjs.Polygon({
	...
	private: {
		test: 123,
		init: function () {
			console.log(this.text);
			// "123"
		}
	}
});

console.log(object.test);
// "123"

You can access any parameter of an object (except of private) as it's property using point. Parameters and methods from private object become regular object properties (check the code above).

new RectJS.Sprite(options)

  • options <object>
    • pos <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - position on the scene
    • size <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - size of the sprite
    • scale <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(1, 1) - object scaling
    • angle <number> Default: 0 - angle (degrees)
    • origin <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - origin offset
    • points <array> [<RectJS.Vector2>, ...] Default: new Array() - bounding points
    • texture <object> (RectJS.Texture) Default: null - texture
    • color <arrat> (<rgb> | <rgba>) Default: rgb(255, 255, 255) - color
    • colors <array> [<rgb> | <rgba>, ...] Default: [...this.color] - Array with color of every single vertex. Works only if colorMode = "VERTEX"!
    • colorMode <string> Default: SINGLE - Color mode. Theres're 3 modes:
      • "SINGLE" - the object is entirely filled with one single color. Amount of objects with different colors doesn't affect on the performance.
      • "UNIFORM" - useful in case when there are a lot of objects with the same color, on a large scale increases the performance.
      • "VERTEX" - works almost like "SINGLE", but allows to color every single vertex (colors set in the colors property)
    • filters <array> [<rgb> | <rgba>, ...] Default: new Array() - array with color filters
    • opacity <number> Default: 100 - percentage of the opacity
    • render <boolean> Default: true - object visibility
    • layer <object> (<RectJS.Layer>) - object layer
    • id <string> Default: "object_{object number}" - object unique identifier
    • textOverlap <boolean> Default: false - texts overlapping
    • families <array> [<RectJS.Family>, ...] Default: new Array() - object families
    • program <object> (<RectJS.Program>) Default: RectJS.renderer.programs["DEFAULT"] - shader program (large amount of different shaders on the scene decreases the performane)
    • private <object> Default: new Object() - object with custom properties and methods.
      • init <function> Default: undefined - runs after the object created

Rectangle shaped object (Sprite) creation. Returns a game Object.

new RectJS.Text(options)

  • options <object>
    • pos <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - position on the scene
    • size <number> - text size
    • scale <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(1, 1) - object scaling
    • angle <number> Default: 0 - angle (degrees)
    • origin <string> Default: "center-middle" - text align
    • points <array> [<RectJS.Vector2>, ...] Default: new Array() - bounding points
    • font <string> - text font
    • text <string> - text content
    • color <arrat> (<rgb> | <rgba>) Default: rgb(255, 255, 255) - color
    • filters <array> [<rgb> | <rgba>, ...] Default: new Array() - array with color filters
    • opacity <number> Default: 100 - percentage of the opacity
    • render <boolean> Default: true - object visibility
    • layer <object> (<RectJS.Layer>) - object layer
    • id <string> Default: "object_{object number}" - object unique identifier
    • families <array> [<RectJS.Family>, ...] Default: new Array() - object families
    • private <object> Default: new Object() - object with custom properties and methods.
      • init <function> Default: undefined - runs after the object created

Text creation. Returns a game Object.

Object.destroy()

Destroying the object.

Object.setLayer(layer)

  • layer <object> (<RectJS.Layer>) - new object layer

Changing a layer of the object

Object.getPoint(id)

  • id <number> - bounding point index in the array Object.points

Returns the vector of bounding point position on the scene (according to it's transformations).

Loops

new RectJS.GameLoop(callback[, active[, scene[, absl]]])

  • callback <function> - callback function
  • active <boolean> Default: true - loop status
  • scene <object> (<RectJS.Scene>) Default: null - the loop works only on this scene. If = null - loop works on every scene in the game
  • absl <boolean> Default: false - if true the loop works even in loader mode.

Game loop creation. Returns a loop object. Loop.callback runs synchronously before every single frame (if the is active of course)

Loop.start()

Enable game loop.

Loop.stop()

Disable game loop.

new RectJS.Interval(callback, timeout[, active[, scene]])

  • callback <function> - interval callback
  • timeout <number> - interval in milliseconds
  • active <boolean> Default: true - interval status
  • scene <object> (<RectJS.Scene>) Default: null - the interval works only on this scene. If = null - the interval works on every scene in the game

Creation of the loop with given interval. Runs asynchronously.

new RectJS.Wait(callback[, delay[, active[, scene[, absl]]]])

  • callback <function> - callback runs when the time is up
  • delay <number> Default: 0 - delay in game ticks (~60/sec)
  • active <boolean> Default: true - active status
  • scene <object> (<RectJS.Scene>) Default: null -the delay works only on this scene. Если = null - the delay works on every scene in the game
  • absl <boolean> Default: false - if true the delay works even in loader mode.

Crating a delay. Using that you can run some after the given delay set in milliseconds. Runs synchronously after all the loops and right before the scene rendering.

Sources

new RectJS.Texture(src[, scale[, custom_size]])

  • src <string> - relative path to the image file
  • scale <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(1, 1) - source texture scaling, affects on the resolution of the texture
  • custom_size <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - absolute size of source texture, affects on the resolution of the texture. If = 0 - size will be set to the source image size automatically

Texture loading. Returns a source texture object.

RectJS.Image(src[, scale[, custom_size]])

  • src <string> - relative path from the images directory (RectJS.imagePath, Sources/images/ by default) to the image file
  • scale <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(1, 1) - source texture scaling, affects on the resolution of the texture
  • custom_size <object> (<RectJS.Vector2>) Default: new RectJS.Vector2(0, 0) - absolute size of source texture, affects on the resolution of the texture. If = 0 - size will be set to the source image size automatically

Texture loading. Returns a source texture object.

new RectJS.Tiled(origin, size)

  • origin <object> (RectJS.Texture) - source texture
  • size <object> (<RectJS.Vector2>) - size of every single tile

Creation of the repeating texture. Returns a texture object.

new RectJS.Crop(origin, pos, size)

  • origin <object> (RectJS.Texture) - source texture
  • pos <object> (<RectJS.Vector2>) - origin of the cropping area (its left-top corner)
  • size <object> (<RectJS.Vector2>) - size of the cropping area

Cropping a texture, one tiel from a tilemap for example. Returns a texture object.

new RectJS.Animation(options)

  • options <object>
    • frames <array> [<RectJS.Texture>, ...] Default: new Array() - an array includes all of the frames
    • speed <number> Default: 60 - amount of animation frames per 60 game ticks
    • id <string> Default "animation_{animation number}" - animation indentifier

Animation creation. Returns an animation object.

You can access the index of the current frame using a .currentIndex property, you can set a certain frame by changing this property. The speed of the animation can be accessed with .speed property, you can stop the animation, change its framerate or start it again by changing this property.

WARNING! Animations in RE-5 aren't finished yet. All objects with a common animation will be animated at the time.

RectJS.loadFont(name, src)

  • name <string> - unique name of the font
  • src <string> - path to the .ttf file

Loading a custom .ttf font. Returns the unique name of the font, you can pass it to the font property of a text object.

RectJS.Font(name, src)

  • name <string> - unique name of the font
  • src <string> - relative path to the .ttf file from the fonts library (RectJS.fontPath, Sources/fonts/ by default)

Loading a custom .ttf font. Returns the unique name of the font, you can pass it to the font property of a text object.

RectJS.JSON(src)

  • src <string> - relative path to the JSON file from the fonts library (RectJS.jsonPath, Sources/json/ by default)

Loading a JSON file. Returns an object with content of the JSON.

new RectJS.Sound(src[, distance])

  • src <string> - path to the audio file
  • distance <number> Default 100 - valume multiplier if the sound is bound to an object

Loading an external sound. Returns a sound object.

RectJS.Audio(src[, distance])

  • src <string> - relative path to the audio file from the fonts library (RectJS.audioPath, Sources/audio/ by default)
  • distance <number> Default 100 - valume multiplier if the sound is bound to an object

Loading an external sound. Returns a sound object.

Sound.play()

Start or resume the sound.

Sound.stop()

Pause the sound.

Sound.reset()

Stops the sound playing and reset the time.

Sound.restart()

Restart the sound from the very beginning.

Sound.getDuration()

Returns the sound duration (in milliseconds)

Sound.setTime(time)

  • time <number> - time (in milliseconds)

Sets playing current time.

Sound.getTime()

Returns current playing time (in milliseconds)

Sound.setVolume(volume)

  • volume <number> - volume percentage (0-100)

Sets the sound volume.

Sound.getVolume()

Returns the volume percentage.

Sound.bind(object)

  • object <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object

Binds the sound to an object.

Sound.unbind(object)

  • object <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object

Unbinds the sound from an object

Shaders

new RectJS.Shader(type, src, id)

  • type <string> - shader type "VERTEX" - vertex shader "FRAGMENT" - fragment shader
  • src <string> - path to the .glsl file
  • id <string> - identifier

Don't use identifier "DEFAULT" if you aren't aware of what you are doing.

Loading and compilation of the custom shader script.

new RectJS.Program(options)

  • options <object> - settings
    • vertex <object> | <string> (<RectJS.Shader> | <RectJS.Shader.id>) - vertex shader, if you wanna add default shader to this program set this to "DEFAULT"
    • fragment <object> | <string> (<RectJS.Shader> | <RectJS.Shader.id>) - fragment shader, if you wanna add default shader to this program set this to "DEFAULT"
    • id <string> - program identifier. Set it to "DEFAULT" if you wanna make this program default for all game objects.

Creates a custom shader program.

Shaders in RE-5 works on GLSL

Vertex shader parameters

attribute vec2 vertex

Take vertex coordinates

attribute vec2 UV

Takes texture coords of a vertex

attribute mat3 matrix

Takes a transformation matrix

attribute vec4 color

Takes a color filter (if RectJS.Object.colorMode = "SINGLE" | "VERTEX")

uniform bool colorMode

If RectJS.Object.colorMode = "UNIFORM" - takes true, otherwise - false

uniform vec4 uColor

Takes a color filter (if RectJS.Object.colorMode = "UNIFORM")

varying vec4 vColor

Passes the necessary color filter to the fragment shader

varying vec2 vUV

Passes the texture coords to the fragment shader

Fragment shader parameters

uniform sampler2D tex

Takes a texture

varying vec4 vColor

Takes a color filter

varying vec2 vUV

Takes texture coords

Mouse and keyboard

new RectJS.Click(callback[, active[, scene[, target]]])

Left click event

new RectJS.RightClick(callback[, active[, scene[, target]]])

Right click event

new RectJS.MouseDown(callback[, active[, scene[, target]]])

Event of pressing down the left mouse button

new RectJS.MouseUp(callback[, active[, scene[, target]]])

Left mouse button release event

new RectJS.MouseRightDown(callback[, active[, scene[, target]]])

Event of pressing down the right mouse button

new RectJS.MouseRightUp(callback[, active[, scene[, target]]])

Right mouse button release event

new RectJS.MouseWheelDown(callback[, active[, scene[, target]]])

Event of pressing down the middle mouse button (wheel)

new RectJS.MouseWheelUp(callback[, active[, scene[, target]]])

Middle mouse button (wheel) release event

new RectJS.MouseMove(callback[, active[, scene[, target]]])

Event of mouse movement

new RectJS.Wheel(callback[, active[, scene[, target]]])

Mouse wheel scrolling event

new RectJS.WheelUp(callback[, active[, scene[, target]]])

Mouse wheel scrolling up event

new RectJS.WheelDown(callback[, active[, scene[, target]]])

Mouse wheel scrolling down event

  • callback <function> - callback, takes the default event event object as a parameter
  • active <boolean> Defaut: true - event listener status
  • scene <object> (<RectJS.Scene>) Defaut: null - the scene of the listener. If = null - listener works on every scene.
  • target <DOM> Defaut: RectJS.eventDetector - DOM-element which handles the listener

new RectJS.MousePress(callback[, active[, scene]])

Left mouse button holding event.

  • callback <function> - callback, takes the default event event object as a parameter
  • active <boolean> Defaut: true - event listener status
  • scene <object> (<RectJS.Scene>) Defaut: null - the scene of the listener. If = null - listener works on every scene.

new RectJS.TouchStart(callback[, id[, active[, scene[, target]]]])

Touch start event

new RectJS.TouchEnd(callback[, id[, active[, scene[, target]]]])

Touch end event

new RectJS.TouchMove(callback[, id[, active[, scene[, target]]]])

Touch move event

  • callback <function> - callback, takes the default event event object as a parameter
  • id <number> Defaut: null - touch index. If = null - listener responds on all the touches
  • active <boolean> Defaut: true - event listener status
  • scene <object> (<RectJS.Scene>) Defaut: null - the scene of the listener. If = null - listener works on every scene.
  • target <DOM> Defaut: RectJS.eventDetector - DOM-element which handles the listener

new RectJS.KeyDown(callback[, key[, active[, scene[, target]]]])

Key down event

new RectJS.KeyUp(callback[, key[, active[, scene[, target]]]])

Key up event

  • callback <function> - callback, takes the default event event object as a parameter
  • key <number> Defaut: null - event.keyCode of the key. If = null - listener responds on all the keys
  • active <boolean> Defaut: true - event listener status
  • scene <object> (<RectJS.Scene>) Defaut: null - the scene of the listener. If = null - listener works on every scene.
  • target <DOM> Defaut: RectJS.eventDetector - DOM-element which handles the listener

new RectJS.KeyPress(callback[, key[, active[, scene]]])

Key press event

  • callback <function> - callback, takes the default event event object as a parameter
  • key <number> Defaut: null - event.keyCode of the key. If = null - listener responds on all the keys
  • active <boolean> Defaut: true - event listener status
  • scene <object> (<RectJS.Scene>) Defaut: null - the scene of the listener. If = null - listener works on every scene.

WARNING! All the constructors above return a RectJS.Event object.

Properties of RectJS.Event:

  • fnc <function> - listener callback
  • active <boolean> - listener status
  • scene <object> (<RectJS.Scene>) - the scene of the listener. If = null - listener works on every scene.
  • event <eventListener> - javascript event listener

Event.start()

Starts the listener.

Event.stop()

Stops the listener.

RectJS.KeyPressed(key)

  • key <number> Defaut: null - event.keyCode of the key. If = null - method responds of all the keys

Returns true if the given key is pressed.

new RectJS.Mouse()

Returns the mouse object.

Properties of RectJS.Mouse:

  • x <number> - x position on the screen
  • y <number> - y position on the screen

Mouse.get(layer[, scale[, parallax]])

  • layer <obejct> (<RectJS.Layer>) - layer to get the parallax and scaling values
  • scale <object> (<RectJS.Vector2>) Defaut: layer.scale - scaling
  • parallax <object> (<RectJS.Vector2>) Defaut: layer.parallax - percentage of parallax (0-100)

Returns the mouse position on a scene as a RectJS.Vector2 (according to camera offset, layer parallax and scaling values).

new RectJS.Touch();

Returns the reference to an array with all the touches.

Properties of a touch (RectJS.Touch)[index]:

  • x <number> - x position on the screen
  • y <number> - y position on the screen

Touch[index].get(layer)

  • layer <obejct> (<RectJS.Layer>) - слой

Returns the touch point on a scene as a RectJS.Vector2 (according to camera offset, layer parallax and scaling values).

WARNING! Event system in RE-5 isn't finished yet, some of the methods about could work not correctly.

Families and assets

new RectJS.Family()

Creation of an objects family.

Family.get(id)

  • id <string> - object id

Returns an object from the family.

Family.getByIndex(index)

  • index <number> - object index in the family (in order of addition)

Returns an object from the family by its index.

Family.isset(o)

  • o <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object

Checks if the object belongs to family.

Family.for(callback)

  • callback <function> - callback, takes an object as a parameter

Runs the callback for every single object in the family

Family.forNearTo(pos, callback[, dist])

  • pos <object> (<RectJS.Vector2>) - point
  • callback <function> - callback, takes an object as a parameter
  • dist <number> Default: 100 - radius of a area around the point

Runs the callback for every object within a certain radius around the given point.

Family.count()

Returns amount of objects in the family

Family.add(object)

  • object <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object

Adds an object to the family

Family.remove(object)

  • object <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object

Removes an object from the family

new RectJS.Asset(options)

  • options <obejct> - settings
    • type <string> - object type (constructor)
      • "Polygon" || rjs.Polygon - polygon
      • "Sprite || rjs.Sprite" - tinged rectangle or sprite
      • "Text" || rjs.Text - text object
      • <rjs.Asset> - you can make an asset based on the other one
    • the rest of parameters are the same as in object constructor with the given type

Returns the object constructor with the given settings:

new Asset(options)

  • options <object> - set of options as it is in default object constructor

WARNING! Properties given to the constructor overwrite asset's ones WARNING! When specifying colors or vectors in the asset there are only references in the object instance, so changing some properties of the vector or colors of one single object you can cause the same changes in all of the instances of this asset. To avoid this problem you can overwrite vectors or color when you change them.

Asset using example

// creation of an asset
const BOX = new rjs.Asset({
	type: rjs.Sprite,
	size: vec2(256, 256),
	color: rgb(100, 50, 255),
	private: {
		init: function () {
			// object's gonna rotate on 0 to 90 degrees after it's initialized
			this.angle = Math.random()*90;
		},
		name: "John"
	}
});

// create an object
const box1 = new BOX({
	pos: vec2(0, 0),
	layer: new_main
});

// create the second one
const box2 = new BOX({
	pos: vec2(500, 0),
	layer: new_main,
	private: {
		// specify another name
		name: "Jack"
	}
});

// change a property "x" of vector-typed parameter "size" of the second object
box2.size.x = 128;

log(box1.name);
// will get "John" (as it was in asset)

log(box2.name);
// will get "Jack" (as it was in instance)

log(box1.size.x);
// instead of default 256, we'll get 128,
// because we've changed a property of a vector attached to both "box1" and "box2"

log(box2.size.x);
// will get 128

Collision detection

RectJS.Collision(a, b)

  • a <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object
  • b <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object

Returns false if objects aren't collided, otherwise it returns an object with overlapping parameters

RectJS.getBoundingBox(object[, calc_angle])

  • object <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object
  • calc_angle <boolean> Default: true - considering an angle of the object

Returns a simplified bounding box as an plain javascript object either considering an angle nor not.

RectJS.AABB(a, b)

  • a <object> (RectJS.getBoundingBox) - first bounding box
  • b <object> (RectJS.getBoundingBox) - second bounding box

Returns true if bounding boxes are collided

RectJS.MouseOver(object)

  • object <object> (<RectJS.Polygon> | <RectJS.Sprite> | <RectJS.Text>) - game object

Returns true if mouse cursor is over the given object

Engine properties

RectJS.sourceHOST

<string> Default: "" - relative path to index.html, main.js, to engine and project folders

RectJS.engineSource

<string> Default: "Engine/" - path from RectJS.sourceHOST directory to the engine folder

RectJS.pluginSource

<string> Default: "Plugins/" - path from RectJS.sourceHOST directory to the plugins folder

RectJS.scenePath

<string> Default: "Scenes/" - path from RectJS.sourceHOST directory tp the scenes folder

RectJS.scriptPath

<string> Default: "Scripts/" - path from RectJS.sourceHOST directory to the scrips folder

RectJS.imagePath

<string> Default: "Sources/images/" - path from RectJS.sourceHOST directory to the images folder

RectJS.jsonPath

<string> Default: "Sources/json/" - path from RectJS.sourceHOST directory to the JSON folder

RectJS.fontPath

<string> Default: "Sources/fonts/" - path from RectJS.sourceHOST directory to the fonts folder

RectJS.audioPath

<string> Default: "Sources/audio/" - path from RectJS.sourceHOST directory tp the audio folder

RectJS.container

<DOM> - div wich contain all the rendering canvases and event handlers

RectJS.WebGL_Canvas

<DOM> - canvas for the WebGL graphics for sprites and polygons rendering

RectJS.ctx2D_Canvas

<DOM> - canvas for rendering text using CanvasRenderingContext2D

RectJS.eventDetector

<DOM> - div which is located on top of canvases and handles all the mouse and touch events

RectJS.gl

<object> - WebGL2RenderingContext

RectJS.ctx

<object> - CanvasRenderingContext2D

RectJS.client.w

<number> Default: 1920 - width of the viewport in units, which you can use to set the positions and sizes of game objects

RectJS.client.h

<number> Default: 1080 - height of the viewport in units, which you can use to set the positions and sizes of game objects

RectJS.resolution.w

<number> Default: 1536 - horizontal distance from the middle of canvas to its edge in real pixels

RectJS.resolution.h

<number> Default: 864 - vertical distance from the middle of canvas to its edge in real pixels

RectJS.canvas_width

<number> - current width of the canvases

RectJS.canvas_height

<number> - current height of the canvases

RectJS.con_width

<number> - current width of the container

RectJS.con_height

<number> - current height of the container

RectJS.CLEAR_COLOR

<object> (<rgba>) - background color of the canvas

RectJS.BG_COLOR

<object> (<rgba>) - background color of the page

RectJS.scenes

<object> - object contains all the scenes

RectJS.currentScene

<object> (<RectJS.Scene>) - reference to the current scene

RectJS.layers

<object> - object, contains all the layers of every scene

RectJS.sources

<object> - object, contains all external sources (except of fonts)

RectJS.images

<object> - object, contains all primary images

RectJS.textures

<object> - object, contains all the primary, secondary textures (created by cropping or looping the primery ones) and animations as well

RectJS.LOADER_MODE

<boolean> Default: false - loading screen mode

RectJS.sourceLoaded

<boolean> - absence of sources still unloaded

RectJS.timeStep

<number> (<integer>) Default: 1 - integer which represents amount of ticks per each requestAnimationFrame()

RectJS.animations

<object> - object, contains all the animations

RectJS.cameras

<object> - object, contains all the cameras

RectJS.currentCamera

<object> (<RectJS.camera>) - reference to the current camera

RectJS.waits

<array> - an array contains all the waits

RectJS.gameLoops

<array> - an array contains all the game loops

RectJS.CUT_FPS

<boolean> Default: false - FPS limitation mode (not recomended)

RectJS.MAX_FPS

<number> (<integer>) Default: 60 - mazimum FPS (in FPS limitation mode)

RectJS.SFRM

Single Frame Reqest Mode <boolean> Default: true - Single frame rendering request mode. If turn it off the rendering will run separately from main requestAnimationFrame. In rare cases it could increase the performance, but usually it decreases it and can couse some bugs.

RectJS.events

<array> - an array contains all the event listeners

RectJS.MousePressed

<boolean> - represents the status of left mouse button

RectJS.RightMousePressed

<boolean> - represents the status of right mouse button

RectJS.families

<array> - an array contains all the families

RectJS.sounds

<array> - an array contains all the sounds

RectJS.FPS

<number> - current FPS

RectJS.render

<function> - rendering function of graphic engine

RectJS.renderCore

<object> - class contains all the core methods of graphic engine

For compatibility with old plugins there's an old reference: RectJS.renderTools

RectJS.renderer

<object> - interface of graphics engine

RectJS.renderer.DRAWING_MODE

<string> Default: "mipmap" - texture interpolation mode

  • mipmap - mipmapping, generates loads automatically (lower performance)
  • linear - bilinear interpolation (recomended for higher performance)
  • pixel - without any interpolation (for pixelart projects)

RectJS.renderer.DCPF

Draw Call Per Frame <number> - amount of draw call per frame (text rendering doesn't count)

RectJS.renderer.DEBUG_MODE

<boolean> Default: false - rendering debug mode. Allows you to see how the rendering works step by step in purpose of making optimization

RectJS.renderer.DEBUG_CONSOLE_MODE

<boolean> Default: "false - this mode allows you to switch the frames with devTools

Plugins

What plugin consist of?

To work correctly the plugin should be in Plugins/plugin_name.rjs folder. Plugin consist at least of 2 files:

  • package.json - JSON with plugin settings and some important information about it.
{
	"name": "plugin_name",
	"version": "1.0.0",
	"main": "plugin.js",
	"settings": {
		
	}
}
  • plugin.js - its code.
(plugin) => {

	
	const exports = plugin.exports = {}; // plugin interface
	const rjs = plugin.engine; // reference to the engine (`RectJS`)
	const pack = plugin.pack; // object converted from package.json
	const sets = plugin.pack.settings; // settings from the package.json
	const params = plugin.params; // plugin initialization parameters
	const global = plugin.global; // properties of this object become global methods after its initialization
	const code = plugin.code; // function from file plugin.js (this code)
	
	// here's gotta be some plugin code

}

Including

new RectJS.Plugin(name[, ...params])

  • name <string> - a plugins name set in package.json
  • ...params - plugin initialization parameters. Described in its documentation.

Including a plugin. Returns an interface of the plugin plugin.exports.

WARNING! All the plugins should be included in the callback function of engine initializer if there's no different instructions in plugin's docs.

You can make plugin yourself or download already existing one on our website.

© GameDev United

re-5's People

Contributors

gdu-lord avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.