only platform jumpable!
This commit is contained in:
parent
5484f0c960
commit
c597a9c6a9
1 changed files with 42 additions and 21 deletions
61
src/game.rs
61
src/game.rs
|
@ -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_airbound_event_system)
|
||||
.add_system_to_stage(CoreStage::PostUpdate, char_platform_collision_event_system)
|
||||
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +107,27 @@ pub struct Player;
|
|||
pub struct Platform;
|
||||
|
||||
#[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)]
|
||||
pub struct Melty(pub Color);
|
||||
|
@ -195,7 +215,7 @@ pub fn spawn_character(
|
|||
.insert(Sensor)
|
||||
.insert(Collider::cuboid(30., 0.5))
|
||||
.insert(ActiveEvents::COLLISION_EVENTS)
|
||||
.insert(Airbound(true));
|
||||
.insert(PlatformCount(0));
|
||||
});
|
||||
|
||||
character_list.0.insert(entity_commands.id());
|
||||
|
@ -336,9 +356,9 @@ fn char_char_collision_event_system(
|
|||
}
|
||||
}
|
||||
|
||||
fn char_airbound_event_system(
|
||||
fn char_platform_collision_event_system(
|
||||
mut collision_events: EventReader<CollisionEvent>,
|
||||
mut airbound_query: Query<&mut Airbound>,
|
||||
mut platform_count_query: Query<&mut PlatformCount>,
|
||||
platform_query: Query<&Platform>,
|
||||
) {
|
||||
// detect platform + player collisions only
|
||||
|
@ -346,32 +366,32 @@ fn char_airbound_event_system(
|
|||
match collision_event {
|
||||
CollisionEvent::Started(e1, e2, flags) => {
|
||||
if *flags == CollisionEventFlags::SENSOR {
|
||||
if let (Ok(mut airbound), Ok(_)) = (
|
||||
airbound_query.get_mut(*e1),
|
||||
if let (Ok(mut platform_count), Ok(_)) = (
|
||||
platform_count_query.get_mut(*e1),
|
||||
platform_query.get(*e2),
|
||||
) {
|
||||
airbound.0 = false;
|
||||
} else if let (Ok(mut airbound), Ok(_)) = (
|
||||
airbound_query.get_mut(*e2),
|
||||
platform_count.increment();
|
||||
} else if let (Ok(mut platform_count), Ok(_)) = (
|
||||
platform_count_query.get_mut(*e2),
|
||||
platform_query.get(*e1),
|
||||
) {
|
||||
airbound.0 = false;
|
||||
platform_count.increment();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CollisionEvent::Stopped(e1, e2, flags) => {
|
||||
if *flags == CollisionEventFlags::SENSOR {
|
||||
if let (Ok(mut airbound), Ok(_)) = (
|
||||
airbound_query.get_mut(*e1),
|
||||
if let (Ok(mut platform_count), Ok(_)) = (
|
||||
platform_count_query.get_mut(*e1),
|
||||
platform_query.get(*e2),
|
||||
) {
|
||||
airbound.0 = true;
|
||||
} else if let (Ok(mut airbound), Ok(_)) = (
|
||||
airbound_query.get_mut(*e2),
|
||||
platform_count.decrement();
|
||||
} else if let (Ok(mut platform_count), Ok(_)) = (
|
||||
platform_count_query.get_mut(*e2),
|
||||
platform_query.get(*e1),
|
||||
) {
|
||||
airbound.0 = true;
|
||||
platform_count.decrement();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -491,7 +511,7 @@ fn change_character_system(
|
|||
fn player_movement_system(
|
||||
keyboard_input: Res<Input<KeyCode>>,
|
||||
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>>,
|
||||
) {
|
||||
let right_pressed: bool =
|
||||
|
@ -502,10 +522,11 @@ 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 airbound: &Airbound = airbound_query.get(children[0]).unwrap();
|
||||
if keyboard_input.just_pressed(KeyCode::Space) && !airbound.0 {
|
||||
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() {
|
||||
audio.send(AudioMsg::Jump).ok();
|
||||
velocity.linvel.y = 700.;
|
||||
platform_count.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue