mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Get rid of 'builtins' file since its useless and causes performance issues
This commit is contained in:
@@ -4,8 +4,6 @@ import { Signal } from "./signal";
|
||||
import BackgroundAnimationFrameEmitterWorker from "../webworkers/background_animation_frame_emittter.worker";
|
||||
|
||||
import { createLogger } from "./logging";
|
||||
import { performanceNow } from "./builtins";
|
||||
|
||||
const logger = createLogger("animation_frame");
|
||||
|
||||
const maxDtMs = 1000;
|
||||
@@ -34,7 +32,7 @@ export class AnimationFrame {
|
||||
* @param {MessageEvent} event
|
||||
*/
|
||||
handleBackgroundTick(event) {
|
||||
const time = performanceNow();
|
||||
const time = performance.now();
|
||||
if (!this.bgLastTime) {
|
||||
// First update, first delta is always 16ms
|
||||
this.bgFrameEmitted.dispatch(1000 / 60);
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
import CompressionWorker from "../webworkers/compression.worker";
|
||||
import { createLogger } from "./logging";
|
||||
import { compressX64 } from "./lzstring";
|
||||
import { performanceNow, JSON_stringify } from "./builtins";
|
||||
|
||||
const logger = createLogger("async_compression");
|
||||
|
||||
export let compressionPrefix = String.fromCodePoint(1);
|
||||
@@ -53,7 +51,7 @@ class AsynCompression {
|
||||
return;
|
||||
}
|
||||
|
||||
const duration = performanceNow() - jobData.startTime;
|
||||
const duration = performance.now() - jobData.startTime;
|
||||
// log(this, "Got response from worker within", duration.toFixed(2), "ms");
|
||||
const resolver = jobData.resolver;
|
||||
delete this.currentJobs[jobId];
|
||||
@@ -100,7 +98,7 @@ class AsynCompression {
|
||||
this.currentJobs[jobId] = {
|
||||
errorHandler,
|
||||
resolver: resolve,
|
||||
startTime: performanceNow(),
|
||||
startTime: performance.now(),
|
||||
};
|
||||
this.worker.postMessage({ jobId, job, data });
|
||||
});
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { globalConfig } from "./config";
|
||||
import { Math_max, Math_floor, Math_abs } from "./builtins";
|
||||
import { fastArrayDelete } from "./utils";
|
||||
import { createLogger } from "./logging";
|
||||
|
||||
@@ -70,8 +69,8 @@ export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, labe
|
||||
}
|
||||
if (w < 1 || h < 1) {
|
||||
logger.error("Offscreen buffer size < 0:", w, "x", h);
|
||||
w = Math_max(1, w);
|
||||
h = Math_max(1, h);
|
||||
w = Math.max(1, w);
|
||||
h = Math.max(1, h);
|
||||
}
|
||||
|
||||
const recommendedSize = 1024 * 1024;
|
||||
@@ -79,8 +78,8 @@ export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, labe
|
||||
logger.warn("Creating huge buffer:", w, "x", h, "with label", label);
|
||||
}
|
||||
|
||||
w = Math_floor(w);
|
||||
h = Math_floor(h);
|
||||
w = Math.floor(w);
|
||||
h = Math.floor(h);
|
||||
|
||||
let canvas = null;
|
||||
let context = null;
|
||||
@@ -103,7 +102,7 @@ export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, labe
|
||||
}
|
||||
|
||||
const otherPixels = useableCanvas.width * useableCanvas.height;
|
||||
const diff = Math_abs(otherPixels - currentPixels);
|
||||
const diff = Math.abs(otherPixels - currentPixels);
|
||||
if (diff < bestMatchingPixelsDiff) {
|
||||
bestMatchingPixelsDiff = diff;
|
||||
bestMatchingOne = {
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
// Store the original version of all builtins to prevent modification
|
||||
|
||||
export const JSON_stringify = JSON.stringify.bind(JSON);
|
||||
export const JSON_parse = JSON.parse.bind(JSON);
|
||||
|
||||
export function Math_radians(degrees) {
|
||||
return (degrees * Math_PI) / 180.0;
|
||||
}
|
||||
|
||||
export function Math_degrees(radians) {
|
||||
return (radians * 180.0) / Math_PI;
|
||||
}
|
||||
|
||||
export const performanceNow = performance.now.bind(performance);
|
||||
|
||||
export const Math_abs = Math.abs.bind(Math);
|
||||
export const Math_ceil = Math.ceil.bind(Math);
|
||||
export const Math_floor = Math.floor.bind(Math);
|
||||
export const Math_round = Math.round.bind(Math);
|
||||
export const Math_sign = Math.sign.bind(Math);
|
||||
export const Math_sqrt = Math.sqrt.bind(Math);
|
||||
export const Math_min = Math.min.bind(Math);
|
||||
export const Math_max = Math.max.bind(Math);
|
||||
export const Math_sin = Math.sin.bind(Math);
|
||||
export const Math_cos = Math.cos.bind(Math);
|
||||
export const Math_tan = Math.tan.bind(Math);
|
||||
export const Math_hypot = Math.hypot.bind(Math);
|
||||
export const Math_atan2 = Math.atan2.bind(Math);
|
||||
export const Math_pow = Math.pow.bind(Math);
|
||||
export const Math_random = Math.random.bind(Math);
|
||||
export const Math_exp = Math.exp.bind(Math);
|
||||
export const Math_log10 = Math.log10.bind(Math);
|
||||
|
||||
export const Math_PI = 3.1415926;
|
||||
@@ -1,4 +1,3 @@
|
||||
import { performanceNow } from "../core/builtins";
|
||||
import { createLogger } from "../core/logging";
|
||||
import { Signal } from "../core/signal";
|
||||
import { fastArrayDelete, fastArrayDeleteValueIfContained } from "./utils";
|
||||
@@ -246,7 +245,7 @@ export class ClickDetector {
|
||||
}
|
||||
|
||||
if (window.TouchEvent && event instanceof TouchEvent) {
|
||||
clickDetectorGlobals.lastTouchTime = performanceNow();
|
||||
clickDetectorGlobals.lastTouchTime = performance.now();
|
||||
|
||||
// console.log("Got touches", event.targetTouches.length, "vs", expectedRemainingTouches);
|
||||
if (event.targetTouches.length !== expectedRemainingTouches) {
|
||||
@@ -255,7 +254,7 @@ export class ClickDetector {
|
||||
}
|
||||
|
||||
if (event instanceof MouseEvent) {
|
||||
if (performanceNow() - clickDetectorGlobals.lastTouchTime < 1000.0) {
|
||||
if (performance.now() - clickDetectorGlobals.lastTouchTime < 1000.0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -340,7 +339,7 @@ export class ClickDetector {
|
||||
|
||||
// Store where the touch started
|
||||
this.clickDownPosition = position;
|
||||
this.clickStartTime = performanceNow();
|
||||
this.clickStartTime = performance.now();
|
||||
this.touchstartSimple.dispatch(this.clickDownPosition.x, this.clickDownPosition.y);
|
||||
|
||||
// If we are not currently within a click, register it
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { globalConfig } from "../core/config";
|
||||
import { Math_ceil, Math_floor, Math_round } from "./builtins";
|
||||
import { round1Digit, round2Digits } from "./utils";
|
||||
|
||||
/**
|
||||
@@ -23,7 +22,7 @@ export function smoothenDpi(dpi) {
|
||||
} else if (dpi < 1) {
|
||||
return round1Digit(dpi);
|
||||
} else {
|
||||
return round1Digit(Math_round(dpi / 0.5) * 0.5);
|
||||
return round1Digit(Math.round(dpi / 0.5) * 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,11 +58,11 @@ export function prepareHighDPIContext(context, smooth = true) {
|
||||
export function resizeHighDPICanvas(canvas, w, h, smooth = true) {
|
||||
const dpi = getDeviceDPI();
|
||||
|
||||
const wNumber = Math_floor(w);
|
||||
const hNumber = Math_floor(h);
|
||||
const wNumber = Math.floor(w);
|
||||
const hNumber = Math.floor(h);
|
||||
|
||||
const targetW = Math_floor(wNumber * dpi);
|
||||
const targetH = Math_floor(hNumber * dpi);
|
||||
const targetW = Math.floor(wNumber * dpi);
|
||||
const targetH = Math.floor(hNumber * dpi);
|
||||
|
||||
if (targetW !== canvas.width || targetH !== canvas.height) {
|
||||
// console.log("Resize Canvas from", canvas.width, canvas.height, "to", targetW, targetH)
|
||||
@@ -82,8 +81,8 @@ export function resizeHighDPICanvas(canvas, w, h, smooth = true) {
|
||||
* @param {number} h
|
||||
*/
|
||||
export function resizeCanvas(canvas, w, h, setStyle = true) {
|
||||
const actualW = Math_ceil(w);
|
||||
const actualH = Math_ceil(h);
|
||||
const actualW = Math.ceil(w);
|
||||
const actualH = Math.ceil(h);
|
||||
if (actualW !== canvas.width || actualH !== canvas.height) {
|
||||
canvas.width = actualW;
|
||||
canvas.height = actualH;
|
||||
@@ -103,8 +102,8 @@ export function resizeCanvas(canvas, w, h, setStyle = true) {
|
||||
* @param {number} h
|
||||
*/
|
||||
export function resizeCanvasAndClear(canvas, context, w, h) {
|
||||
const actualW = Math_ceil(w);
|
||||
const actualH = Math_ceil(h);
|
||||
const actualW = Math.ceil(w);
|
||||
const actualH = Math.ceil(h);
|
||||
if (actualW !== canvas.width || actualH !== canvas.height) {
|
||||
canvas.width = actualW;
|
||||
canvas.height = actualH;
|
||||
|
||||
@@ -3,7 +3,6 @@ import { AtlasSprite } from "./sprites";
|
||||
import { DrawParameters } from "./draw_parameters";
|
||||
/* typehints:end */
|
||||
|
||||
import { Math_PI, Math_round, Math_atan2, Math_hypot, Math_floor } from "./builtins";
|
||||
import { Vector } from "./vector";
|
||||
import { Rectangle } from "./rectangle";
|
||||
import { createLogger } from "./logging";
|
||||
@@ -40,7 +39,7 @@ export function initDrawUtils() {
|
||||
return;
|
||||
}
|
||||
this.beginPath();
|
||||
this.arc(x, y, r, 0, 2.0 * Math_PI);
|
||||
this.arc(x, y, r, 0, 2.0 * Math.PI);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -68,8 +67,8 @@ export function drawLineFast(context, { x1, x2, y1, y2, color = null, lineSize =
|
||||
const dX = x2 - x1;
|
||||
const dY = y2 - y1;
|
||||
|
||||
const angle = Math_atan2(dY, dX) + 0.0 * Math_PI;
|
||||
const len = Math_hypot(dX, dY);
|
||||
const angle = Math.atan2(dY, dX) + 0.0 * Math.PI;
|
||||
const len = Math.hypot(dX, dY);
|
||||
|
||||
context.translate(x1, y1);
|
||||
context.rotate(angle);
|
||||
@@ -247,7 +246,7 @@ export function hslToRgb(h, s, l) {
|
||||
b = hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
|
||||
return [Math_round(r * 255), Math_round(g * 255), Math_round(b * 255)];
|
||||
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
|
||||
}
|
||||
|
||||
export function wrapText(context, text, x, y, maxWidth, lineHeight, stroke = false) {
|
||||
@@ -306,7 +305,7 @@ export function rotateTrapezRightFaced(x, y, w, h, leftHeight, angle) {
|
||||
*/
|
||||
export function mapClampedColorValueToHex(value) {
|
||||
const hex = "0123456789abcdef";
|
||||
return hex[Math_floor(value / 16)] + hex[value % 16];
|
||||
return hex[Math.floor(value / 16)] + hex[value % 16];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { globalConfig } from "../core/config";
|
||||
import { Math_floor, performanceNow } from "./builtins";
|
||||
|
||||
const circularJson = require("circular-json");
|
||||
|
||||
/*
|
||||
@@ -231,7 +229,7 @@ function logInternal(handle, consoleMethod, args) {
|
||||
const labelColor = handle && handle.LOG_LABEL_COLOR ? handle.LOG_LABEL_COLOR : "#aaa";
|
||||
|
||||
if (G_IS_DEV && globalConfig.debug.logTimestamps) {
|
||||
const timestamp = "⏱ %c" + (Math_floor(performanceNow()) + "").padEnd(6, " ") + "";
|
||||
const timestamp = "⏱ %c" + (Math.floor(performance.now()) + "").padEnd(6, " ") + "";
|
||||
consoleMethod.call(
|
||||
console,
|
||||
timestamp + " %c" + context,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
function mathPolyfills() {
|
||||
// Converts from degrees to radians.
|
||||
Math.radians = function (degrees) {
|
||||
return (degrees * Math_PI) / 180.0;
|
||||
return (degrees * Math.PI) / 180.0;
|
||||
};
|
||||
|
||||
// Converts from radians to degrees.
|
||||
Math.degrees = function (radians) {
|
||||
return (radians * 180.0) / Math_PI;
|
||||
return (radians * 180.0) / Math.PI;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -98,8 +98,6 @@ function initExtensions() {
|
||||
|
||||
// Fetch polyfill
|
||||
import "whatwg-fetch";
|
||||
import { Math_PI } from "./builtins";
|
||||
|
||||
// Other polyfills
|
||||
initPolyfills();
|
||||
initExtensions();
|
||||
|
||||
@@ -7,7 +7,6 @@ import { createLogger } from "./logging";
|
||||
import { FILE_NOT_FOUND } from "../platform/storage";
|
||||
import { accessNestedPropertyReverse } from "./utils";
|
||||
import { IS_DEBUG, globalConfig } from "./config";
|
||||
import { JSON_stringify, JSON_parse } from "./builtins";
|
||||
import { ExplainedResult } from "./explained_result";
|
||||
import { decompressX64, compressX64 } from ".//lzstring";
|
||||
import { asyncCompressor, compressionPrefix } from "./async_compression";
|
||||
@@ -84,7 +83,7 @@ export class ReadWriteProxy {
|
||||
* @param {object} obj
|
||||
*/
|
||||
static serializeObject(obj) {
|
||||
const jsonString = JSON_stringify(compressObject(obj));
|
||||
const jsonString = JSON.stringify(compressObject(obj));
|
||||
const checksum = sha1(jsonString + salt);
|
||||
return compressionPrefix + compressX64(checksum + jsonString);
|
||||
}
|
||||
@@ -129,7 +128,7 @@ export class ReadWriteProxy {
|
||||
logger.error("Tried to write invalid data to", this.filename, "reason:", verifyResult.reason);
|
||||
return Promise.reject(verifyResult.reason);
|
||||
}
|
||||
const jsonString = JSON_stringify(compressObject(this.currentData));
|
||||
const jsonString = JSON.stringify(compressObject(this.currentData));
|
||||
|
||||
// if (!this.app.pageVisible || this.app.unloaded) {
|
||||
// logger.log("Saving file sync because in unload handler");
|
||||
@@ -189,7 +188,7 @@ export class ReadWriteProxy {
|
||||
.then(rawData => {
|
||||
if (rawData == null) {
|
||||
// So, the file has not been found, use default data
|
||||
return JSON_stringify(compressObject(this.getDefaultData()));
|
||||
return JSON.stringify(compressObject(this.getDefaultData()));
|
||||
}
|
||||
|
||||
if (rawData.startsWith(compressionPrefix)) {
|
||||
@@ -223,7 +222,7 @@ export class ReadWriteProxy {
|
||||
// Parse JSON, this could throw but thats fine
|
||||
.then(res => {
|
||||
try {
|
||||
return JSON_parse(res);
|
||||
return JSON.parse(res);
|
||||
} catch (ex) {
|
||||
logger.error(
|
||||
"Failed to parse file content of",
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { globalConfig } from "./config";
|
||||
import { Math_ceil, Math_floor, Math_max, Math_min } from "./builtins";
|
||||
import { clamp, epsilonCompare, round2Digits } from "./utils";
|
||||
import { Vector } from "./vector";
|
||||
|
||||
@@ -38,10 +37,10 @@ export class Rectangle {
|
||||
* @param {Vector} p2
|
||||
*/
|
||||
static fromTwoPoints(p1, p2) {
|
||||
const left = Math_min(p1.x, p2.x);
|
||||
const top = Math_min(p1.y, p2.y);
|
||||
const right = Math_max(p1.x, p2.x);
|
||||
const bottom = Math_max(p1.y, p2.y);
|
||||
const left = Math.min(p1.x, p2.x);
|
||||
const top = Math.min(p1.y, p2.y);
|
||||
const right = Math.max(p1.x, p2.x);
|
||||
const bottom = Math.max(p1.y, p2.y);
|
||||
return new Rectangle(left, top, right - left, bottom - top);
|
||||
}
|
||||
|
||||
@@ -67,10 +66,10 @@ export class Rectangle {
|
||||
let maxY = -1e10;
|
||||
for (let i = 0; i < points.length; ++i) {
|
||||
const rotated = points[i].rotated(angle);
|
||||
minX = Math_min(minX, rotated.x);
|
||||
minY = Math_min(minY, rotated.y);
|
||||
maxX = Math_max(maxX, rotated.x);
|
||||
maxY = Math_max(maxY, rotated.y);
|
||||
minX = Math.min(minX, rotated.x);
|
||||
minY = Math.min(minY, rotated.y);
|
||||
maxX = Math.max(maxX, rotated.x);
|
||||
maxY = Math.max(maxY, rotated.y);
|
||||
}
|
||||
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
@@ -98,10 +97,10 @@ export class Rectangle {
|
||||
this.w = halfWidth * 2;
|
||||
this.h = halfHeight * 2;
|
||||
} else {
|
||||
this.setLeft(Math_min(this.x, centerX - halfWidth));
|
||||
this.setRight(Math_max(this.right(), centerX + halfWidth));
|
||||
this.setTop(Math_min(this.y, centerY - halfHeight));
|
||||
this.setBottom(Math_max(this.bottom(), centerY + halfHeight));
|
||||
this.setLeft(Math.min(this.x, centerX - halfWidth));
|
||||
this.setRight(Math.max(this.right(), centerX + halfWidth));
|
||||
this.setTop(Math.min(this.y, centerY - halfHeight));
|
||||
this.setBottom(Math.max(this.bottom(), centerY + halfHeight));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,11 +325,11 @@ export class Rectangle {
|
||||
* @returns {Rectangle|null}
|
||||
*/
|
||||
getIntersection(rect) {
|
||||
const left = Math_max(this.x, rect.x);
|
||||
const top = Math_max(this.y, rect.y);
|
||||
const left = Math.max(this.x, rect.x);
|
||||
const top = Math.max(this.y, rect.y);
|
||||
|
||||
const right = Math_min(this.x + this.w, rect.x + rect.w);
|
||||
const bottom = Math_min(this.y + this.h, rect.y + rect.h);
|
||||
const right = Math.min(this.x + this.w, rect.x + rect.w);
|
||||
const bottom = Math.min(this.y + this.h, rect.y + rect.h);
|
||||
|
||||
if (right <= left || bottom <= top) {
|
||||
return null;
|
||||
@@ -354,10 +353,10 @@ export class Rectangle {
|
||||
}
|
||||
|
||||
// Find contained area
|
||||
const left = Math_min(this.x, rect.x);
|
||||
const top = Math_min(this.y, rect.y);
|
||||
const right = Math_max(this.right(), rect.right());
|
||||
const bottom = Math_max(this.bottom(), rect.bottom());
|
||||
const left = Math.min(this.x, rect.x);
|
||||
const top = Math.min(this.y, rect.y);
|
||||
const right = Math.max(this.right(), rect.right());
|
||||
const bottom = Math.max(this.bottom(), rect.bottom());
|
||||
|
||||
return Rectangle.fromTRBL(top, right, bottom, left);
|
||||
}
|
||||
@@ -388,10 +387,10 @@ export class Rectangle {
|
||||
if (includeHalfTiles) {
|
||||
// Increase rectangle size
|
||||
scaled = Rectangle.fromTRBL(
|
||||
Math_floor(scaled.y),
|
||||
Math_ceil(scaled.right()),
|
||||
Math_ceil(scaled.bottom()),
|
||||
Math_floor(scaled.x)
|
||||
Math.floor(scaled.y),
|
||||
Math.ceil(scaled.right()),
|
||||
Math.ceil(scaled.bottom()),
|
||||
Math.floor(scaled.x)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { Math_random } from "./builtins";
|
||||
|
||||
// ALEA RNG
|
||||
|
||||
function Mash() {
|
||||
@@ -72,7 +70,7 @@ export class RandomNumberGenerator {
|
||||
* @param {number|string=} seed
|
||||
*/
|
||||
constructor(seed) {
|
||||
this.internalRng = makeNewRng(seed || Math_random());
|
||||
this.internalRng = makeNewRng(seed || Math.random());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +78,7 @@ export class RandomNumberGenerator {
|
||||
* @param {number|string} seed
|
||||
*/
|
||||
reseed(seed) {
|
||||
this.internalRng = makeNewRng(seed || Math_random());
|
||||
this.internalRng = makeNewRng(seed || Math.random());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { DrawParameters } from "./draw_parameters";
|
||||
import { Math_floor } from "./builtins";
|
||||
import { Rectangle } from "./rectangle";
|
||||
import { epsilonCompare, round3Digits } from "./utils";
|
||||
|
||||
@@ -195,20 +194,20 @@ export class AtlasSprite extends BaseSprite {
|
||||
link.atlas,
|
||||
|
||||
// atlas src pos
|
||||
Math_floor(srcX),
|
||||
Math_floor(srcY),
|
||||
Math.floor(srcX),
|
||||
Math.floor(srcY),
|
||||
|
||||
// atlas src size
|
||||
Math_floor(srcW),
|
||||
Math_floor(srcH),
|
||||
Math.floor(srcW),
|
||||
Math.floor(srcH),
|
||||
|
||||
// dest pos
|
||||
Math_floor(destX),
|
||||
Math_floor(destY),
|
||||
Math.floor(destX),
|
||||
Math.floor(destY),
|
||||
|
||||
// dest size
|
||||
Math_floor(destW),
|
||||
Math_floor(destH)
|
||||
Math.floor(destW),
|
||||
Math.floor(destH)
|
||||
);
|
||||
} else {
|
||||
parameters.context.drawImage(
|
||||
|
||||
@@ -1,19 +1,4 @@
|
||||
import { globalConfig, IS_DEBUG } from "./config";
|
||||
import {
|
||||
Math_abs,
|
||||
Math_atan2,
|
||||
Math_ceil,
|
||||
Math_floor,
|
||||
Math_log10,
|
||||
Math_max,
|
||||
Math_min,
|
||||
Math_PI,
|
||||
Math_pow,
|
||||
Math_random,
|
||||
Math_round,
|
||||
Math_sin,
|
||||
performanceNow,
|
||||
} from "./builtins";
|
||||
import { Vector } from "./vector";
|
||||
import { T } from "../translations";
|
||||
|
||||
@@ -212,7 +197,7 @@ export function newEmptyMap() {
|
||||
* @param {number} end
|
||||
*/
|
||||
export function randomInt(start, end) {
|
||||
return start + Math_round(Math_random() * (end - start));
|
||||
return start + Math.round(Math.random() * (end - start));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,7 +218,7 @@ export function accessNestedPropertyReverse(obj, keys) {
|
||||
* @param {Array | string} arr
|
||||
*/
|
||||
export function randomChoice(arr) {
|
||||
return arr[Math_floor(Math_random() * arr.length)];
|
||||
return arr[Math.floor(Math.random() * arr.length)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -327,12 +312,12 @@ export function arrayDeleteValue(array, value) {
|
||||
* @returns {number} in range [0, 7]
|
||||
*/
|
||||
export function angleToSpriteIndex(offset, inverse = false) {
|
||||
const twoPi = 2.0 * Math_PI;
|
||||
const twoPi = 2.0 * Math.PI;
|
||||
const factor = inverse ? -1 : 1;
|
||||
const offs = inverse ? 2.5 : 3.5;
|
||||
const angle = (factor * Math_atan2(offset.y, offset.x) + offs * Math_PI) % twoPi;
|
||||
const angle = (factor * Math.atan2(offset.y, offset.x) + offs * Math.PI) % twoPi;
|
||||
|
||||
const index = Math_round((angle / twoPi) * 8) % 8;
|
||||
const index = Math.round((angle / twoPi) * 8) % 8;
|
||||
return index;
|
||||
}
|
||||
|
||||
@@ -343,7 +328,7 @@ export function angleToSpriteIndex(offset, inverse = false) {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function epsilonCompare(a, b, epsilon = 1e-5) {
|
||||
return Math_abs(a - b) < epsilon;
|
||||
return Math.abs(a - b) < epsilon;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -394,15 +379,15 @@ export function findNiceValue(num) {
|
||||
roundAmount = 5;
|
||||
}
|
||||
|
||||
const niceValue = Math_floor(num / roundAmount) * roundAmount;
|
||||
const niceValue = Math.floor(num / roundAmount) * roundAmount;
|
||||
if (num >= 10) {
|
||||
return Math_round(niceValue);
|
||||
return Math.round(niceValue);
|
||||
}
|
||||
if (num >= 1) {
|
||||
return Math_round(niceValue * 10) / 10;
|
||||
return Math.round(niceValue * 10) / 10;
|
||||
}
|
||||
|
||||
return Math_round(niceValue * 100) / 100;
|
||||
return Math.round(niceValue * 100) / 100;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -411,7 +396,7 @@ export function findNiceValue(num) {
|
||||
* @param {number} num
|
||||
*/
|
||||
export function findNiceIntegerValue(num) {
|
||||
return Math_ceil(findNiceValue(num));
|
||||
return Math.ceil(findNiceValue(num));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -422,7 +407,7 @@ function roundSmart(n) {
|
||||
if (n < 100) {
|
||||
return n.toFixed(1);
|
||||
}
|
||||
return Math_round(n);
|
||||
return Math.round(n);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -433,7 +418,7 @@ function roundSmart(n) {
|
||||
*/
|
||||
export function formatBigNumber(num, divider = ".") {
|
||||
const sign = num < 0 ? "-" : "";
|
||||
num = Math_abs(num);
|
||||
num = Math.abs(num);
|
||||
|
||||
if (num > 1e54) {
|
||||
return sign + T.global.infinite;
|
||||
@@ -445,7 +430,7 @@ export function formatBigNumber(num, divider = ".") {
|
||||
if (num < 50 && !Number.isInteger(num)) {
|
||||
return sign + num.toFixed(1);
|
||||
}
|
||||
num = Math_floor(num);
|
||||
num = Math.floor(num);
|
||||
|
||||
if (num < 1000) {
|
||||
return sign + "" + num;
|
||||
@@ -482,7 +467,7 @@ export function formatBigNumberFull(num, divider = T.global.thousandsDivider) {
|
||||
let out = "";
|
||||
while (rest >= 1000) {
|
||||
out = (rest % 1000).toString().padStart(3, "0") + divider + out;
|
||||
rest = Math_floor(rest / 1000);
|
||||
rest = Math.floor(rest / 1000);
|
||||
}
|
||||
out = rest + divider + out;
|
||||
|
||||
@@ -500,11 +485,11 @@ export function artificialDelayedPromise(promise, minTimeMs = 500) {
|
||||
return promise;
|
||||
}
|
||||
|
||||
const startTime = performanceNow();
|
||||
const startTime = performance.now();
|
||||
return promise.then(
|
||||
result => {
|
||||
const timeTaken = performanceNow() - startTime;
|
||||
const waitTime = Math_floor(minTimeMs - timeTaken);
|
||||
const timeTaken = performance.now() - startTime;
|
||||
const waitTime = Math.floor(minTimeMs - timeTaken);
|
||||
if (waitTime > 0) {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
@@ -516,8 +501,8 @@ export function artificialDelayedPromise(promise, minTimeMs = 500) {
|
||||
}
|
||||
},
|
||||
error => {
|
||||
const timeTaken = performanceNow() - startTime;
|
||||
const waitTime = Math_floor(minTimeMs - timeTaken);
|
||||
const timeTaken = performance.now() - startTime;
|
||||
const waitTime = Math.floor(minTimeMs - timeTaken);
|
||||
if (waitTime > 0) {
|
||||
// @ts-ignore
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -539,7 +524,7 @@ export function artificialDelayedPromise(promise, minTimeMs = 500) {
|
||||
* @param {number} seed Seed to offset the animation
|
||||
*/
|
||||
export function pulseAnimation(time, duration = 1.0, seed = 0.0) {
|
||||
return Math_sin((time * Math_PI * 2.0) / duration + seed * 5642.86729349) * 0.5 + 0.5;
|
||||
return Math.sin((time * Math.PI * 2.0) / duration + seed * 5642.86729349) * 0.5 + 0.5;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -549,7 +534,7 @@ export function pulseAnimation(time, duration = 1.0, seed = 0.0) {
|
||||
* @returns {number} 0 .. 2 PI
|
||||
*/
|
||||
export function smallestAngle(a, b) {
|
||||
return safeMod(a - b + Math_PI, 2.0 * Math_PI) - Math_PI;
|
||||
return safeMod(a - b + Math.PI, 2.0 * Math.PI) - Math.PI;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -566,7 +551,7 @@ export function safeMod(n, m) {
|
||||
* @param {number} angle
|
||||
*/
|
||||
export function wrapAngle(angle) {
|
||||
return safeMod(angle, 2.0 * Math_PI);
|
||||
return safeMod(angle, 2.0 * Math.PI);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -589,7 +574,7 @@ export function waitNextFrame() {
|
||||
* @returns {number}
|
||||
*/
|
||||
export function round1Digit(n) {
|
||||
return Math_floor(n * 10.0) / 10.0;
|
||||
return Math.floor(n * 10.0) / 10.0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -598,7 +583,7 @@ export function round1Digit(n) {
|
||||
* @returns {number}
|
||||
*/
|
||||
export function round2Digits(n) {
|
||||
return Math_floor(n * 100.0) / 100.0;
|
||||
return Math.floor(n * 100.0) / 100.0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -607,7 +592,7 @@ export function round2Digits(n) {
|
||||
* @returns {number}
|
||||
*/
|
||||
export function round3Digits(n) {
|
||||
return Math_floor(n * 1000.0) / 1000.0;
|
||||
return Math.floor(n * 1000.0) / 1000.0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -616,7 +601,7 @@ export function round3Digits(n) {
|
||||
* @returns {number}
|
||||
*/
|
||||
export function round4Digits(n) {
|
||||
return Math_floor(n * 10000.0) / 10000.0;
|
||||
return Math.floor(n * 10000.0) / 10000.0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -626,7 +611,7 @@ export function round4Digits(n) {
|
||||
* @param {number=} maximum Default 1
|
||||
*/
|
||||
export function clamp(v, minimum = 0, maximum = 1) {
|
||||
return Math_max(minimum, Math_min(maximum, v));
|
||||
return Math.max(minimum, Math.min(maximum, v));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -635,11 +620,11 @@ export function clamp(v, minimum = 0, maximum = 1) {
|
||||
* @param {function():void} target
|
||||
*/
|
||||
export function measure(name, target) {
|
||||
const now = performanceNow();
|
||||
const now = performance.now();
|
||||
for (let i = 0; i < 25; ++i) {
|
||||
target();
|
||||
}
|
||||
const dur = (performanceNow() - now) / 25.0;
|
||||
const dur = (performance.now() - now) / 25.0;
|
||||
console.warn("->", name, "took", dur.toFixed(2), "ms");
|
||||
}
|
||||
|
||||
@@ -889,10 +874,10 @@ export function fastRotateMultipleOf90(x, y, deg) {
|
||||
* @returns {string}
|
||||
*/
|
||||
export function formatSecondsToTimeAgo(secs) {
|
||||
const seconds = Math_floor(secs);
|
||||
const minutes = Math_floor(seconds / 60);
|
||||
const hours = Math_floor(minutes / 60);
|
||||
const days = Math_floor(hours / 24);
|
||||
const seconds = Math.floor(secs);
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const days = Math.floor(hours / 24);
|
||||
|
||||
if (seconds <= 60) {
|
||||
if (seconds <= 1) {
|
||||
@@ -924,18 +909,18 @@ export function formatSecondsToTimeAgo(secs) {
|
||||
*/
|
||||
export function formatSeconds(secs) {
|
||||
const trans = T.global.time;
|
||||
secs = Math_ceil(secs);
|
||||
secs = Math.ceil(secs);
|
||||
if (secs < 60) {
|
||||
return trans.secondsShort.replace("<seconds>", "" + secs);
|
||||
} else if (secs < 60 * 60) {
|
||||
const minutes = Math_floor(secs / 60);
|
||||
const minutes = Math.floor(secs / 60);
|
||||
const seconds = secs % 60;
|
||||
return trans.minutesAndSecondsShort
|
||||
.replace("<seconds>", "" + seconds)
|
||||
.replace("<minutes>", "" + minutes);
|
||||
} else {
|
||||
const hours = Math_floor(secs / 3600);
|
||||
const minutes = Math_floor(secs / 60) % 60;
|
||||
const hours = Math.floor(secs / 3600);
|
||||
const minutes = Math.floor(secs / 60) % 60;
|
||||
return trans.hoursAndMinutesShort.replace("<minutes>", "" + minutes).replace("<hours>", "" + hours);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,4 @@
|
||||
import { globalConfig } from "./config";
|
||||
import {
|
||||
Math_abs,
|
||||
Math_floor,
|
||||
Math_PI,
|
||||
Math_max,
|
||||
Math_min,
|
||||
Math_round,
|
||||
Math_hypot,
|
||||
Math_atan2,
|
||||
Math_sin,
|
||||
Math_cos,
|
||||
Math_ceil,
|
||||
} from "./builtins";
|
||||
|
||||
const tileSize = globalConfig.tileSize;
|
||||
const halfTileSize = globalConfig.halfTileSize;
|
||||
@@ -158,7 +145,7 @@ export class Vector {
|
||||
* @returns {number}
|
||||
*/
|
||||
length() {
|
||||
return Math_hypot(this.x, this.y);
|
||||
return Math.hypot(this.x, this.y);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,7 +213,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
maxScalar(f) {
|
||||
return new Vector(Math_max(f, this.x), Math_max(f, this.y));
|
||||
return new Vector(Math.max(f, this.x), Math.max(f, this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,7 +231,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
min(v) {
|
||||
return new Vector(Math_min(v.x, this.x), Math_min(v.y, this.y));
|
||||
return new Vector(Math.min(v.x, this.x), Math.min(v.y, this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,14 +240,14 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
max(v) {
|
||||
return new Vector(Math_max(v.x, this.x), Math_max(v.y, this.y));
|
||||
return new Vector(Math.max(v.x, this.x), Math.max(v.y, this.y));
|
||||
}
|
||||
/**
|
||||
* Computes the component wise absolute
|
||||
* @returns {Vector}
|
||||
*/
|
||||
abs() {
|
||||
return new Vector(Math_abs(this.x), Math_abs(this.y));
|
||||
return new Vector(Math.abs(this.x), Math.abs(this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -278,7 +265,7 @@ export class Vector {
|
||||
* @returns {number}
|
||||
*/
|
||||
distance(v) {
|
||||
return Math_hypot(this.x - v.x, this.y - v.y);
|
||||
return Math.hypot(this.x - v.x, this.y - v.y);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -308,7 +295,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
floor() {
|
||||
return new Vector(Math_floor(this.x), Math_floor(this.y));
|
||||
return new Vector(Math.floor(this.x), Math.floor(this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,7 +303,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
ceil() {
|
||||
return new Vector(Math_ceil(this.x), Math_ceil(this.y));
|
||||
return new Vector(Math.ceil(this.x), Math.ceil(this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -324,7 +311,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
round() {
|
||||
return new Vector(Math_round(this.x), Math_round(this.y));
|
||||
return new Vector(Math.round(this.x), Math.round(this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,7 +319,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
toTileSpace() {
|
||||
return new Vector(Math_floor(this.x / tileSize), Math_floor(this.y / tileSize));
|
||||
return new Vector(Math.floor(this.x / tileSize), Math.floor(this.y / tileSize));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -340,7 +327,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
toStreetSpace() {
|
||||
return new Vector(Math_floor(this.x / halfTileSize + 0.25), Math_floor(this.y / halfTileSize + 0.25));
|
||||
return new Vector(Math.floor(this.x / halfTileSize + 0.25), Math.floor(this.y / halfTileSize + 0.25));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -364,7 +351,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
snapWorldToTile() {
|
||||
return new Vector(Math_floor(this.x / tileSize) * tileSize, Math_floor(this.y / tileSize) * tileSize);
|
||||
return new Vector(Math.floor(this.x / tileSize) * tileSize, Math.floor(this.y / tileSize) * tileSize);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -372,7 +359,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
normalize() {
|
||||
const len = Math_max(1e-5, Math_hypot(this.x, this.y));
|
||||
const len = Math.max(1e-5, Math.hypot(this.x, this.y));
|
||||
return new Vector(this.x / len, this.y / len);
|
||||
}
|
||||
|
||||
@@ -381,7 +368,7 @@ export class Vector {
|
||||
* @returns {Vector}
|
||||
*/
|
||||
normalizeIfGreaterOne() {
|
||||
const len = Math_max(1, Math_hypot(this.x, this.y));
|
||||
const len = Math.max(1, Math.hypot(this.x, this.y));
|
||||
return new Vector(this.x / len, this.y / len);
|
||||
}
|
||||
|
||||
@@ -393,7 +380,7 @@ export class Vector {
|
||||
normalizedDirection(v) {
|
||||
const dx = v.x - this.x;
|
||||
const dy = v.y - this.y;
|
||||
const len = Math_max(1e-5, Math_hypot(dx, dy));
|
||||
const len = Math.max(1e-5, Math.hypot(dx, dy));
|
||||
return new Vector(dx / len, dy / len);
|
||||
}
|
||||
|
||||
@@ -437,8 +424,8 @@ export class Vector {
|
||||
* @returns {Vector} new vector
|
||||
*/
|
||||
rotated(angle) {
|
||||
const sin = Math_sin(angle);
|
||||
const cos = Math_cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
const cos = Math.cos(angle);
|
||||
return new Vector(this.x * cos - this.y * sin, this.x * sin + this.y * cos);
|
||||
}
|
||||
|
||||
@@ -448,8 +435,8 @@ export class Vector {
|
||||
* @returns {Vector} this vector
|
||||
*/
|
||||
rotateInplaceFastMultipleOf90(angle) {
|
||||
// const sin = Math_sin(angle);
|
||||
// const cos = Math_cos(angle);
|
||||
// const sin = Math.sin(angle);
|
||||
// const cos = Math.cos(angle);
|
||||
// let sin = 0, cos = 1;
|
||||
assert(angle >= 0 && angle <= 360, "Invalid angle, please clamp first: " + angle);
|
||||
|
||||
@@ -598,7 +585,7 @@ export class Vector {
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
equalsEpsilon(v, epsilon = 1e-5) {
|
||||
return Math_abs(this.x - v.x) < 1e-5 && Math_abs(this.y - v.y) < epsilon;
|
||||
return Math.abs(this.x - v.x) < 1e-5 && Math.abs(this.y - v.y) < epsilon;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -606,7 +593,7 @@ export class Vector {
|
||||
* @returns {number} 0 .. 2 PI
|
||||
*/
|
||||
angle() {
|
||||
return Math_atan2(this.y, this.x) + Math_PI / 2;
|
||||
return Math.atan2(this.y, this.x) + Math.PI / 2;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -638,7 +625,7 @@ export class Vector {
|
||||
*/
|
||||
static deserializeTileFromInt(i) {
|
||||
const x = i % 256;
|
||||
const y = Math_floor(i / 256);
|
||||
const y = Math.floor(i / 256);
|
||||
return new Vector(x, y);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user