Skip to content

Commit

Permalink
Merge pull request #60 from creativelifeform/bugfix/check-for-zero-life
Browse files Browse the repository at this point in the history
bugfix/check-for-zero-life
  • Loading branch information
rohan-deshpande committed Sep 2, 2019
2 parents 1b02c12 + a38b6c8 commit c8387f4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
27 changes: 23 additions & 4 deletions src/behaviour/Behaviour.js
Expand Up @@ -2,6 +2,7 @@ import { DEFAULT_BEHAVIOUR_EASING, DEFAULT_LIFE } from './constants';

import { BEHAVIOUR_TYPE_ABSTRACT } from './types';
import { MEASURE } from '../constants';
import isNumber from 'lodash/isNumber';
import { uid } from '../utils';

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class Behaviour {
* @desc The life of the behaviour
* @type {number}
*/
this.life = life || Infinity;
this.life = life;

/**
* @desc The behaviour's decaying trend
Expand Down Expand Up @@ -77,14 +78,32 @@ export default class Behaviour {
/**
* Reset this behaviour's parameters
*
* @param {number} [life=Infinity] - The life of the behaviour
* @param {number} [life=DEFAULT_LIFE] - The life of the behaviour
* @param {function} [easing=DEFAULT_BEHAVIOUR_EASING] - The behaviour's decaying trend
*/
reset(life = Infinity, easing = DEFAULT_BEHAVIOUR_EASING) {
this.life = life || DEFAULT_LIFE;
reset(life = DEFAULT_LIFE, easing = DEFAULT_BEHAVIOUR_EASING) {
this.life = life;
this.easing = easing || DEFAULT_BEHAVIOUR_EASING;
}

/**
* Ensures that life is infinity if an invalid value is supplied.
*
* @return void
*/
set life(life) {
this._life = isNumber(life) ? life : DEFAULT_LIFE;
}

/**
* Gets the behaviour's life.
*
* @return {Number}
*/
get life() {
return this._life;
}

/**
* Normalize a force by 1:100;
*
Expand Down
8 changes: 4 additions & 4 deletions src/emitter/Emitter.js
Expand Up @@ -10,12 +10,12 @@ import EventDispatcher, {
PARTICLE_UPDATE,
} from '../events';
import { INTEGRATION_TYPE_EULER, integrate } from '../math';
import { Util, uid } from '../utils';

import { InitializerUtil } from '../initializer';
import Particle from '../core/Particle';
import Util from '../utils/Util';
import isNumber from 'lodash/isNumber';
import { EMITTER_TYPE_EMITTER as type } from './types';
import uid from '../utils/uid';

/**
* Emitters are the System engine's particle factories. They cause particles to
Expand Down Expand Up @@ -178,12 +178,12 @@ export default class Emitter extends Particle {
*/
emit(totalEmitTimes = Infinity, life = Infinity) {
this.currentEmitTime = 0;
this.totalEmitTimes = totalEmitTimes || Infinity;
this.totalEmitTimes = isNumber(totalEmitTimes) ? totalEmitTimes : Infinity;

if (totalEmitTimes === 1) {
this.life = totalEmitTimes;
} else {
this.life = life || Infinity;
this.life = isNumber(life) ? life : Infinity;
}

this.rate.init();
Expand Down
16 changes: 16 additions & 0 deletions test/behaviour/Behaviour.spec.js
Expand Up @@ -33,6 +33,22 @@ describe('behaviour -> Behaviour', () => {
done();
});

it('should set life to infinity if null is supplied', done => {
const { life } = new Behaviour(null);

assert.strictEqual(life, Infinity);

done();
});

it('should set life to infinity if NaN is supplied', done => {
const { life } = new Behaviour('null');

assert.strictEqual(life, Infinity);

done();
});

it('should normalize force correctly', done => {
const force = behaviour.normalizeForce(new Nebula.Vector3D(1, 2.4, 3));

Expand Down
22 changes: 22 additions & 0 deletions test/emitter/Emitter.spec.js
Expand Up @@ -121,6 +121,28 @@ describe('emitter -> Emitter', () => {
done();
});

it('should set the life to 0 if a life of 0 is provided', done => {
const emitter = new Emitter();

emitter.emit(2, 0);

assert.equal(emitter.totalEmitTimes, 2);
assert.equal(emitter.life, 0);

done();
});

it('should set the totalEmitTimes to 0 if a life of 0 is provided', done => {
const emitter = new Emitter();

emitter.emit(0, 1);

assert.equal(emitter.totalEmitTimes, 0);
assert.equal(emitter.life, 1);

done();
});

it('should set the correct properties to stop particles emitting', done => {
const emitter = new Emitter();

Expand Down

0 comments on commit c8387f4

Please sign in to comment.