From fe50029b1c727b932f4f47cc69fcaf246a92df4e Mon Sep 17 00:00:00 2001 From: jmoenig Date: Mon, 12 Aug 2013 13:05:42 +0200 Subject: Nestable Sprites Collision Detection & fixes --- threads.js | 78 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 22 deletions(-) (limited to 'threads.js') diff --git a/threads.js b/threads.js index b46e18c..ab36e87 100644 --- a/threads.js +++ b/threads.js @@ -83,7 +83,7 @@ ArgLabelMorph, localize, XML_Element, hex_sha512*/ // Global stuff //////////////////////////////////////////////////////// -modules.threads = '2013-August-02'; +modules.threads = '2013-August-12'; var ThreadManager; var Process; @@ -2161,61 +2161,95 @@ Process.prototype.createClone = function (name) { // Process sensing primitives Process.prototype.reportTouchingObject = function (name) { - // also check for temparary clones, as in Scratch 2.0 - var thisObj = this.homeContext.receiver, + var thisObj = this.homeContext.receiver; + + if (thisObj) { + return this.objectTouchingObject(thisObj, name); + } + return false; +}; + +Process.prototype.objectTouchingObject = function (thisObj, name) { + // helper function for reportTouchingObject() + // also check for temparary clones, as in Scratch 2.0, + // and for any parts (subsprites) + var myself = this, those, stage, mouse; - if (thisObj) { - if (this.inputOption(name) === 'mouse-pointer') { - mouse = thisObj.world().hand.position(); - if (thisObj.bounds.containsPoint(mouse)) { - return !thisObj.isTransparentAt(mouse); - } - return false; + if (this.inputOption(name) === 'mouse-pointer') { + mouse = thisObj.world().hand.position(); + if (thisObj.bounds.containsPoint(mouse) && + !thisObj.isTransparentAt(mouse)) { + return true; } + } else { stage = thisObj.parentThatIsA(StageMorph); if (stage) { - if (this.inputOption(name) === 'edge') { - return !stage.bounds.containsRectangle(thisObj.bounds); + if (this.inputOption(name) === 'edge' && + !stage.bounds.containsRectangle(thisObj.bounds)) { + return true; } - if (this.inputOption(name) === 'pen trails') { - return thisObj.isTouching(stage.penTrailsMorph()); + if (this.inputOption(name) === 'pen trails' && + thisObj.isTouching(stage.penTrailsMorph())) { + return true; } those = this.getObjectsNamed(name, thisObj, stage); // clones - return those.some( - function (any) { + if (those.some(function (any) { return thisObj.isTouching(any); - } - ); + })) { + return true; + } } } - return false; + return thisObj.parts.some( + function (any) { + return myself.objectTouchingObject(any, name); + } + ); }; Process.prototype.reportTouchingColor = function (aColor) { + // also check for any parts (subsprites) var thisObj = this.homeContext.receiver, stage; if (thisObj) { stage = thisObj.parentThatIsA(StageMorph); if (stage) { - return thisObj.isTouching(stage.colorFiltered(aColor, thisObj)); + if (thisObj.isTouching(stage.colorFiltered(aColor, thisObj))) { + return true; + } + return thisObj.parts.some( + function (any) { + return any.isTouching(stage.colorFiltered(aColor, any)); + } + ); } } return false; }; Process.prototype.reportColorIsTouchingColor = function (color1, color2) { + // also check for any parts (subsprites) var thisObj = this.homeContext.receiver, stage; if (thisObj) { stage = thisObj.parentThatIsA(StageMorph); if (stage) { - return thisObj.colorFiltered(color1).isTouching( - stage.colorFiltered(color2, thisObj) + if (thisObj.colorFiltered(color1).isTouching( + stage.colorFiltered(color2, thisObj) + )) { + return true; + } + return thisObj.parts.some( + function (any) { + return any.colorFiltered(color1).isTouching( + stage.colorFiltered(color2, any) + ); + } ); } } -- cgit v1.3.1