Skip to content

Commit

Permalink
Add ability to select variable duration
Browse files Browse the repository at this point in the history
Used /Util/Math to calculate randomness.
This would set a random interval after receiving a range  of numbers

Resolves: excaliburjs#396
  • Loading branch information
malitherl committed May 16, 2022
1 parent 429fcb7 commit 382d134
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added new display mode `ex.DisplayMode.FitScreenAndFill`.
- Added new display mode `ex.DisplayMode.FitContainerAndZoom`.
- Added new display mode `ex.DisplayMode.FitScreenAndZoom`.

- Added the ability to select variable duration into Timer constructor.
### Fixed

- Fixed unreleased bug where CompositeCollider components would not collide appropriately because contacts did not have unique ids
Expand Down
63 changes: 42 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions src/engine/Timer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Scene } from './Scene';
import { Logger } from './Util/Log';

import { Random } from './Math/Random';
export interface TimerOptions {
repeats?: boolean;
numberOfRepeats?: number;
fcn?: () => void;
interval: number;
randomRange?: [number, number];
}

/**
Expand All @@ -29,6 +30,7 @@ export class Timer {
public repeats: boolean = false;
public maxNumberOfRepeats: number = -1;

public randomRange: Array<number> = [0,0];
private _complete = false;
public get complete() {
return this._complete;
Expand All @@ -40,15 +42,18 @@ export class Timer {
* @param repeats Indicates whether this call back should be fired only once, or repeat after every interval as completed.
* @param numberOfRepeats Specifies a maximum number of times that this timer will execute.
* @param fcn The callback to be fired after the interval is complete.
* @param randomRange Indicates a range to select a random number to be added onto the interval
*/
constructor(options: TimerOptions);
constructor(fcn: TimerOptions | (() => void), interval?: number, repeats?: boolean, numberOfRepeats?: number) {
constructor(fcn: TimerOptions | (() => void), interval?: number,
repeats?: boolean, numberOfRepeats?: number, randomRange?: [number, number]) {
if (typeof fcn !== 'function') {
const options = fcn;
fcn = options.fcn;
interval = options.interval;
repeats = options.repeats;
numberOfRepeats = options.numberOfRepeats;
randomRange = options.randomRange;
}

if (!!numberOfRepeats && numberOfRepeats >= 0) {
Expand All @@ -59,7 +64,17 @@ export class Timer {
}

this.id = Timer._MAX_ID++;
this.interval = interval || this.interval;
if (!!randomRange){
if(randomRange[0] > randomRange[1]){
throw new Error('min value must be lower than max value for range');
} else {
this.interval = interval + new Random().integer(randomRange[0], randomRange[1]);
}
} else {
this.interval = interval;
}


this.repeats = repeats || this.repeats;

this._callbacks = [];
Expand Down
27 changes: 27 additions & 0 deletions src/spec/TimerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,31 @@ describe('A Timer', () => {
// assert
expect(count).toBe(1);
});

it('can be initialized with random time range', ()=> {
const timer = new ex.Timer({
interval: 100,
fcn: () => {/**Do nothing*/} ,
randomRange: [0, 200]
})
scene.add(timer);
timer.start();
scene.update(engine, 100);
expect(timer.complete).toBeFalse;
})

it('is no longer random after reset', ()=>{
const timer = new ex.Timer({
interval: 500,
randomRange: [0, 100]
})
scene.add(timer);
timer.start();
timer.reset(700);
timer.start();
scene.update(engine, 500)
expect(timer.complete).toBeFalse;
})

});

0 comments on commit 382d134

Please sign in to comment.