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

bevy 0.15 #340

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions book/src/explanation/anatomy-of-the-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Naturally, this can only occur in Tile/AutoTile layers (or IntGrid layers with A
## Level backgrounds
LDtk allows you to supply a background color and a background image for individual levels.
`bevy_ecs_ldtk` renders these by default.
The background color is spawned as a normal bevy [`SpriteBundle`](https://docs.rs/bevy/latest/bevy/prelude/struct.SpriteBundle.html), as a child of the level entity.
The background image, if it exists, is also spawned as a `SpriteBundle`.
The background color is spawned as a normal bevy [`Sprite`](https://docs.rs/bevy/latest/bevy/prelude/struct.Sprite.html), as a child of the level entity.
The background image, if it exists, is also spawned as a `Sprite`.

These background sprites can be disabled (not spawned) using the settings resource [`LdtkSettings`](https://docs.rs/bevy_ecs_ldtk/0.10.0/bevy_ecs_ldtk/prelude/struct.LdtkSettings.html): <!-- x-release-please-version -->
```rust,no_run
Expand Down
19 changes: 9 additions & 10 deletions book/src/explanation/game-logic-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ struct Player;
#[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle {
player: Player,
#[sprite_bundle]
sprite_bundle: SpriteBundle,
#[sprite]
sprite: Sprite,
}
```

How does `LdtkEntity`/`LdtkIntCell` construct the bundle when derived?
Without any intervention, the bundle's fields are constructed using the bundle's `Default` implementation.
However, various attributes are available to override this behavior, like `#[sprite_bundle]` in the above example.
However, various attributes are available to override this behavior, like `#[sprite]` in the above example.
This attribute gives the entity a sprite using the tileset in its LDtk editor visual.
For documentation about all the available attributes, check out the API reference for these traits:
- [`LdtkEntity`](https://docs.rs/bevy_ecs_ldtk/0.10.0/bevy_ecs_ldtk/app/trait.LdtkEntity.html) <!-- x-release-please-version -->
Expand Down Expand Up @@ -74,11 +74,10 @@ fn process_player(
commands
.entity(entity)
.insert(Player)
.insert(SpriteBundle {
texture: assets.load("player.png"),
transform: *transform,
..default()
})
.insert((
Sprite::from_image(assets.load("player.png")),
*transform
alxgnon marked this conversation as resolved.
Show resolved Hide resolved
))
.with_children(|commands| {
commands.spawn(PlayerChild);
});
Expand Down Expand Up @@ -121,8 +120,8 @@ struct Player;
#[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle {
player: Player,
#[sprite_bundle]
sprite_bundle: SpriteBundle,
#[sprite]
sprite: Sprite,
}

fn process_player(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Use the transforms of the spawned levels and width/height info from the level's


To access the level asset data, you first need to access the project asset data.
Assuming you only have one project, query for the only `Handle<LdtkProject>` entity and look up its asset data in the `LdtkProject` asset store.
Assuming you only have one project, query for the only `LdtkProjectHandle` entity and look up its asset data in the `LdtkProject` asset store.
Then, get the raw level data for every spawned level using the level entity's `LevelIid` component (there is a provided method for this).

```rust,no_run
Expand Down
118 changes: 118 additions & 0 deletions book/src/how-to-guides/migration-guides/migrate-from-0.10-to-0.11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Migrate from 0.10 to 0.11

## Bevy upgrade
`bevy_ecs_ldtk` has upgraded to Bevy and `bevy_ecs_tilemap` version `0.15`.
A Bevy `0.15` migration guide is available on [Bevy's website](https://bevyengine.org/learn/migration-guides/0-14-to-0-15/).

## `LdtkSpriteSheetBundle` replaced with `Sprite`
The use of `LdtkSpriteSheetBundle` has been replaced by a simple use of `Sprite`. The macro has changed as well, and is now named `#[sprite_sheet]`.
alxgnon marked this conversation as resolved.
Show resolved Hide resolved
```rust,ignore
// 0.10
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
#[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle {
#[sprite_sheet_bundle]
sprite_bundle: LdtkSpriteSheetBundle,
#[grid_coords]
grid_coords: GridCoords,
}
```
```rust,no_run
// 0.11
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
#[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle {
#[sprite_sheet]
sprite_sheet: Sprite,
#[grid_coords]
grid_coords: GridCoords,
}
```

## `SpriteBundle` also replaced with `Sprite`
When using a `SpriteBundle` with the `#[sprite_bundle]` macro, use a `Sprite` instead. The macro is now named `#[sprite]`.
```rust,ignore
// 0.10
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
#[derive(Bundle, LdtkEntity, Default)]
pub struct Player {
player: PlayerComponent,
health: Health,
#[sprite_bundle]
sprite_bundle: SpriteBundle,
}
```
```rust,no_run
// 0.11
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
#[derive(Bundle, LdtkEntity, Default)]
pub struct Player {
player: PlayerComponent,
health: Health,
#[sprite]
sprite: Sprite,
}
```

## `Handle<LdtkProject>` replaced with `LdtkProjectHandle`
Handles cannot be used as components in Bevy `0.15` onward. This has two changes.
### Call `.into()` when loading a project
First, you must call `.into()` when loading the world.
```rust,ignore
// 0.10
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(LdtkWorldBundle {
ldtk_handle: asset_server.load("my_project.ldtk"),
..Default::default()
});
}
```
```rust,no_run
// 0.11
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(LdtkWorldBundle {
ldtk_handle: asset_server.load("my_project.ldtk").into(),
..Default::default()
});
}
```
### Replace usages of `Handle<LdtkProject>`
Second, uses of `Handle<LdtkProject>` in queries must be replaced with `LdtkProjectHandle`. It is enough to replace the type in the signature, as the `LdtkProjectHandle` type is a drop-in replacement for the handle.

```rust,ignore
// 0.10
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
fn respawn_world(
mut commands: Commands,
ldtk_projects: Query<Entity, With<Handle<LdtkProject>>>,
input: Res<ButtonInput<KeyCode>>,
) {
if input.just_pressed(KeyCode::KeyR) {
commands.entity(ldtk_projects.single()).insert(Respawn);
}
}
```
```rust,no_run
// 0.11
# use bevy_ecs_ldtk::prelude::*;
# use bevy::prelude::*;
fn respawn_world(
mut commands: Commands,
ldtk_projects: Query<Entity, With<LdtkProjectHandle>>,
input: Res<ButtonInput<KeyCode>>,
) {
if input.just_pressed(KeyCode::KeyR) {
commands.entity(ldtk_projects.single()).insert(Respawn);
}
}
```

2 changes: 1 addition & 1 deletion book/src/how-to-guides/respawn-levels-and-worlds.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ There is a method on `LdtkProject` to perform this search.
# use bevy::prelude::*;
# use bevy_ecs_ldtk::prelude::*;
{{ #include ../../../examples/collectathon/respawn.rs:13:17 }}
ldtk_projects: Query<&Handle<LdtkProject>>,
ldtk_projects: Query<&LdtkProjectHandle>,
ldtk_project_assets: Res<Assets<LdtkProject>>,
) {
if input.just_pressed(KeyCode::KeyL) {
Expand Down
22 changes: 11 additions & 11 deletions book/src/tutorials/tile-based-game/add-gameplay-to-your-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Derive `Default` for this component.
```rust,no_run
# use bevy::prelude::*;
# use bevy_ecs_ldtk::prelude::*;
{{#include ../../../../examples/tile_based_game.rs:42:52}}
{{#include ../../../../examples/tile_based_game.rs:45:55}}
```

## Implement tile-based movement
Expand All @@ -38,8 +38,8 @@ fn main() {
.run();
}

{{#include ../../../../examples/tile_based_game.rs:91:93}}
{{#include ../../../../examples/tile_based_game.rs:95:109}}
{{#include ../../../../examples/tile_based_game.rs:94:96}}
{{#include ../../../../examples/tile_based_game.rs:98:112}}
*player_grid_coords = destination;
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ fn main() {
.run();
}

{{#include ../../../../examples/tile_based_game.rs:116:126}}
{{#include ../../../../examples/tile_based_game.rs:119:129}}
```

## Prevent tile-based movement into walls
Expand All @@ -88,7 +88,7 @@ fn main() {
.run();
}

{{#include ../../../../examples/tile_based_game.rs:66:72}}
{{#include ../../../../examples/tile_based_game.rs:69:75}}
```

There are a lot of ways to go about implementing the collision systems.
Expand All @@ -111,12 +111,12 @@ fn main() {
.run();
}

{{#include ../../../../examples/tile_based_game.rs:74:89}}
{{#include ../../../../examples/tile_based_game.rs:77:92}}
```

Now, add a system that listens for `LevelEvent::Spawned` and populates this resource.
It will need access to all of the wall locations to populate the `HashSet` (`Query<&GridCoords, With<Wall>>`).
It will also need access to the `LdtkProject` data to find the current level's width/height (`Query<&Handle<LdtkProject>>` and `Res<Assets<LdtkProject>>`).
It will also need access to the `LdtkProject` data to find the current level's width/height (`Query<&LdtkProjectHandle>` and `Res<Assets<LdtkProject>>`).
```rust,no_run
# use bevy::prelude::*;
# use bevy_ecs_ldtk::prelude::*;
Expand Down Expand Up @@ -150,7 +150,7 @@ fn main() {
.run();
}

{{#include ../../../../examples/tile_based_game.rs:128:155}}
{{#include ../../../../examples/tile_based_game.rs:131:158}}
```

Finally, update the `move_player_from_input` system to access the `LevelWalls` resource and check whether or not the player's destination is in a wall.
Expand All @@ -175,7 +175,7 @@ Finally, update the `move_player_from_input` system to access the `LevelWalls` r
# || self.wall_locations.contains(grid_coords)
# }
# }
{{#include ../../../../examples/tile_based_game.rs:91:114}}
{{#include ../../../../examples/tile_based_game.rs:94:117}}
```

With this check in place, the player should now be unable to move into walls!
Expand All @@ -188,7 +188,7 @@ Similar to the `PlayerBundle`, give the `GoalBundle` its own marker component an
```rust,no_run
# use bevy::prelude::*;
# use bevy_ecs_ldtk::prelude::*;
{{#include ../../../../examples/tile_based_game.rs:54:64}}
{{#include ../../../../examples/tile_based_game.rs:57:67}}
```

Then, write a system that checks if the player's `GridCoords` and the goal's `GridCoords` match.
Expand All @@ -212,7 +212,7 @@ fn main() {
.run();
}

{{#include ../../../../examples/tile_based_game.rs:157::}}
{{#include ../../../../examples/tile_based_game.rs:160::}}
```

With this, the simple tile-based game is complete.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
.run();
}

{{#include ../../../../examples/tile_based_game.rs:29:40}}
{{#include ../../../../examples/tile_based_game.rs:29:43}}
```

Finally, insert the `LevelSelection` resource to tell the plugin to spawn the first level.
Expand All @@ -60,24 +60,24 @@ Now, run the game with `$ cargo run --release` to see your first level spawning
You may have noticed that the Player and Goal are not rendered here.
They are there, but they require a little more work to become visible.

Create a `PlayerBundle` and `GoalBundle`, each with an `LdtkSpriteSheetBundle` field.
Create a `PlayerBundle` and `GoalBundle`, each with an `sprite_sheet` field.
You will develop these bundles a little bit more in the next chapter, but for now they will be similar.
Derive `LdtkEntity` for these bundles, and give the field a `#[sprite_sheet_bundle]` attribute.
Derive `LdtkEntity` for these bundles, and give the field a `#[sprite_sheet]` attribute.
This trait implementation defines how these bundles should be spawned by the plugin.
More specifically - they should be spawned as sprites identical to the entity's editor visual.
```rust,no_run
# use bevy::prelude::*;
# use bevy_ecs_ldtk::prelude::*;
#[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle {
#[sprite_sheet_bundle]
sprite_sheet_bundle: LdtkSpriteSheetBundle,
#[sprite_sheet]
sprite_sheet: Sprite,
}

#[derive(Default, Bundle, LdtkEntity)]
struct GoalBundle {
#[sprite_sheet_bundle]
sprite_sheet_bundle: LdtkSpriteSheetBundle,
#[sprite_sheet]
sprite_sheet: Sprite,
}
```

Expand All @@ -94,13 +94,13 @@ fn main() {
}
# #[derive(Default, Bundle, LdtkEntity)]
# struct PlayerBundle {
# #[sprite_sheet_bundle]
# sprite_sheet_bundle: LdtkSpriteSheetBundle,
# #[sprite_sheet]
# sprite_sheet: Sprite,
# }
# #[derive(Default, Bundle, LdtkEntity)]
# struct GoalBundle {
# #[sprite_sheet_bundle]
# sprite_sheet_bundle: LdtkSpriteSheetBundle,
# #[sprite_sheet]
# sprite_sheet: Sprite,
# }
```

Expand Down