Skip to content
Snippets Groups Projects
Commit 977b9c24 authored by Jonas Zohren's avatar Jonas Zohren :speech_balloon:
Browse files

Chore: Improve type docs

parent 1898b1b4
No related branches found
No related tags found
No related merge requests found
<script lang="ts">
import { gameFactsStore, addGameFactToFactArray, toggleFactInFactArray } from './gameFacts'
import type { Dialog, DialogMap, DialogSet, FactId } from './types';
import type { Dialog, DialogMap, DialogSet } from './types';
export let currentDialog: Dialog;
export let dialogSet: DialogSet;
$: dialogNames = Object.keys(dialogSet.dialogs)
export let selectedDialogName: string
function getDialogFacts(dialogMap: DialogMap): FactId[] {
function getDialogFacts(dialogMap: DialogMap): String[] {
const dialogs = [];
for (const dmKey of Object.keys(dialogMap)) dialogs.push(dialogMap[dmKey]);
return dialogs.map(dialog => [...(dialog.addFacts || []), ...(dialog.removeFacts || [])]).flat()
}
let seenFactIds = new Set<FactId>([...$gameFactsStore, ...getDialogFacts(dialogSet.dialogs)]);
let seenFactIds = new Set<String>([...$gameFactsStore, ...getDialogFacts(dialogSet.dialogs)]);
$: seenFactIdsArray = Array.from(seenFactIds)
let addFactInputValue: string;
</script>
......
<script lang="ts">
import type { DialogOption, FactId } from "./types";
import type { DialogOption } from "./types";
import { createEventDispatcher } from "svelte";
import { gameFactsStore, addGameFactToFactArray } from './gameFacts'
......@@ -10,14 +10,14 @@
export let text: string;
export let options: DialogOption[] = [];
$: usableOptions = options.filter(option => isDialogOptionAllowedByGameFacts(option, $gameFactsStore))
export let addFacts: FactId[] = [];
export let addFacts: String[] = [];
$: {
if (Array.isArray(addFacts)) {
const gameFactsAfterAddedFacts = addFacts.reduce((accFacts, factToAdd) => addGameFactToFactArray(factToAdd, accFacts), $gameFactsStore)
$gameFactsStore = gameFactsAfterAddedFacts;
}
}
export let removeFacts: FactId[] = [];
export let removeFacts: String[] = [];
$: {
if (Array.isArray(removeFacts)) {
const gameFactsAfterRemovedFacts = removeFacts.reduce((accFacts, factToRemove) => accFacts.filter(f => f !== factToRemove), $gameFactsStore)
......@@ -25,7 +25,7 @@
}
}
function isDialogOptionAllowedByGameFacts(option: DialogOption, gameFacts: FactId[]): boolean {
function isDialogOptionAllowedByGameFacts(option: DialogOption, gameFacts: String[]): boolean {
// Check if all required facts are given
if (Array.isArray(option.requiredFacts)) {
const isOk = option.requiredFacts.every(requiredFact => gameFacts.includes(requiredFact));
......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Dialog": {
"description": "A dialog situation that a player can encounter in this dialogSet.\n\nPrinciple: NPC says something, user has options how to react.",
"properties": {
"addFacts": {
"description": "Upon entering this dialog, register these facts for the user.\n\nFacts are texts and may unlock or block dialog options across the game.\nE.g. The fact \"acceptedMissionX\" could unlock a dialog option regarding Mission X with another NPC.",
"items": {
"additionalProperties": false,
"description": "Allows manipulation and formatting of text strings and determination and location of substrings within strings.",
"patternProperties": {
"^[0-9]+$": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
},
"imageUrl": {
"description": "Can be used to overwrite imageUrl from DialogSet.\nE.g. when a different emotion should be shown for this dialog,",
"type": "string"
},
"options": {
"description": "Choices of what the user might say in this dialog and where those choices lead to.\nThe player should be given between 0 and 4 options.",
"items": {
"$ref": "#/definitions/DialogOption"
},
"type": "array"
},
"removeFacts": {
"description": "Upon entering this dialog, remove these facts from the user.\n\nFacts are texts and may unlock or block dialog options across the game.\nE.g. The fact \"acceptedMissionX\" could unlock a dialog option regarding Mission X with another NPC.",
"items": {
"additionalProperties": false,
"description": "Allows manipulation and formatting of text strings and determination and location of substrings within strings.",
"patternProperties": {
"^[0-9]+$": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
},
"text": {
"description": "What the characters says to the player.\nLine breaks (\\n) are rendered as paragraphs.\nYou may leave this empty or omit it completely.",
"type": "string"
},
"title": {
"description": "The title is displayed at the very top of the dialog screen\nand usually corresponds to the name of the NPC the player is speaking to.\n\nCan be used to overwrite title from DialogSet.",
"type": "string"
}
},
"type": "object"
},
"DialogOption": {
"properties": {
"forbiddenFacts": {
"description": "All facts in this array must *not* be registered for this player for this option to show up.\n\nFacts are texts and may unlock or block dialog options across the game.\nE.g. The fact \"acceptedMissionX\" could unlock a dialog option regarding Mission X with another NPC.",
"items": {
"additionalProperties": false,
"description": "Allows manipulation and formatting of text strings and determination and location of substrings within strings.",
"patternProperties": {
"^[0-9]+$": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
},
"linksToDialog": {
"description": "Which dialog in this dialogSet to jump to if this option is chosen.",
"type": "string"
},
"requiredFacts": {
"description": "All facts in this array must be registered for this player for this option to show up.\n\nFacts are texts and may unlock or block dialog options across the game.\nE.g. The fact \"acceptedMissionX\" could unlock a dialog option regarding Mission X with another NPC.",
"items": {
"additionalProperties": false,
"description": "Allows manipulation and formatting of text strings and determination and location of substrings within strings.",
"patternProperties": {
"^[0-9]+$": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
},
"text": {
"description": "Text on option button.\nShould be rather short and not have line breaks.",
"type": "string"
}
},
"type": "object"
}
},
"description": "Set of dialogs, beginning with startDialogName.\nThis should be",
"properties": {
"dialogs": {
"additionalProperties": {
"$ref": "#/definitions/Dialog"
},
"description": "All dialogs in this dialogSet.",
"type": "object"
},
"imageUrl": {
"description": "Url of image.\n\nShould link to image in this repo, to avoid CORS errors.",
"type": "string"
},
"startDialogName": {
"description": "At which dialog to start when the dialogSet is first opened by the player.",
"type": "string"
},
"title": {
"description": "The title is displayed at the very top of the dialog screen\nand usually corresponds to the name of the NPC the player is speaking to.",
"type": "string"
}
},
"type": "object"
}
import { writable } from "svelte-local-storage-store";
import type { Writable } from "svelte/store";
import type { FactId } from "./types";
export const gameFactsStore: Writable<FactId[]> = writable("gameFacts", [
export const gameFactsStore: Writable<String[]> = writable("gameFacts", [
"debugRequiredFact",
]);
export function addGameFactToFactArray(
gameFact: FactId,
factArray: FactId[]
): FactId[] {
gameFact: String,
factArray: String[]
): String[] {
if (!factArray.includes(gameFact)) {
return [...factArray, gameFact];
} else {
......@@ -18,9 +17,9 @@ export function addGameFactToFactArray(
}
export function toggleFactInFactArray(
gameFact: FactId,
factArray: FactId[]
): FactId[] {
gameFact: String,
factArray: String[]
): String[] {
if (!factArray.includes(gameFact)) {
return [...factArray, gameFact];
} else {
......
/**
* Set of dialogs, beginning with startDialogName.
* This should be
*/
export interface DialogSet {
/**
* The title is displayed at the very top of the dialog screen
* and usually corresponds to the name of the NPC the player is speaking to.
*/
title?: string;
/**
* Url of image.
*
* Should link to image in this repo, to avoid CORS errors.
*/
imageUrl: string;
/**
* At which dialog to start when the dialogSet is first opened by the player.
*/
startDialogName: string;
/**
* All dialogs in this dialogSet.
*/
dialogs: DialogMap;
}
......@@ -9,20 +28,73 @@ export type DialogMap = {
[key: string]: Dialog;
};
/**
* A dialog situation that a player can encounter in this dialogSet.
*
* Principle: NPC says something, user has options how to react.
*/
export interface Dialog {
title?: string; // Can be used to overwrite title from DialogSet
imageUrl?: string; // Can be used to overwrite imageUrl from DialogSet
/**
* The title is displayed at the very top of the dialog screen
* and usually corresponds to the name of the NPC the player is speaking to.
*
* Can be used to overwrite title from DialogSet.
*/
title?: string;
/**
* Can be used to overwrite imageUrl from DialogSet.
* E.g. when a different emotion should be shown for this dialog,
*/
imageUrl?: string;
/**
* What the characters says to the player.
* Line breaks (\n) are rendered as paragraphs.
* You may leave this empty or omit it completely.
*/
text?: string;
/**
* Choices of what the user might say in this dialog and where those choices lead to.
* The player should be given between 0 and 4 options.
*/
options: DialogOption[];
addFacts?: FactId[];
removeFacts?: FactId[];
/**
* Upon entering this dialog, register these facts for the user.
*
* Facts are texts and may unlock or block dialog options across the game.
* E.g. The fact "acceptedMissionX" could unlock a dialog option regarding Mission X with another NPC.
*/
addFacts?: String[];
/**
* Upon entering this dialog, remove these facts from the user.
*
* Facts are texts and may unlock or block dialog options across the game.
* E.g. The fact "acceptedMissionX" could unlock a dialog option regarding Mission X with another NPC.
*/
removeFacts?: String[];
}
export interface DialogOption {
/**
* Text on option button.
* Should be rather short and not have line breaks.
*/
text: string;
/**
* Which dialog in this dialogSet to jump to if this option is chosen.
*/
linksToDialog: string;
requiredFacts?: FactId[];
forbiddenFacts?: FactId[];
/**
* All facts in this array must be registered for this player for this option to show up.
*
* Facts are texts and may unlock or block dialog options across the game.
* E.g. The fact "acceptedMissionX" could unlock a dialog option regarding Mission X with another NPC.
*/
requiredFacts?: String[];
/**
* All facts in this array must *not* be registered for this player for this option to show up.
*
* Facts are texts and may unlock or block dialog options across the game.
* E.g. The fact "acceptedMissionX" could unlock a dialog option regarding Mission X with another NPC.
*/
forbiddenFacts?: String[];
}
export type FactId = String;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment