fix: character ordering

This commit is contained in:
Pascal Engélibert 2022-08-27 10:17:49 +02:00
parent 9f04ec861e
commit 84ba344597
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
2 changed files with 46 additions and 30 deletions

View file

@ -13,6 +13,7 @@ use bevy::{
};
use bevy_rapier2d::prelude::*;
use rapier2d::geometry::CollisionEventFlags;
use std::collections::BTreeSet;
pub enum AudioMsg {
Color([f32; 3]),
@ -33,6 +34,7 @@ impl Plugin for GamePlugin {
app.add_event::<LevelStartupEvent>()
.init_resource::<CharacterMeshes>()
.insert_resource(CurrentLevel(None))
.init_resource::<CharacterList>()
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup))
.add_system_set(SystemSet::on_enter(AppState::Win).with_system(win_setup))
.add_system_set(
@ -67,6 +69,9 @@ pub struct LevelStartupEvent;
pub struct CurrentLevel(pub Option<LevelId>);
#[derive(Default)]
pub struct CharacterList(pub BTreeSet<Entity>);
pub struct CharacterMeshes {
square: Mesh2dHandle,
}
@ -125,6 +130,7 @@ pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>(
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
character_list: &mut ResMut<CharacterList>,
characters: I,
) {
@ -134,6 +140,7 @@ pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>(
character_meshes,
materials,
audio,
character_list,
transform,
color,
i == 0,
@ -146,6 +153,7 @@ pub fn spawn_character(
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
character_list: &mut ResMut<CharacterList>,
mut transform: Transform,
color: Color,
is_player: bool,
@ -185,6 +193,8 @@ pub fn spawn_character(
.insert(CollisionCount(0));
});
character_list.0.insert(entity_commands.id());
if is_player {
entity_commands.insert(Player);
audio
@ -273,6 +283,7 @@ fn collision_event_system(
mut collision_counter_query: Query<&mut CollisionCount>,
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
mut character_list: ResMut<CharacterList>,
) {
for collision_event in collision_events.iter() {
match collision_event {
@ -283,6 +294,8 @@ fn collision_event_system(
Ok((c2_color, c2_transform, _c2_material, c2_player)),
) = (character_query.get(*e1), character_query.get(*e2))
{
character_list.0.remove(e1);
character_list.0.remove(e2);
commands.entity(*e1).despawn_recursive();
commands.entity(*e2).despawn_recursive();
@ -301,6 +314,7 @@ fn collision_event_system(
&character_meshes,
&mut materials,
&audio,
&mut character_list,
if c1_player.is_some() {
*c1_transform
} else if c2_player.is_some() {
@ -404,41 +418,32 @@ fn change_character_system(
keyboard_input: Res<Input<KeyCode>>,
characters: Query<(Entity, &CharacterColor, Option<&Player>)>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
character_list: Res<CharacterList>,
) {
if !keyboard_input.just_pressed(KeyCode::Tab) {
return;
}
let mut player_idx: usize = 0;
let mut player_count: usize = 0;
// find player idx
for (_entity, _color, player) in characters.iter() {
if player.is_some() {
player_idx = player_count;
}
player_count += 1;
}
// calculate next player index
let next_player_idx = (player_idx + 1) % player_count;
player_count = 0;
// exchange `Player` component from old `player_idx` to new `next_player_idx`
for (entity, color, _player) in characters.iter() {
if player_count == player_idx {
commands.entity(entity).remove::<Player>();
}
if player_count == next_player_idx {
commands.entity(entity).insert(Player);
if let Some((player_entity, _color, _)) = characters
.iter()
.find(|(_entity, _color, player)| player.is_some())
.or_else(|| characters.iter().next())
{
commands.entity(player_entity).remove::<Player>();
if let Some(new_player_entity) = character_list
.0
.range(player_entity..)
.nth(1)
.or_else(|| character_list.0.iter().next())
{
commands.entity(*new_player_entity).insert(Player);
if let Ok((_entity, color, _player)) = characters.get(*new_player_entity) {
audio
.send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()]))
.ok();
audio.send(AudioMsg::Switch).ok();
}
player_count += 1;
}
}
}
@ -557,6 +562,7 @@ fn level_keyboard_system(
mut level_startup_event: EventWriter<LevelStartupEvent>,
mut camera_query: Query<&mut Transform, With<Camera>>,
keyboard_input: Res<Input<KeyCode>>,
mut character_list: ResMut<CharacterList>,
level_query: Query<Entity, With<Level>>,
mut app_state: ResMut<State<AppState>>,
) {
@ -568,6 +574,7 @@ fn level_keyboard_system(
}
if keyboard_input.just_pressed(KeyCode::R) {
character_list.0.clear();
for entity in level_query.iter() {
commands.entity(entity).despawn_recursive();
}

View file

@ -16,7 +16,12 @@ pub fn setup_level(
level_startup_event.send(LevelStartupEvent);
}
pub fn despawn_level(mut commands: Commands, level_query: Query<Entity, With<Level>>) {
pub fn despawn_level(
mut commands: Commands,
mut character_list: ResMut<CharacterList>,
level_query: Query<Entity, With<Level>>,
) {
character_list.0.clear();
for entity in level_query.iter() {
commands.entity(entity).despawn_recursive();
}
@ -32,6 +37,7 @@ pub fn post_setup_level(
mut level_startup_event: EventReader<LevelStartupEvent>,
asset_server: Res<AssetServer>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
mut character_list: ResMut<CharacterList>,
stored_levels_assets: Res<Assets<StoredLevels>>,
stored_levels_handle: Res<Handle<StoredLevels>>,
) {
@ -50,6 +56,7 @@ pub fn post_setup_level(
&mut materials,
&asset_server,
&audio,
&mut character_list,
stored_level,
);
}
@ -64,6 +71,7 @@ pub fn spawn_stored_level(
materials: &mut ResMut<Assets<ColorMaterial>>,
asset_server: &Res<AssetServer>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
character_list: &mut ResMut<CharacterList>,
stored_level: &StoredLevel,
) {
@ -84,6 +92,7 @@ pub fn spawn_stored_level(
character_meshes,
materials,
audio,
character_list,
stored_level.characters.iter().map(|character| {
(
Transform::from_xyz(character.pos.x, character.pos.y, 0.),