Skip to content

Instantly share code, notes, and snippets.

@harrybanda
Created February 24, 2019 10:27
Show Gist options
  • Save harrybanda/46a7552903fbe02b04653927891cbdf3 to your computer and use it in GitHub Desktop.
Save harrybanda/46a7552903fbe02b04653927891cbdf3 to your computer and use it in GitHub Desktop.
Virtual Coder Level Handing
function update(args, ctx) {
var area = 0.05; // Area for robot to stand on
var yOffset = 0.22; // Offset on the Y axis
var py = 0.21; // Y position on 3D space
// Check if robot has reached the on goal
if (isBetween(ctx.entity.getTranslation().x, ctx.goalx + area, ctx.goalx - area)
&& isBetween(ctx.entity.getTranslation().z, ctx.goalz + area, ctx.goalz - area)
&& ctx.entity.getTranslation().y === py) {
ctx.entity.setTranslation(ctx.goalx, yOffset, ctx.goalz);
clearInterval(ctx.worldData.intervalId);
SystemBus.emit('levelComplete'); // Emit a message to switch the level
// Make AI host talk if level is complete
ctx.worldData.speech.updateConfig({body: lvlCompleteSpeech(ctx)});
setTimeout(() => {
ctx.worldData.speech.play().then(() => {
if (lvlNum === 11) {
document.getElementById("btnNext").innerText = 'First';
} else {
document.getElementById("btnNext").innerText = 'Next';
}
document.getElementById("btnNext").style.display = 'block';
});
}, 1000);
}
// Check if the robot makes a wrong move
for (var i = 0; i < ctx.dangerZones.length; i++) {
if (isBetween(ctx.entity.getTranslation().x, ctx.dangerZones[i][0] + area, ctx.dangerZones[i][0] - area)
&& isBetween(ctx.entity.getTranslation().z, ctx.dangerZones[i][1] + area, ctx.dangerZones[i][1] - area)
&& ctx.entity.getTranslation().y === py) {
ctx.entity.setTranslation(ctx.dangerZones[i][0], yOffset, ctx.dangerZones[i][1]);
clearInterval(ctx.worldData.intervalId);
SystemBus.emit('wrongWay'); // Emit a message restart the level
ctx.worldData.speech.updateConfig({body: wrongMoveSpeech()});
setTimeout(() => {
ctx.worldData.speech.play();
}, 1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment