summaryrefslogtreecommitdiff
path: root/objects.js
diff options
context:
space:
mode:
authorjmoenig <jens@moenig.org>2013-08-12 13:05:42 +0200
committerjmoenig <jens@moenig.org>2013-08-12 13:05:42 +0200
commitfe50029b1c727b932f4f47cc69fcaf246a92df4e (patch)
treed95c075978fb2db5a66f41d723d1ab4c99c5998d /objects.js
parentbf4ebd3a5a41a90bfac9c70f6082b4fe4e20ad77 (diff)
downloadsnap-byow-fe50029b1c727b932f4f47cc69fcaf246a92df4e.tar.gz
snap-byow-fe50029b1c727b932f4f47cc69fcaf246a92df4e.zip
Nestable Sprites Collision Detection & fixes
Diffstat (limited to 'objects.js')
-rw-r--r--objects.js58
1 files changed, 50 insertions, 8 deletions
diff --git a/objects.js b/objects.js
index e29a0f5..022594b 100644
--- a/objects.js
+++ b/objects.js
@@ -124,7 +124,7 @@ PrototypeHatBlockMorph*/
// Global stuff ////////////////////////////////////////////////////////
-modules.objects = '2013-August-10';
+modules.objects = '2013-August-12';
var SpriteMorph;
var StageMorph;
@@ -2649,7 +2649,7 @@ SpriteMorph.prototype.drawLine = function (start, dest) {
}
};
-// SpriteMorph motion
+// SpriteMorph motion - adjustments due to nesting
SpriteMorph.prototype.moveBy = function (delta, justMe) {
// override the inherited default to make sure my parts follow
@@ -2667,6 +2667,32 @@ SpriteMorph.prototype.moveBy = function (delta, justMe) {
}
};
+SpriteMorph.prototype.slideBackTo = function (situation, inSteps) {
+ // override the inherited default to make sure my parts follow
+ var steps = inSteps || 5,
+ pos = situation.origin.position().add(situation.position),
+ xStep = -(this.left() - pos.x) / steps,
+ yStep = -(this.top() - pos.y) / steps,
+ stepCount = 0,
+ oldStep = this.step,
+ oldFps = this.fps,
+ myself = this;
+
+ this.fps = 0;
+ this.step = function () {
+ myself.moveBy(new Point(xStep, yStep));
+ stepCount += 1;
+ if (stepCount === steps) {
+ situation.origin.add(myself);
+ if (situation.origin.reactToDropOf) {
+ situation.origin.reactToDropOf(myself);
+ }
+ myself.step = oldStep;
+ myself.fps = oldFps;
+ }
+ };
+};
+
SpriteMorph.prototype.setCenter = function (aPoint, justMe) {
// override the inherited default to make sure my parts follow
// unless it's justMe
@@ -2674,6 +2700,20 @@ SpriteMorph.prototype.setCenter = function (aPoint, justMe) {
this.moveBy(delta, justMe);
};
+SpriteMorph.prototype.nestingBounds = function () {
+ // same as fullBounds(), except that it uses "parts" instead of children
+ var result;
+ result = this.bounds;
+ this.parts.forEach(function (part) {
+ if (part.isVisible) {
+ result = result.merge(part.nestingBounds());
+ }
+ });
+ return result;
+};
+
+// SpriteMorph motion primitives
+
Morph.prototype.setPosition = function (aPoint, justMe) {
// override the inherited default to make sure my parts follow
// unless it's justMe
@@ -2830,32 +2870,34 @@ SpriteMorph.prototype.glide = function (
};
SpriteMorph.prototype.bounceOffEdge = function () {
+ // taking nested parts into account
var stage = this.parentThatIsA(StageMorph),
+ fb = this.nestingBounds(),
dirX,
dirY;
if (!stage) {return null; }
- if (stage.bounds.containsRectangle(this.bounds)) {return null; }
+ if (stage.bounds.containsRectangle(fb)) {return null; }
dirX = Math.cos(radians(this.heading - 90));
dirY = -(Math.sin(radians(this.heading - 90)));
- if (this.left() < stage.left()) {
+ if (fb.left() < stage.left()) {
dirX = Math.abs(dirX);
}
- if (this.right() > stage.right()) {
+ if (fb.right() > stage.right()) {
dirX = -(Math.abs(dirX));
}
- if (this.top() < stage.top()) {
+ if (fb.top() < stage.top()) {
dirY = -(Math.abs(dirY));
}
- if (this.bottom() > stage.bottom()) {
+ if (fb.bottom() > stage.bottom()) {
dirY = Math.abs(dirY);
}
this.setHeading(degrees(Math.atan2(-dirY, dirX)) + 90);
this.setPosition(this.position().add(
- this.bounds.amountToTranslateWithin(stage.bounds)
+ fb.amountToTranslateWithin(stage.bounds)
));
this.positionTalkBubble();
};