diff --git a/lib/Lighting.js b/lib/Lighting.js deleted file mode 100644 index 096c032..0000000 --- a/lib/Lighting.js +++ /dev/null @@ -1,24 +0,0 @@ -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 deleted file mode 100644 index bd3146e..0000000 --- a/lib/Lightsource.js +++ /dev/null @@ -1,12 +0,0 @@ -class Lightsource { - 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.d.ts b/lib/index.d.ts index c24a601..fa014e6 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -571,6 +571,33 @@ export class Camera extends Container { update(t: number, dt: number): void; } +export class Sound { + + src: string; + options: { + loop: boolean, + volume: number + }; + + /** + * Initiates HTML5 audio element for source audio file with control methods + * @param src Source audio file + * @param options Play settings + */ + constructor(src: String, options = {}); + + /** + * Starts playing the audio file + * @param overrides sets options for playing the sound using different setting as defined in `constructor()` + */ + play(overrides = {}): void; + + /** + * Stops playing the audio file + */ + stop(): void; +} + interface NumericalEntity {pos: Coordinates, w: number, h: number} interface NumericalEntityWithHitbox extends NumericalEntity {hitBox: NumericalEntity} diff --git a/lib/index.js b/lib/index.js index fdbacc0..0be923d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,11 +5,9 @@ var AnimManager = require("./AnimManager.js"), Game = require("./Game.js"), math = require("./utilities/math.js"), entity = require("./utilities/entity.js"), - deadInTracks = require("./movement/deadInTracks.js"), wallslide = require("./movement/wallslide.js"), + Sound = require("./sound/Sound.js"), Rect = require("./Rect.js"), - Lighting = require("./Lighting.js"), - Lightsource = require("./Lightsource.js"), KeyControls = require("./controls/KeyControls.js"), MouseControls = require("./controls/MouseControls.js"), Sprite = require("./Sprite.js"), @@ -30,11 +28,9 @@ module.exports = { Game, math, entity, - deadInTracks, wallslide, + Sound, Rect, - Lighting, - Lightsource, KeyControls, MouseControls, Sprite, diff --git a/lib/movement/deadInTracks.js b/lib/movement/deadInTracks.js deleted file mode 100644 index cc51fbf..0000000 --- a/lib/movement/deadInTracks.js +++ /dev/null @@ -1,16 +0,0 @@ -const entity = require("../utilities/entity"); - -function deadInTracks(ent, map, x = 0, y = 0) { - const bounds = entity.bounds(ent); - const tiles = map.tilesAtCorners(bounds, x, y); - const walks = tiles.map(t => t && t.frame.walkable); - const blocked = walks.some(w => !w); - if (blocked) { - x = 0, - y = 0; - } - return { x, y }; -} -module.exports = { - deadInTracks -}; \ No newline at end of file diff --git a/lib/sound/Sound.js b/lib/sound/Sound.js new file mode 100644 index 0000000..8191190 --- /dev/null +++ b/lib/sound/Sound.js @@ -0,0 +1,53 @@ +class Sound { + constructor(src, options = {}) { + this.playing = false; + this.src = src; + this.options = Object.assign({ volume: 1 }, options); + + // Configure audio element + const audio = new Audio(); + audio.src = src; + if (options.loop) { + audio.loop = true; + } + audio.addEventListener( + "error", + () => { + throw Error(`Error loading audio: ${src}`); + }, + false + ); + audio.addEventListener( + "ended", + () => { + this.playing = false; + }, + false + ); + this.audio = audio; + } + + play(overrides) { + const { audio, options } = this; + const opts = Object.assign({ time: 0 }, options, overrides); + audio.volume = opts.volume; + audio.currentTime = opts.time; + audio.play(); + this.playing = true; + } + + stop() { + this.audio.pause(); + this.playing = false; + } + + get volume() { + return this.audio.volume; + } + + set volume(volume) { + this.options.volume = this.audio.volume = volume; + } +} + +module.exports = Sound; \ No newline at end of file