Skip to content

Commit 149e822

Browse files
authored
Add cargo examples (#6)
docs: add cargo examples
1 parent c4e1005 commit 149e822

File tree

6 files changed

+149
-74
lines changed

6 files changed

+149
-74
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ serde = "1.0.195"
2828
serde_json = "1.0.111"
2929
wasm-bindgen = "0.2.89"
3030

31+
[dev-dependencies]
32+
bevy = "0.14.0"
33+
3134
# Enable max optimizations for dependencies, but not for our code:
3235
[profile.dev.package."*"]
3336
opt-level = 3

README.md

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -31,81 +31,10 @@ cargo add bevy_generative
3131

3232
## Examples
3333

34-
### Maps and Textures
35-
36-
```rust
37-
use bevy::prelude::*;
38-
use bevy_generative::map::{MapBundle, MapPlugin};
39-
40-
fn main() {
41-
App::new()
42-
.add_plugins(DefaultPlugins)
43-
.add_plugins(MapPlugin)
44-
.add_systems(Startup, setup)
45-
.run();
46-
}
47-
48-
fn setup(mut commands: Commands) {
49-
commands.spawn(Camera2dBundle::default());
50-
commands.spawn(MapBundle::default());
51-
}
52-
53-
```
54-
55-
### Terrain
56-
57-
```rust
58-
use bevy::prelude::*;
59-
use bevy_generative::terrain::{TerrainBundle, TerrainPlugin};
60-
61-
fn main() {
62-
App::new()
63-
.add_plugins(DefaultPlugins)
64-
.add_plugins(TerrainPlugin)
65-
.add_systems(Startup, setup)
66-
.run();
67-
}
68-
69-
fn setup(mut commands: Commands) {
70-
commands.spawn(PointLightBundle {
71-
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
72-
..default()
73-
});
74-
commands.spawn(Camera3dBundle {
75-
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
76-
..default()
77-
});
78-
commands.spawn(TerrainBundle::default());
79-
}
80-
81-
```
82-
83-
### Planets
84-
85-
```rust
86-
use bevy::prelude::*;
87-
use bevy_generative::planet::{PlanetBundle, PlanetPlugin};
88-
89-
fn main() {
90-
App::new()
91-
.add_plugins(DefaultPlugins)
92-
.add_plugins(PlanetPlugin)
93-
.add_systems(Startup, setup)
94-
.run();
95-
}
96-
97-
fn setup(mut commands: Commands) {
98-
commands.spawn(PointLightBundle {
99-
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
100-
..default()
101-
});
102-
commands.spawn(Camera3dBundle {
103-
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
104-
..default()
105-
});
106-
commands.spawn(PlanetBundle::default());
107-
}
34+
Examples are provided in the [examples](./examples) directory. To run an example, clone this repository and invoke cargo like this:
10835

36+
```sh
37+
cargo run --example map
10938
```
11039

11140
## Bevy Compatibility

examples/export.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use bevy::prelude::*;
2+
use bevy_generative::terrain::{Terrain, TerrainBundle, TerrainPlugin};
3+
4+
fn main() {
5+
App::new()
6+
.add_plugins(DefaultPlugins)
7+
.add_plugins(TerrainPlugin)
8+
.add_systems(Startup, setup)
9+
.add_systems(Update, (button_appearance, export_button))
10+
.run();
11+
}
12+
13+
fn setup(mut commands: Commands) {
14+
commands.spawn(PointLightBundle {
15+
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
16+
..default()
17+
});
18+
commands.spawn(Camera3dBundle {
19+
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
20+
..default()
21+
});
22+
commands.spawn(TerrainBundle::default());
23+
24+
commands
25+
.spawn(ButtonBundle {
26+
style: Style {
27+
padding: UiRect::all(Val::Px(12.)),
28+
justify_content: JustifyContent::Center,
29+
align_items: AlignItems::Center,
30+
margin: UiRect::all(Val::Px(12.)),
31+
..default()
32+
},
33+
border_radius: BorderRadius::all(Val::Px(5.)),
34+
background_color: NORMAL_BUTTON.into(),
35+
..default()
36+
})
37+
.with_children(|parent| {
38+
parent.spawn(TextBundle::from_section(
39+
"Export",
40+
TextStyle {
41+
font_size: 30.0,
42+
color: BUTTON_TEXT.into(),
43+
..default()
44+
},
45+
));
46+
});
47+
}
48+
49+
fn export_button(
50+
interaction_query: Query<&Interaction, (Changed<Interaction>, With<Button>)>,
51+
mut terrain_query: Query<&mut Terrain>,
52+
) {
53+
if interaction_query.iter().any(|i| *i == Interaction::Pressed) {
54+
for mut terrain in &mut terrain_query {
55+
terrain.export = true;
56+
}
57+
}
58+
}
59+
60+
const NORMAL_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_500;
61+
const HOVERED_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_600;
62+
const PRESSED_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_700;
63+
const BUTTON_TEXT: Srgba = bevy::color::palettes::tailwind::BLUE_50;
64+
65+
fn button_appearance(
66+
mut interaction_query: Query<
67+
(&Interaction, &mut BackgroundColor),
68+
(Changed<Interaction>, With<Button>),
69+
>,
70+
) {
71+
for (interaction, mut color) in &mut interaction_query {
72+
*color = match *interaction {
73+
Interaction::Pressed => PRESSED_BUTTON.into(),
74+
Interaction::Hovered => HOVERED_BUTTON.into(),
75+
Interaction::None => NORMAL_BUTTON.into(),
76+
}
77+
}
78+
}

examples/map.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use bevy::prelude::*;
2+
use bevy_generative::map::{MapBundle, MapPlugin};
3+
4+
fn main() {
5+
App::new()
6+
.add_plugins(DefaultPlugins)
7+
.add_plugins(MapPlugin)
8+
.add_systems(Startup, setup)
9+
.run();
10+
}
11+
12+
fn setup(mut commands: Commands) {
13+
commands.spawn(Camera2dBundle::default());
14+
commands.spawn(MapBundle::default());
15+
}

examples/planet.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use bevy::prelude::*;
2+
use bevy_generative::planet::{PlanetBundle, PlanetPlugin};
3+
4+
fn main() {
5+
App::new()
6+
.add_plugins(DefaultPlugins)
7+
.add_plugins(PlanetPlugin)
8+
.add_systems(Startup, setup)
9+
.run();
10+
}
11+
12+
fn setup(mut commands: Commands) {
13+
commands.spawn(PointLightBundle {
14+
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
15+
..default()
16+
});
17+
commands.spawn(Camera3dBundle {
18+
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
19+
..default()
20+
});
21+
commands.spawn(PlanetBundle::default());
22+
}

examples/terrain.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use bevy::prelude::*;
2+
use bevy_generative::terrain::{TerrainBundle, TerrainPlugin};
3+
4+
fn main() {
5+
App::new()
6+
.add_plugins(DefaultPlugins)
7+
.add_plugins(TerrainPlugin)
8+
.add_systems(Startup, setup)
9+
.run();
10+
}
11+
12+
fn setup(mut commands: Commands) {
13+
commands.spawn(PointLightBundle {
14+
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
15+
..default()
16+
});
17+
commands.spawn(Camera3dBundle {
18+
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
19+
..default()
20+
});
21+
commands.spawn(TerrainBundle {
22+
terrain: bevy_generative::terrain::Terrain {
23+
resolution: 4,
24+
..default()
25+
},
26+
..default()
27+
});
28+
}

0 commit comments

Comments
 (0)