UNPKG

@osbjs/osbjs

Version:

a minimalist osu! storyboarding framework

91 lines (90 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Particles = void 0; const Core_1 = require("../Core"); const Math_1 = require("../Math"); class Particles extends Core_1.Component { /** * * @param path Path to the image * @param startTime times in milliseconds/timestamp indicate when the effect starts. * @param endTime times in milliseconds/timestamp indicate when the effect ends. * @param options Additional options. */ constructor(path, startTime, endTime, options) { super(); this.options = { duration: 2000, amount: 16, startPosition: new Core_1.OsbVector2(-107, 0), endPosition: new Core_1.OsbVector2(747, 480), axis: 'x', easing: Core_1.Easing.Linear, randomEasing: false, fadeInDuration: 200, fadeOutDuration: 200, color: new Core_1.OsbColor(1, 1, 1), startScale: 0.1, endScale: 1, randomScale: false, startRotation: 0, endRotation: 0, randomRotation: false, origin: Core_1.Origin.Center, additive: true, opacity: 1, }; this.path = path; this.startTime = startTime; this.endTime = endTime; this.options = { ...this.options, ...options }; } generate() { //#region extract options const { randomScale, opacity, duration, amount, startPosition, endPosition, axis, easing, randomEasing, fadeInDuration, fadeOutDuration, color, startScale, endScale, startRotation, endRotation, randomRotation, origin, additive, } = this.options; //#endregion const timestep = duration / amount; for (let startTime = this.startTime; startTime <= this.endTime - duration; Math.round((startTime += timestep))) { let endTime = startTime + duration; const spr = new Core_1.Sprite(this.path, Core_1.Layer.Background, origin); if (color.r < 1 || color.b < 1 || color.g < 1) spr.ColorAtTime(startTime, color); if (startScale == endScale && startScale != 1) spr.ScaleAtTime(startTime, startScale); if (startRotation == endRotation && startRotation != 0) spr.RotateAtTime(startTime, (0, Math_1.degToRad)(startRotation)); if (additive) spr.Parameter(startTime, startTime, Core_1.Parameter.AdditiveBlending); const eas = randomEasing ? Core_1.Easing[Core_1.Easing[(0, Math_1.randInt)(0, 34)]] : easing; const startX = axis == 'y' ? (0, Math_1.randInt)(startPosition.x, endPosition.x) : startPosition.x; const startY = axis == 'x' ? (0, Math_1.randInt)(startPosition.y, endPosition.y) : startPosition.y; const endX = axis == 'y' ? startX : endPosition.x; const endY = axis == 'x' ? startY : endPosition.y; spr.Move(startTime, endTime, new Core_1.OsbVector2(startX, startY), new Core_1.OsbVector2(endX, endY), eas); if (fadeInDuration > 0 || fadeOutDuration > 0) { let fadeInTime = startTime + fadeInDuration; let fadeOutTime = endTime - fadeOutDuration; if (fadeOutTime < fadeInTime) fadeInTime = fadeOutTime = (fadeInTime + fadeOutTime) / 2; spr.Fade(startTime, Math.max(startTime, fadeInTime), 0, opacity, eas); spr.Fade(Math.min(fadeOutTime, endTime), endTime, opacity, 0, eas); } else { spr.FadeAtTime(startTime, opacity); } if (startScale != endScale) { if (randomScale) spr.Scale(startTime, endTime, (0, Math_1.randFloat)(startScale, endScale), (0, Math_1.randFloat)(startScale, endScale), eas); else spr.Scale(startTime, endTime, startScale, endScale, eas); } if (startRotation != endRotation) if (randomRotation) spr.Rotate(startTime, endTime, (0, Math_1.degToRad)((0, Math_1.randFloat)(startRotation, endRotation)), (0, Math_1.degToRad)((0, Math_1.randFloat)(startRotation, endRotation)), eas); else spr.Rotate(startTime, endTime, (0, Math_1.degToRad)(startRotation), (0, Math_1.degToRad)(endRotation), eas); this.registerComponents(spr); } } } exports.Particles = Particles;