Skip to content

Commit

Permalink
Merge remote-tracking branch 'pm/develop' into merge-upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
someCatInTheWorld committed Nov 25, 2024
2 parents 458d086 + ef7daf2 commit 32c55ea
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/extension-support/extension-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ const defaultBuiltinExtensions = {
jwXml: () => require("../extensions/jw_xml"),
// vector type blah blah blah
jwVector: () => require("../extensions/jwVector"),
// my own array system yipee
jwArray: () => require("../extensions/jwArray"),
// mid extension but i need it
jwTargets: () => require("../extensions/jwTargets"),
// cool new physics extension
Expand Down
185 changes: 185 additions & 0 deletions src/extensions/jwArray/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
const BlockType = require('../../extension-support/block-type')
const BlockShape = require('../../extension-support/block-shape')
const ArgumentType = require('../../extension-support/argument-type')
const Cast = require('../../util/cast')

let arrayLimit = 2 ** 32 - 1

/**
* @param {number} x
* @returns {string}
*/
function formatNumber(x) {
if (x >= 1e6) {
return x.toExponential(4)
} else {
x = Math.floor(x * 1000) / 1000
return x.toFixed(Math.min(3, (String(x).split('.')[1] || '').length))
}
}

function span(text) {
let el = document.createElement('span')
el.innerHTML = text
el.style.display = 'hidden'
el.style.whiteSpace = 'nowrap'
el.style.width = '100%'
el.style.textAlign = 'center'
return el
}

class ArrayType {
customId = "jwArray"

array = []

constructor(array = []) {
this.array = array
}

static display(x) {
try {
switch (typeof x) {
case "object":
if (typeof x.jwArrayHandler == "function") {
return x.jwArrayHandler()
}
return "Object"
case "undefined":
return "null"
case "number":
return formatNumber(x)
case "boolean":
return x ? "true" : "false"
case "string":
return `"${Cast.toString(x)}"`
}
} catch {}
return "?"
}

jwArrayHandler() {
return `Array[${formatNumber(this.array.length)}]`
}

toString() {
return JSON.stringify(this.array)
}
toMonitorContent = () => span(this.toString())

toReporterContent() {
let root = document.createElement('div')
root.style.display = 'flex'
root.style.flexDirection = 'column'
root.style.justifyContent = 'center'

let arrayDisplay = span(`[${this.array.slice(0, 50).map(v => ArrayType.display(v)).join(', ')}]`)
arrayDisplay.style.overflow = "hidden"
arrayDisplay.style.whiteSpace = "nowrap"
arrayDisplay.style.textOverflow = "ellipsis"
arrayDisplay.style.maxWidth = "256px"
root.appendChild(arrayDisplay)

root.appendChild(span(`Length: ${this.array.length}`))

return root
}
}

const jwArray = {
Type: ArrayType,
Block: {
blockType: BlockType.REPORTER,
blockShape: BlockShape.SQUARE,
forceOutputType: "Array",
disableMonitor: true
},
Argument: {
shape: BlockShape.SQUARE,
check: ["Array"]
}
}

class Extension {
constructor() {
vm.jwArray = jwArray
}

getInfo() {
return {
id: "jwArray",
name: "Arrays",
color1: "#ff513d",
blocks: [
{
opcode: 'blank',
text: 'blank array',
...jwArray.Block
},
{
opcode: 'blankLength',
text: 'blank array of length [LENGTH]',
arguments: {
LENGTH: {
type: ArgumentType.NUMBER,
defaultValue: 1
}
},
...jwArray.Block
},
"---",
{
opcode: 'get',
text: 'get [ARRAY] at [INDEX]',
blockType: BlockType.REPORTER,
arguments: {
ARRAY: jwArray.Argument,
INDEX: {
type: ArgumentType.NUMBER,
defaultValue: 1
}
}
},
"---",
{
opcode: 'set',
text: 'set [ARRAY] at [INDEX] to [VALUE]',
arguments: {
ARRAY: jwArray.Argument,
INDEX: {
type: ArgumentType.NUMBER,
defaultValue: 1
},
VALUE: {
type: ArgumentType.STRING,
defaultValue: "foo"
}
},
...jwArray.Block
}
]
};
}

blank() {
return new jwArray.Type()
}

blankLength({LENGTH}) {
LENGTH = Cast.toNumber(LENGTH)
LENGTH = Math.min(Math.max(LENGTH, 1), arrayLimit)

return new jwArray.Type(Array(LENGTH))
}

get({ARRAY, INDEX}) {
return ARRAY.array[Cast.toNumber(INDEX)]
}

set({ARRAY, INDEX, VALUE}) {
ARRAY.array[Cast.toNumber(INDEX)] = VALUE
return ARRAY
}
}

module.exports = Extension
4 changes: 0 additions & 4 deletions src/extensions/jwPsychic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class Extension {
{
opcode: 'getPos',
text: 'position',
disableMonitor: true,
filter: [TargetType.SPRITE],
...Vector.Block
},
Expand All @@ -111,7 +110,6 @@ class Extension {
{
opcode: 'getVel',
text: 'velocity',
disableMonitor: true,
filter: [TargetType.SPRITE],
...Vector.Block
},
Expand All @@ -131,7 +129,6 @@ class Extension {
opcode: 'getRot',
text: 'rotation',
blockType: BlockType.REPORTER,
disableMonitor: true,
filter: [TargetType.SPRITE]
},
{
Expand All @@ -150,7 +147,6 @@ class Extension {
opcode: 'getAngVel',
text: 'angular velocity',
blockType: BlockType.REPORTER,
disableMonitor: true,
filter: [TargetType.SPRITE]
}
],
Expand Down
23 changes: 15 additions & 8 deletions src/extensions/jwTargets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class TargetType {
this.targetId = targetId
}

jwArrayHandler() {
return 'Target'
}

toString() {
return this.targetId
}
Expand Down Expand Up @@ -52,19 +56,14 @@ class TargetType {
return span("Unknown")
}
}

/** @returns {number} */
get magnitude() { return Math.hypot(this.x, this.y) }

/** @returns {number} */
get angle() {return Math.atan2(this.x, this.y) * (180 / Math.PI)}
}

const Target = {
Type: TargetType,
Block: {
blockType: BlockType.REPORTER,
forceOutputType: "Target"
forceOutputType: "Target",
disableMonitor: true
},
Argument: {
check: ["Target"]
Expand All @@ -85,7 +84,11 @@ class Extension {
{
opcode: 'this',
text: 'this target',
disableMonitor: true,
...Target.Block
},
{
opcode: 'stage',
text: 'stage target',
...Target.Block
},
'---',
Expand All @@ -100,6 +103,10 @@ class Extension {
this({}, util) {
return new Target.Type(util.target.id)
}

stage() {
return new Target.Type(vm.runtime._stageTarget.id)
}
}

module.exports = Extension
7 changes: 6 additions & 1 deletion src/extensions/jwVector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class VectorType {
return new VectorType(0, 0)
}

jwArrayHandler() {
return 'Vector'
}

toString() {
return `${this.x},${this.y}`
}
Expand Down Expand Up @@ -92,7 +96,8 @@ const Vector = {
Block: {
blockType: BlockType.REPORTER,
blockShape: BlockShape.LEAF,
forceOutputType: "Vector"
forceOutputType: "Vector",
disableMonitor: true
},
Argument: {
shape: BlockShape.LEAF,
Expand Down

0 comments on commit 32c55ea

Please sign in to comment.