only platform jumpable!

This commit is contained in:
Nixon 2022-08-27 21:26:31 +08:00
parent 9778367226
commit 9d58c12955

View file

@ -58,7 +58,7 @@ impl Plugin for GamePlugin {
.with_system(move_win_text_system), .with_system(move_win_text_system),
) )
.add_system_to_stage(CoreStage::PostUpdate, char_char_collision_event_system) .add_system_to_stage(CoreStage::PostUpdate, char_char_collision_event_system)
.add_system_to_stage(CoreStage::PostUpdate, char_airbound_event_system) .add_system_to_stage(CoreStage::PostUpdate, char_platform_collision_event_system)
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system); .add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
} }
} }
@ -107,7 +107,27 @@ pub struct Player;
pub struct Platform; pub struct Platform;
#[derive(Component)] #[derive(Component)]
pub struct Airbound(bool); pub struct PlatformCount(usize);
impl PlatformCount {
fn increment(&mut self) {
println!("increment");
self.0 += 1;
}
fn decrement(&mut self) {
println!("decrement");
self.0 = self.0.saturating_sub(1);
}
fn reset(&mut self) {
self.0 = 0;
}
fn is_landed(&self) -> bool {
return self.0 != 0;
}
}
#[derive(Component)] #[derive(Component)]
pub struct Melty(pub Color); pub struct Melty(pub Color);
@ -203,7 +223,7 @@ pub fn spawn_character(
.insert(Sensor) .insert(Sensor)
.insert(Collider::cuboid(30., 0.5)) .insert(Collider::cuboid(30., 0.5))
.insert(ActiveEvents::COLLISION_EVENTS) .insert(ActiveEvents::COLLISION_EVENTS)
.insert(Airbound(true)); .insert(PlatformCount(0));
}); });
character_list.0.insert(entity_commands.id()); character_list.0.insert(entity_commands.id());
@ -344,9 +364,9 @@ fn char_char_collision_event_system(
} }
} }
fn char_airbound_event_system( fn char_platform_collision_event_system(
mut collision_events: EventReader<CollisionEvent>, mut collision_events: EventReader<CollisionEvent>,
mut airbound_query: Query<&mut Airbound>, mut platform_count_query: Query<&mut PlatformCount>,
platform_query: Query<&Platform>, platform_query: Query<&Platform>,
) { ) {
// detect platform + player collisions only // detect platform + player collisions only
@ -354,32 +374,32 @@ fn char_airbound_event_system(
match collision_event { match collision_event {
CollisionEvent::Started(e1, e2, flags) => { CollisionEvent::Started(e1, e2, flags) => {
if *flags == CollisionEventFlags::SENSOR { if *flags == CollisionEventFlags::SENSOR {
if let (Ok(mut airbound), Ok(_)) = ( if let (Ok(mut platform_count), Ok(_)) = (
airbound_query.get_mut(*e1), platform_count_query.get_mut(*e1),
platform_query.get(*e2), platform_query.get(*e2),
) { ) {
airbound.0 = false; platform_count.increment();
} else if let (Ok(mut airbound), Ok(_)) = ( } else if let (Ok(mut platform_count), Ok(_)) = (
airbound_query.get_mut(*e2), platform_count_query.get_mut(*e2),
platform_query.get(*e1), platform_query.get(*e1),
) { ) {
airbound.0 = false; platform_count.increment();
} }
} }
} }
CollisionEvent::Stopped(e1, e2, flags) => { CollisionEvent::Stopped(e1, e2, flags) => {
if *flags == CollisionEventFlags::SENSOR { if *flags == CollisionEventFlags::SENSOR {
if let (Ok(mut airbound), Ok(_)) = ( if let (Ok(mut platform_count), Ok(_)) = (
airbound_query.get_mut(*e1), platform_count_query.get_mut(*e1),
platform_query.get(*e2), platform_query.get(*e2),
) { ) {
airbound.0 = true; platform_count.decrement();
} else if let (Ok(mut airbound), Ok(_)) = ( } else if let (Ok(mut platform_count), Ok(_)) = (
airbound_query.get_mut(*e2), platform_count_query.get_mut(*e2),
platform_query.get(*e1), platform_query.get(*e1),
) { ) {
airbound.0 = true; platform_count.decrement();
} }
} }
} }
@ -499,7 +519,7 @@ fn change_character_system(
fn player_movement_system( fn player_movement_system(
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut characters: Query<(&mut Velocity, &Children), With<Player>>, mut characters: Query<(&mut Velocity, &Children), With<Player>>,
airbound_query: Query<&Airbound>, mut platform_count_query: Query<&mut PlatformCount>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>, audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) { ) {
let right_pressed: bool = let right_pressed: bool =
@ -510,10 +530,11 @@ fn player_movement_system(
for (mut velocity, children) in characters.iter_mut() { for (mut velocity, children) in characters.iter_mut() {
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32; velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
let airbound: &Airbound = airbound_query.get(children[0]).unwrap(); let mut platform_count: Mut<PlatformCount> = platform_count_query.get_mut(children[0]).unwrap();
if keyboard_input.just_pressed(KeyCode::Space) && !airbound.0 { if keyboard_input.just_pressed(KeyCode::Space) && platform_count.is_landed() {
audio.send(AudioMsg::Jump).ok(); audio.send(AudioMsg::Jump).ok();
velocity.linvel.y = 700.; velocity.linvel.y = 700.;
platform_count.reset();
} }
} }
} }