-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: animations, better docs, new dialog panel options (#170)
* feat: animations and docs and things * feat: animations working * feat: docs updates * chore: docs and inputs improvements
- Loading branch information
Showing
63 changed files
with
983 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
--- | ||
title: Animations in Narrat | ||
description: Narrat has a powerful animations feature which lets you animate any element on screen using CSS animations. | ||
--- | ||
|
||
# {{ $frontmatter.title }} | ||
|
||
Narrat animations leverage the versatility of [CSS animations](https://www.w3schools.com/css/css3_animations.asp) to let you animate any element on screen. | ||
|
||
Animations can be defined in the `animations.yaml` file, and then used in scripts with the `animate` command. | ||
|
||
## Example | ||
|
||
<video controls="controls" src="./animations/animations-demo.mp4" type="video/mp4" autoplay="true"></video> | ||
|
||
### Code for the example above: | ||
|
||
**config.yaml** | ||
|
||
```yaml | ||
animations: data/animations.yaml | ||
``` | ||
**animations.yaml** | ||
```yaml | ||
animations: | ||
rotate: | ||
keyframes: rotate | ||
options: | ||
duration: 1000 | ||
iterations: 1 | ||
long-screenshake: | ||
# narrat-screenshake is a built-in animation that already comes with narrat | ||
keyframes: narrat-screenshake | ||
options: | ||
# Here we're taking the built-in screenshake but using it with different options | ||
duration: 150 | ||
iterations: 5 | ||
|
||
keyframes: | ||
rotate: | ||
- transform: rotate(0deg) | ||
- transform: rotate(360deg) | ||
``` | ||
**animations.narrat** | ||
```narrat | ||
test_animations: | ||
choice: | ||
"Animation test" | ||
"Screenshake": | ||
run test_screenshake | ||
"Rotate": | ||
run test_rotate | ||
"Long screenshake": | ||
run long_screenshake | ||
jump test_animations | ||
|
||
test_screenshake: | ||
animate #narrat-app narrat-screenshake | ||
|
||
long_screenshake: | ||
animate .dialog long-screenshake 150 20 | ||
test_rotate: | ||
animate_wait .dialog rotate | ||
``` | ||
## How it works | ||
Animations in narrat use the [animate](https://developer.mozilla.org/en-US/docs/Web/API/Element/animate) browser function. This function is an API to control [CSS animations](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Using_CSS_animations) via code. | ||
Narrat will find the relevant element via the DOM selector you provide, then find the animation data from config files, and finally call the `animate` function with the relevant parameters. | ||
|
||
## How to use the `animate` command | ||
|
||
The format for the `animate` command in narrat is the following: | ||
|
||
`animate [DOM selector] [animation name] [duration (optional)] [iterations (optional)]` | ||
|
||
- `DOM selector`: A CSS selector to select the element to animate. If you want to animate the whole game screen, you can use `#narrat-app` as the selector for example. Use `#my-id` to get an element by its id, or `.my-class` to get an element by its class. With this you should be able to get most things on screen easily | ||
- `animation name`: The name of the animation to use. This is the name of the animation as defined in the animations list of your `animations.yaml` file. If an animation doesn't exist, it can fall back to default built-in animations | ||
- `duration`: The duration of the animation in milliseconds. This is optional and will default to the duration defined in the animation config | ||
- `iterations`: The number of times the animation should repeat. This is optional and will default to the number of iterations defined in the animation config (note: to specify this option, you have to also specify duration) | ||
|
||
::: tip | ||
There is an `animate_wait` command, which works exactly the same but will wait until the animation is over. By default, `animate` will play the animation in background and continue the script immediately. | ||
::: | ||
|
||
## Animation config | ||
|
||
Animations are defined in the `animations.yaml` file. The format is the following: | ||
|
||
```yaml | ||
animations: | ||
[animation name]: | ||
keyframes: [keyframes name OR keyframes object] # If it's a string, it will pick keyframes from the keyframes list. If it's an object, it will use the object directly | ||
options: [animation options] | ||
keyframes: | ||
[keyframes name]: # This is an array of keyframes | ||
- [keyframe object] # Those are individual steps of the animation. See the mozilla animation docs for examples | ||
- [keyframe object] | ||
- [keyframe object] | ||
``` | ||
|
||
- `animation options`: Options when playing the animation. Can include things like `duration` (in ms) and `iterations` (number of times the animation is played). See list of animation options [there](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options) | ||
- `keyframe object`: An object defining one key frame of the animation. The keyframes array is a list of them, and they get played in order as the animation progresses. Keyframes have keys that are CSS properties and their values, like when using css transitions. They can also have an `offset` property to specify when in the animation they should be played. See [mozilla docs](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats) for examples | ||
|
||
## Built-in animations | ||
|
||
At time of writing there is only the `narrat-screenshake` animation. This is a simple animation that just shakes an element for a bit. | ||
|
||
## Code for more info: | ||
|
||
- [utils/animations.ts](https://github.com/liana-p/narrat-engine/blob/main/packages/narrat/src/utils/animations.ts): File where the animation code is and where the default built-in animations are defined. | ||
- [config/animations-config.ts](https://github.com/liana-p/narrat-engine/blob/main/packages/narrat/src/config/animations-config.ts): File definine the shape of the animations config and their options | ||
|
||
## Relevant external documentation | ||
|
||
- [Mozilla MDN page on animate](https://developer.mozilla.org/en-US/docs/Web/API/Element/animate): Explains how the animation API works with some example | ||
- [Keyframe formats](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats): Explains how to define keyframes | ||
- [Keyframe options](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options) list of available keyframe options |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# The Dialog Panel | ||
|
||
The Dialog Panel is where all the text in the game appears. When characters talk, the dialog panel opens, and shows what they say. It is also where the player can make choices and advance in the story. | ||
|
||
The dialog panel is where most of the content of a Narrat game usually takes place, while the viewport below it is where visuals are displayed. Sometimes the viewport can have clickable buttons or sprites, but most of the interaction tends to take place in the dialog panel. | ||
|
||
![Dialog Panel screenshot](./dialog-panel/dialog-panel.png) | ||
|
||
## How the dialog panel works | ||
|
||
Whenever the game is **doing something** (that is, running narrat script), the dialog panel will appear. If the narrat script ends, the dialog panel will close. | ||
|
||
::: NOTE | ||
This doesn't apply to games in overlay mode, or on mobile, where the dialog panel is always visible. | ||
::: | ||
|
||
## Dialog panel config options | ||
|
||
The dialog panel has the following options available, which are set in the `dialogPanel` part of `config.yaml`: | ||
|
||
```yaml | ||
textSpeed: 30 | ||
animateText: true | ||
timeBetweenLines: 100 | ||
overlayMode: true | ||
rightOffset: 100 | ||
bottomOffset: 50 | ||
width: 475 | ||
height: 680 | ||
hideDuringTransition: false | ||
showAfterScriptEnd: false | ||
``` | ||
- `textSpeed`: [number] The speed at which text appears in the dialog panel, in characters per second. Defaults to 30. If `animateText` is off, this still controls the speed of auto play. Players can override this setting in the system menu | ||
- `animateText`: [boolean] Whether text should appear one character at a time. Defaults to true. Players can override this setting in the system menu | ||
- `timeBetweenLines`: [number] Time to wait between two lines of dialog in auto mode | ||
- `overlayMode`: [boolean] Whether the dialog panel should be in overlay mode. Defaults to true. Overlay mode means that the dialog panel will appear on top of the viewport. In games where overlay mode is off, the dialog panel is instead to the side of the viewport | ||
- `rightOffset`: [number] How far from the right edge of the screen the dialog panel should be, in pixels. | ||
- `bottomOffset`: [number] How far from the bottom edge of the screen the dialog panel should be, in pixels. | ||
- `width`: [number] The width of the dialog panel, in pixels. | ||
- `height`: [number] The height of the dialog panel, in pixels. | ||
- `hideDuringTransition`: [boolean] Whether the dialog panel should be hidden during screen transitions. Defaults to true. | ||
- `showAfterScriptEnd`: [boolean] Whether the dialog panel should stay on even if the narrat script ends. Defaults to false. | ||
|
||
## Controlling when the dialog panel appears | ||
|
||
The default behaviour of the dialog panel should be good enough for most games, but if your game needs finer control of it, there are a few ways to control it: | ||
|
||
The two config options `hideDuringTransition` and `showAfterScriptEnd` allow you to control whether the dialog panel should be hidden during screen transitions, and whether it should stay on even if the narrat script ends. | ||
|
||
You can also use the `set_dialog_panel_mode` command to manually force the dialog panel on or off: | ||
|
||
```narrat | ||
test_set_dialog_panel_mode: | ||
choice: | ||
"Which mode": | ||
"Auto (normal default behaviour)": | ||
set_dialog_panel_mode auto | ||
"Force always on": | ||
set_dialog_panel_mode on | ||
"Force always off (will go back on after a timer for testing)": | ||
set_dialog_panel_mode off | ||
wait 1500 | ||
set_dialog_panel_mode auto | ||
jump test_set_dialog_panel_mode | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.
ac9e567
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
narrat-editor-demo – ./packages/narrat-editor
narrat-editor-demo-git-main-nialna.vercel.app
demo.narrat.dev
narrat-editor-demo-nialna.vercel.app
narrat-editor-demo.vercel.app