import { Bounds } from "./Entity" ; import { GameObjectInfo } from "./MapInfo" ; import { MapObjectJSON } from "*/level.json" ; import { METER_PER_PIXEL , SOUND_INTENSITY_MULTIPLIER } from "./constants" ; export function rnd ( minOrMax = 1 , max ? ) { if ( max != null ) { return minOrMax + Math . random ( ) * ( max - minOrMax ) ; } else { return Math . random ( ) * minOrMax ; } } export function rndInt ( minOrMax , max ? ) { if ( max != null ) { return Math . floor ( minOrMax + Math . random ( ) * ( max - minOrMax ) ) ; } else { return Math . floor ( Math . random ( ) * minOrMax ) ; } } export function rndItem ( array ) { const index = Math . floor ( Math . random ( ) * array . length ) ; return array [ index ] ; } export function timedRnd ( dt , averageDelay ) { let count = 0 ; let chance = dt - Math . random ( ) * averageDelay ; while ( chance > 0 ) { count ++ ; chance -= Math . random ( ) * averageDelay ; } return count ; } export function clamp ( v , min , max ) { return v < min ? min : v > max ? max : v ; } export function orientPow ( v , exp ) { if ( v < 0 ) { return - ( ( - v ) ** exp ) ; } else { return v ** exp ; } } const timeDelta = Date . now ( ) - performance . now ( ) ; export function now ( ) { return performance . now ( ) + timeDelta ; } export function shiftValue ( v , trg , inc ) { if ( v === trg ) { return trg ; } if ( v < trg ) { v += inc ; if ( v >= trg ) { return trg ; } } else { v -= inc ; if ( v <= trg ) { return trg ; } } return v ; } export async function sleep ( ms = 0 ) { await new Promise ( ( resolve ) => { setTimeout ( resolve , ms ) ; } ) ; } param array return export function shuffle < T > ( array ) { for ( let i = 1 ; i < array . length ; i ++ ) { const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ; if ( i !== j ) { const tmp = array [ i ] ; array [ i ] = array [ j ] ; array [ j ] = tmp ; } } return array ; } export function boundsFromMapObject ( o , margin = 0 ) { const width = o . width + ( margin * 2 ) ; const height = o . height + ( margin * 2 ) ; const x = o . x - margin ; const y = o . y + margin ; return { x , y , width , height } ; } export function isElectron ( ) { return ! ! navigator . userAgent . match ( /\belectron\b/i ) ; } /**
 * Figures out if development mode is enabled or not.
 */ export function isDev ( ) { if ( window . location . port === "8000" ) { return true ; } if ( ! ! window . location . search ) { return ! ! window . location . search . substr ( 1 ) . split ( "&" ) . find ( key => { if ( key . toLowerCase ( ) . startsWith ( "dev" ) ) { return key . length === 3 || key . endsWith ( "=true" ) ; } return false ; } ) ; } return false ; } param distance param intensity param maxVolume export function calculateVolume ( distance , maxVolume = 1 , intensity = 1 ) { return Math . max ( 0 , maxVolume - ( ( distance * METER_PER_PIXEL ) / ( SOUND_INTENSITY_MULTIPLIER * intensity ) ) ) ; } /** Factor to convert radians to degrees. */ const RAD_TO_DEG = 180 / Math . PI ; param degrees return export function radians ( degrees ) { return degrees / RAD_TO_DEG ; } param radians return export function degrees ( radians ) { return radians * RAD_TO_DEG ; } param degrees return export function normalizeRadians ( angle ) { const pi2 = Math . PI * 2 ; return ( ( angle % pi2 ) + pi2 ) % pi2 ; } param degrees return export function normalizeDegrees ( degrees ) { return ( ( degrees % 360 ) + 360 ) % 360 ; }