Skip to content

Commit

Permalink
Merge pull request #182 from 17cupsofcoffee/tui-example
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim authored Feb 29, 2024
2 parents 18c4f77 + 1c5df01 commit 980f564
Show file tree
Hide file tree
Showing 16 changed files with 810 additions and 1 deletion.
141 changes: 141 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 = { version = "0.13", features = ["file_watcher"] }
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()
}
16 changes: 16 additions & 0 deletions examples/yarnspinner_without_bevy/src/bin/custom_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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/custom_function.yarn", "CustomFunction")?;

runner.add_function("pow", pow);
runner.run()?;

Ok(())
}

fn pow(base: f32, exponent: f32) -> f32 {
base.powf(exponent)
}
6 changes: 6 additions & 0 deletions examples/yarnspinner_without_bevy/src/bin/hello_world.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/hello_world.yarn", "HelloWorld")?.run()
}
Loading

0 comments on commit 980f564

Please sign in to comment.