From 276f73ef0e2196b37ac4ff5d3b3abb4da6d4658d Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 19 Feb 2023 17:46:24 -0400 Subject: [PATCH 1/3] compat support: toNumber, toString, string/array ops --- src/Sprite.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Sprite.js b/src/Sprite.js index 55082d5..172400b 100644 --- a/src/Sprite.js +++ b/src/Sprite.js @@ -319,6 +319,21 @@ class SpriteBase { return this._project.loudness; } + toNumber(value) { + if (typeof value === 'number') { + if (isNaN(value)) { + return 0; + } + return value; + } + + const n = Number(value); + if (Number.isNaN(n)) { + return 0; + } + return n; + } + toBoolean(value) { if (typeof value === 'boolean') { return value; @@ -334,6 +349,22 @@ class SpriteBase { return Boolean(value); } + toString(value) { + return String(value); + } + + stringIncludes(string, substring) { + return string.toLowerCase().includes(substring.toLowerCase()); + } + + arrayIncludes(array, value) { + return array.some(item => this.compare(item, value) === 0); + } + + indexInArray(array, value) { + return array.findIndex(item => this.compare(item, value) === 0); + } + compare(v1, v2) { if (v1 === v2) { return 0; From ea3b0943739e1f8b0d07e9425f62c634a6f85ad9 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 20 Feb 2023 14:47:55 -0400 Subject: [PATCH 2/3] compat support: letterOf, itemOf --- src/Sprite.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Sprite.js b/src/Sprite.js index 172400b..4a51933 100644 --- a/src/Sprite.js +++ b/src/Sprite.js @@ -361,6 +361,20 @@ class SpriteBase { return array.some(item => this.compare(item, value) === 0); } + letterOf(string, index) { + if (index < 0 || index >= string.length) { + return ""; + } + return string[index]; + } + + itemOf(array, index) { + if (index < 0 || index >= array.length) { + return ""; + } + return array[index]; + } + indexInArray(array, value) { return array.findIndex(item => this.compare(item, value) === 0); } From 26367d969e0111e41da1c6c536b5a7b894c6da84 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 20 Feb 2023 14:55:31 -0400 Subject: [PATCH 3/3] compat support: scratchTan --- src/Sprite.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Sprite.js b/src/Sprite.js index 4a51933..39ee887 100644 --- a/src/Sprite.js +++ b/src/Sprite.js @@ -194,6 +194,21 @@ class SpriteBase { return this.degToRad(this.scratchToDeg(scratchDir)); } + // From scratch-vm's math-util. + scratchTan(angle) { + angle = angle % 360; + switch (angle) { + case -270: + case 90: + return Infinity; + case -90: + case 270: + return -Infinity; + default: + return parseFloat(Math.tan((Math.PI * angle) / 180).toFixed(10)); + } + } + // Wrap rotation from -180 to 180. normalizeDeg(deg) { // This is a pretty big math expression, but it's necessary because in JavaScript,