Skip to content

Commit

Permalink
fix: Improve timer docs + remove old constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Sep 9, 2024
1 parent fa62f5a commit a99f25f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Changes

- `ex.Timer` now only takes the option bag constructor
- `PreDrawEvent`, `PostDrawEvent`, `PreTransformDrawEvent`, `PostTransformDrawEvent`, `PreUpdateEvent`, `PostUpdateEvent` now use `elapsedMs` instead of `delta` for the elapsed milliseconds between the last frame.460696
- `Trigger` API has been slightly changed:
- `action` now returns the triggering entity: `(entity: Entity) => void`
Expand Down
66 changes: 35 additions & 31 deletions src/engine/Timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,33 @@ import * as ex from './index';
import { Random } from './Math/Random';

export interface TimerOptions {
/**
* If true the timer repeats every interval infinitely
*/
repeats?: boolean;
/**
* If a number is specified then it will only repeat a number of times
*/
numberOfRepeats?: number;
/**
* @deprecated use action: () => void, will be removed in v1.0
*/
fcn?: () => void;
/**
* Action to perform every time the timer fires
*/
action?: () => void;
/**
* Interval in milliseconds for the timer to fire
*/
interval: number;
/**
* Optionally specify a random range of milliseconds for the timer to fire
*/
randomRange?: [number, number];
/**
* Optionally provide a random instance to use for random behavior, otherwise a new random will be created seeded from the current time.
*/
random?: ex.Random;
}

Expand Down Expand Up @@ -46,31 +68,13 @@ export class Timer {

public scene: Scene = null;

/**
* @param options Options - repeats, numberOfRepeats, fcn, interval
* @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,
randomRange?: [number, number],
random?: ex.Random
) {
if (typeof fcn !== 'function') {
const options = fcn;
fcn = options.fcn;
interval = options.interval;
repeats = options.repeats;
numberOfRepeats = options.numberOfRepeats;
randomRange = options.randomRange;
random = options.random;
}
constructor(options: TimerOptions) {
const fcn = options.action ?? options.fcn;
const interval = options.interval;
const repeats = options.repeats;
const numberOfRepeats = options.numberOfRepeats;
const randomRange = options.randomRange;
const random = options.random;

if (!!numberOfRepeats && numberOfRepeats >= 0) {
this.maxNumberOfRepeats = numberOfRepeats;
Expand Down Expand Up @@ -103,18 +107,18 @@ export class Timer {

/**
* Adds a new callback to be fired after the interval is complete
* @param fcn The callback to be added to the callback list, to be fired after the interval is complete.
* @param action The callback to be added to the callback list, to be fired after the interval is complete.
*/
public on(fcn: () => void) {
this._callbacks.push(fcn);
public on(action: () => void) {
this._callbacks.push(action);
}

/**
* Removes a callback from the callback list to be fired after the interval is complete.
* @param fcn The callback to be removed from the callback list, to be fired after the interval is complete.
* @param action The callback to be removed from the callback list, to be fired after the interval is complete.
*/
public off(fcn: () => void) {
const index = this._callbacks.indexOf(fcn);
public off(action: () => void) {
const index = this._callbacks.indexOf(action);
this._callbacks.splice(index, 1);
}
/**
Expand Down

0 comments on commit a99f25f

Please sign in to comment.