Skip to content

Commit

Permalink
Eliminate dependency on Property for update tick (#5)
Browse files Browse the repository at this point in the history
* Eliminate dependency on Property for update tick

* update language in comments on GameTime
  • Loading branch information
ericente authored Nov 20, 2020
1 parent affba6a commit c38d147
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 24 deletions.
44 changes: 34 additions & 10 deletions dist/gamelib.js

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

20 changes: 17 additions & 3 deletions dts/timer/GameTime.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
/**
*
* GameTime is a relay singleton that any object can hook into via its SpringRoll Property to get the next tick (gameTick) of the game clock.
* GameTime is a relay singleton that any object can hook into via its static subscribe() method to get the next tick of the game clock.
* Its update() should be called on any live tick of the game; determining whether the tick is live (e.g. checking paused) should happen elsewhere.
*
* Call in the game's main tick/update function, using the singleton syntax on the class - GameTime.Instance.update(deltaTime);
* Subscribe to changes using singleton syntax on the class - GameTime.Instance.gameTick.subscribe(callbackfunction)
* Call in the game's main tick/update function, using the static method on the class - GameTime.update(deltaTime);
* Subscribe to changes using static method on the class - GameTime.subscribe(callbackfunction)
*
*/
import { Property } from "springroll";
export default class GameTime {
static listeners: ((elapsed?: number) => any)[];
/**
* @deprecated use GameTime.subscribe() and GameTime.unsubscribe() directly instead
*/
static gameTick: Property<number>;
static update(deltaTime: number): void;
/**
* Adds an update listener
* @param {function} callback The listener to call every frame update
*/
static subscribe(callback: (elapsed?: number) => any): void;
/**
* Removes an update listener
* @param {function} callback The listener to unsubscribe.
*/
static unsubscribe(callback: (elapsed?: number) => any): void;
static destroy(): void;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"rollup-plugin-terser": "^3.0.0",
"rollup-plugin-tslint": "^0.1.38",
"rollup-plugin-typescript2": "^0.20.1",
"springroll": "^2.0.0",
"springroll": "^2.3.0",
"tslib": "^1.9.3",
"typedoc": "^0.14.2",
"typescript": "^3.2.2"
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/StageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export default class StageManager{
if (this.captions) {
this.captions.update(elapsed/1000); // captions go by seconds, not ms
}
GameTime.gameTick.value = elapsed;
GameTime.update(elapsed);
if (this.transitioning || !this._currentScene){
return;
}
Expand Down
39 changes: 34 additions & 5 deletions src/timer/GameTime.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
/**
*
* GameTime is a relay singleton that any object can hook into via its SpringRoll Property to get the next tick (gameTick) of the game clock.
* GameTime is a relay singleton that any object can hook into via its static subscribe() method to get the next tick of the game clock.
* Its update() should be called on any live tick of the game; determining whether the tick is live (e.g. checking paused) should happen elsewhere.
*
* Call in the game's main tick/update function, using the singleton syntax on the class - GameTime.Instance.update(deltaTime);
* Subscribe to changes using singleton syntax on the class - GameTime.Instance.gameTick.subscribe(callbackfunction)
* Call in the game's main tick/update function, using the static method on the class - GameTime.update(deltaTime);
* Subscribe to changes using static method on the class - GameTime.subscribe(callbackfunction)
*
*/
import { Property } from "springroll";


export default class GameTime {
static gameTick: Property<number> = new Property(0);

static listeners:((elapsed?:number)=>any)[] = [];

/**
* @deprecated use GameTime.subscribe() and GameTime.unsubscribe() directly instead
*/
static gameTick: Property<number> = new Property(0, true);

static update(deltaTime:number){
GameTime.gameTick.value = deltaTime;
for (let i = 0; i < this.listeners.length; i++) {
this.listeners[i](deltaTime);
}

if(GameTime.gameTick.hasListeners){
GameTime.gameTick.value = deltaTime;
}
}

/**
* Adds an update listener
* @param {function} callback The listener to call every frame update
*/
static subscribe(callback:(elapsed?:number)=>any) {
GameTime.listeners.push(callback);
}

/**
* Removes an update listener
* @param {function} callback The listener to unsubscribe.
*/
static unsubscribe(callback:(elapsed?:number)=>any) {
GameTime.listeners = GameTime.listeners.filter(listener => listener !== callback);
}

static destroy(){
GameTime.listeners.length = 0;
GameTime.gameTick.value = null;
}
}
4 changes: 2 additions & 2 deletions src/timer/PauseableTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class PauseableTimer {
this.onComplete = callback;

this.repeat = loop;
GameTime.gameTick.subscribe(this.update);
GameTime.subscribe(this.update);
PauseableTimer.timers.push(this);
}

Expand Down Expand Up @@ -87,7 +87,7 @@ export default class PauseableTimer {
this.reject = null;
this.targetTime = null;
this.onComplete = null;
GameTime.gameTick.unsubscribe(this.update);
GameTime.unsubscribe(this.update);
PauseableTimer.timers.splice(PauseableTimer.timers.indexOf(this), 1);
}
}
4 changes: 2 additions & 2 deletions src/tween/Tween.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Tween{
tween.onComplete = options.onComplete;
}
Tween.tweens.push(tween);
GameTime.gameTick.subscribe(tween.update);
GameTime.subscribe(tween.update);
return tween;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ export default class Tween{
}

destroy(){
GameTime.gameTick.unsubscribe(this.update);
GameTime.unsubscribe(this.update);
Tween.tweens.splice(Tween.tweens.indexOf(this), 1);
this.target = null;
this.steps = null;
Expand Down

0 comments on commit c38d147

Please sign in to comment.