UNPKG

@knopkem/little-game-engine-ts

Version:

LittleGameEngineTS - Tiny and Fast HTML5 Game Engine typescript port of LittleJS

335 lines (334 loc) 13.5 kB
/** * LittleJS Utility Classes and Functions * <br> - General purpose math library * <br> - Vector2 - fast, simple, easy 2D vector class * <br> - Color - holds a rgba color with some math functions * <br> - Timer - tracks time automatically * @namespace Utilities */ /** A shortcut to get Math.PI * @const * @memberof Utilities */ export declare const PI: number; /** True if running a Chromium based browser * @const * @memberof Utilities */ export declare const isChrome: Window; /** Returns absoulte value of value passed in * @param {Number} value * @return {Number} * @memberof Utilities */ export declare const abs: (a: number) => number; /** Returns the sign of value passed in * @param {Number} value * @return {Number} * @memberof Utilities */ export declare const sign: (a: number) => number; /** Returns lowest of two values passed in * @param {Number} valueA * @param {Number} valueB * @return {Number} * @memberof Utilities */ export declare const min: (a: number, b: number) => number; /** Returns highest of two values passed in * @param {Number} valueA * @param {Number} valueB * @return {Number} * @memberof Utilities */ export declare const max: (a: number, b: number) => number; /** Returns first parm modulo the second param, but adjusted so negative numbers work as expected * @param {Number} dividend * @param {Number} divisor * @return {Number} * @memberof Utilities */ export declare const mod: (a: number, b: number) => number; /** Clamps the value beween max and min * @param {Number} value * @param {Number} [max=1] * @param {Number} [min=0] * @return {Number} * @memberof Utilities */ export declare const clamp: (v: number, max?: number, min?: number) => number; /** Returns what percentage the value is between max and min * @param {Number} value * @param {Number} [max=1] * @param {Number} [min=0] * @return {Number} * @memberof Utilities */ export declare const percent: (v: number, max?: number, min?: number) => number; /** Linearly interpolates the percent value between max and min * @param {Number} percent * @param {Number} [max=1] * @param {Number} [min=0] * @return {Number} * @memberof Utilities */ export declare const lerp: (p: number, max?: number, min?: number) => number; /** Formats seconds to 00:00 style for display purposes * @param {Number} t - time in seconds * @return {String} * @memberof Utilities */ export declare const formatTime: (t: number) => string; /** Returns the nearest power of two not less then the value * @param {Number} value * @return {Number} * @memberof Utilities */ export declare const nearestPowerOfTwo: (v: number) => number; /** Applies smoothstep function to the percentage value * @param {Number} value * @return {Number} * @memberof Utilities */ export declare const smoothStep: (p: number) => number; /** Returns true if two axis aligned bounding boxes are overlapping * @param {Vector2} pointA - Center of box A * @param {Vector2} sizeA - Size of box A * @param {Vector2} pointB - Center of box B * @param {Vector2} sizeB - Size of box B * @return {Boolean} - True if overlapping * @memberof Utilities */ export declare const isOverlapping: (pA: any, sA: any, pB: any, sB: any) => number; /** Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default * @param {Number} [frequency=1] - Frequency of the wave in Hz * @param {Number} [amplitude=1] - Amplitude (max height) of the wave * @param {Number} [t=time] - Value to use for time of the wave * @return {Number} - Value waving between 0 and amplitude * @memberof Utilities */ export declare const wave: (frequency?: number, amplitude?: number, t?: number) => number; /** Random global functions * @namespace Random */ /** Returns a random value between the two values passed in * @param {Number} [valueA=1] * @param {Number} [valueB=0] * @return {Number} * @memberof Random */ export declare const rand: (a?: number, b?: number) => number; /** Returns a floored random value the two values passed in * @param {Number} [valueA=1] * @param {Number} [valueB=0] * @return {Number} * @memberof Random */ export declare const randInt: (a?: number, b?: number) => number; /** Randomly returns either -1 or 1 * @return {Number} * @memberof Random */ export declare const randSign: () => number; /** Returns a random Vector2 within a circular shape * @param {Number} [radius=1] * @param {Number} [minRadius=0] * @return {Vector2} * @memberof Random */ export declare const randInCircle: (radius?: number, minRadius?: number) => Vector2; /** Returns a random Vector2 with the passed in length * @param {Number} [length=1] * @return {Vector2} * @memberof Random */ export declare const randVector: (length?: number) => Vector2; /** Returns a random color between the two passed in colors, combine components if linear * @param {Color} [colorA=new Color(1,1,1,1)] * @param {Color} [colorB=new Color(0,0,0,1)] * @param {Boolean} [linear] * @return {Color} * @memberof Random */ export declare const randColor: (cA?: Color, cB?: Color, linear?: boolean) => Color; /** The seed used by the randSeeded function, should not be 0 * @memberof Random */ export declare let randSeed: number; /** Returns a seeded random value between the two values passed in using randSeed * @param {Number} [valueA=1] * @param {Number} [valueB=0] * @return {Number} * @memberof Random */ export declare const randSeeded: (a?: number, b?: number) => number; /** 2D Vector object with vector math library */ export declare class Vector2 { x: any; y: any; /** Create a 2D vector with the x and y passed in, can also be created with vec2() * @param {Number} [x=0] - x axis position * @param {Number} [y=0] - y axis position */ constructor(x?: number, y?: number); /** Returns a new vector that is a copy of this * @return {Vector2} */ copy(): Vector2; /** Returns a copy of this vector plus the vector passed in * @param {Vector2} vector * @return {Vector2} */ add(v: any): Vector2; /** Returns a copy of this vector minus the vector passed in * @param {Vector2} vector * @return {Vector2} */ subtract(v: any): Vector2; /** Returns a copy of this vector times the vector passed in * @param {Vector2} vector * @return {Vector2} */ multiply(v: any): Vector2; /** Returns a copy of this vector divided by the vector passed in * @param {Vector2} vector * @return {Vector2} */ divide(v: any): Vector2; /** Returns a copy of this vector scaled by the vector passed in * @param {Number} scale * @return {Vector2} */ scale(s: any): Vector2; /** Returns the length of this vector * @return {Number} */ length(): number; /** Returns the length of this vector squared * @return {Number} */ lengthSquared(): number; /** Returns the distance from this vector to vector passed in * @param {Vector2} vector * @return {Number} */ distance(v: any): number; /** Returns the distance squared from this vector to vector passed in * @param {Vector2} vector * @return {Number} */ distanceSquared(v: any): number; /** Returns a new vector in same direction as this one with the length passed in * @param {Number} [length=1] * @return {Vector2} */ normalize(length?: number): Vector2; /** Returns a new vector clamped to length passed in * @param {Number} [length=1] * @return {Vector2} */ clampLength(length?: number): Vector2; /** Returns the dot product of this and the vector passed in * @param {Vector2} vector * @return {Number} */ dot(v: any): number; /** Returns the cross product of this and the vector passed in * @param {Vector2} vector * @return {Number} */ cross(v: any): number; /** Returns the angle of this vector, up is angle 0 * @return {Number} */ angle(): number; /** Sets this vector with angle and length passed in * @param {Number} [angle=0] * @param {Number} [length=1] */ setAngle(a?: number, length?: number): this; /** Returns copy of this vector rotated by the angle passed in * @param {Number} angle * @return {Vector2} */ rotate(a: any): Vector2; /** Returns the integer direction of this vector, corrosponding to multiples of 90 degree rotation (0-3) * @return {Number} */ direction(): 1 | 0 | 2 | 3; /** Returns a copy of this vector that has been inverted * @return {Vector2} */ invert(): Vector2; /** Returns a copy of this vector with the axies flipped * @return {Vector2} */ flip(): Vector2; /** Returns a copy of this vector with each axis floored * @return {Vector2} */ floor(): Vector2; /** Returns the area this vector covers as a rectangle * @return {Number} */ area(): number; /** Returns a new vector that is p percent between this and the vector passed in * @param {Vector2} vector * @param {Number} percent * @return {Vector2} */ lerp(v: any, p: any): Vector2; /** Returns true if this vector is within the bounds of an array size passed in * @param {Vector2} arraySize * @return {Boolean} */ arrayCheck(arraySize: any): boolean; } /** Create a 2d vector, can take another Vector2 to copy, 2 scalars, or 1 scalar * @param {Number} [x=0] * @param {Number} [y=0] * @return {Vector2} * @memberof Utilities */ export declare function vec2(x?: any, y?: any): Vector2; /** Color object (red, green, blue, alpha) with some helpful functions */ export declare class Color { a: any; b: any; g: any; r: any; /** Create a color with the components passed in, white by default * @param {Number} [r=1] - red * @param {Number} [g=1] - green * @param {Number} [b=1] - blue * @param {Number} [a=1] - alpha */ constructor(r?: number, g?: number, b?: number, a?: number); /** Returns a new color that is a copy of this * @return {Color} */ copy(): Color; /** Returns a copy of this color plus the color passed in * @param {Color} color * @return {Color} */ add(c: any): Color; /** Returns a copy of this color minus the color passed in * @param {Color} color * @return {Color} */ subtract(c: any): Color; /** Returns a copy of this color times the color passed in * @param {Color} color * @return {Color} */ multiply(c: any): Color; /** Returns a copy of this color divided by the color passed in * @param {Color} color * @return {Color} */ divide(c: any): Color; /** Returns a copy of this color scaled by the value passed in, alpha can be scaled separately * @param {Number} scale * @param {Number} [alphaScale=scale] * @return {Color} */ scale(s: any, a?: any): Color; /** Returns a copy of this color clamped to the valid range between 0 and 1 * @return {Color} */ clamp(): Color; /** Returns a new color that is p percent between this and the color passed in * @param {Color} color * @param {Number} percent * @return {Color} */ lerp(c: any, p: any): Color; /** Sets this color given a hue, saturation, lightness , and alpha * @param {Number} [hue=0] * @param {Number} [saturation=0] * @param {Number} [lightness=1] * @param {Number} [alpha=1] * @return {Color} */ setHSLA(h?: number, s?: number, l?: number, a?: number): this; /** Returns a new color that has each component randomly adjusted * @param {Number} [amount=.05] * @param {Number} [alphaAmount=0] * @return {Color} */ mutate(amount?: number, alphaAmount?: number): Color; /** Returns this color expressed as an rgba string * @return {String} */ rgba(): string; /** Returns this color expressed as 32 bit integer value * @return {Number} */ rgbaInt(): number; } /** Timer object tracks how long has passed since it was set */ export declare class Timer { setTime: any; time: any; /** Create a timer object set time passed in * @param {Number} [timeLeft] - How much time left before the timer elapses in seconds */ constructor(timeLeft: any); /** Set the timer with seconds passed in * @param {Number} [timeLeft=0] - How much time left before the timer is elapsed in seconds */ set(timeLeft?: number): void; /** Unset the timer */ unset(): void; /** Returns true if set * @return {Boolean} */ isSet(): boolean; /** Returns true if set and has not elapsed * @return {Boolean} */ active(): boolean; /** Returns true if set and elapsed * @return {Boolean} */ elapsed(): boolean; /** Get how long since elapsed, returns 0 if not set * @return {Number} */ get(): number; /** Get percentage elapsed based on time it was set to, returns 0 if not set * @return {Number} */ getPercent(): number; }