-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eliminate dependency on Property for update tick (#5)
* Eliminate dependency on Property for update tick * update language in comments on GameTime
- Loading branch information
Showing
7 changed files
with
91 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters