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