Skip to content

Commit

Permalink
feat!: upgrade to bevy 0.13 (#302)
Browse files Browse the repository at this point in the history
~~Leaving this in draft until
StarArawn/bevy_ecs_tilemap#508 merges.~~

Upstream `bevy_ecs_tilemap` PR has merged, but a new release hasn't been
cut. PR updated to note that `main` here tracks to `main` for ECS
Tilemap which both allow for Bevy 0.13 support and PR should be
mergeable.

Most of the changes here are related to the new `TextureAtlasLayout` and
the examples in the repo look correct to me still at least.

Fixes #301
  • Loading branch information
theshortcut committed Apr 2, 2024
1 parent e040747 commit 2ee602f
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 114 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ members = ["macros"]
[dependencies]
bevy_ecs_ldtk_macros = { version = "0.9.0", optional = true, path = "macros" }
bevy_ecs_tilemap = { version = "0.12", default-features = false }
bevy = { version = "0.12", default-features = false, features = ["bevy_sprite"] }
bevy = { version = "0.13", default-features = false, features = ["bevy_sprite"] }
derive-getters = "0.3.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand All @@ -28,11 +28,11 @@ derive_more = "0.99.17"
path-clean = "1.0.1"

[dev-dependencies]
bevy = "0.12"
bevy_rapier2d = "0.23.0"
bevy = "0.13"
bevy_rapier2d = "0.25.0"
fake = { version = "2.8.0", features = ["uuid"] }
rand = "0.8"
bevy-inspector-egui = "0.21.0"
bevy-inspector-egui = "0.23.0"

[features]
default = ["derive", "render", "internal_levels"]
Expand All @@ -56,3 +56,6 @@ path = "examples/field_instances/main.rs"
[[example]]
name = "collectathon"
path = "examples/collectathon/main.rs"

[patch.crates-io]
bevy_ecs_tilemap = { git = "https://github.com/StarArawn/bevy_ecs_tilemap/", branch = "main" }
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $ cargo run --example example-name
## Compatibility
| bevy | bevy_ecs_tilemap | LDtk | bevy_ecs_ldtk |
| --- | --- | --- | --- |
| 0.13 | main | 1.5.3 | main |
| 0.12 | 0.12 | 1.5.3 | 0.9 |
| 0.11 | 0.11 | 1.3.3 | 0.8 |
| 0.10 | 0.10 | 1.1 | 0.7 |
Expand Down
6 changes: 3 additions & 3 deletions book/src/how-to-guides/respawn-levels-and-worlds.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ For example, if the game should only spawn one level at a time, operate under th
fn respawn_only_level(
mut commands: Commands,
levels: Query<Entity, With<LevelIid>>,
input: Res<Input<KeyCode>>
input: Res<ButtonInput<KeyCode>>
) {
if input.just_pressed(KeyCode::L) {
if input.just_pressed(KeyCode::KeyL) {
commands.entity(levels.single()).insert(Respawn);
}
}
Expand All @@ -54,7 +54,7 @@ There is a method on `LdtkProject` to perform this search.
ldtk_projects: Query<&Handle<LdtkProject>>,
ldtk_project_assets: Res<Assets<LdtkProject>>,
) {
if input.just_pressed(KeyCode::L) {
if input.just_pressed(KeyCode::KeyL) {
if let Some(only_project) = ldtk_project_assets.get(ldtk_projects.single()) {
let level_selection_iid = LevelIid::new(
only_project
Expand Down
10 changes: 5 additions & 5 deletions examples/collectathon/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ const MOVEMENT_SPEED: f32 = 96.;

fn move_player(
mut players: Query<&mut Transform, With<Player>>,
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
for mut player_transform in players.iter_mut() {
let mut movement = Vec2::ZERO;

if input.pressed(KeyCode::W) || input.pressed(KeyCode::Up) {
if input.pressed(KeyCode::KeyW) || input.pressed(KeyCode::ArrowUp) {
movement += Vec2::Y;
}
if input.pressed(KeyCode::A) || input.pressed(KeyCode::Left) {
if input.pressed(KeyCode::KeyA) || input.pressed(KeyCode::ArrowLeft) {
movement -= Vec2::X;
}
if input.pressed(KeyCode::S) || input.pressed(KeyCode::Down) {
if input.pressed(KeyCode::KeyS) || input.pressed(KeyCode::ArrowDown) {
movement -= Vec2::Y;
}
if input.pressed(KeyCode::D) || input.pressed(KeyCode::Right) {
if input.pressed(KeyCode::KeyD) || input.pressed(KeyCode::ArrowRight) {
movement += Vec2::X;
}

Expand Down
8 changes: 4 additions & 4 deletions examples/collectathon/respawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn respawn_level(
mut commands: Commands,
level_selection: Res<LevelSelection>,
levels: Query<(Entity, &LevelIid)>,
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
) {
if input.just_pressed(KeyCode::L) {
if input.just_pressed(KeyCode::KeyL) {
let level_selection_iid = match level_selection.as_ref() {
LevelSelection::Iid(iid) => iid,
_ => panic!("level should always be selected by iid in this example"),
Expand All @@ -33,9 +33,9 @@ fn respawn_level(
fn respawn_world(
mut commands: Commands,
ldtk_projects: Query<Entity, With<Handle<LdtkProject>>>,
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
) {
if input.just_pressed(KeyCode::R) {
if input.just_pressed(KeyCode::KeyR) {
commands.entity(ldtk_projects.single()).insert(Respawn);
}
}
2 changes: 1 addition & 1 deletion examples/level_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

// This function is a demonstation that changes to the LevelSet have the expected results.
// Hit spacebar and watch what happens!
fn toggle_levels(input: Res<Input<KeyCode>>, mut level_sets: Query<&mut LevelSet>) {
fn toggle_levels(input: Res<ButtonInput<KeyCode>>, mut level_sets: Query<&mut LevelSet>) {
if input.just_pressed(KeyCode::Space) {
let mut rng = rand::thread_rng();
let level_to_toggle = LevelIid::new(*LEVEL_IIDS.choose(&mut rng).unwrap());
Expand Down
2 changes: 1 addition & 1 deletion examples/platformer/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl LdtkEntity for Patrol {
_: Option<&Handle<Image>>,
_: Option<&TilesetDefinition>,
_: &AssetServer,
_: &mut Assets<TextureAtlas>,
_: &mut Assets<TextureAtlasLayout>,
) -> Patrol {
let mut points = Vec::new();
points.push(ldtk_pixel_coords_to_translation_pivoted(
Expand Down
26 changes: 13 additions & 13 deletions examples/platformer/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@ pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
}

pub fn dbg_player_items(
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&Items, &EntityInstance), With<Player>>,
) {
for (items, entity_instance) in &mut query {
if input.just_pressed(KeyCode::P) {
if input.just_pressed(KeyCode::KeyP) {
dbg!(&items);
dbg!(&entity_instance);
}
}
}

pub fn movement(
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&mut Velocity, &mut Climber, &GroundDetection), With<Player>>,
) {
for (mut velocity, mut climber, ground_detection) in &mut query {
let right = if input.pressed(KeyCode::D) { 1. } else { 0. };
let left = if input.pressed(KeyCode::A) { 1. } else { 0. };
let right = if input.pressed(KeyCode::KeyD) { 1. } else { 0. };
let left = if input.pressed(KeyCode::KeyA) { 1. } else { 0. };

velocity.linvel.x = (right - left) * 200.;

if climber.intersecting_climbables.is_empty() {
climber.climbing = false;
} else if input.just_pressed(KeyCode::W) || input.just_pressed(KeyCode::S) {
} else if input.just_pressed(KeyCode::KeyW) || input.just_pressed(KeyCode::KeyS) {
climber.climbing = true;
}

if climber.climbing {
let up = if input.pressed(KeyCode::W) { 1. } else { 0. };
let down = if input.pressed(KeyCode::S) { 1. } else { 0. };
let up = if input.pressed(KeyCode::KeyW) { 1. } else { 0. };
let down = if input.pressed(KeyCode::KeyS) { 1. } else { 0. };

velocity.linvel.y = (up - down) * 200.;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn spawn_wall_collision(
// 2. it lets us easily add the collision entities as children of the appropriate level entity
let mut level_to_wall_locations: HashMap<Entity, HashSet<GridCoords>> = HashMap::new();

wall_query.for_each(|(&grid_coords, parent)| {
wall_query.iter().for_each(|(&grid_coords, parent)| {
// An intgrid tile's direct parent will be a layer entity, not the level entity
// To get the level entity, you need the tile's grandparent.
// This is where parent_query comes in.
Expand All @@ -121,7 +121,7 @@ pub fn spawn_wall_collision(
});

if !wall_query.is_empty() {
level_query.for_each(|(level_entity, level_iid)| {
level_query.iter().for_each(|(level_entity, level_iid)| {
if let Some(level_walls) = level_to_wall_locations.get(&level_entity) {
let ldtk_project = ldtk_project_assets
.get(ldtk_projects.single())
Expand Down Expand Up @@ -448,7 +448,7 @@ pub fn spawn_ground_sensor(
pub fn ground_detection(
mut ground_sensors: Query<&mut GroundSensor>,
mut collisions: EventReader<CollisionEvent>,
collidables: Query<With<Collider>, Without<Sensor>>,
collidables: Query<Entity, (With<Collider>, Without<Sensor>)>,
) {
for collision_event in collisions.read() {
match collision_event {
Expand Down Expand Up @@ -492,9 +492,9 @@ pub fn update_on_ground(
pub fn restart_level(
mut commands: Commands,
level_query: Query<Entity, With<LevelIid>>,
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
) {
if input.just_pressed(KeyCode::R) {
if input.just_pressed(KeyCode::KeyR) {
for level_entity in &level_query {
commands.entity(level_entity).insert(Respawn);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/tile_based_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ impl LevelWalls {

fn move_player_from_input(
mut players: Query<&mut GridCoords, With<Player>>,
input: Res<Input<KeyCode>>,
input: Res<ButtonInput<KeyCode>>,
level_walls: Res<LevelWalls>,
) {
let movement_direction = if input.just_pressed(KeyCode::W) {
let movement_direction = if input.just_pressed(KeyCode::KeyW) {
GridCoords::new(0, 1)
} else if input.just_pressed(KeyCode::A) {
} else if input.just_pressed(KeyCode::KeyA) {
GridCoords::new(-1, 0)
} else if input.just_pressed(KeyCode::S) {
} else if input.just_pressed(KeyCode::KeyS) {
GridCoords::new(0, -1)
} else if input.just_pressed(KeyCode::D) {
} else if input.just_pressed(KeyCode::KeyD) {
GridCoords::new(1, 0)
} else {
return;
Expand Down
15 changes: 7 additions & 8 deletions examples/traitless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,30 @@ struct ComponentB;
fn process_my_entity(
mut commands: Commands,
entity_query: Query<(Entity, &Transform, &EntityInstance), Added<EntityInstance>>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
asset_server: Res<AssetServer>,
) {
for (entity, transform, entity_instance) in entity_query.iter() {
if entity_instance.identifier == *"MyEntityIdentifier" {
let tileset = asset_server.load("atlas/MV Icons Complete Sheet Free - ALL.png");
let texture = asset_server.load("atlas/MV Icons Complete Sheet Free - ALL.png");

if let Some(tile) = &entity_instance.tile {
let texture_atlas = texture_atlases.add(TextureAtlas::from_grid(
tileset.clone(),
let layout = texture_atlases.add(TextureAtlasLayout::from_grid(
Vec2::new(tile.w as f32, tile.h as f32),
16,
95,
None,
None,
));

let sprite = TextureAtlasSprite {
let atlas = TextureAtlas {
index: (tile.y / tile.h) as usize * 16 + (tile.x / tile.w) as usize,
..Default::default()
layout,
};

commands.entity(entity).insert(SpriteSheetBundle {
texture_atlas,
sprite,
atlas,
texture,
transform: *transform,
..Default::default()
});
Expand Down
22 changes: 10 additions & 12 deletions macros/src/ldtk_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn expand_ldtk_entity_derive(ast: syn::DeriveInput) -> proc_macro::TokenStre
tileset: Option<&bevy::prelude::Handle<bevy::prelude::Image>>,
tileset_definition: Option<&bevy_ecs_ldtk::prelude::TilesetDefinition>,
asset_server: &bevy::prelude::AssetServer,
texture_atlases: &mut bevy::prelude::Assets<bevy::prelude::TextureAtlas>,
texture_atlases: &mut bevy::prelude::Assets<bevy::prelude::TextureAtlasLayout>,
) -> Self {
Self {
#(#field_constructions)*
Expand Down Expand Up @@ -231,18 +231,16 @@ fn expand_sprite_sheet_bundle_attribute(

quote! {
#field_name: bevy::prelude::SpriteSheetBundle {
texture_atlas: texture_atlases.add(
bevy::prelude::TextureAtlas::from_grid(
asset_server.load(#asset_path).into(),
bevy::prelude::Vec2::new(#tile_width, #tile_height),
#columns, #rows, Some(bevy::prelude::Vec2::splat(#padding)),
Some(bevy::prelude::Vec2::splat(#offset)),
)
),
sprite: bevy::prelude::TextureAtlasSprite {
index: #index,
..Default::default()
atlas: bevy::prelude::TextureAtlas {
layout: texture_atlases.add(
bevy::prelude::TextureAtlasLayout::from_grid(
bevy::prelude::Vec2::new(#tile_width, #tile_height),
#columns, #rows, Some(bevy::prelude::Vec2::splat(#padding)),
Some(bevy::prelude::Vec2::splat(#offset)),
)),
index: #index
},
texture: asset_server.load(#asset_path).into(),
..Default::default()
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/entity_app_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod tests {
_: Option<&Handle<Image>>,
_: Option<&TilesetDefinition>,
_: &AssetServer,
_: &mut Assets<TextureAtlas>,
_: &mut Assets<TextureAtlasLayout>,
) -> LdtkEntityBundle {
LdtkEntityBundle::default()
}
Expand Down

0 comments on commit 2ee602f

Please sign in to comment.