add change character system to play stageasdasd

This commit is contained in:
Nixon 2022-08-24 19:45:13 +08:00
parent e86ca82bc3
commit 3f79054ed7

View file

@ -34,13 +34,13 @@ impl Plugin for GamePlugin {
.add_system_set(
SystemSet::on_update(AppState::Game)
.with_system(crate::levels::post_setup_level)
.with_system(change_character_system)
.with_system(player_movement_system)
.with_system(move_camera)
.with_system(character_particle_effect_system),
)
.add_system_set(
SystemSet::on_update(AppState::Win)
.with_system(change_character_system)
.with_system(player_movement_system)
.with_system(move_camera)
.with_system(character_particle_effect_system),
@ -186,14 +186,16 @@ fn collision_event_system(
character_meshes: Res<CharacterMeshes>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut collision_events: EventReader<CollisionEvent>,
character_query: Query<(&CharacterColor, &Transform)>,
character_query: Query<(&CharacterColor, &Transform, Option<&Player>)>,
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
for collision_event in collision_events.iter() {
if let CollisionEvent::Started(e1, e2, flags) = collision_event {
if flags.is_empty() {
if let (Ok((c1_color, c1_transform)), Ok((c2_color, _c2_transform))) =
if let (
Ok((c1_color, c1_transform, c1_player)),
Ok((c2_color, c2_transform, c2_player))) =
(character_query.get(*e1), character_query.get(*e2))
{
// TODO completely remove particles
@ -210,6 +212,8 @@ fn collision_event_system(
app_state.replace(AppState::Win).ok();
}
// position character based on current player location
if let Some(_player) = c1_player {
spawn_character(
&mut commands,
&character_meshes,
@ -219,6 +223,17 @@ fn collision_event_system(
new_color.into(),
true,
);
} else if let Some(_player) = c2_player {
spawn_character(
&mut commands,
&character_meshes,
&mut materials,
&audio,
*c2_transform,
new_color.into(),
true,
);
}
}
}
}
@ -229,7 +244,7 @@ fn change_character_system(
mut commands: Commands,
keyboard_input: Res<Input<KeyCode>>,
characters: Query<(Entity, &Transform, &CharacterColor, Option<&Player>)>,
characters: Query<(Entity, &CharacterColor, Option<&Player>)>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
if !keyboard_input.just_pressed(KeyCode::Tab) { return; }
@ -238,7 +253,7 @@ fn change_character_system(
let mut player_count: usize = 0;
// find player idx
for (_entity, _transform, _color, player) in characters.iter() {
for (_entity, _color, player) in characters.iter() {
if let Some(_player) = player {
player_idx = player_count;
}
@ -250,7 +265,7 @@ fn change_character_system(
player_count = 0;
// exchange `Player` component from old `player_idx` to new `next_player_idx`
for (entity, _transform, color, _player) in characters.iter() {
for (entity, color, _player) in characters.iter() {
if player_count == player_idx {
commands.entity(entity).remove::<Player>();
}
@ -260,6 +275,7 @@ fn change_character_system(
audio
.send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()]))
.ok();
audio.send(AudioMsg::Jump).ok();
}
player_count += 1;