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

Update to Bevy 0.10 #168

Merged
merged 9 commits into from Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions Cargo.toml
Expand Up @@ -16,7 +16,7 @@ members = ["macros"]
[dependencies]
bevy_ecs_ldtk_macros = { version = "0.5.0", optional = true, path = "macros" }
bevy_ecs_tilemap = { version = "0.9", default-features = false }
bevy = { version = "0.9", default-features = false, features = ["bevy_sprite"] }
bevy = { version = "0.10", default-features = false, features = ["bevy_sprite"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
regex = "1"
Expand All @@ -25,8 +25,8 @@ anyhow = "1.0"
thiserror = "1.0"

[dev-dependencies]
bevy = "0.9"
bevy_rapier2d = "0.19"
bevy = "0.10"
bevy_rapier2d = "0.20"
rand = "0.8"

[features]
Expand All @@ -38,3 +38,8 @@ render = ["bevy_ecs_tilemap/render"]
[[example]]
name = "platformer"
path = "examples/platformer/main.rs"

[patch.crates-io]
bevy_ecs_tilemap = { version = "0.9", git = "https://github.com/geieredgar/bevy_ecs_tilemap", branch = "bevy_track" }
bevy_rapier2d = { version = "0.20", git = "https://github.com/devil-ira/bevy_rapier", branch = "bevy-0.10" }
nalgebra = { version = "0.32.1", git = "https://github.com/MarijnS95/nalgebra", branch = "glam-0.23" }
33 changes: 15 additions & 18 deletions examples/platformer/components.rs
Expand Up @@ -70,7 +70,6 @@ impl From<IntGridCell> for SensorBundle {
sensor: Sensor,
rotation_constraints,
active_events: ActiveEvents::COLLISION_EVENTS,
..Default::default()
}
} else {
SensorBundle::default()
Expand Down Expand Up @@ -189,23 +188,21 @@ impl LdtkEntity for Patrol {
.find(|f| f.identifier == *"patrol")
.unwrap();
if let FieldValue::Points(ldtk_points) = &ldtk_patrol.value {
for ldtk_point in ldtk_points {
if let Some(ldtk_point) = ldtk_point {
// The +1 is necessary here due to the pivot of the entities in the sample
// file.
// The patrols set up in the file look flat and grounded,
// but technically they're not if you consider the pivot,
// which is at the bottom-center for the skulls.
let pixel_coords = (ldtk_point.as_vec2() + Vec2::new(0.5, 1.))
* Vec2::splat(layer_instance.grid_size as f32);

points.push(ldtk_pixel_coords_to_translation_pivoted(
pixel_coords.as_ivec2(),
layer_instance.c_hei * layer_instance.grid_size,
IVec2::new(entity_instance.width, entity_instance.height),
entity_instance.pivot,
));
}
for ldtk_point in ldtk_points.iter().flatten() {
// The +1 is necessary here due to the pivot of the entities in the sample
// file.
// The patrols set up in the file look flat and grounded,
// but technically they're not if you consider the pivot,
// which is at the bottom-center for the skulls.
let pixel_coords = (ldtk_point.as_vec2() + Vec2::new(0.5, 1.))
* Vec2::splat(layer_instance.grid_size as f32);

points.push(ldtk_pixel_coords_to_translation_pivoted(
pixel_coords.as_ivec2(),
layer_instance.c_hei * layer_instance.grid_size,
IVec2::new(entity_instance.width, entity_instance.height),
entity_instance.pivot,
));
}
}

Expand Down
32 changes: 16 additions & 16 deletions examples/platformer/systems.rs
Expand Up @@ -309,6 +309,7 @@ pub fn patrol(mut query: Query<(&mut Transform, &mut Velocity, &mut Patrol)>) {

const ASPECT_RATIO: f32 = 16. / 9.;

#[allow(clippy::type_complexity)]
pub fn camera_fit_inside_current_level(
mut camera_query: Query<
(
Expand Down Expand Up @@ -339,27 +340,26 @@ pub fn camera_fit_inside_current_level(
let level = &ldtk_level.level;
if level_selection.is_match(&0, level) {
let level_ratio = level.px_wid as f32 / ldtk_level.level.px_hei as f32;

orthographic_projection.scaling_mode = bevy::render::camera::ScalingMode::None;
orthographic_projection.bottom = 0.;
orthographic_projection.left = 0.;
orthographic_projection.viewport_origin = Vec2::ZERO;
if level_ratio > ASPECT_RATIO {
// level is wider than the screen
orthographic_projection.top = (level.px_hei as f32 / 9.).round() * 9.;
orthographic_projection.right = orthographic_projection.top * ASPECT_RATIO;
camera_transform.translation.x = (player_translation.x
- level_transform.translation.x
- orthographic_projection.right / 2.)
.clamp(0., level.px_wid as f32 - orthographic_projection.right);
let height = (level.px_hei as f32 / 9.).round() * 9.;
let width = height * ASPECT_RATIO;
orthographic_projection.scaling_mode =
bevy::render::camera::ScalingMode::Fixed { width, height };
camera_transform.translation.x =
(player_translation.x - level_transform.translation.x - width / 2.)
.clamp(0., level.px_wid as f32 - width);
camera_transform.translation.y = 0.;
} else {
// level is taller than the screen
orthographic_projection.right = (level.px_wid as f32 / 16.).round() * 16.;
orthographic_projection.top = orthographic_projection.right / ASPECT_RATIO;
camera_transform.translation.y = (player_translation.y
- level_transform.translation.y
- orthographic_projection.top / 2.)
.clamp(0., level.px_hei as f32 - orthographic_projection.top);
let width = (level.px_wid as f32 / 16.).round() * 16.;
let height = width / ASPECT_RATIO;
orthographic_projection.scaling_mode =
bevy::render::camera::ScalingMode::Fixed { width, height };
camera_transform.translation.y =
(player_translation.y - level_transform.translation.y - height / 2.)
.clamp(0., level.px_hei as f32 - height);
camera_transform.translation.x = 0.;
}

Expand Down
63 changes: 20 additions & 43 deletions src/lib.rs
Expand Up @@ -138,19 +138,10 @@ mod plugin {

use super::*;

/// [SystemLabel] used by the plugin for scheduling its systems.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, SystemLabel)]
pub enum LdtkSystemLabel {
ProcessAssets,
LevelSelection,
LevelSet,
LevelSpawning,
Other,
}

/// [StageLabel] for stages added by the plugin.
geieredgar marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, StageLabel)]
pub enum LdtkStage {
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, SystemSet)]
#[system_set(base)]
pub enum LdtkSystemSet {
geieredgar marked this conversation as resolved.
Show resolved Hide resolved
/// Occurs immediately after [CoreStage::Update].
///
/// Used for systems that process components and resources provided by this plugin's API.
Expand All @@ -163,6 +154,7 @@ mod plugin {
/// As a result, you can expect minimal frame delay when updating these in
/// [CoreStage::Update].
ProcessApi,
PostProcessApi,
geieredgar marked this conversation as resolved.
Show resolved Hide resolved
}

/// Adds the default systems, assets, and resources used by `bevy_ecs_ldtk`.
Expand All @@ -178,10 +170,11 @@ mod plugin {
app = app.add_plugin(bevy_ecs_tilemap::TilemapPlugin);
}

app.add_stage_after(
CoreStage::Update,
LdtkStage::ProcessApi,
SystemStage::parallel(),
app.configure_sets(
(LdtkSystemSet::ProcessApi, LdtkSystemSet::PostProcessApi)
.chain()
.after(CoreSet::Update)
geieredgar marked this conversation as resolved.
Show resolved Hide resolved
.before(CoreSet::PostUpdate),
)
.init_non_send_resource::<app::LdtkEntityMap>()
.init_non_send_resource::<app::LdtkIntCellMap>()
Expand All @@ -191,37 +184,21 @@ mod plugin {
.add_asset::<assets::LdtkLevel>()
.init_asset_loader::<assets::LdtkLevelLoader>()
.add_event::<resources::LevelEvent>()
.add_system_to_stage(
CoreStage::PreUpdate,
systems::process_ldtk_assets.label(LdtkSystemLabel::ProcessAssets),
)
.add_system_to_stage(
CoreStage::PreUpdate,
systems::process_ldtk_levels.label(LdtkSystemLabel::LevelSpawning),
)
.add_system_to_stage(
LdtkStage::ProcessApi,
systems::worldly_adoption.label(LdtkSystemLabel::Other),
)
.add_system_to_stage(
LdtkStage::ProcessApi,
systems::apply_level_selection.label(LdtkSystemLabel::LevelSelection),
)
.add_system_to_stage(
LdtkStage::ProcessApi,
systems::apply_level_set
.label(LdtkSystemLabel::LevelSet)
.after(LdtkSystemLabel::LevelSelection),
.add_systems(
(systems::process_ldtk_assets, systems::process_ldtk_levels)
.in_base_set(CoreSet::PreUpdate),
)
.add_system_to_stage(
LdtkStage::ProcessApi,
systems::clean_respawn_entities.at_end(),
.add_system(systems::worldly_adoption.in_base_set(LdtkSystemSet::ProcessApi))
.add_systems(
(systems::apply_level_selection, systems::apply_level_set)
.chain()
.in_base_set(LdtkSystemSet::ProcessApi),
)
.add_system_to_stage(
CoreStage::PostUpdate,
.add_system(systems::clean_respawn_entities.in_base_set(LdtkSystemSet::PostProcessApi))
geieredgar marked this conversation as resolved.
Show resolved Hide resolved
.add_system(
systems::detect_level_spawned_events
.pipe(systems::fire_level_transformed_events)
.label(LdtkSystemLabel::Other),
.in_base_set(CoreSet::PostUpdate),
)
.register_type::<GridCoords>()
.register_type::<TileMetadata>()
Expand Down