From 826b0abe049d65554f63d57102702f4b521c1010 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Sat, 9 May 2020 15:22:21 -0500 Subject: [PATCH] added callback hooks for story start and finish --- README.md | 2 ++ package.json | 2 +- src/Model/PlayerOptions.ts | 2 ++ src/Player.ts | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1cc99f8..c9b3a33 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package.json b/package.json index 5c06a0c..72353c6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Model/PlayerOptions.ts b/src/Model/PlayerOptions.ts index 958596e..8f17cee 100644 --- a/src/Model/PlayerOptions.ts +++ b/src/Model/PlayerOptions.ts @@ -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 } diff --git a/src/Player.ts b/src/Player.ts index 69fef5a..999968b 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -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(); + } } } }