profectus-docs/docs/guide/recipes/prestige.md

61 lines
2.8 KiB
Markdown
Raw Normal View History

2023-04-19 04:20:45 +00:00
# Prestige Mechanic
2023-04-19 05:05:50 +00:00
Prestige mechanics are a common feature in incremental games. They involve resetting a portion of the game's progress in exchange for a new currency that persists between these resets. Many games even have multiple layers of reset mechanics, each resetting everything that came before them.
2023-04-19 04:20:45 +00:00
## Create a conversion
2023-04-19 05:05:50 +00:00
The first step is to create a conversion that will handle calculating the amount of prestige currency generated. In 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:
2023-04-19 04:20:45 +00:00
```ts
const conversion = createCumulativeConversion(() => ({
baseResource: main.points,
gainResource: noPersist(points),
formula: x => x.div(10).sqrt()
}));
```
2023-04-19 05:05:50 +00:00
In this case, `points` refers to `prestige.points`, as it's within the `prestige` layer.
2023-04-19 04:20:45 +00:00
## Create a reset button
2023-04-19 05:05:50 +00:00
Next, create a button that will perform the reset and generate points. Create a clickable like this:
2023-04-19 04:20:45 +00:00
```ts
const convertButton = createClickable(() => ({
requirements: createBooleanRequirement(() => Decimal.gt(unref(conversion.actualGain), 0)),
display: {
description: () => `Reset for ${formatWhole(conversion.currentGain)} prestige points`
},
onClick() {
conversion.convert();
}
}));
```
2023-04-19 05:05:50 +00:00
This creates a simple clickable that can only be clicked when the player will gain some amount of the prestige currency, and it displays how much they'll get. This can be further improved by displaying how many points the player will need to generate one more prestige point.
2023-04-19 04:20:45 +00:00
## Resetting previous content
2023-04-19 05:05:50 +00:00
Now, return to the `main` layer. Create a `reset` feature that receives a list of all the things it needs to reset - in this example, we'll just reset the entire layer:
2023-04-19 04:20:45 +00:00
```ts
const reset = createReset(() => ({
thingsToReset: [layer]
}));
```
2023-04-19 05:05:50 +00:00
Then, modify the convert button to trigger the reset:
2023-04-19 04:20:45 +00:00
```ts
onClick() {
conversion.convert();
reset.reset();
}
```
2023-04-19 05:05:50 +00:00
If you have multiple tiers of resets, you may add an `onReset` function to each reset that triggers the next reset before it, causing the resets to cascade.
2023-04-19 04:20:45 +00:00
## Branching resets
2023-04-19 05:05:50 +00:00
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 of 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 will propagate the reset as appropriate.