From e77fc5ff897561c82468aa0a0ed8bb2ec80c1fd9 Mon Sep 17 00:00:00 2001 From: Code WvS Date: Sun, 22 Mar 2015 11:09:08 +0100 Subject: support sprite distances/touching --- blocks.js | 6 ++-- objects.js | 15 ++++---- overpass.xml | 2 +- threads.js | 109 ++++++++++++++++++----------------------------------------- 4 files changed, 44 insertions(+), 88 deletions(-) diff --git a/blocks.js b/blocks.js index 2e686cd..8745f3f 100644 --- a/blocks.js +++ b/blocks.js @@ -6653,9 +6653,9 @@ InputSlotMorph.prototype.messagesReceivedMenu = function () { InputSlotMorph.prototype.collidablesMenu = function () { var dict = { - 'mouse-pointer' : ['mouse-pointer'], + /*'mouse-pointer' : ['mouse-pointer'], edge : ['edge'], - 'pen trails' : ['pen trails'] + 'pen trails' : ['pen trails']*/ }, rcvr = this.parentThatIsA(BlockMorph).receiver(), stage = rcvr.parentThatIsA(StageMorph), @@ -6679,7 +6679,7 @@ InputSlotMorph.prototype.collidablesMenu = function () { InputSlotMorph.prototype.distancesMenu = function () { var dict = { - 'mouse-pointer' : ['mouse-pointer'] + //'mouse-pointer' : ['mouse-pointer'] }, rcvr = this.parentThatIsA(BlockMorph).receiver(), stage = rcvr.parentThatIsA(StageMorph), diff --git a/objects.js b/objects.js index e13d32d..227c105 100644 --- a/objects.js +++ b/objects.js @@ -815,12 +815,6 @@ SpriteMorph.prototype.initBlocks = function () { category: 'sensing', spec: 'touching %clr ?' }, - reportColorIsTouchingColor: { - only: SpriteMorph, - type: 'predicate', - category: 'sensing', - spec: 'color %clr is touching %clr ?' - }, colorFiltered: { dev: true, type: 'reporter', @@ -2030,7 +2024,6 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportTouchingObject')); blocks.push(block('reportTouchingColor')); - blocks.push(block('reportColorIsTouchingColor')); blocks.push('-'); blocks.push(block('doAsk')); blocks.push(watcherToggle('getLastAnswer')); @@ -2899,6 +2892,14 @@ SpriteMorph.prototype.removeClone = function () { // SpriteMorph primitives +SpriteMorph.prototype.isTouching = function (other) { + var me = L.latLng(this.yPosition(), this.xPosition()); + target = L.latLng(other.yPosition(), other.xPosition()); + + // could also use leaflet's equals + return me.distanceTo(target) < 1; // 1 meter magic number +}; + // SpriteMorph hiding and showing: /* diff --git a/overpass.xml b/overpass.xml index 50fc971..9299943 100644 --- a/overpass.xml +++ b/overpass.xml @@ -1 +1 @@ -
Sprite
Sprite
\ No newline at end of file +
Sprite
\ No newline at end of file diff --git a/threads.js b/threads.js index af384ef..519922c 100644 --- a/threads.js +++ b/threads.js @@ -2444,39 +2444,16 @@ Process.prototype.objectTouchingObject = function (thisObj, name) { // and for any parts (subsprites) var myself = this, those, - stage, - box, - mouse; + box; - if (this.inputOption(name) === 'mouse-pointer') { - mouse = thisObj.world().hand.position(); - if (thisObj.bounds.containsPoint(mouse) && - !thisObj.isTransparentAt(mouse)) { + stage = thisObj.parentThatIsA(StageMorph); + if (stage) { + those = this.getObjectsNamed(name, thisObj, stage); // clones + if (those.some(function (any) { + return thisObj.isTouching(any); + })) { return true; } - } else { - stage = thisObj.parentThatIsA(StageMorph); - if (stage) { - if (this.inputOption(name) === 'edge') { - box = thisObj.bounds; - if (!thisObj.costume && thisObj.penBounds) { - box = thisObj.penBounds.translateBy(thisObj.position()); - } - if (!stage.bounds.containsRectangle(box)) { - return true; - } - } - if (this.inputOption(name) === 'pen trails' && - thisObj.isTouching(stage.penTrailsMorph())) { - return true; - } - those = this.getObjectsNamed(name, thisObj, stage); // clones - if (those.some(function (any) { - return thisObj.isTouching(any); - })) { - return true; - } - } } return thisObj.parts.some( function (any) { @@ -2488,68 +2465,46 @@ Process.prototype.objectTouchingObject = function (thisObj, name) { Process.prototype.reportTouchingColor = function (aColor) { // also check for any parts (subsprites) var thisObj = this.homeContext.receiver, - stage; + color = aColor.toString(), + stage, sprites; + console.log('color', color); if (thisObj) { - stage = thisObj.parentThatIsA(StageMorph); - if (stage) { - if (thisObj.isTouching(stage.colorFiltered(aColor, thisObj))) { - return true; - } - return thisObj.parts.some( - function (any) { - return any.isTouching(stage.colorFiltered(aColor, any)); + console.log(thisObj.parts.length); + return thisObj.parts.concat([thisObj]).some( + function (any) { + // check if a sprite is inside a polygon colored 'aColor' + var matches = leafletPip.pointInLayer( + // leafletPip swaps lat/lon, be careful + [any.xPosition(), any.yPosition()], + window.penShapes); + for (var j = 0; j < matches.length; j++) { + console.log('match color', matches[j].options.color); + if (matches[j].options.color == color) { + console.log('Hoooray!'); + return true; + } } - ); - } - } - 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) { - if (thisObj.colorFiltered(color1).isTouching( - stage.colorFiltered(color2, thisObj) - )) { - return true; + return false; } - return thisObj.parts.some( - function (any) { - return any.colorFiltered(color1).isTouching( - stage.colorFiltered(color2, any) - ); - } - ); - } + ); } return false; }; Process.prototype.reportDistanceTo = function (name) { var thisObj = this.blockReceiver(), - thatObj, stage, - rc, - point; + thatObj; if (thisObj) { - rc = thisObj.rotationCenter(); - point = rc; - if (this.inputOption(name) === 'mouse-pointer') { - point = thisObj.world().hand.position(); - } stage = thisObj.parentThatIsA(StageMorph); thatObj = this.getOtherObject(name, thisObj, stage); - if (thatObj) { - point = thatObj.rotationCenter(); - } - return rc.distanceTo(point) / stage.scale; + var thisPoint = L.latLng(thisObj.yPosition(), thisObj.xPosition()); + thatPoint = L.latLng(thatObj.yPosition(), thatObj.xPosition()); + + // in meters + return thisPoint.distanceTo(thatPoint); } return 0; }; -- cgit v1.3.1