Character fusion

This commit is contained in:
Pascal Engélibert 2022-08-22 23:34:54 +02:00
parent a735ae7713
commit 55ab83588a
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
2 changed files with 35 additions and 20 deletions

View file

@ -5,7 +5,6 @@
* name * name
* scene management * scene management
* stream audio (with HexoSynthDSP) * stream audio (with HexoSynthDSP)
* character fusion
* color filters * color filters
* level design * level design
* win * win

View file

@ -6,7 +6,7 @@ use crate::AppState;
use bevy::{ use bevy::{
input::{keyboard::KeyCode, Input}, input::{keyboard::KeyCode, Input},
prelude::{shape::Quad, *}, prelude::{shape::Quad, *},
sprite::{MaterialMesh2dBundle, Mesh2dHandle}, sprite::Mesh2dHandle,
}; };
use bevy_fundsp::prelude::*; use bevy_fundsp::prelude::*;
use bevy_hanabi::*; use bevy_hanabi::*;
@ -70,6 +70,9 @@ struct SelectedCharacterId(Option<CharacterId>);
#[derive(Component)] #[derive(Component)]
struct CharacterIdList(BTreeSet<CharacterId>); struct CharacterIdList(BTreeSet<CharacterId>);
#[derive(Clone, Component, PartialEq)]
struct CharacterColor(Color);
// Systems // Systems
fn setup( fn setup(
@ -175,13 +178,14 @@ fn spawn_character(
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)), .clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)),
); );
commands commands
.spawn_bundle(MaterialMesh2dBundle { .spawn_bundle(ColorMesh2dBundle {
mesh: character_meshes.square.clone(), mesh: character_meshes.square.clone(),
material: materials.add(ColorMaterial::from(color)), material: materials.add(ColorMaterial::from(color)),
transform, transform,
..default() ..default()
}) })
.insert(character_id) .insert(character_id)
.insert(CharacterColor(color))
.insert(RigidBody::Dynamic) .insert(RigidBody::Dynamic)
.insert(Collider::cuboid(32., 32.)) .insert(Collider::cuboid(32., 32.))
.insert(ExternalForce::default()) .insert(ExternalForce::default())
@ -232,30 +236,43 @@ fn spawn_character(
fn collision_event_system( fn collision_event_system(
mut commands: Commands, mut commands: Commands,
//materials: ResMut<Assets<ColorMaterial>>, character_meshes: Res<CharacterMeshes>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut effects: ResMut<Assets<EffectAsset>>,
current_level: Res<CurrentLevel>, current_level: Res<CurrentLevel>,
mut collision_events: EventReader<CollisionEvent>, mut collision_events: EventReader<CollisionEvent>,
//character_query: Query<(&CharacterId, &Handle<ColorMaterial>)>, character_query: Query<(&CharacterId, &CharacterColor, &Transform)>,
character_query: Query<&CharacterId>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>, mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
) { ) {
if let Some(level_entity) = current_level.0 { if let Some(level_entity) = current_level.0 {
for collision_event in collision_events.iter() { for collision_event in collision_events.iter() {
if let CollisionEvent::Started(e1, e2, flags) = collision_event { if let CollisionEvent::Started(e1, e2, flags) = collision_event {
if flags.is_empty() { if flags.is_empty() {
//if let (Ok((c1_id, c1_material)), Ok((c2_id, c2_material))) = if let (
if let (Ok(c1_id), Ok(c2_id)) = Ok((c1_id, c1_color, c1_transform)),
(character_query.get(*e1), character_query.get(*e2)) Ok((c2_id, c2_color, _c2_transform)),
) = (character_query.get(*e1), character_query.get(*e2))
{ {
//c1_material.color = (Vec4::from(c1_material.color) + Vec4::from(c2_material.color)).into();
let (mut selected_character_id, mut character_id_list) = let (mut selected_character_id, mut character_id_list) =
level_query.get_mut(level_entity).unwrap(); level_query.get_mut(level_entity).unwrap();
character_id_list.0.remove(c1_id);
character_id_list.0.remove(c2_id); character_id_list.0.remove(c2_id);
if selected_character_id.0 == Some(*c2_id) { selected_character_id.0 = None;
selected_character_id.0 = Some(*c1_id);
} // TODO completely remove particles
commands.entity(*e1).despawn_recursive();
commands.entity(*e2).despawn_recursive(); commands.entity(*e2).despawn_recursive();
spawn_character(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
*c1_transform,
(Vec4::from(c1_color.0) + Vec4::from(c2_color.0)).into(),
);
} }
} }
} }
@ -330,12 +347,11 @@ fn keyboard_input_system(
keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A); keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A);
if let Some(selected_character_id) = &selected_character_id.0 { if let Some(selected_character_id) = &selected_character_id.0 {
if let Some((_character_id, mut velocity, _impulse, _force, _children)) = if let Some((_character_id, mut velocity, _impulse, _force, _children)) = characters
characters.iter_mut().find( .iter_mut()
|(character_id, _velocity, _impulse, _force, _children)| { .find(|(character_id, _velocity, _impulse, _force, _children)| {
*character_id == selected_character_id *character_id == selected_character_id
}, }) {
) {
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32; velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
if keyboard_input.just_pressed(KeyCode::Space) { if keyboard_input.just_pressed(KeyCode::Space) {