Compare commits
No commits in common. "9d58c12955c4173ecb3fda14d9e92d7b0bf8d81f" and "3c721c322551f7e4c150e557a779c03a92b13340" have entirely different histories.
9d58c12955
...
3c721c3225
1 changed files with 21 additions and 43 deletions
62
src/game.rs
62
src/game.rs
|
@ -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_platform_collision_event_system)
|
.add_system_to_stage(CoreStage::PostUpdate, char_airbound_event_system)
|
||||||
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
|
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,27 +107,7 @@ pub struct Player;
|
||||||
pub struct Platform;
|
pub struct Platform;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct PlatformCount(usize);
|
pub struct Airbound(bool);
|
||||||
|
|
||||||
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);
|
||||||
|
@ -223,7 +203,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(PlatformCount(0));
|
.insert(Airbound(true));
|
||||||
});
|
});
|
||||||
|
|
||||||
character_list.0.insert(entity_commands.id());
|
character_list.0.insert(entity_commands.id());
|
||||||
|
@ -292,7 +272,6 @@ pub fn spawn_melty_platform(
|
||||||
.insert(Collider::cuboid(48., 8.))
|
.insert(Collider::cuboid(48., 8.))
|
||||||
.insert(Melty(color))
|
.insert(Melty(color))
|
||||||
.insert(Level)
|
.insert(Level)
|
||||||
.insert(Platform)
|
|
||||||
.with_children(|c| {
|
.with_children(|c| {
|
||||||
c.spawn_bundle(SpriteBundle {
|
c.spawn_bundle(SpriteBundle {
|
||||||
texture: asset_server.get_handle("melty.png"),
|
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 collision_events: EventReader<CollisionEvent>,
|
||||||
mut platform_count_query: Query<&mut PlatformCount>,
|
mut airbound_query: Query<&mut Airbound>,
|
||||||
platform_query: Query<&Platform>,
|
platform_query: Query<&Platform>,
|
||||||
) {
|
) {
|
||||||
// detect platform + player collisions only
|
// detect platform + player collisions only
|
||||||
|
@ -374,32 +353,32 @@ fn char_platform_collision_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 platform_count), Ok(_)) = (
|
if let (Ok(mut airbound), Ok(_)) = (
|
||||||
platform_count_query.get_mut(*e1),
|
airbound_query.get_mut(*e1),
|
||||||
platform_query.get(*e2),
|
platform_query.get(*e2),
|
||||||
) {
|
) {
|
||||||
platform_count.increment();
|
airbound.0 = false;
|
||||||
} else if let (Ok(mut platform_count), Ok(_)) = (
|
} else if let (Ok(mut airbound), Ok(_)) = (
|
||||||
platform_count_query.get_mut(*e2),
|
airbound_query.get_mut(*e2),
|
||||||
platform_query.get(*e1),
|
platform_query.get(*e1),
|
||||||
) {
|
) {
|
||||||
platform_count.increment();
|
airbound.0 = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CollisionEvent::Stopped(e1, e2, flags) => {
|
CollisionEvent::Stopped(e1, e2, flags) => {
|
||||||
if *flags == CollisionEventFlags::SENSOR {
|
if *flags == CollisionEventFlags::SENSOR {
|
||||||
if let (Ok(mut platform_count), Ok(_)) = (
|
if let (Ok(mut airbound), Ok(_)) = (
|
||||||
platform_count_query.get_mut(*e1),
|
airbound_query.get_mut(*e1),
|
||||||
platform_query.get(*e2),
|
platform_query.get(*e2),
|
||||||
) {
|
) {
|
||||||
platform_count.decrement();
|
airbound.0 = true;
|
||||||
} else if let (Ok(mut platform_count), Ok(_)) = (
|
} else if let (Ok(mut airbound), Ok(_)) = (
|
||||||
platform_count_query.get_mut(*e2),
|
airbound_query.get_mut(*e2),
|
||||||
platform_query.get(*e1),
|
platform_query.get(*e1),
|
||||||
) {
|
) {
|
||||||
platform_count.decrement();
|
airbound.0 = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -519,7 +498,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>>,
|
||||||
mut platform_count_query: Query<&mut PlatformCount>,
|
airbound_query: Query<&Airbound>,
|
||||||
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
||||||
) {
|
) {
|
||||||
let right_pressed: bool =
|
let right_pressed: bool =
|
||||||
|
@ -530,11 +509,10 @@ 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 mut platform_count: Mut<PlatformCount> = platform_count_query.get_mut(children[0]).unwrap();
|
let airbound: &Airbound = airbound_query.get(children[0]).unwrap();
|
||||||
if keyboard_input.just_pressed(KeyCode::Space) && platform_count.is_landed() {
|
if keyboard_input.just_pressed(KeyCode::Space) && !airbound.0 {
|
||||||
audio.send(AudioMsg::Jump).ok();
|
audio.send(AudioMsg::Jump).ok();
|
||||||
velocity.linvel.y = 700.;
|
velocity.linvel.y = 700.;
|
||||||
platform_count.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue