summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCode WvS <code-wvs@quantentunnel.de>2015-03-22 11:09:08 +0100
committerCode WvS <code-wvs@quantentunnel.de>2015-03-22 11:09:08 +0100
commite77fc5ff897561c82468aa0a0ed8bb2ec80c1fd9 (patch)
tree5ce3a11f7a3337d353d309b4b6f13325bbfee44f
parentf6a5169f7f9673d298f3e2016f8a1cd834d79530 (diff)
downloadsnap-yow-e77fc5ff897561c82468aa0a0ed8bb2ec80c1fd9.tar.gz
snap-yow-e77fc5ff897561c82468aa0a0ed8bb2ec80c1fd9.zip
support sprite distances/touching
-rw-r--r--blocks.js6
-rw-r--r--objects.js15
-rw-r--r--overpass.xml2
-rw-r--r--threads.js109
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 @@
-<blocks app="Snap! 4.0, http://snap.berkeley.edu" version="1"><block-definition s="ist Längengrad: %&apos;Längengrad&apos; Breitengrad: %&apos;Breitengrad&apos; innerhalb einer Fläche der Farbe von %&apos;SpriteName&apos; ?" type="predicate" category="sensing"><header></header><code></code><inputs><input type="%n"></input><input type="%n"></input><input type="%txt">Sprite</input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>spriteName</l><l>lat</l><l>lon</l></list><l>var layers = window.penShapes, sprites = world.children[0].sprites.asArray(), sprite;&#xD;for (j = 0; j &lt; sprites.length; j++) {&#xD; if (sprites[j].name == spriteName) {&#xD; sprite = sprites[j];&#xD; break;&#xD; }&#xD;}&#xD;if (!sprite) {&#xD; return false;&#xD;}&#xD;var matches = leafletPip.pointInLayer([lon, lat], layers);&#xD;&#xD;for (var j = 0; j &lt; matches.length; j++) {&#xD; if (matches[j].options.color == sprite.color) {&#xD; return true;&#xD; }&#xD;}&#xD;&#xD;return false;</l></block><list><block var="SpriteName"/><block var="Breitengrad"/><block var="Längengrad"/></list></block></block></script></block-definition><block-definition s="ist eine Straße nahe Längengrad: %&apos;lon&apos; Breitengrad: %&apos;lat&apos;" type="predicate" category="sensing"><header></header><code></code><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportGreaterThan"><block s="reportListLength"><block s="overpassQuery"><block s="reportJoinWords"><list><l>way(around:7,</l><block var="lat"/><l>,</l><block var="lon"/><l>)[highway];out;</l></list></block></block></block><l>0</l></block></block></script></block-definition><block-definition s="ist ein Gebäude nahe Längengrad: %&apos;lon&apos; Breitengrad: %&apos;lat&apos;" type="predicate" category="sensing"><header></header><code></code><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportGreaterThan"><block s="reportListLength"><block s="overpassQuery"><block s="reportJoinWords"><list><l>way(around:5,</l><block var="lat"/><l>,</l><block var="lon"/><l>)[building];out;</l></list></block></block></block><l>0</l></block></block></script></block-definition><block-definition s="Größe des von mit %&apos;SpriteName&apos; s Farbe markierten Feldes" type="reporter" category="sensing"><header></header><code></code><inputs><input type="%txt">Sprite</input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>spriteName</l></list><l>var size = 0, layers = window.penShapes.getLayers(), sprites = world.children[0].sprites.asArray(), sprite;&#xD;for (j = 0; j &lt; sprites.length; j++) {&#xD; if (sprites[j].name == spriteName) {&#xD; sprite = sprites[j];&#xD; break;&#xD; }&#xD;}&#xD;if (!sprite) {&#xD; return 0;&#xD;}&#xD;for (j = 0; j &lt; layers.length; j++) {&#xD; if (layers[j].options.color == sprite.color) {&#xD; size += LGeo.area(layers[j]);&#xD; }&#xD;}&#xD;return size;</l></block><list><block var="SpriteName"/></list></block></block></script></block-definition></blocks> \ No newline at end of file
+<blocks app="Snap! 4.0, http://snap.berkeley.edu" version="1"><block-definition s="ist eine Straße nahe Längengrad: %&apos;lon&apos; Breitengrad: %&apos;lat&apos;" type="predicate" category="sensing"><header></header><code></code><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportGreaterThan"><block s="reportListLength"><block s="overpassQuery"><block s="reportJoinWords"><list><l>way(around:7,</l><block var="lat"/><l>,</l><block var="lon"/><l>)[highway];out;</l></list></block></block></block><l>0</l></block></block></script></block-definition><block-definition s="ist ein Gebäude nahe Längengrad: %&apos;lon&apos; Breitengrad: %&apos;lat&apos;" type="predicate" category="sensing"><header></header><code></code><inputs><input type="%n"></input><input type="%n"></input></inputs><script><block s="doReport"><block s="reportGreaterThan"><block s="reportListLength"><block s="overpassQuery"><block s="reportJoinWords"><list><l>way(around:5,</l><block var="lat"/><l>,</l><block var="lon"/><l>)[building];out;</l></list></block></block></block><l>0</l></block></block></script></block-definition><block-definition s="Größe des von mit %&apos;SpriteName&apos; s Farbe markierten Feldes" type="reporter" category="sensing"><header></header><code></code><inputs><input type="%txt">Sprite</input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>spriteName</l></list><l>var size = 0, layers = window.penShapes.getLayers(), sprites = world.children[0].sprites.asArray(), sprite;&#xD;for (j = 0; j &lt; sprites.length; j++) {&#xD; if (sprites[j].name == spriteName) {&#xD; sprite = sprites[j];&#xD; break;&#xD; }&#xD;}&#xD;if (!sprite) {&#xD; return 0;&#xD;}&#xD;for (j = 0; j &lt; layers.length; j++) {&#xD; if (layers[j].options.color == sprite.color) {&#xD; size += LGeo.area(layers[j]);&#xD; }&#xD;}&#xD;return size;</l></block><list><block var="SpriteName"/></list></block></block></script></block-definition></blocks> \ 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;
};