Skip to content

Light Class

SoJS coder edited this page Dec 12, 2023 · 4 revisions

Light Class

The Light class represents a light source within a 2D scene. It allows for the manipulation of the light's position, brightness, color, and interaction with other objects. This documentation outlines the properties and methods of the Light class. For a directional light, use the DirectionalLight class.

Constructor

Parameters

  • position (Array, required): An array representing the initial position of the light source.
  • diffuse (Number, required): How much the light is diffused across a scene.
  • strength (Number, optional): The strength of the light source. Default is 0.8.
  • color (Array, optional): An array representing the RGB color of the light. Default is [255, 255, 255].

Example Usage

const lightPosition = [100, 100];
const diffuse = 0.5;
const lightStrength = 0.8;
const lightColor = [255, 255, 255];

const myLight = new Light(lightPosition, diffuse, lightStrength, lightColor);

To add the light to the scene, use scene.addLight(myLight). (Ensure that scene.lighting is set to true)

Properties

  • point (Array): An array representing the current position of the light source.
  • diffuse (Boolean): How the light diffuses across a scene.
  • strength (Number): The strength of the light source.
  • color (Array): An array representing the RGB color of the light.
  • pinnedTo (Object): The object to which the light source is pinned, if any

Methods

pin(object)

Pins the light source to a specified object.

  • object (Object): The object to which the light source is pinned.
const myObject = /* some GameObject (Polygon/Sprite) */;
myLight.pin(myObject);

brighten(factor)

Increases the brightness of the light source by a specified factor.

  • factor (Number): The factor by which to increase the brightness.
const brightnessFactor = 1.2;
myLight.brighten(brightnessFactor);

dim(factor)

Decreases the brightness of the light source by a specified factor.

  • factor (Number): The factor by which to decrease the brightness.
const dimmingFactor = 0.8;
myLight.dim(dimmingFactor);

move(vector)

Moves the light source by a specified vector.

  • vector (Array): An array representing the movement vector. (x,y)
const movementVector = [10, 0];
myLight.move(movementVector);

moveToObject(object)

Moves the light source to the center of a specified object.

  • object (Object): The GameObject to which the light source should move.
const myObject = myPolygon;
myLight.moveToObject(myObject);
Clone this wiki locally