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

Reorganize platformer example for readability #329

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions examples/platformer/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*;

use crate::player::Player;

const ASPECT_RATIO: f32 = 16. / 9.;

#[allow(clippy::type_complexity)]
pub fn camera_fit_inside_current_level(
mut camera_query: Query<
(
&mut bevy::render::camera::OrthographicProjection,
&mut Transform,
),
Without<Player>,
>,
player_query: Query<&Transform, With<Player>>,
level_query: Query<(&Transform, &LevelIid), (Without<OrthographicProjection>, Without<Player>)>,
ldtk_projects: Query<&Handle<LdtkProject>>,
level_selection: Res<LevelSelection>,
ldtk_project_assets: Res<Assets<LdtkProject>>,
) {
if let Ok(Transform {
translation: player_translation,
..
}) = player_query.get_single()
{
let player_translation = *player_translation;

let (mut orthographic_projection, mut camera_transform) = camera_query.single_mut();

for (level_transform, level_iid) in &level_query {
let ldtk_project = ldtk_project_assets
.get(ldtk_projects.single())
.expect("Project should be loaded if level has spawned");

let level = ldtk_project
.get_raw_level_by_iid(&level_iid.to_string())
.expect("Spawned level should exist in LDtk project");

if level_selection.is_match(&LevelIndices::default(), level) {
let level_ratio = level.px_wid as f32 / level.px_hei as f32;
orthographic_projection.viewport_origin = Vec2::ZERO;
if level_ratio > ASPECT_RATIO {
// level is wider than the screen
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
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.;
}

camera_transform.translation.x += level_transform.translation.x;
camera_transform.translation.y += level_transform.translation.y;
}
}
}
}
78 changes: 78 additions & 0 deletions examples/platformer/climbing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use bevy::{prelude::*, utils::HashSet};
use bevy_ecs_ldtk::prelude::*;
use bevy_rapier2d::prelude::*;

use crate::physics::SensorBundle;

#[derive(Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Climber {
pub climbing: bool,
pub intersecting_climbables: HashSet<Entity>,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Climbable;

#[derive(Clone, Default, Bundle, LdtkIntCell)]
pub struct LadderBundle {
#[from_int_grid_cell]
pub sensor_bundle: SensorBundle,
pub climbable: Climbable,
}

pub fn detect_climb_range(
mut climbers: Query<&mut Climber>,
climbables: Query<Entity, With<Climbable>>,
mut collisions: EventReader<CollisionEvent>,
) {
for collision in collisions.read() {
match collision {
CollisionEvent::Started(collider_a, collider_b, _) => {
if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_a), climbables.get(*collider_b))
{
climber.intersecting_climbables.insert(climbable);
}
if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_b), climbables.get(*collider_a))
{
climber.intersecting_climbables.insert(climbable);
};
}
CollisionEvent::Stopped(collider_a, collider_b, _) => {
if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_a), climbables.get(*collider_b))
{
climber.intersecting_climbables.remove(&climbable);
}

if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_b), climbables.get(*collider_a))
{
climber.intersecting_climbables.remove(&climbable);
}
}
}
}
}

pub fn ignore_gravity_if_climbing(
mut query: Query<(&Climber, &mut GravityScale), Changed<Climber>>,
) {
for (climber, mut gravity_scale) in &mut query {
if climber.climbing {
gravity_scale.0 = 0.0;
} else {
gravity_scale.0 = 1.0;
}
}
}

pub struct ClimbingPlugin;

impl Plugin for ClimbingPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, detect_climb_range)
.add_systems(Update, ignore_gravity_if_climbing);
}
}
233 changes: 0 additions & 233 deletions examples/platformer/components.rs

This file was deleted.

Loading
Loading