Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compat support: toNumber, toString, string/array ops #159

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -319,6 +334,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;
Expand All @@ -334,6 +364,36 @@ 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);
}

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);
}

compare(v1, v2) {
if (v1 === v2) {
return 0;
Expand Down