fix: character fusion without player

This commit is contained in:
Pascal Engélibert 2022-08-24 18:51:18 +02:00
parent ec7d04c8c8
commit b42a032cfd
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
4 changed files with 67 additions and 115 deletions

View file

@ -6,9 +6,10 @@ pub use crate::filters::*;
use crate::AppState;
use bevy::{
ecs::system::EntityCommands,
input::{keyboard::KeyCode, Input},
prelude::{shape::Quad, *},
sprite::Mesh2dHandle, ecs::system::EntityCommands,
sprite::Mesh2dHandle,
};
use bevy_rapier2d::prelude::*;
@ -107,34 +108,23 @@ fn setup(
);
}
pub fn spawn_characters(
pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
transforms: &mut Vec<Transform>,
colors: &Vec<Color>,
characters: I,
) {
for (i, (transform, color)) in characters.into_iter().enumerate() {
spawn_character(
commands,
character_meshes,
materials,
audio,
transforms[0],
colors[0],
true,
);
for c in 1 .. transforms.len() {
spawn_character(
commands,
character_meshes,
materials,
audio,
transforms[c],
colors[c],
false,
transform,
color,
i == 0,
);
}
}
@ -198,8 +188,8 @@ fn collision_event_system(
if flags.is_empty() {
if let (
Ok((c1_color, c1_transform, c1_player)),
Ok((c2_color, c2_transform, c2_player))) =
(character_query.get(*e1), character_query.get(*e2))
Ok((c2_color, c2_transform, c2_player)),
) = (character_query.get(*e1), character_query.get(*e2))
{
commands.entity(*e1).despawn_recursive();
commands.entity(*e2).despawn_recursive();
@ -215,27 +205,23 @@ fn collision_event_system(
}
// position character based on current player location
if let Some(_player) = c1_player {
spawn_character(
&mut commands,
&character_meshes,
&mut materials,
&audio,
*c1_transform,
if c1_player.is_some() {
*c1_transform
} else if c2_player.is_some() {
*c2_transform
} else {
Transform::identity().with_translation(
(c1_transform.translation + c2_transform.translation) / 2.,
)
},
new_color.into(),
true,
c1_player.is_some() || c2_player.is_some(),
);
} else if let Some(_player) = c2_player {
spawn_character(
&mut commands,
&character_meshes,
&mut materials,
&audio,
*c2_transform,
new_color.into(),
true,
);
}
audio.send(AudioMsg::Fusion).ok();
}
@ -251,14 +237,16 @@ fn change_character_system(
characters: Query<(Entity, &CharacterColor, Option<&Player>)>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
if !keyboard_input.just_pressed(KeyCode::Tab) { return; }
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 let Some(_player) = player {
if player.is_some() {
player_idx = player_count;
}
player_count += 1;
@ -292,7 +280,6 @@ fn player_movement_system(
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
let right_pressed: bool =
keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D);
let left_pressed: bool =
@ -349,13 +336,13 @@ fn move_camera(
const FOLLOW_SPEED: f32 = std::f32::consts::PI;
for character_transform in characters.iter() {
let (camera, mut camera_transform) =
camera_query.single_mut();
let (camera, mut camera_transform) = camera_query.single_mut();
let size: Vec2 = camera.logical_viewport_size().unwrap();
let half_height: f32 = size.y * 0.5;
camera_transform.translation = camera_transform.translation.lerp(
character_transform.translation, time.delta_seconds() * FOLLOW_SPEED
character_transform.translation,
time.delta_seconds() * FOLLOW_SPEED,
);
// prevent camera from going too low

View file

@ -15,10 +15,7 @@ pub fn setup_level(
camera_query: &mut Query<&mut Transform, With<Camera>>,
level_id: LevelId,
) {
let level_entity = commands
.spawn()
.insert(Level)
.id();
let level_entity = commands.spawn().insert(Level).id();
current_level.0 = Some(level_id);
camera_query.single_mut().translation = Default::default();
@ -43,7 +40,7 @@ pub fn post_setup_level(
asset_server: Res<AssetServer>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
for LevelStartupEvent(level_entity) in level_startup_event.iter() {
for LevelStartupEvent(_level_entity) in level_startup_event.iter() {
if let Some(level_id) = current_level.0 {
match level_id.0 {
0 => level0::setup(

View file

@ -25,31 +25,15 @@ pub fn setup(
.insert(Collider::cuboid(400., 8.))
.insert(Level);
spawn_character(
spawn_characters(
commands,
character_meshes,
materials,
audio,
Transform::from_xyz(-128., -64., 0.),
Color::RED,
true,
);
spawn_character(
commands,
character_meshes,
materials,
audio,
Transform::from_xyz(0., -64., 0.),
Color::GREEN,
false,
);
spawn_character(
commands,
character_meshes,
materials,
audio,
Transform::from_xyz(128., -64., 0.),
Color::BLUE,
false,
[
(Transform::from_xyz(-128., -64., 0.), Color::RED),
(Transform::from_xyz(0., -64., 0.), Color::GREEN),
(Transform::from_xyz(128., -64., 0.), Color::BLUE),
],
);
}

View file

@ -39,32 +39,16 @@ pub fn setup(
.insert(Collider::cuboid(200., 8.))
.insert(Level);
spawn_character(
spawn_characters(
commands,
character_meshes,
materials,
audio,
Transform::from_xyz(128., 64., 0.),
Color::BLUE,
true,
);
spawn_character(
commands,
character_meshes,
materials,
audio,
Transform::from_xyz(-128., -128., 0.),
Color::RED,
false,
);
spawn_character(
commands,
character_meshes,
materials,
audio,
Transform::from_xyz(0., -128., 0.),
Color::GREEN,
false,
[
(Transform::from_xyz(128., 64., 0.), Color::BLUE),
(Transform::from_xyz(-128., -128., 0.), Color::RED),
(Transform::from_xyz(0., -128., 0.), Color::GREEN),
],
);
commands