Optimize and rename clearing to multiplier in gdjs.Force

This commit is contained in:
Florian Rival
2018-11-28 20:24:27 +00:00
committed by Florian Rival
parent 34b460e968
commit 137ffc2b84
3 changed files with 35 additions and 34 deletions

View File

@@ -11,16 +11,16 @@
* @class Force
* @param {number} x The initial x component
* @param {number} y The initial y component
* @param {number} damping The damping (0 for a force that disappear on next frame, 1 to keep it)
* @param {number} multiplier The multiplier (0 for a force that disappear on next frame, 1 for a permanent force)
*/
gdjs.Force = function(x,y, clearing)
gdjs.Force = function(x,y, multiplier)
{
this._x = x || 0;
this._y = y || 0;
this._angle = Math.atan2(y,x)*180/Math.PI;
this._length = Math.sqrt(x*x+y*y);
this._dirty = false;
this._clearing = clearing;
this._multiplier = multiplier;
}
/**
@@ -119,16 +119,16 @@ gdjs.Force.prototype.getLength = function() {
};
/**
* Return true if the force is temporary, false if it is permanent.
* Return 1 (true) if the force is permanent, 0 (false) if it is instant.
*/
gdjs.Force.prototype.getClearing = function() {
return this._clearing;
gdjs.Force.prototype.getMultiplier = function() {
return this._multiplier;
};
/**
* Set if the force clearing.
* @param {number} clearing The new value
* Set if the force multiplier.
* @param {number} multiplier The new value
*/
gdjs.Force.prototype.setClearing = function(clearing) {
this._clearing = clearing;
gdjs.Force.prototype.setMultiplier = function(multiplier) {
this._multiplier = multiplier;
};

View File

@@ -570,16 +570,16 @@ gdjs.RuntimeObject.prototype.getCenterY = function() {
* @private
* @param {number} x The x coordinates of the force
* @param {number} y The y coordinates of the force
* @param {number} clearing Set the force clearing
* @param {number} multiplier Set the force multiplier
*/
gdjs.RuntimeObject.prototype._getRecycledForce = function(x, y, clearing) {
gdjs.RuntimeObject.prototype._getRecycledForce = function(x, y, multiplier) {
if ( gdjs.RuntimeObject.forcesGarbage.length === 0 )
return new gdjs.Force(x, y, clearing);
return new gdjs.Force(x, y, multiplier);
else {
var recycledForce = gdjs.RuntimeObject.forcesGarbage.pop();
recycledForce.setX(x);
recycledForce.setY(y);
recycledForce.setClearing(clearing);
recycledForce.setMultiplier(multiplier);
return recycledForce;
}
};
@@ -588,24 +588,24 @@ gdjs.RuntimeObject.prototype._getRecycledForce = function(x, y, clearing) {
* Add a force to the object to move it.
* @param {number} x The x coordinates of the force
* @param {number} y The y coordinates of the force
* @param {number} clearing Set the force clearing
* @param {number} multiplier Set the force multiplier
*/
gdjs.RuntimeObject.prototype.addForce = function(x,y, clearing) {
this._forces.push(this._getRecycledForce(x, y, clearing));
gdjs.RuntimeObject.prototype.addForce = function(x,y, multiplier) {
this._forces.push(this._getRecycledForce(x, y, multiplier));
};
/**
* Add a force using polar coordinates.
* @param {number} angle The angle of the force, in degrees.
* @param {number} len The length of the force, in pixels.
* @param {number} clearing Set the force clearing
* @param {number} multiplier Set the force multiplier
*/
gdjs.RuntimeObject.prototype.addPolarForce = function(angle, len, clearing) {
gdjs.RuntimeObject.prototype.addPolarForce = function(angle, len, multiplier) {
var angleInRadians = angle/180*3.14159; //TODO: Benchmark with Math.PI
var forceX = Math.cos(angleInRadians)*len;
var forceY = Math.sin(angleInRadians)*len;
this._forces.push(this._getRecycledForce(forceX, forceY, clearing));
this._forces.push(this._getRecycledForce(forceX, forceY, multiplier));
};
/**
@@ -613,16 +613,16 @@ gdjs.RuntimeObject.prototype.addPolarForce = function(angle, len, clearing) {
* @param {number} x The target x position
* @param {number} y The target y position
* @param {number} len The force length, in pixels.
* @param {number} clearing Set the force clearing
* @param {number} multiplier Set the force multiplier
*/
gdjs.RuntimeObject.prototype.addForceTowardPosition = function(x,y, len, clearing) {
gdjs.RuntimeObject.prototype.addForceTowardPosition = function(x,y, len, multiplier) {
var angle = Math.atan2(y - (this.getDrawableY()+this.getCenterY()),
x - (this.getDrawableX()+this.getCenterX()));
var forceX = Math.cos(angle)*len;
var forceY = Math.sin(angle)*len;
this._forces.push(this._getRecycledForce(forceX, forceY, clearing));
this._forces.push(this._getRecycledForce(forceX, forceY, multiplier));
};
/**
@@ -630,14 +630,14 @@ gdjs.RuntimeObject.prototype.addForceTowardPosition = function(x,y, len, clearin
* (Shortcut for addForceTowardPosition)
* @param {gdjs.RuntimeObject} object The target object
* @param {number} len The force length, in pixels.
* @param {number} clearing Set the force clearing
* @param {number} multiplier Set the force multiplier
*/
gdjs.RuntimeObject.prototype.addForceTowardObject = function(obj, len, clearing) {
gdjs.RuntimeObject.prototype.addForceTowardObject = function(obj, len, multiplier) {
if ( obj == null ) return;
this.addForceTowardPosition(obj.getDrawableX() + obj.getCenterX(),
obj.getDrawableY() + obj.getCenterY(),
len, clearing);
len, multiplier);
};
/**
@@ -662,14 +662,15 @@ gdjs.RuntimeObject.prototype.hasNoForces = function() {
*/
gdjs.RuntimeObject.prototype.updateForces = function(elapsedTime) {
for(var i = 0;i<this._forces.length;) {
if(this._forces[i].getClearing() === 0 || this._forces[i].getLength() <= 0.001)
{
gdjs.RuntimeObject.forcesGarbage.push(this._forces[i]);
var force = this._forces[i];
var multiplier = force.getMultiplier();
if (multiplier === 1) { // Permanent force
++i;
} else if (multiplier === 0 || force.getLength() <= 0.001) { // Instant or force disappearing
gdjs.RuntimeObject.forcesGarbage.push(force);
this._forces.remove(i);
}
else
{
this._forces[i].setLength(this._forces[i].getLength() - this._forces[i].getLength() * ( 1 - this._forces[i].getClearing() ) * elapsedTime);
} else { // Deprecated way of updating forces progressively.
force.setLength(force.getLength() - force.getLength() * ( 1 - multiplier ) * elapsedTime);
++i;
}
}

View File

@@ -85,6 +85,6 @@ export default class ForceMultiplierField extends Component<
export const renderForceMultiplierString = (value: string) => {
if (value === '1') return 'a permanent';
else if (value === '0' || value === '') return 'an instant';
return 'a (multiplier: ' + value + ')';
};