Skip to content

Commit

Permalink
Add cargo examples (#6)
Browse files Browse the repository at this point in the history
docs: add cargo examples
  • Loading branch information
rparrett authored Dec 19, 2024
1 parent c4e1005 commit 149e822
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 74 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ serde = "1.0.195"
serde_json = "1.0.111"
wasm-bindgen = "0.2.89"

[dev-dependencies]
bevy = "0.14.0"

# Enable max optimizations for dependencies, but not for our code:
[profile.dev.package."*"]
opt-level = 3
Expand Down
77 changes: 3 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,81 +31,10 @@ cargo add bevy_generative

## Examples

### Maps and Textures

```rust
use bevy::prelude::*;
use bevy_generative::map::{MapBundle, MapPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(MapPlugin)
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(MapBundle::default());
}

```

### Terrain

```rust
use bevy::prelude::*;
use bevy_generative::terrain::{TerrainBundle, TerrainPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(TerrainPlugin)
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(TerrainBundle::default());
}

```

### Planets

```rust
use bevy::prelude::*;
use bevy_generative::planet::{PlanetBundle, PlanetPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PlanetPlugin)
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(PlanetBundle::default());
}
Examples are provided in the [examples](./examples) directory. To run an example, clone this repository and invoke cargo like this:

```sh
cargo run --example map
```

## Bevy Compatibility
Expand Down
78 changes: 78 additions & 0 deletions examples/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use bevy::prelude::*;
use bevy_generative::terrain::{Terrain, TerrainBundle, TerrainPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(TerrainPlugin)
.add_systems(Startup, setup)
.add_systems(Update, (button_appearance, export_button))
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(TerrainBundle::default());

commands
.spawn(ButtonBundle {
style: Style {
padding: UiRect::all(Val::Px(12.)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
margin: UiRect::all(Val::Px(12.)),
..default()
},
border_radius: BorderRadius::all(Val::Px(5.)),
background_color: NORMAL_BUTTON.into(),
..default()
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Export",
TextStyle {
font_size: 30.0,
color: BUTTON_TEXT.into(),
..default()
},
));
});
}

fn export_button(
interaction_query: Query<&Interaction, (Changed<Interaction>, With<Button>)>,
mut terrain_query: Query<&mut Terrain>,
) {
if interaction_query.iter().any(|i| *i == Interaction::Pressed) {
for mut terrain in &mut terrain_query {
terrain.export = true;
}
}
}

const NORMAL_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_500;
const HOVERED_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_600;
const PRESSED_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_700;
const BUTTON_TEXT: Srgba = bevy::color::palettes::tailwind::BLUE_50;

fn button_appearance(
mut interaction_query: Query<
(&Interaction, &mut BackgroundColor),
(Changed<Interaction>, With<Button>),
>,
) {
for (interaction, mut color) in &mut interaction_query {
*color = match *interaction {
Interaction::Pressed => PRESSED_BUTTON.into(),
Interaction::Hovered => HOVERED_BUTTON.into(),
Interaction::None => NORMAL_BUTTON.into(),
}
}
}
15 changes: 15 additions & 0 deletions examples/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use bevy::prelude::*;
use bevy_generative::map::{MapBundle, MapPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(MapPlugin)
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(MapBundle::default());
}
22 changes: 22 additions & 0 deletions examples/planet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use bevy::prelude::*;
use bevy_generative::planet::{PlanetBundle, PlanetPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PlanetPlugin)
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(PlanetBundle::default());
}
28 changes: 28 additions & 0 deletions examples/terrain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use bevy::prelude::*;
use bevy_generative::terrain::{TerrainBundle, TerrainPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(TerrainPlugin)
.add_systems(Startup, setup)
.run();
}

fn setup(mut commands: Commands) {
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(TerrainBundle {
terrain: bevy_generative::terrain::Terrain {
resolution: 4,
..default()
},
..default()
});
}

0 comments on commit 149e822

Please sign in to comment.