From 8e4c68ef1e3ed1c9e2b170dc522332e161148f01 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Tue, 18 Apr 2023 23:20:45 -0500 Subject: [PATCH] Add prestige mechanic recipe --- docs/.vitepress/config.js | 1 + docs/guide/recipes/prestige.md | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 docs/guide/recipes/prestige.md diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index e9adb4c2..dd6938cf 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -75,6 +75,7 @@ module.exports = { text: "Recipes", collapsed: false, items: [ + { text: "Prestige Mechanic", link: "/guide/recipes/prestige" }, { text: "Display Save Progress", link: "/guide/recipes/save-progress" }, { text: "Display Particle Effect", link: "/guide/recipes/particles" } ] diff --git a/docs/guide/recipes/prestige.md b/docs/guide/recipes/prestige.md new file mode 100644 index 00000000..42370707 --- /dev/null +++ b/docs/guide/recipes/prestige.md @@ -0,0 +1,60 @@ +# Prestige Mechanic + +Prestige mechanics are a common feature within incremental games. They involve resetting some amount of progress within the game in exchange for a new currency that persists between these resets. Many games will even have multiple layers of reset mechanics that each reset everything before them. + +## Create a conversion + +The first step is creating a conversion that'll handle calculating how much of the prestige currency gets generated. For our example, let's assume there's a base layer called `main` and a prestige layer called `prestige`. This conversion will be based on the amount of `points` the player has, a resource within `main`. Here's an example conversion, using the formula from [The Prestige Tree](https://jacorb90.me/Prestige-Tree/)'s prestige layer: + +```ts +const conversion = createCumulativeConversion(() => ({ + baseResource: main.points, + gainResource: noPersist(points), + formula: x => x.div(10).sqrt() +})); +``` + +Keep in mind `points` here refers to `prestige.points` since it's within the `prestige` layer. + +## Create a reset button + +Next let's create a button that will perform the reset and generate points. We'll create a clickable, like so: + +```ts +const convertButton = createClickable(() => ({ + requirements: createBooleanRequirement(() => Decimal.gt(unref(conversion.actualGain), 0)), + display: { + description: () => `Reset for ${formatWhole(conversion.currentGain)} prestige points` + }, + onClick() { + conversion.convert(); + } +})); +``` + +This creates a simple clickable that can only be clicked when we'll gain some amount of the prestige currency, and it tells us how much we'll get. This could be further improved by making the display convey how many points the player will need to generate 1 more prestige point. + +## Resetting previous content + +Now we need to go back to the `main` layer. Here we'll create a `reset` feature that gets a list of all the things it needs to reset - for this example, we'll just reset the entire layer: + +```ts +const reset = createReset(() => ({ + thingsToReset: [layer] +})); +``` + +And then we can modify the convert button to trigger the reset: + +```ts +onClick() { + conversion.convert(); + reset.reset(); +} +``` + +If you have multiple tiers of resets, you may add a `onReset` function to each reset that triggers the next reset before it. That will cause the resets to cascade. + +## Branching resets + +A common pattern for reset mechanics is to display these various layers in a tree and have the tree propagate the resets for you. There are a couple utility functions to help you accomplish this: [createResetButton](../../api/modules/data/common#createresetbutton) and [createLayerTreeNode](../../api/modules/data/common#createlayertreenode). You'll typically place the tree itself in the `main` layer, and create a tree node for each layer. Then you can call the tree's `reset` function with the layer that's resetting and it'll propagate the reset as appropriate.