Convert to cleaner OOP style.

This commit is contained in:
Jürg Lehni 2014-03-12 10:46:45 +01:00
parent 76c0f7dd12
commit cd985d8fb3

View file

@ -8,7 +8,7 @@
<script type="text/paperscript" canvas="canvas">
// kynd.info 2014
var Ball = function(r, p, v) {
function Ball(r, p, v) {
this.radius = r;
this.point = p;
this.vector = v;
@ -37,24 +37,23 @@
}
}
Ball.prototype.iterate = function() {
Ball.prototype = {
iterate: function() {
this.checkWallCollision();
if (this.vector.length > this.maxVec) {
if (this.vector.length > this.maxVec)
this.vector.length = this.maxVec;
}
this.point += this.vector;
this.updateShape();
}
},
Ball.prototype.checkWallCollision = function() {
checkWallCollision: function() {
if (this.point.x < -this.radius) { this.point.x = view.size.width + this.radius;}
if (this.point.x > view.size.width + this.radius) { this.point.x = -this.radius;}
if (this.point.y < -this.radius) { this.point.y = view.size.height + this.radius;}
if (this.point.y > view.size.height + this.radius) { this.point.y = -this.radius;}
}
},
Ball.prototype.updateShape = function() {
updateShape: function() {
var segments = this.path.segments;
for (var i = 0; i < this.numSegment; i ++) {
segments[i].point = this.point + {
@ -73,9 +72,9 @@
this.boundOffsetBuff[i] = this.boundOffset[i] += (this.radius - this.boundOffset[i]) / 15;
this.boundOffsetBuff[i] = this.boundOffset[i] += ((this.boundOffset[next] + this.boundOffset[prev]) / 2 - this.boundOffset[i]) / 3;
}
}
},
Ball.prototype.react = function(b) {
react: function(b) {
var dist = this.point.getDistance(b.point);
if (dist < this.radius + b.radius && dist != 0) {
var overlap = this.radius + b.radius - dist;
@ -88,15 +87,15 @@
this.updateBounds();
b.updateBounds();
}
}
},
Ball.prototype.getboundOffset = function(b) {
getboundOffset: function(b) {
var diff = this.point - b;
var angle = (diff.angle + 180) % 360;
return this.boundOffset[Math.floor(angle / 360 * this.boundOffset.length)];
}
},
Ball.prototype.calcBounds = function(b) {
calcBounds: function(b) {
for (var i = 0; i < this.numSegment; i ++) {
var tp = this.getSidePoint(i);
var bLen = b.getboundOffset(tp);
@ -105,18 +104,18 @@
this.boundOffsetBuff[i] -= (bLen - td) / 2;
}
}
}
},
Ball.prototype.getSidePoint = function(index) {
getSidePoint: function(index) {
return this.point + this.sidePoints[index] * this.boundOffset[index];
}
},
Ball.prototype.updateBounds = function() {
updateBounds: function() {
for (var i = 0; i < this.numSegment; i ++) {
this.boundOffset[i] = this.boundOffsetBuff[i];
}
}
};
//--------------------- main ---------------------