diff --git a/lib/Lighting.js b/lib/Lighting.js new file mode 100644 index 0000000..096c032 --- /dev/null +++ b/lib/Lighting.js @@ -0,0 +1,24 @@ +const Lightsource = require("./Lightsource.js"); +const Container = require("./Container.js"); + +class Lighting extends Container { + constructor(x, y, mapW, mapH, sources, style = { start: "rgba(0, 0, 0, 0)", stop: "rgba(0, 0, 0, 0.5)", radius: 75 }) { + super(); + this.pos = { x, y }; + this.w = mapW; + this.h = mapH; + + this.style = style; + if (sources.length > 1) { + this.style.stop = `rgba(0, 0, 0, ${1 - Math.round(Math.pow(0.25, 1 / sources.length) * 100) / 100})`; + console.log(this.style.stop); + } + + for (let index = 0; index < sources.length; index++) { + const element = sources[index]; + this.children.push(new Lightsource(element.x, element.y, x, y, mapW, mapH, this.style)); + } + } +} + +module.exports = Lighting; \ No newline at end of file diff --git a/lib/Lightsource.js b/lib/Lightsource.js index 43f533a..bd3146e 100644 --- a/lib/Lightsource.js +++ b/lib/Lightsource.js @@ -1,9 +1,12 @@ class Lightsource { - constructor(radius, style = { start: "rgba(255,255,255,0.5)", stop: "rgba(0,0,0,0.9)" }) { - this.pos = { x: 0, y: 0 }; - this.radius = radius; + constructor(x, y, mapX, mapY, mapW, mapH, style = { start: "rgba(0, 0, 0, 0)", stop: "rgba(0, 0, 0, 0.5)", radius: 100 }) { + this.pos = { mapX, mapY }; + this.radPos = { x, y }; + this.mapW = mapW; + this.mapH = mapH; + this.radius = style.radius; this.style = style; } } - + module.exports = Lightsource; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 5d974b9..fdbacc0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ var AnimManager = require("./AnimManager.js"), deadInTracks = require("./movement/deadInTracks.js"), wallslide = require("./movement/wallslide.js"), Rect = require("./Rect.js"), + Lighting = require("./Lighting.js"), Lightsource = require("./Lightsource.js"), KeyControls = require("./controls/KeyControls.js"), MouseControls = require("./controls/MouseControls.js"), @@ -32,6 +33,7 @@ module.exports = { deadInTracks, wallslide, Rect, + Lighting, Lightsource, KeyControls, MouseControls,