Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a basic TUI example for using yarnspinner standalone (fixes #123) #182

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"crates/codegen",
"demo",
"examples/bevy_yarnspinner",
"examples/yarnspinner_without_bevy",
]

# Source: https://github.com/bevyengine/bevy/blob/main/examples/README.md#1-tweak-your-cargotoml
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ mod yarn_value;

pub mod prelude {
//! Types and functions used all throughout the runtime and compiler.
#[cfg(any(feature = "bevy", feature = "serde"))]
pub use crate::feature_gates::*;

pub use crate::{
feature_gates::*,
generated::{
instruction::OpCode, operand::Value as OperandValue, Header, Instruction,
InvalidOpCodeError, Node, Operand, Program,
Expand Down
16 changes: 16 additions & 0 deletions examples/bevy_yarnspinner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ publish = false
bevy = "0.13"
bevy_yarnspinner = { path = "../../crates/bevy_plugin", version = "0.2" }
bevy_yarnspinner_example_dialogue_view = { path = "../../crates/example_dialogue_view", version = "0.2" }

[[bin]]
name = "access_variables"
doc = false

[[bin]]
name = "custom_command"
doc = false

[[bin]]
name = "custom_function"
doc = false

[[bin]]
name = "hello_world"
doc = false
32 changes: 32 additions & 0 deletions examples/yarnspinner_without_bevy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "yarnspinner_without_bevy_examples"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/YarnSpinnerTool/YarnSpinner-Rust"
homepage = "https://docs.yarnspinner.dev/"
keywords = ["gamedev", "dialog", "yarn", "bevy"]
categories = ["game-development", "compilers"]
authors = ["Joe Clay <[email protected]>"]
publish = false

[dependencies]
crossterm = "0.27"
ratatui = "0.26"
anyhow = "1.0"
yarnspinner = { path = "../../crates/yarnspinner", version = "0.2" }

[[bin]]
name = "access_variables"
doc = false

[[bin]]
name = "custom_command"
doc = false

[[bin]]
name = "custom_function"
doc = false

[[bin]]
name = "hello_world"
doc = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: AccessVariables
---
Setting $foo to 1 after you continue this line
<<set $foo to 1>>
$foo is now {$foo}. This information should also be printed in your console once the UI exits.
===
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: CustomCommand
---
When a command is encountered, a 'Command' DialogueEvent will be triggered.
You can handle this in your game code to do anything you'd like.
For example, here's a command that changes the background of the terminal!
<<set_background "cyan">>
Ooh, pretty.
===
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: CustomFunction
---
Rust functions can be registered into your dialogue's library to make them runnable via Yarn.
Let's take a look at a custom function now!
pow(2,3) = {pow(2,3)}
===
34 changes: 34 additions & 0 deletions examples/yarnspinner_without_bevy/assets/dialogue/hello_world.yarn
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
title: HelloWorld
---
Hello World! To continue the dialogue, press the enter key. You can also press Q to quit.
These are options. You can select one by clicking on it or pressing the corresponding number on your keyboard.
-> Some cool option
-> Some other cool option
Now we'll jump to another node!
<<jump AnotherNode>>

===

title: AnotherNode
---
Now, a character will talk. Notice how the upper left corner of the dialogue will show their name.
Hohenheim: Hi, I'm Jan Hohenheim, creator of Yarn Spinner for Rust. I hope you enjoy using it!
Let's set a condition. Do you prefer dogs or cats?
-> Dogs
<<set $animal = "dog">>
-> Cats
<<set $animal = "cats">>
-> Turtles
I, uuuh... okay, why not.
<<set $animal = "turtles">>
Now let's print the result of the condition. Your preference is...
(Drum roll)
<<if $animal == "dog">>
Dogs! Arf Arf!
<<elseif $animal == "cats">>
Cats! (Can't say I agree, but you do you)
<<else>>
Turtles! Solid choice.
<<endif>>
Et voilà! That was all. Thanks for checking out Yarn Spinner for Rust! Continuing from the last node will exit the dialogue.
===
24 changes: 24 additions & 0 deletions examples/yarnspinner_without_bevy/src/bin/access_variables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use yarnspinner::core::YarnValue;
use yarnspinner_without_bevy_examples::TuiDialogueRunner;

fn main() -> anyhow::Result<()> {
// See lib.rs for more details on how this works!
let mut runner =
TuiDialogueRunner::new("./assets/dialogue/access_variables.yarn", "AccessVariables")?;

runner.set_variable("$foo", YarnValue::Number(0.0))?;

println!(
"Value of $foo before dialogue: {:?}",
runner.get_variable("$foo")?
);

runner.run()?;

println!(
"Value of $foo after dialogue: {:?}",
runner.get_variable("$foo")?
);

Ok(())
}
6 changes: 6 additions & 0 deletions examples/yarnspinner_without_bevy/src/bin/custom_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use yarnspinner_without_bevy_examples::TuiDialogueRunner;

fn main() -> anyhow::Result<()> {
// See lib.rs for more details on how this works!
TuiDialogueRunner::new("./assets/dialogue/custom_command.yarn", "CustomCommand")?.run()
}
Loading
Loading