Skip to content

Commit

Permalink
makes Mover more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
blindfish3 committed Jun 3, 2020
1 parent 87fbb0c commit e478523
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/lib/balls/Mover.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,51 @@ Mover.prototype.draw = function () {

Mover.prototype.update = function () {
this.draw();
moveObjects.call(this, true, this.p);
const { width, height, mouseX, mouseY } = this.p;
const bounds = { width, height };
const override = { x: mouseX, y: mouseY };
moveObjects(this, true, bounds, override);
};

// By using the base characteristics of a Mover object we
// can write generic functions that add desired behaviours
// without making them methods of a class
function moveObjects(keepInBounds, p) {
if (this instanceof Mover) {
if (this.moving) {
this.x += this.vx;
this.y += this.vy;
if (this.globals.gravityOn) {
this.vy -= this.globals.gravity;
function moveObjects(object, keepInBounds, bounds, override) {
const { x, y, vx, vy, rad, bounce, globals, moving } = object;
const { friction, gravity, gravityOn } = globals;

if (object instanceof Mover) {
if (moving) {
object.x += vx;
object.y += vy;
if (gravityOn) {
object.vy -= gravity;
}
this.vx *= this.globals.friction;
this.vy *= this.globals.friction;
object.vx *= friction;
object.vy *= friction;

if (keepInBounds) {
if (this.x > p.width - this.rad) {
this.x = p.width - this.rad;
this.vx *= -this.bounce;
} else if (this.x < 0 + this.rad) {
this.x = 0 + this.rad;
this.vx *= -this.bounce;
if (x > bounds.width - rad) {
object.x = bounds.width - rad;
object.vx *= -bounce;
} else if (x < 0 + rad) {
object.x = 0 + rad;
object.vx *= -bounce;
}

if (this.y > p.height - this.rad) {
this.y = p.height - this.rad;
this.vy *= -this.bounce;
} else if (this.y < 0 + this.rad) {
this.y = 0 + this.rad;
this.vy *= -this.bounce;
if (y > bounds.height - rad) {
object.y = bounds.height - rad;
object.vy *= -bounce;
} else if (y < 0 + rad) {
object.y = 0 + rad;
object.vy *= -bounce;
}
}
}
//WARNING: kludge
else {
this.x = p.mouseX;
this.y = p.mouseY;
object.x = override.x;
object.y = override.y;
}
}
// else throw error
Expand Down

0 comments on commit e478523

Please sign in to comment.