1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use super::*;

pub static ID: Lazy<Ustr> = Lazy::new(|| ustr("core::midair"));

pub fn install(session: &mut Session) {
    PlayerState::add_player_state_transition_system(session, player_state_transition);
    PlayerState::add_player_state_update_system(session, handle_player_state);
    PlayerState::add_player_state_update_system(session, use_drop_or_grab_items_system(*ID));
}

pub fn player_state_transition(
    entities: Res<Entities>,
    player_inputs: Res<MatchInputs>,
    player_indexes: Comp<PlayerIdx>,
    assets: Res<AssetServer>,
    mut player_states: CompMut<PlayerState>,
    bodies: Comp<KinematicBody>,
    mut audio_center: ResMut<AudioCenter>,
) {
    for (_ent, (player_idx, player_state, body)) in
        entities.iter_with((&player_indexes, &mut player_states, &bodies))
    {
        let meta_handle = player_inputs.players[player_idx.0 as usize].selected_player;
        let meta = assets.get(meta_handle);
        let control = &player_inputs.players[player_idx.0 as usize].control;
        if player_state.current != *ID {
            continue;
        }

        if body.is_on_ground {
            // Play land sound
            audio_center.play_sound(meta.sounds.land, meta.sounds.land_volume);
            // Switch to idle state
            player_state.current = *idle::ID;
        } else if control.ragdoll_just_pressed {
            // TODO audio
            player_state.current = *ragdoll::ID;
        }
    }
}

pub fn handle_player_state(
    entities: Res<Entities>,
    player_inputs: Res<MatchInputs>,
    player_indexes: Comp<PlayerIdx>,
    player_states: Comp<PlayerState>,
    assets: Res<AssetServer>,
    mut sprites: CompMut<AtlasSprite>,
    mut animations: CompMut<AnimationBankSprite>,
    mut bodies: CompMut<KinematicBody>,
) {
    let players = entities.iter_with((
        &player_states,
        &player_indexes,
        &mut animations,
        &mut sprites,
        &mut bodies,
    ));
    for (_player_ent, (player_state, player_idx, animation, sprite, body)) in players {
        if player_state.current != *ID {
            continue;
        }
        let meta_handle = player_inputs.players[player_idx.0 as usize].selected_player;
        let meta = assets.get(meta_handle);
        let control = &player_inputs.players[player_idx.0 as usize].control;

        if body.velocity.y > 0.0 {
            animation.current = "rise".into();
        } else {
            animation.current = "fall".into();
        }

        // Limit fall speed if holding jump button
        if control.jump_pressed {
            body.velocity.y = body.velocity.y.max(-meta.stats.slow_fall_speed);
        }

        // Walk in movement direction
        body.velocity.x += meta.stats.accel_air_speed * control.move_direction.x;
        if control.move_direction.x.is_sign_positive() {
            body.velocity.x = body.velocity.x.min(meta.stats.air_speed);
        } else {
            body.velocity.x = body.velocity.x.max(-meta.stats.air_speed);
        }

        if control.move_direction.x == 0.0 {
            if body.velocity.x.is_sign_positive() {
                body.velocity.x = (body.velocity.x - meta.stats.slowdown).max(0.0);
            } else {
                body.velocity.x = (body.velocity.x + meta.stats.slowdown).min(0.0);
            }
        }

        // Fall through platforms
        body.fall_through = control.move_direction.y < -0.5 && control.jump_pressed;

        // Point in movement direction
        if control.move_direction.x > 0.0 {
            sprite.flip_x = false;
        } else if control.move_direction.x < 0.0 {
            sprite.flip_x = true;
        }
    }
}