Compare commits

..

No commits in common. "9d58c12955c4173ecb3fda14d9e92d7b0bf8d81f" and "3c721c322551f7e4c150e557a779c03a92b13340" have entirely different histories.

View file

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