Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Oct 29, 2024
1 parent 88c570a commit c578e96
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 77 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "bevy_workshop"
version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = "1.0.65"

[dependencies.bevy]
version = "0.15.0-rc.2"
default-features = false
Expand Down
7 changes: 7 additions & 0 deletions assets/level.bw
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@





⬜⬜⬜⬜⬜⬜🙂
⬜⬜⬜⬜⬜🟩🟩🟩🟩🟩🟩🟩🟩🟩
2 changes: 1 addition & 1 deletion bevy-workshop/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- [Basic Physics](./basics/physics.md)
- [✍️ Exercises](./basics/exercises2.md)
- [Progress Report](./basics/progress.md)
- [Level Loading](./level/intro.md)
- [Level Loading](./level/index.md)
- [Custom Asset Format](./level/loading.md)
- [Displaying the Level](./level/display.md)
- [✍️ Exercises](./level/exercises.md)
Expand Down
4 changes: 2 additions & 2 deletions bevy-workshop/src/basics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

At the end of this section, you'll be able to move around the player, and lose the game.

You will have learned to:
You will:
* Load, display and animate sprites
* Move a sprite with your keyboard
* React to user input
* Query entities and components in more complex cases
12 changes: 6 additions & 6 deletions bevy-workshop/src/basics/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
## What You've learned

* Loading sprites and displaying them
* [the `AssetServer::load` function](https://docs.rs/bevy/0.15.0-rc.2/bevy/asset/struct.AssetServer.html#method.load)
* [the `Sprite` component](https://docs.rs/bevy/0.15.0-rc.2/bevy/prelude/struct.Sprite.html)
* with the [`AssetServer::load`](https://docs.rs/bevy/0.15.0-rc.2/bevy/asset/struct.AssetServer.html#method.load) function
* by adding the [`Sprite`](https://docs.rs/bevy/0.15.0-rc.2/bevy/prelude/struct.Sprite.html) component
* Handling user input
* [`ButtonInput<T>`](https://docs.rs/bevy/0.15.0-rc.2/bevy/input/struct.ButtonInput.html)
* [`KeyCode`](https://docs.rs/bevy/0.15.0-rc.2/bevy/input/keyboard/enum.KeyCode.html)
* reading the [`ButtonInput<T>`](https://docs.rs/bevy/0.15.0-rc.2/bevy/input/struct.ButtonInput.html) resource
* for the input [`KeyCode`](https://docs.rs/bevy/0.15.0-rc.2/bevy/input/keyboard/enum.KeyCode.html)
* Writing more complex queries, and updating components
* [`With` query filter](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.With.html)
* [`&mut` to query data mutably](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/change_detection/struct.Mut.html)
* the [`With`](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.With.html) query filter
* and using [`&mut`](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/change_detection/struct.Mut.html) to query data mutably

## Going Further

Expand Down
24 changes: 12 additions & 12 deletions bevy-workshop/src/intro/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
## What You've learned

* Application creation and adding Bevy default plugins
* [the `App` struct](https://docs.rs/bevy/0.15.0-rc.2/bevy/app/struct.App.html)
* [the `DefaultPlugins`](https://docs.rs/bevy/0.15.0-rc.2/bevy/struct.DefaultPlugins.html)
* creating the [`App`](https://docs.rs/bevy/0.15.0-rc.2/bevy/app/struct.App.html) struct
* and adding the [`DefaultPlugins`](https://docs.rs/bevy/0.15.0-rc.2/bevy/struct.DefaultPlugins.html)
* Schedules and adding systems
* [`Schedule`](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.Schedule.html)
* [List of schedules](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/schedule/trait.ScheduleLabel.html#implementors)
* [`App::add_systems`](https://docs.rs/bevy/0.15.0-rc.2/bevy/app/struct.App.html#method.add_systems)
* adding system with [`App::add_systems`](https://docs.rs/bevy/0.15.0-rc.2/bevy/app/struct.App.html#method.add_systems)
* to a [`Schedule`](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.Schedule.html)
* from the [list of schedules](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/schedule/trait.ScheduleLabel.html#implementors)
* Basic use of commands and queries
* [the `Commands` queue](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.Commands.html)
* [List of commands](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/trait.Command.html#implementors)
* [`Query`](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.Query.html)
* the [`Commands`](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.Commands.html) queue
* to issue a command from the [list of commands](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/trait.Command.html#implementors)
* and using a [`Query`](https://docs.rs/bevy/0.15.0-rc.2/bevy/ecs/prelude/struct.Query.html) to access components
* States, and running system only on a state or during state transition
* [the `States` trait](https://docs.rs/bevy/0.15.0-rc.2/bevy/prelude/trait.States.html)
* [`OnEnter` state transition](https://docs.rs/bevy/0.15.0-rc.2/bevy/state/prelude/struct.OnEnter.html)
* [`NextState` resource](https://docs.rs/bevy/0.15.0-rc.2/bevy/prelude/enum.NextState.html)
* using [`States`](https://docs.rs/bevy/0.15.0-rc.2/bevy/prelude/trait.States.html) trait
* and the [`OnEnter`](https://docs.rs/bevy/0.15.0-rc.2/bevy/state/prelude/struct.OnEnter.html) state transition
* with the [`NextState`](https://docs.rs/bevy/0.15.0-rc.2/bevy/prelude/enum.NextState.html) resource
* Code organization with plugins
* [the `Plugin` trait](https://docs.rs/bevy/0.15.0-rc.2/bevy/app/trait.Plugin.html)
* the [`Plugin`](https://docs.rs/bevy/0.15.0-rc.2/bevy/app/trait.Plugin.html) trait
93 changes: 93 additions & 0 deletions bevy-workshop/src/level/display.md
Original file line number Diff line number Diff line change
@@ -1 +1,94 @@
# Displaying the Level

```rust
# extern crate bevy;
# use bevy::prelude::*;
# #[derive(Asset, TypePath)]
# struct Level {pub tiles: Vec<Vec<Tile>>}
# enum Tile {Empty, Ground, Start}
# #[derive(Resource)]
# struct GameAssets {
# player_image: Handle<Image>,
# player_layout: Handle<TextureAtlasLayout>,
# ground_image: Handle<Image>,
# ground_layout: Handle<TextureAtlasLayout>,
# }
# #[derive(Resource)]
# pub struct LoadedLevel { pub level: Handle<Level> }
# #[derive(Component)]
# struct Player;
# #[derive(Component)]
# struct Ground;
# #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, States, Default)]
# enum GameState { #[default] Game }
fn ground_tile_index(line: &[Tile], i: usize) -> usize {
match (
i == 0 || !matches!(line.get(i - 1).unwrap_or(&Tile::Empty), Tile::Ground),
!matches!(line.get(i + 1).unwrap_or(&Tile::Empty), Tile::Ground),
) {
(true, true) => 8,
(true, false) => 14,
(false, true) => 0,
(false, false) => 7,
}
}

fn display_tile(
commands: &mut Commands,
tile: &Tile,
i: usize,
x: f32,
y: f32,
line: &[Tile],
assets: &GameAssets,
) {
match tile {
Tile::Ground => {
let index = ground_tile_index(line, i);
commands.spawn((
Sprite::from_atlas_image(
assets.ground_image.clone(),
TextureAtlas {
layout: assets.ground_layout.clone(),
index,
},
),
Transform::from_xyz(x, y, 0.0).with_scale(Vec3::splat(0.5)),
Ground,
StateScoped(GameState::Game),
));
}
Tile::Start => {
commands.spawn((
Sprite::from_atlas_image(
assets.player_image.clone(),
TextureAtlas {
layout: assets.player_layout.clone(),
index: 0,
},
),
Transform::from_xyz(x, y + 32.0, 0.0).with_scale(Vec3::splat(0.5)),
StateScoped(GameState::Game),
Player,
));
}
Tile::Empty => {}
}
}

fn display_level(
mut commands: Commands,
assets: Res<GameAssets>,
level: Res<LoadedLevel>,
levels: Res<Assets<Level>>,
) {
let level = levels.get(&level.level).unwrap();

for (j, line) in level.tiles.iter().enumerate() {
for (i, tile) in line.iter().enumerate() {
let (x, y) = ((i as f32 - 9.0) * 64.0, -(j as f32 - 5.0) * 64.0);
display_tile(&mut commands, tile, i, x, y, line, &assets);
}
}
}
```
File renamed without changes.
79 changes: 77 additions & 2 deletions bevy-workshop/src/level/loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,86 @@ LDtk support is not built in, and we won't have time to build a custom editor. L

```level
🙂
🟩🟩🟩🟩🟩
⬜⬜⬜⬜⬜⬜🙂
⬜⬜⬜⬜⬜🟩🟩🟩🟩🟩🟩🟩🟩🟩
```

## Asset Type

```rust
# extern crate bevy;
# use bevy::prelude::*;
#[derive(Asset, TypePath)]
struct Level {
pub tiles: Vec<Vec<Tile>>,
}

enum Tile {
Empty,
Ground,
Start,
}
```

## Asset Loader

```rust,edition2021
# extern crate bevy;
# extern crate thiserror;
# use bevy::{asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext}, prelude::*};
# use thiserror::Error;
# #[derive(Asset, TypePath)]
# struct Level {pub tiles: Vec<Vec<Tile>>}
# enum Tile {Empty, Ground, Start}
#[derive(Default)]
struct LevelLoader;
#[derive(Debug, Error)]
enum LevelLoaderError {
#[error("Could not load asset: {0}")]
Io(#[from] std::io::Error),
#[error("Unknown tile: {0}")]
UnknownTile(char),
}
impl AssetLoader for LevelLoader {
type Asset = Level;
type Settings = ();
type Error = LevelLoaderError;
async fn load(
&self,
reader: &mut dyn Reader,
_settings: &(),
_load_context: &mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut buf = String::new();
reader.read_to_string(&mut buf).await?;
let mut tiles = vec![];
let mut line = vec![];
for char in buf.chars() {
match char {
'⬜' => line.push(Tile::Empty),
'🟩' => line.push(Tile::Ground),
'🙂' => line.push(Tile::Start),
'\n' => {
tiles.push(line);
line = vec![];
}
char => Err(LevelLoaderError::UnknownTile(char))?,
}
}
Ok(Level { tiles })
}
fn extensions(&self) -> &[&str] {
&["bw"]
}
}
```

## Loading the Level
2 changes: 2 additions & 0 deletions bevy-workshop/src/level/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
## What You've learned

* Loading a custom asset file
* implementing the [`AssetLoader`](https://docs.rs/bevy/0.15.0-rc.2/bevy/asset/trait.AssetLoader.html) trait
* Getting an asset
* Using the [`Assets<T>`](https://docs.rs/bevy/0.15.0-rc.2/bevy/asset/struct.Assets.html) resource
* Hot reloading
Loading

0 comments on commit c578e96

Please sign in to comment.