Skip to content

Commit

Permalink
Add inherit utility
Browse files Browse the repository at this point in the history
  • Loading branch information
milcktoast committed May 4, 2015
1 parent 4f95aef commit 1cec22b
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 31 deletions.
4 changes: 1 addition & 3 deletions src/constraints/AngleConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ function AngleConstraint(angle, a, b, c) {
@method create
@static
*/
AngleConstraint.create = lib.ctor(AngleConstraint);
AngleConstraint.prototype = Object.create(lib.Constraint.prototype);
AngleConstraint.prototype.constructor = AngleConstraint;
lib.inherit(AngleConstraint, lib.Constraint);

/**
Set angle
Expand Down
4 changes: 1 addition & 3 deletions src/constraints/AxisConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ function AxisConstraint(axisA, axisB, a) {
@method create
@static
*/
AxisConstraint.create = lib.ctor(AxisConstraint);
AxisConstraint.prototype = Object.create(lib.Constraint.prototype);
AxisConstraint.prototype.constructor = AxisConstraint;
lib.inherit(AxisConstraint, lib.Constraint);

/**
Set particles defining constraint axis
Expand Down
4 changes: 1 addition & 3 deletions src/constraints/BoundingPlaneConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ function BoundingPlaneConstraint(origin, normal, distance) {
@method create
@static
*/
BoundingPlaneConstraint.create = lib.ctor(BoundingPlaneConstraint);
BoundingPlaneConstraint.prototype = Object.create(lib.Constraint.prototype);
BoundingPlaneConstraint.prototype.constructor = BoundingPlaneConstraint;
lib.inherit(BoundingPlaneConstraint, lib.Constraint);

/**
Global constraint flag
Expand Down
4 changes: 1 addition & 3 deletions src/constraints/BoxConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ function BoxConstraint(min, max) {
@method create
@static
*/
BoxConstraint.create = lib.ctor(BoxConstraint);
BoxConstraint.prototype = Object.create(lib.Constraint.prototype);
BoxConstraint.prototype.constructor = BoxConstraint;
lib.inherit(BoxConstraint, lib.Constraint);

/**
Global constraint flag
Expand Down
2 changes: 1 addition & 1 deletion src/constraints/Constraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function Constraint(size, itemSize, indexOffset) {
@method create
@static
*/
Constraint.create = lib.ctor(Constraint);
lib.inherit(Constraint, Object);

/**
Set particle indices with `Array` or list of `arguments`.
Expand Down
4 changes: 1 addition & 3 deletions src/constraints/DistanceConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ function DistanceConstraint(distance, a, b) {
@method create
@static
*/
DistanceConstraint.create = lib.ctor(DistanceConstraint);
DistanceConstraint.prototype = Object.create(lib.Constraint.prototype);
DistanceConstraint.prototype.constructor = DistanceConstraint;
lib.inherit(DistanceConstraint, lib.Constraint);

/**
Set distance
Expand Down
4 changes: 1 addition & 3 deletions src/constraints/PlaneConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ function PlaneConstraint(planeA, planeB, planeC, a) {
@method create
@static
*/
PlaneConstraint.create = lib.ctor(PlaneConstraint);
PlaneConstraint.prototype = Object.create(lib.Constraint.prototype);
PlaneConstraint.prototype.constructor = PlaneConstraint;
lib.inherit(PlaneConstraint, lib.Constraint);

/**
Set particles defining constraint plane
Expand Down
4 changes: 1 addition & 3 deletions src/constraints/PointConstraint.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ function PointConstraint(position, a) {
@method create
@static
*/
PointConstraint.create = lib.ctor(PointConstraint);
PointConstraint.prototype = Object.create(lib.Constraint.prototype);
PointConstraint.prototype.constructor = PointConstraint;
lib.inherit(PointConstraint, lib.Constraint);

/**
Set point position.
Expand Down
4 changes: 1 addition & 3 deletions src/forces/DirectionalForce.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ function DirectionalForce(vector) {
@method create
@static
*/
DirectionalForce.create = lib.ctor(DirectionalForce);
DirectionalForce.prototype = Object.create(lib.Force.prototype);
DirectionalForce.prototype.constructor = DirectionalForce;
lib.inherit(DirectionalForce, lib.Force);

DirectionalForce.prototype.applyForce = function (ix, f0, p0, p1) {
var v0 = this.vector;
Expand Down
2 changes: 1 addition & 1 deletion src/forces/Force.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Force(vector, opts) {
@method create
@static
*/
Force.create = lib.ctor(Force);
lib.inherit(Force, Object);

/**
Force type enum: `Force.ATTRACTOR`, `Force.REPULSOR`, `Force.ATTRACTOR_REPULSOR`.
Expand Down
4 changes: 1 addition & 3 deletions src/forces/PointForce.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ var pf_ATTRACTOR_REPULSOR = lib.Force.ATTRACTOR_REPULSOR;
@method create
@static
*/
PointForce.create = lib.ctor(PointForce);
PointForce.prototype = Object.create(lib.Force.prototype);
PointForce.prototype.constructor = PointForce;
lib.inherit(PointForce, lib.Force);

/**
Set radius
Expand Down
3 changes: 1 addition & 2 deletions src/systems/ParticleSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ function ParticleSystem(particles, iterations) {
@method create
@static
*/
ParticleSystem.create = lib.ctor(ParticleSystem);
ParticleSystem.prototype.constructor = ParticleSystem;
lib.inherit(ParticleSystem, Object);

/**
Alias for `Vec3.set`. Sets vector of `positions` and `positionsPrev`.
Expand Down
15 changes: 15 additions & 0 deletions src/utils/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ lib.ctor = function ctor(Ctor) {
return instance;
};
};

/**
Functional inheritance utility
@method inherit
@param {Function} Ctor Class constructor
@param {Function} ParentCtor Parent class constructor
@private
@static
*/
lib.inherit = function inherit(Ctor, ParentCtor) {
Ctor.create = lib.ctor(Ctor);
Ctor.prototype = Object.create(ParentCtor.prototype);
Ctor.prototype.constructor = Ctor;
};

0 comments on commit 1cec22b

Please sign in to comment.