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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
//! Player controller, states, and animation implementation.

use std::collections::VecDeque;

use crate::prelude::*;

mod state;
pub use state::*;
use turborand::GenCore;

const PLAYER_COLORS: [Color; 4] = [
    Color::RED,
    Color::GREEN,
    Color::BLUE,
    Color::rgb(1.0, 0.0, 1.0),
];

pub fn plugin(session: &mut Session) {
    session.install_plugin(state::plugin);

    // Add other player systems
    session
        .stages
        .add_system_to_stage(CoreStage::First, hydrate_players)
        .add_system_to_stage(CoreStage::First, player_ai_system)
        .add_system_to_stage(CoreStage::PostUpdate, play_itemless_fin_animations)
        .add_system_to_stage(CoreStage::PostUpdate, player_facial_animations)
        .add_system_to_stage(CoreStage::PostUpdate, equip_hats)
        .add_system_to_stage(CoreStage::Last, delete_dead_ai_swords)
        .add_system_to_stage(CoreStage::Last, update_player_layers);
}

/// The player index, for example Player 1, Player 2, and so on.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HasSchema, Deref, DerefMut, Default)]
pub struct PlayerIdx(pub u32);

/// Contains the entities of the extra player layers, such as the player face and fin.
#[derive(Clone, HasSchema, Default)]
pub struct PlayerLayers {
    pub fin_anim: Ustr,
    pub fin_ent: Entity,
    pub fin_offset: Vec2,
    pub face_ent: Entity,
    pub face_anim: Ustr,
    pub hat_ent: Option<Entity>,
}

impl PlayerLayers {
    pub const FIN_Z_OFFSET: f32 = 0.5;
    pub const FACE_Z_OFFSET: f32 = 0.01;
    pub const HAT_Z_OFFSET: f32 = 0.02;
}

/// A component representing the current emote state of a player.
#[derive(Clone, HasSchema, Default)]
enum EmoteState {
    /// The player is not emoting
    #[default]
    Neutral,
    /// The player is emoting.
    Emoting((Emote, Entity)),
}

/// A component representing a region in which a player should emote in some way.
///
/// For example, a lit grenade could have a
#[derive(Debug, Clone, HasSchema)]
pub struct EmoteRegion {
    /// The entity that owns this emote region.
    pub owner: Option<Entity>,
    /// A buffer to prevent the player from spamming emotes.
    pub buffer: Option<Timer>,
    /// Whether or not the player must be looking at the center of the region to emote.
    pub direction_sensitive: bool,
    /// The size of the emote region
    pub size: Vec2,
    /// The emote the player should make
    pub emote: Emote,
    /// Whether or not this emote region is active. This provides an easy way to disable a region
    /// temporarily, without having to remove the component.
    pub active: bool,
}

impl Default for EmoteRegion {
    fn default() -> Self {
        Self {
            owner: None,
            active: true,
            buffer: None,
            emote: default(),
            size: Vec2::ZERO,
            direction_sensitive: true,
        }
    }
}

impl EmoteRegion {
    pub fn basic(emote: Emote, size: Vec2, sensitive: bool) -> Self {
        Self {
            emote,
            size,
            active: true,
            direction_sensitive: sensitive,
            ..default()
        }
    }
}

/// A kind of emote the player can make.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Emote {
    /// The player is alarmed!! Like a lit grenade was just thrown at them.
    #[default]
    Alarm,
}

impl Emote {
    pub fn face_animation_key(&self) -> Ustr {
        match self {
            Emote::Alarm => ustr("emote_alarm"),
        }
    }

    pub fn emote_animation_key(&self) -> Ustr {
        match self {
            Emote::Alarm => ustr("alarm"),
        }
    }
}

impl Emote {
    pub fn start_animation(player_ent: Entity, emote: Self) -> StaticSystem<(), ()> {
        (move |meta: Root<GameMeta>,
               assets: ResMut<AssetServer>,
               mut emote_states: CompMut<EmoteState>,
               mut entities: ResMut<Entities>,
               mut transforms: CompMut<Transform>,
               mut attachments: CompMut<PlayerBodyAttachment>,
               mut sprites: CompMut<AtlasSprite>,
               mut animated_sprites: CompMut<AnimatedSprite>| {
            let emote_key = emote.emote_animation_key();
            let Some(emote_meta) = meta
                .core
                .player_emotes
                .iter()
                .find(|(key, _)| **key == emote_key)
                .map(|(_, handle)| assets.get(*handle))
            else {
                return;
            };

            let ent = entities.create();

            let emote_state = emote_states.get_mut(player_ent).unwrap();
            *emote_state = EmoteState::Emoting((emote, ent));

            let transform = *transforms.get(player_ent).unwrap();
            transforms.insert(ent, transform);
            attachments.insert(
                ent,
                PlayerBodyAttachment {
                    player: player_ent,
                    offset: emote_meta.offset.extend(PlayerLayers::FACE_Z_OFFSET),
                    head: true,
                    ..default()
                },
            );
            sprites.insert(
                ent,
                AtlasSprite {
                    atlas: emote_meta.atlas,
                    ..default()
                },
            );
            animated_sprites.insert(
                ent,
                AnimatedSprite {
                    frames: emote_meta.animation.frames.clone(),
                    fps: emote_meta.animation.fps,
                    repeat: true,
                    ..default()
                },
            );
        })
        .system()
    }

    pub fn stop_animation(player_ent: Entity) -> StaticSystem<(), ()> {
        (move |mut emote_states: CompMut<EmoteState>, mut entities: ResMut<Entities>| {
            let emote_state = emote_states.get_mut(player_ent).unwrap();
            if let EmoteState::Emoting((_, emote_ent)) = *emote_state {
                entities.kill(emote_ent);
            }
            *emote_state = EmoteState::Neutral;
        })
        .system()
    }
}

/// Marker component indicating that a player has been killed.
///
/// This usually means their death animation is playing, and they are about to be de-spawned.
#[derive(Clone, HasSchema, Default)]
pub struct PlayerKilled {
    pub hit_from: Option<Vec2>,
}

/// Events that can be used to trigger player actions, such as killing, setting inventory, etc.
#[derive(Clone, Debug)]
pub struct PlayerCommand;

impl PlayerCommand {
    /// Kill a player.
    ///
    /// > **Note:** This doesn't despawn the player, it just puts the player into it's death animation.
    pub fn kill(player: Entity, hit_from: Option<Vec2>) -> StaticSystem<(), ()> {
        (move |entities: Res<Entities>,
               mut players_killed: CompMut<PlayerKilled>,
               mut items_dropped: CompMut<ItemDropped>,
               mut inventories: CompMut<Inventory>,
               player_indexes: Comp<PlayerIdx>| {
            if players_killed.contains(player) {
                // No need to kill him again
                return;
            }

            let Some(idx) = player_indexes.get(player) else {
                // Not a player, just ignore it.
                warn!("Tried to kill non-player entity.");
                return;
            };

            debug!("Killing player: {}", idx.0);

            // Drop any items the player was carrying
            let inventory = inventories.get(player).cloned().unwrap_or_default();
            if let Some(item) = inventory.0 {
                if entities.is_alive(item) {
                    items_dropped.insert(item, ItemDropped { player });
                }
            }

            // Update the inventory
            inventories.insert(player, Inventory(None));

            players_killed.insert(player, PlayerKilled { hit_from });
        })
        .system()
    }

    /// Despawn a player.
    ///
    /// > **Note:** This is different than the [`kill`][Self::kill] event in that it immediately
    /// > removes the player from the world, while [`kill`][Self::kill] will usually cause the
    /// > player to enter the death animation.
    /// >
    /// > [`despawn`][Self::despawn] is usually sent at the end of the player death animation.
    pub fn despawn(player: Entity) -> StaticSystem<(), ()> {
        (move |mut entities: ResMutInit<Entities>,
               attachments: Comp<Attachment>,
               player_indexes: Comp<PlayerIdx>,
               player_layers: Comp<PlayerLayers>,
               player_spawners: Comp<PlayerSpawner>,
               mut spawner_manager: SpawnerManager| {
            if player_indexes.contains(player) {
                entities
                    .iter_with(&attachments)
                    .filter(|(_, attachment)| attachment.entity == player)
                    .map(|(entity, _)| entity)
                    .collect::<Vec<_>>()
                    .iter()
                    .for_each(|entity| {
                        entities.kill(*entity);
                    });
                let layers = player_layers.get(player).unwrap();
                entities.kill(layers.fin_ent);
                entities.kill(layers.face_ent);
                entities.kill(player);

                // remove player from all player spawners
                spawner_manager.remove_spawned_entity_from_grouped_spawner(
                    player,
                    &player_spawners,
                    &entities,
                );
            } else {
                warn!("Tried to despawn non-player entity.");
            }
        })
        .system()
    }

    /// Set the player's inventory
    pub fn set_inventory(player: Entity, item: Option<Entity>) -> StaticSystem<(), ()> {
        (move |mut items_grabbed: CompMut<ItemGrabbed>,
               mut items_dropped: CompMut<ItemDropped>,
               mut inventories: CompMut<Inventory>| {
            let inventory = inventories.get(player).cloned().unwrap_or_default();

            // If there was a previous item, drop it
            if let Some(item) = inventory.0 {
                items_dropped.insert(item, ItemDropped { player });
            }

            // If there is a new item, grab it
            if let Some(item) = item {
                items_grabbed.insert(item, ItemGrabbed { player });
            }

            // Update the inventory
            inventories.insert(player, Inventory(item));
        })
        .system()
    }

    /// Have the player use the item they are carrying, if any.
    pub fn use_item(player: Entity) -> StaticSystem<(), ()> {
        (move |mut items_used: CompMut<ItemUsed>, inventories: CompMut<Inventory>| {
            // If the player has an item
            if let Some(item) = inventories.get(player).and_then(|x| x.0) {
                // Use it
                items_used.insert(item, ItemUsed { owner: player });
            }
        })
        .system()
    }

    /// Drop the player's hat and reset its state.
    pub fn drop_hat(player: Entity) -> StaticSystem<(), ()> {
        (move |player_layers: Comp<PlayerLayers>,
               mut player_body_attachments: CompMut<PlayerBodyAttachment>,
               mut bodies: CompMut<KinematicBody>,
               mut atlas_sprites: CompMut<AtlasSprite>| {
            let Some(layers) = player_layers.get(player) else {
                return;
            };
            let Some(hat_ent) = layers.hat_ent else {
                return;
            };
            // Drop the hat
            player_body_attachments.remove(hat_ent);
            bodies.get_mut(hat_ent).unwrap().is_deactivated = false;
            // Reset its states
            if let Some(hat_sprite) = atlas_sprites.get_mut(hat_ent) {
                hat_sprite.color.set_a(1.0);
            }
        })
        .system()
    }
}

#[derive(Clone, Debug, HasSchema)]
pub struct AiPlayer {
    /// Tick timer that is used for AI pausing logic.
    tick: Timer,
    /// Indicates the player is taking pause for the given number of ticks.
    pausing: u32,
    /// Buffers planned AI movements
    movement_buffer: Option<VecDeque<PlayerControl>>,
    /// The player that the AI is targeting.
    target_player: Option<Entity>,
}

impl Default for AiPlayer {
    fn default() -> Self {
        Self {
            tick: Timer::from_seconds(0.5, TimerMode::Repeating),
            pausing: 0,
            movement_buffer: Default::default(),
            target_player: Default::default(),
        }
    }
}

#[derive(Debug, HasSchema, Clone)]
#[schema(no_default)]
pub struct PathfindingDebugLines {
    pub entities: Vec<Entity>,
}

impl FromWorld for PathfindingDebugLines {
    fn from_world(world: &World) -> Self {
        let entities = world.run_system(
            |mut entities: ResMut<Entities>, mut transforms: CompMut<Transform>| {
                (0..MAX_PLAYERS)
                    .map(|_| {
                        let ent = entities.create();

                        transforms
                            .insert(ent, Transform::from_translation(Vec3::new(0.0, 0.0, -1.0)));

                        ent
                    })
                    .collect::<Vec<_>>()
            },
            (),
        );

        Self { entities }
    }
}

fn player_ai_system(
    entities: Res<Entities>,
    nav_graph: ResMutInit<NavGraph>,
    mut player_inputs: ResMutInit<MatchInputs>,
    mut ai_players: CompMut<AiPlayer>,
    player_indexes: Comp<PlayerIdx>,
    map: Res<LoadedMap>,
    transforms: Comp<Transform>,
    pathfinding_debug_line: ResMutInit<PathfindingDebugLines>,
    mut paths: CompMut<Path2d>,
    bodies: Comp<KinematicBody>,
    debug_settings: ResInit<DebugSettings>,
    rng: Res<GlobalRng>,
    time: Res<Time>,
) {
    const SWORD_SWING_DIST: f32 = 10.0;
    const AI_SPEED_MULTIPLIER: f32 = 0.65;

    for (ai_ent, (player_idx, transform, ai_player)) in
        entities.iter_with((&player_indexes, &transforms, &mut ai_players))
    {
        // Tick the AI timer
        ai_player.tick.tick(time.delta());

        // If a tick has elapsed
        if ai_player.tick.just_finished() {
            // If the player isn't pausing, then there's a 40% chance
            if ai_player.pausing == 0 && rng.chance(0.4) {
                // That we will pause for a random number of ticks between 0 and 2
                ai_player.pausing = (rng.f32_normalized() * 2.0).round() as u32
            }

            // If the player is pausing
            if ai_player.pausing > 0 {
                // Subtract a tick from how long they should pause.
                ai_player.pausing -= 1;
            }
        }

        // If the player is pausing, don't have the AI move this frame.
        if ai_player.pausing > 0 {
            continue;
        }

        let target_transform = match ai_player.target_player {
            Some(target_player) if transforms.contains(target_player) => {
                transforms.get(target_player).unwrap()
            }
            _ => {
                let players = entities
                    .iter_with((&player_indexes, &transforms))
                    .filter(|(ent, _)| *ent != ai_ent)
                    .collect::<Vec<_>>();
                if players.is_empty() {
                    continue;
                }

                let (target_player, (_, transform)) = players[rng.gen_usize() % players.len()];

                ai_player.target_player = Some(target_player);
                transform
            }
        };
        let target_pos = target_transform.translation.truncate();
        let tile = (target_pos / map.tile_size).floor().as_ivec2();
        let target_node = NavNode(tile);
        let ai_pos = transform.translation.truncate();
        let tile = (ai_pos / map.tile_size).floor().as_ivec2();
        let current_node = NavNode(tile);

        // Complete any previous movement instructions if we are in the middle of any
        if let Some(movement_buffer) = &mut ai_player.movement_buffer {
            if let Some(control) = movement_buffer.pop_front() {
                player_inputs.players[player_idx.0 as usize].control = control;
                if movement_buffer.is_empty() {
                    ai_player.movement_buffer = None;
                }
                continue;
            }
        }

        let path = petgraph::algo::astar(
            nav_graph.as_ref(),
            current_node,
            |x| x == target_node,
            |(_, _, edge)| edge.distance,
            |_| 0.0,
        );

        if let Some((_cost, path)) = path {
            if debug_settings.show_pathfinding_lines {
                paths.insert(
                    pathfinding_debug_line.entities[player_idx.0 as usize],
                    Path2d {
                        points: path
                            .iter()
                            .map(|x| x.0.as_vec2() * map.tile_size + map.tile_size / 2.0)
                            .collect(),
                        thickness: 2.0,
                        color: PLAYER_COLORS[player_idx.0 as usize],
                        ..default()
                    },
                );
            }

            if let Some(&next_node) = path.get(1) {
                let edge = nav_graph.edge_weight(current_node, next_node).unwrap();
                let mut movement_buffer = edge.inputs.clone();
                let mut first_movement = movement_buffer.pop_front().unwrap();

                // Slow down the AI movement according to the fixed multiplier
                first_movement.move_direction *= vec2(AI_SPEED_MULTIPLIER, 1.0);

                // This is a hack to prevent us from getting stuck when we think we should be falling
                // straight down and we actually need to move off of the block we're half-standing on.
                //
                // If we aren't moving at all, just move in the direction of player 1
                if bodies.get(ai_ent).unwrap().velocity == Vec2::ZERO
                    && first_movement.move_direction == Vec2::ZERO
                {
                    let sign = (path.get(2).unwrap_or(&next_node).x as f32 * map.tile_size.x
                        - transform.translation.x)
                        .signum();
                    first_movement.move_direction.x = sign;
                }

                player_inputs.players[player_idx.0 as usize].control = first_movement;
                if !movement_buffer.is_empty() {
                    ai_player.movement_buffer = Some(movement_buffer)
                }
            }

            if (target_pos - ai_pos).length() < SWORD_SWING_DIST {
                player_inputs.players[player_idx.0 as usize]
                    .control
                    .shoot_just_pressed = true;
                player_inputs.players[player_idx.0 as usize]
                    .control
                    .shoot_pressed = true;
            }
        } else if debug_settings.show_pathfinding_lines {
            let pos =
                current_node.0.as_vec2() * map.tile_size + map.tile_size / 2.0 - vec2(0.0, 4.0);
            paths.insert(
                pathfinding_debug_line.entities[player_idx.0 as usize],
                Path2d {
                    points: vec![pos, pos + vec2(0.0, 4.0)],
                    thickness: 8.0,
                    color: Color::RED,
                    ..default()
                },
            );
        }

        if !debug_settings.show_pathfinding_lines {
            paths.remove(pathfinding_debug_line.entities[player_idx.0 as usize]);
        }
    }
}

/// Resource that tracks which players have already been spawned before.
///
/// This lets us handle re-spawns differently, like not spawning you with a hat on a re-spawn.
///
/// TODO: This is probably temporary and will be removed when we have the proper tournament flow
/// down.
#[derive(Debug, Clone, HasSchema, Default)]
struct PlayersHaveSpawned {
    /// For each player, whether they have spawned before.
    pub players: [bool; MAX_PLAYERS as usize],
}

/// Marker component for a player hat.
#[derive(Debug, Clone, HasSchema, Default)]
struct Hat(Handle<HatMeta>);

/// Build `ColliderShape` for player from `PlayerMeta`.
pub fn player_collider_shape(meta: &PlayerMeta) -> ColliderShape {
    ColliderShape::Rectangle {
        size: meta.body_size,
    }
}

fn hydrate_players(
    mut commands: Commands,
    mut entities: ResMutInit<Entities>,
    game_meta: Root<GameMeta>,
    player_inputs: Res<MatchInputs>,
    player_indexes: Comp<PlayerIdx>,
    assets: Res<AssetServer>,
    mut player_states: CompMut<PlayerState>,
    mut inventories: CompMut<Inventory>,
    mut animation_bank_sprites: CompMut<AnimationBankSprite>,
    mut atlas_sprites: CompMut<AtlasSprite>,
    mut kinematic_bodies: CompMut<KinematicBody>,
    mut camera_subjects: CompMut<CameraSubject>,
    mut player_layers: CompMut<PlayerLayers>,
    mut player_body_attachments: CompMut<PlayerBodyAttachment>,
    mut transforms: CompMut<Transform>,
    mut emote_states: CompMut<EmoteState>,
    mut ai_players: CompMut<AiPlayer>,
    mut invincibles: CompMut<Invincibility>,
    mut element_kill_callbacks: CompMut<ElementKillCallback>,
    mut players_have_spawned: ResMutInit<PlayersHaveSpawned>,
    mut item_grabs: CompMut<ItemGrab>,
    mut item_throws: CompMut<ItemThrow>,
    mut items: CompMut<Item>,
    mut hats: CompMut<Hat>,
) {
    let mut not_hydrated_bitset = player_states.bitset().clone();
    not_hydrated_bitset.bit_not();
    not_hydrated_bitset.bit_and(player_indexes.bitset());

    // Create entities that we will use for the extra player layers
    let entity_count = not_hydrated_bitset.bit_count() * 3;
    let mut new_entities = (0..entity_count)
        .map(|_| entities.create())
        .collect::<Vec<_>>()
        .into_iter();
    // If a player doesn't have a hat we'll kill the entity we allocated for it.
    let mut to_kill = Vec::with_capacity(entity_count / 3);

    for player_entity in entities.iter_with_bitset(&not_hydrated_bitset) {
        let player_idx = player_indexes.get(player_entity).unwrap();
        let player_has_spawned = &mut players_have_spawned.players[player_idx.0 as usize];
        let player_handle = player_inputs.players[player_idx.0 as usize].selected_player;
        let player_hat = &player_inputs.players[player_idx.0 as usize].selected_hat;
        let is_ai = player_inputs.players[player_idx.0 as usize].is_ai;

        let meta = assets.get(player_handle);

        let animation_bank_sprite = AnimationBankSprite {
            current: "idle".into(),
            animations: meta.layers.body.animations.frames.clone(),
            last_animation: default(),
        };

        player_states.insert(player_entity, default());
        emote_states.insert(player_entity, default());
        animation_bank_sprites.insert(player_entity, animation_bank_sprite);
        inventories.insert(player_entity, default());
        invincibles.insert(
            player_entity,
            Invincibility::new(game_meta.core.config.respawn_invincibility_time),
        );
        element_kill_callbacks.insert(
            player_entity,
            ElementKillCallback::new(player_kill_callback(player_entity)),
        );

        atlas_sprites.insert(
            player_entity,
            AtlasSprite {
                atlas: meta.layers.body.atlas,
                ..default()
            },
        );
        kinematic_bodies.insert(
            player_entity,
            KinematicBody {
                shape: player_collider_shape(&meta),
                has_mass: true,
                has_friction: false,
                gravity: meta.gravity,
                is_controlled: true,
                ..default()
            },
        );
        camera_subjects.insert(player_entity, default());

        // Spawn the player's fin and face
        let fin_entity = new_entities.next().unwrap();
        let face_entity = new_entities.next().unwrap();

        // Fin
        transforms.insert(fin_entity, default());
        atlas_sprites.insert(
            fin_entity,
            AtlasSprite {
                atlas: meta.layers.fin.atlas,
                ..default()
            },
        );
        animation_bank_sprites.insert(
            fin_entity,
            AnimationBankSprite {
                current: "idle".into(),
                animations: meta.layers.fin.animations.clone(),
                last_animation: default(),
            },
        );
        player_body_attachments.insert(
            fin_entity,
            PlayerBodyAttachment {
                sync_color: true,
                sync_animation: false,
                head: false,
                player: player_entity,
                offset: meta.layers.fin.offset.extend(PlayerLayers::FIN_Z_OFFSET),
            },
        );

        // Face
        transforms.insert(face_entity, default());
        atlas_sprites.insert(
            face_entity,
            AtlasSprite {
                atlas: meta.layers.face.atlas,
                ..default()
            },
        );
        animation_bank_sprites.insert(
            face_entity,
            AnimationBankSprite {
                current: "idle".into(),
                animations: meta.layers.face.animations.clone(),
                last_animation: default(),
            },
        );
        player_body_attachments.insert(
            face_entity,
            PlayerBodyAttachment {
                player: player_entity,
                sync_color: true,
                sync_animation: false,
                head: true,
                offset: meta.layers.face.offset.extend(PlayerLayers::FACE_Z_OFFSET),
            },
        );

        // Hat
        let hat_ent = new_entities.next().unwrap();
        let hat_ent = if !*player_has_spawned {
            if let Some(hat_handle) = player_hat {
                let hat_meta = assets.get(*hat_handle);
                let atlas = hat_meta.atlas;
                let offset = hat_meta.offset.extend(PlayerLayers::HAT_Z_OFFSET);
                hats.insert(hat_ent, Hat(*hat_handle));
                transforms.insert(hat_ent, default());
                atlas_sprites.insert(hat_ent, AtlasSprite { atlas, ..default() });
                player_body_attachments.insert(
                    hat_ent,
                    PlayerBodyAttachment {
                        player: player_entity,
                        offset,
                        head: true,
                        sync_animation: false,
                        sync_color: true,
                    },
                );
                kinematic_bodies.insert(
                    hat_ent,
                    KinematicBody {
                        shape: ColliderShape::Rectangle {
                            size: hat_meta.body_size,
                        },
                        has_mass: true,
                        has_friction: true,
                        gravity: meta.gravity,
                        is_deactivated: true,
                        ..default()
                    },
                );
                items.insert(hat_ent, Item);
                item_grabs.insert(
                    hat_ent,
                    ItemGrab {
                        fin_anim: "grab_2".into(),
                        grab_offset: Vec2::ZERO,
                        sync_animation: false,
                    },
                );
                item_throws.insert(hat_ent, ItemThrow::strength(420.0));
                Some(hat_ent)
            } else {
                to_kill.push(hat_ent);
                None
            }
        } else {
            to_kill.push(hat_ent);
            None
        };

        // Insert player layers component
        player_layers.insert(
            player_entity,
            PlayerLayers {
                fin_anim: "idle".into(),
                fin_ent: fin_entity,
                fin_offset: Vec2::ZERO,
                face_anim: "idle".into(),
                face_ent: face_entity,
                hat_ent,
            },
        );

        *player_has_spawned = true;

        // Handle AI players
        if is_ai {
            ai_players.insert(player_entity, default());

            // Give the player a sword NOTE: It's not good that we're duplicating the sword hydrate
            // functionality here, and this is pretty hacky, but the AI as it stands is temporary
            // anyway, so it's fine for now.
            commands.add(
                move |mut entities: ResMutInit<Entities>,
                      mut swords: CompMut<sword::Sword>,
                      mut element_handles: CompMut<ElementHandle>,
                      assets: Res<AssetServer>,
                      mut hydrated: CompMut<MapElementHydrated>,
                      mut bodies: CompMut<KinematicBody>,
                      mut atlas_sprites: CompMut<AtlasSprite>,
                      mut items: CompMut<Item>,
                      mut transforms: CompMut<Transform>,
                      game_meta: Root<GameMeta>,
                      mut attachments: CompMut<PlayerBodyAttachment>,
                      mut inventories: CompMut<Inventory>| {
                    let element_handle = game_meta
                        .core
                        .map_elements
                        .iter()
                        .find(|handle| {
                            assets
                                .get(assets.get(**handle).data)
                                .try_cast_ref::<SwordMeta>()
                                .is_ok()
                        })
                        .unwrap();
                    let element_meta = assets.get(*element_handle);
                    if let Ok(SwordMeta {
                        atlas,
                        body_size,
                        can_rotate,
                        bounciness,
                        grab_offset,
                        ..
                    }) = assets.get(element_meta.data).try_cast_ref()
                    {
                        let sword_ent = entities.create();
                        inventories.insert(player_entity, Inventory(Some(sword_ent)));
                        items.insert(sword_ent, Item);
                        swords.insert(sword_ent, sword::Sword::default());
                        atlas_sprites.insert(sword_ent, AtlasSprite::new(*atlas));
                        transforms.insert(sword_ent, default());
                        element_handles.insert(sword_ent, ElementHandle(*element_handle));
                        hydrated.insert(sword_ent, MapElementHydrated);
                        bodies.insert(
                            sword_ent,
                            KinematicBody {
                                shape: ColliderShape::Rectangle { size: *body_size },
                                has_mass: true,
                                has_friction: true,
                                can_rotate: *can_rotate,
                                bounciness: *bounciness,
                                gravity: game_meta.core.physics.gravity,
                                ..default()
                            },
                        );
                        attachments.insert(
                            sword_ent,
                            PlayerBodyAttachment {
                                sync_color: false,
                                sync_animation: false,
                                player: player_entity,
                                head: false,
                                offset: grab_offset.extend(1.0),
                            },
                        );
                    }
                },
            );
        }
    }

    for entity in to_kill {
        entities.kill(entity);
    }
}

fn player_kill_callback(player_entity: Entity) -> StaticSystem<(), ()> {
    (move |mut entities: ResMutInit<Entities>,
           attachments: Comp<Attachment>,
           player_layers: Comp<PlayerLayers>| {
        entities
            .iter_with(&attachments)
            .filter(|(_, attachment)| attachment.entity == player_entity)
            .map(|(entity, _)| entity)
            .collect::<Vec<_>>()
            .iter()
            .for_each(|entity| {
                entities.kill(*entity);
            });
        let layers = player_layers.get(player_entity).unwrap();
        entities.kill(layers.fin_ent);
        entities.kill(layers.face_ent);
        entities.kill(player_entity);
    })
    .system()
}

/// System that makes sure the swords held by AI are despawned when they are killed.
fn delete_dead_ai_swords(
    mut entities: ResMutInit<Entities>,
    ai_players: Comp<AiPlayer>,
    items: Comp<Item>,
    dropped: Comp<ItemDropped>,
) {
    let mut to_kill = Vec::new();
    for (ent, (_item, dropped)) in entities.iter_with((&items, &dropped)) {
        if ai_players.contains(dropped.player) {
            to_kill.push(ent);
        }
    }
    for entity in to_kill {
        entities.kill(entity);
    }
}

/// Animate the player's fins while
fn play_itemless_fin_animations(
    entities: Res<Entities>,
    mut player_layers: CompMut<PlayerLayers>,
    animation_bank_sprites: Comp<AnimationBankSprite>,
    player_indexes: Comp<PlayerIdx>,
    player_inventories: PlayerInventories,
) {
    for (_, (player_layers, player_idx, animation_bank)) in
        entities.iter_with((&mut player_layers, &player_indexes, &animation_bank_sprites))
    {
        let inventory = player_inventories[player_idx.0 as usize];

        if inventory.is_none() {
            player_layers.fin_anim = animation_bank.current;
        }
    }
}

fn player_facial_animations(
    time: Res<Time>,
    entities: Res<Entities>,
    mut player_layers: CompMut<PlayerLayers>,
    mut emote_regions: CompMut<EmoteRegion>,
    transforms: Comp<Transform>,
    atlas_sprites: Comp<AtlasSprite>,
    mut emote_states: CompMut<EmoteState>,
    mut commands: Commands,
    players_killed: Comp<PlayerKilled>,
    animation_bank_sprites: CompMut<AnimationBankSprite>,
) {
    for (player_ent, (player_layer, atlas_sprite, animation_bank, emote_state)) in entities
        .iter_with((
            &mut player_layers,
            &atlas_sprites,
            &animation_bank_sprites,
            &mut emote_states,
        ))
    {
        if players_killed.contains(player_ent) {
            *emote_state = EmoteState::Neutral;
            player_layer.face_anim = animation_bank.current;
            continue;
        }

        let player_pos = transforms.get(player_ent).unwrap().translation.truncate();

        let mut triggered_emote = None;
        for (_, (emote_region, transform)) in entities.iter_with((&mut emote_regions, &transforms))
        {
            if !emote_region.active {
                continue;
            }

            // If a buffer exists, tick it.
            if let Some(buffer) = emote_region.buffer.as_mut() {
                buffer.tick(time.delta());

                // If the buffer is not finished, and the player is the owner, skip this emote.
                if let Some(owner) = emote_region.owner.as_ref() {
                    if *owner == player_ent && !(buffer.finished()) {
                        continue;
                    }
                }
            }

            let emote_pos = transform.translation.truncate();
            let direction = if atlas_sprite.flip_x { -1.0f32 } else { 1.0 };
            let is_facing_region = direction.signum() != (player_pos.x - emote_pos.x).signum();

            if !emote_region.direction_sensitive || is_facing_region {
                let emote_rect = Rect::new(
                    emote_pos.x,
                    emote_pos.y,
                    emote_region.size.x,
                    emote_region.size.y,
                );
                if emote_rect.contains(player_pos) {
                    triggered_emote = Some(emote_region.emote);
                }
            }
        }

        if let Some(new_emote) = triggered_emote {
            if let EmoteState::Emoting(already_emote) = emote_state {
                if new_emote != already_emote.0 {
                    player_layer.face_anim = new_emote.face_animation_key();
                    commands.add(Emote::stop_animation(player_ent));
                    commands.add(Emote::start_animation(player_ent, new_emote));
                }
            } else {
                player_layer.face_anim = new_emote.face_animation_key();
                commands.add(Emote::start_animation(player_ent, new_emote));
            }
        } else {
            player_layer.face_anim = animation_bank.current;
            commands.add(Emote::stop_animation(player_ent));
        }
    }
}

/// System that reads the [`PlayerLayers`] component and updates the animated sprite banks to match
/// the animations specified.
fn update_player_layers(
    entities: Res<Entities>,
    player_inputs: ResMutInit<MatchInputs>,
    mut animation_bank_sprites: CompMut<AnimationBankSprite>,
    mut player_body_attachments: CompMut<PlayerBodyAttachment>,
    player_layers: Comp<PlayerLayers>,
    player_indexes: Comp<PlayerIdx>,
    assets: Res<AssetServer>,
) {
    for (_ent, (layers, player_idx)) in entities.iter_with((&player_layers, &player_indexes)) {
        let player_handle = player_inputs.players[player_idx.0 as usize].selected_player;
        let player_meta = assets.get(player_handle);

        let face_bank = animation_bank_sprites.get_mut(layers.face_ent).unwrap();
        face_bank.current = layers.face_anim;

        let base_fin_offset = player_meta.layers.fin.offset;
        let total_fin_offset = base_fin_offset + layers.fin_offset;
        let fin_attachment = player_body_attachments.get_mut(layers.fin_ent).unwrap();
        fin_attachment.offset.x = total_fin_offset.x;
        fin_attachment.offset.y = total_fin_offset.y;

        let fin_bank = animation_bank_sprites.get_mut(layers.fin_ent).unwrap();
        fin_bank.current = layers.fin_anim;
    }
}

/// Equip player hats that have been picked up and used.
fn equip_hats(
    entities: Res<Entities>,
    mut items_used: CompMut<ItemUsed>,
    mut kinematic_bodies: CompMut<KinematicBody>,
    mut player_body_attachments: CompMut<PlayerBodyAttachment>,
    hats: Comp<Hat>,
    assets: Res<AssetServer>,
    player_inventories: PlayerInventories,
    mut player_layers: CompMut<PlayerLayers>,
    mut inventories: CompMut<Inventory>,
) {
    for (hat_ent, hat) in entities.iter_with(&hats) {
        // If the hat is being held
        if let Some(Inv { player, .. }) = player_inventories.find_item(hat_ent) {
            if items_used.remove(hat_ent).is_some() {
                inventories.get_mut(player).unwrap().0 = None;

                let hat_meta = assets.get(hat.0);
                kinematic_bodies.get_mut(hat_ent).unwrap().is_deactivated = true;
                player_body_attachments.insert(
                    hat_ent,
                    PlayerBodyAttachment {
                        player,
                        offset: hat_meta.offset.extend(PlayerLayers::HAT_Z_OFFSET),
                        head: true,
                        sync_animation: false,
                        sync_color: true,
                    },
                );
                player_layers.get_mut(player).unwrap().hat_ent = Some(hat_ent);
            }
        }
    }
}