Added sound class, removed defect classes (Light)

This commit is contained in:
Arne van Iterson 2020-04-12 12:50:57 +02:00
parent 8aa3215f5f
commit 6d735bdfca
6 changed files with 82 additions and 58 deletions

View File

@ -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;

View File

@ -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;

27
lib/index.d.ts vendored
View File

@ -571,6 +571,33 @@ export class Camera extends Container<unknown> {
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}

View File

@ -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,

View File

@ -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
};

53
lib/sound/Sound.js Normal file
View File

@ -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;