Skip to content
This repository was archived by the owner on Jun 19, 2018. It is now read-only.

Added doAsk and answer prims #18

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions js/Interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var Thread = function(block, target) {
this.tmp = null; // used for thread operations like Timer
this.tmpObj = []; // used for Sprite operations like glide
this.firstTime = true;
this.paused = false; // pausing thread for: ask
}

var Interpreter = function() {
Expand Down Expand Up @@ -104,7 +105,7 @@ Interpreter.prototype.stepThreads = function() {
var threadStopped = false;
for (var a = this.threads.length-1; a >= 0; --a) {
this.activeThread = this.threads[a];
this.stepActiveThread();
this.stepActiveThread();
if (!this.activeThread || this.activeThread.nextBlock == null) {
threadStopped = true;
}
Expand Down Expand Up @@ -132,14 +133,17 @@ Interpreter.prototype.stepActiveThread = function() {
if (b == null) return;
this.yield = false;
while (true) {
this.opCount++;
if(this.activeThread.paused) return; // thread pausing allows asynchronous events to trigger program flow

// Advance the "program counter" to the next block before running the primitive.
// Control flow primitives (e.g. if) may change activeThread.nextBlock.
this.opCount++;

this.activeThread.nextBlock = b.nextBlock;
b.primFcn(b);
if (this.yield) { this.activeThread.nextBlock = b; return; }
b = this.activeThread.nextBlock; // refresh local variable b in case primitive did some control flow
while (b == null) {
while (b == null) {
// end of a substack; pop the owning control flow block from stack
// Note: This is a loop to handle nested control flow blocks.
b = this.activeThread.stack.pop();
Expand Down
24 changes: 24 additions & 0 deletions js/primitives/SensingPrims.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ SensingPrims.prototype.addPrimsTo = function(primTable) {

primTable['timeAndDate'] = function(b){ return runtime.getTimeString(interp.arg(b, 0)); };
primTable['timestamp'] = this.primTimestamp;

primTable['doAsk'] = this.primDoAsk;
primTable['answer'] = this.primAnswer;
}

SensingPrims.prototype.primTouching = function(b) {
Expand Down Expand Up @@ -228,6 +231,27 @@ SensingPrims.prototype.primTimestamp = function(b) {
return msSince / 86400000;
}

SensingPrims.prototype.primDoAsk = function(b){
console.log(b);
showBubble(b, 'say');
$("#container").append($('<div id="ask-box"><input type="text" id="answer"></input><input type="submit" id="answer-submit"></input>/div>'));
var activeThread = interp.threads.indexOf(interp.activeThread);

interp.activeThread.paused = true;


$("#answer-submit").bind("click", function(){
console.log(activeThread);
interp.threads[activeThread].paused = false;
SensingPrims._answer = $("#answer")[0].value;
$("#ask-box").remove(); // remove ask box
});
};

SensingPrims.prototype.primAnswer = function(b){
return SensingPrims._answer;
};

// Helpers
SensingPrims.prototype.mouseOrSpritePosition = function(arg) {
if (arg == "_mouse_") {
Expand Down
6 changes: 6 additions & 0 deletions player.css
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@

background: url(img/think-bottom.png) transparent no-repeat;
}

#ask-box {
top: 340px;
position: absolute;
width: 400px;
}