added callback hooks for story start and finish
This commit is contained in:
parent
f8876c7175
commit
826b0abe04
4 changed files with 19 additions and 1 deletions
|
@ -40,3 +40,5 @@ Your `playerOptions` should be an object with the following properties:
|
||||||
- `html` (optional): Set this to true if your ficdown file contains raw html that you want rendered. By default html will not be rendered.
|
- `html` (optional): Set this to true if your ficdown file contains raw html that you want rendered. By default html will not be rendered.
|
||||||
- `startText` (optional): Set this to override the link text that begins the game.
|
- `startText` (optional): Set this to override the link text that begins the game.
|
||||||
- `endMarkdown` (optional): Set this to override the "game over" content that is displayed when the player reaches a scene with no more links to follow. Include an anchor with the href set to `restart()` if you want a link that will start the story over from the beginning.
|
- `endMarkdown` (optional): Set this to override the "game over" content that is displayed when the player reaches a scene with no more links to follow. Include an anchor with the href set to `restart()` if you want a link that will start the story over from the beginning.
|
||||||
|
- `start` (optional): Javascript callback function to execute when the user clicks the first link to begin the story.
|
||||||
|
- `finish` (optional): Javascript callback function to execute when the story has finished.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "ficdown.js",
|
"name": "ficdown.js",
|
||||||
"version": "2.0.2",
|
"version": "2.0.3",
|
||||||
"description": "A parser and player for Interactive Fiction written in Ficdown",
|
"description": "A parser and player for Interactive Fiction written in Ficdown",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rm -rf ./build && tsc",
|
"build": "rm -rf ./build && tsc",
|
||||||
|
|
|
@ -5,4 +5,6 @@ export type PlayerOptions = {
|
||||||
html?: boolean, // allow html in story source
|
html?: boolean, // allow html in story source
|
||||||
startText?: string, // custom link text to start game
|
startText?: string, // custom link text to start game
|
||||||
endMarkdown?: string, // custom markdown when game ends
|
endMarkdown?: string, // custom markdown when game ends
|
||||||
|
start?: () => void, // callback when the game starts
|
||||||
|
finish?: () => void, // callback when the game finishes
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,10 @@ export class Player {
|
||||||
private startText: string;
|
private startText: string;
|
||||||
private endMarkdown: string;
|
private endMarkdown: string;
|
||||||
|
|
||||||
|
private startCallback?: () => void;
|
||||||
|
private finishCallback?: () => void;
|
||||||
|
private started: boolean = false;
|
||||||
|
|
||||||
constructor(private options: PlayerOptions) {
|
constructor(private options: PlayerOptions) {
|
||||||
this.converter = new Markdown({
|
this.converter = new Markdown({
|
||||||
html: options.html,
|
html: options.html,
|
||||||
|
@ -38,6 +42,9 @@ export class Player {
|
||||||
}
|
}
|
||||||
this.container = $(`#${ options.id }`);
|
this.container = $(`#${ options.id }`);
|
||||||
this.container.addClass('ficdown').data('player', this);
|
this.container.addClass('ficdown').data('player', this);
|
||||||
|
|
||||||
|
this.startCallback = options.start;
|
||||||
|
this.finishCallback = options.finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
public play(): void {
|
public play(): void {
|
||||||
|
@ -47,6 +54,10 @@ export class Player {
|
||||||
}
|
}
|
||||||
|
|
||||||
public handleHref(href: string): false {
|
public handleHref(href: string): false {
|
||||||
|
if (this.startCallback && !this.started) {
|
||||||
|
this.started = true;
|
||||||
|
this.startCallback();
|
||||||
|
}
|
||||||
const match = Util.matchHref(href);
|
const match = Util.matchHref(href);
|
||||||
let matchedScene: Scene | undefined = undefined;
|
let matchedScene: Scene | undefined = undefined;
|
||||||
const actions: Action[] = [];
|
const actions: Action[] = [];
|
||||||
|
@ -182,6 +193,9 @@ export class Player {
|
||||||
player.play();
|
player.play();
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
if (this.finishCallback) {
|
||||||
|
this.finishCallback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue