From 84ba344597e54a34d06c1132e8d414b26c420064 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Sat, 27 Aug 2022 10:17:49 +0200 Subject: [PATCH] fix: character ordering --- src/game.rs | 65 ++++++++++++++++++++++++++++----------------------- src/levels.rs | 11 ++++++++- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/game.rs b/src/game.rs index c98f48e..b5c803e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -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::() .init_resource::() .insert_resource(CurrentLevel(None)) + .init_resource::() .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); +#[derive(Default)] +pub struct CharacterList(pub BTreeSet); + pub struct CharacterMeshes { square: Mesh2dHandle, } @@ -125,6 +130,7 @@ pub fn spawn_characters>( character_meshes: &Res, materials: &mut ResMut>, audio: &Res>, + character_list: &mut ResMut, characters: I, ) { @@ -134,6 +140,7 @@ pub fn spawn_characters>( character_meshes, materials, audio, + character_list, transform, color, i == 0, @@ -146,6 +153,7 @@ pub fn spawn_character( character_meshes: &Res, materials: &mut ResMut>, audio: &Res>, + character_list: &mut ResMut, 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>, audio: Res>, + mut character_list: ResMut, ) { 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>, characters: Query<(Entity, &CharacterColor, Option<&Player>)>, audio: Res>, + character_list: Res, ) { 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; + 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::(); + 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; - } - - // 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::(); - } - - if player_count == next_player_idx { - commands.entity(entity).insert(Player); - 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, mut camera_query: Query<&mut Transform, With>, keyboard_input: Res>, + mut character_list: ResMut, level_query: Query>, mut app_state: ResMut>, ) { @@ -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(); } diff --git a/src/levels.rs b/src/levels.rs index 240abdb..9a22e32 100644 --- a/src/levels.rs +++ b/src/levels.rs @@ -16,7 +16,12 @@ pub fn setup_level( level_startup_event.send(LevelStartupEvent); } -pub fn despawn_level(mut commands: Commands, level_query: Query>) { +pub fn despawn_level( + mut commands: Commands, + mut character_list: ResMut, + level_query: Query>, +) { + 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, asset_server: Res, audio: Res>, + mut character_list: ResMut, stored_levels_assets: Res>, stored_levels_handle: Res>, ) { @@ -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>, asset_server: &Res, audio: &Res>, + character_list: &mut ResMut, 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.),