added callback hooks for story start and finish

This commit is contained in:
Rudis Muiznieks 2020-05-09 15:22:21 -05:00
parent f8876c7175
commit 826b0abe04
4 changed files with 19 additions and 1 deletions

View File

@ -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.
- `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.
- `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.

View File

@ -1,6 +1,6 @@
{
"name": "ficdown.js",
"version": "2.0.2",
"version": "2.0.3",
"description": "A parser and player for Interactive Fiction written in Ficdown",
"scripts": {
"build": "rm -rf ./build && tsc",

View File

@ -5,4 +5,6 @@ export type PlayerOptions = {
html?: boolean, // allow html in story source
startText?: string, // custom link text to start game
endMarkdown?: string, // custom markdown when game ends
start?: () => void, // callback when the game starts
finish?: () => void, // callback when the game finishes
}

View File

@ -21,6 +21,10 @@ export class Player {
private startText: string;
private endMarkdown: string;
private startCallback?: () => void;
private finishCallback?: () => void;
private started: boolean = false;
constructor(private options: PlayerOptions) {
this.converter = new Markdown({
html: options.html,
@ -38,6 +42,9 @@ export class Player {
}
this.container = $(`#${ options.id }`);
this.container.addClass('ficdown').data('player', this);
this.startCallback = options.start;
this.finishCallback = options.finish;
}
public play(): void {
@ -47,6 +54,10 @@ export class Player {
}
public handleHref(href: string): false {
if (this.startCallback && !this.started) {
this.started = true;
this.startCallback();
}
const match = Util.matchHref(href);
let matchedScene: Scene | undefined = undefined;
const actions: Action[] = [];
@ -182,6 +193,9 @@ export class Player {
player.play();
return false;
});
if (this.finishCallback) {
this.finishCallback();
}
}
}
}