Last active
March 4, 2021 07:50
-
-
Save Utopiah/26bae9fecc7a921f8bfd38cf5fc91612 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var board_bbox = { | |
| x_min: -1.2, x_max: -0.8, | |
| z_min: -6, z_max: -2, | |
| } | |
| var player = document.querySelector("#avatar-rig") // only works for the current device running the script | |
| player = document.querySelector("#naf-z83mq48") // Fabien - Utopiah on Quest | |
| var intervalExecuteCheck = setInterval(checkProximityAndExecute, 500) | |
| var lastExecution = +new Date() | |
| var executing = false | |
| var turtle = getFirstElementFromHash('c553') | |
| var execute = getFirstElementFromHash('512d') | |
| var direction = { axis:'z', factor:1} | |
| var stepSize = 1 | |
| var stepTime = 1000 | |
| var codeSnippets = [ | |
| {label:"turn left", hash:'778d'}, | |
| {label:"turn right", hash:'ef4c'}, | |
| {label:"move forward", hash:'ded0'}, | |
| {label:"reset to origin", hash:'c4e1'}, | |
| ] | |
| function solveExample(){ | |
| var solution_example_instructions = [ | |
| 'reset_to_origin', | |
| 'move_forward', | |
| 'move_forward', | |
| 'move_forward', | |
| 'turn_left', | |
| 'move_forward', | |
| 'turn_right', | |
| 'move_forward', | |
| 'move_forward', | |
| 'move_forward', | |
| ] | |
| executeInstructions(solution_example_instructions) | |
| //reset_to_origin() | |
| } | |
| function readUserInstructions(){ | |
| var images = document.querySelectorAll("[media-image]") | |
| var user_instructions = [] | |
| for (image of images){ | |
| var pos = image.object3D.position | |
| if (pos.x > board_bbox.x_min | |
| && pos.x < board_bbox.x_max | |
| && pos.z > board_bbox.z_min | |
| && pos.z < board_bbox.z_max | |
| ){ | |
| var src = image.components['media-loader'].attrValue.src | |
| codeSnippets.map( cs => { | |
| var match = src.match(cs.hash) | |
| if (match && match.length) | |
| user_instructions.push( {instruction:cs.label, y:pos.y} ) | |
| }) | |
| } | |
| } | |
| return user_instructions.sort((a, b) => a.y < b.y ).map(i => i.instruction.replace(/ /g, '_') ) | |
| } | |
| function executeInstructions(instructions){ | |
| instructions.forEach( (v,i) => { | |
| setTimeout( _ => { | |
| console.log('executing', v) | |
| window.APP.hubChannel.sendMessage("Instruction:"+v.replace(/_/g, ' ')) | |
| window[v]() | |
| }, stepTime*i, v, i ) | |
| }) | |
| setTimeout( _ => { | |
| executing = false | |
| hubs_utils.setPinEntities([turtle], true) | |
| window.APP.hubChannel.sendMessage("Execution done.") | |
| }, stepTime*instructions.length ) | |
| } | |
| function reset_to_origin(){ | |
| turtle.object3D.position.set(0.5,0.15,0.5) | |
| direction = { axis:'z', factor:1} | |
| turtle.object3D.rotation.y = 0 | |
| } | |
| function move_forward(){ | |
| turtle.object3D.position[direction.axis] += direction.factor * stepSize | |
| } | |
| function turn_left(){ | |
| if (direction.axis == 'z'){ | |
| direction.axis = 'x'; | |
| } else { | |
| direction.axis = 'z'; | |
| (direction.factor == 1) ? direction.factor = -1 : direction.factor = 1; | |
| } | |
| turtle.object3D.rotation.y += Math.PI/2 | |
| } | |
| function turn_right(){ | |
| if (direction.axis == 'z'){ | |
| direction.axis = 'x'; | |
| (direction.factor == 1) ? direction.factor = -1 : direction.factor = 1; | |
| } else { | |
| direction.axis = 'z'; | |
| } | |
| turtle.object3D.rotation.y -= Math.PI/2 | |
| } | |
| function checkProximityAndExecute(){ | |
| if (!executing && player.object3D.position.distanceTo( execute.object3D.position ) < 1 ){ | |
| executing = true | |
| hubs_utils.setPinEntities([turtle], false) | |
| window.APP.hubChannel.sendMessage("Executing!") | |
| // could use hubs_utils.setPinEntities to pin all instructionson board | |
| var user_instructions = readUserInstructions() | |
| // could be stored as history in order to keep multiple solutions | |
| executeInstructions(user_instructions) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is amazing