Rotating filter

This commit is contained in:
Pascal Engélibert 2022-08-25 09:30:47 +02:00
parent 05ecbbbbec
commit 5ac4b4711c
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
6 changed files with 58 additions and 24 deletions

BIN
assets/bevy.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View file

@ -1,20 +1,48 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
#[derive(Component, Default)]
pub struct FilterColor(pub Color);
#[derive(Component)]
pub enum PassThroughFilter {
Absorbing,
/// Absorbs this color (ignoring alpha)
Absorbing(Color),
/// Rotates hue by this angle (in degrees)
Rotating(f32),
}
impl PassThroughFilter {
pub fn apply(&self, color: Color) -> Color {
match self {
PassThroughFilter::Absorbing(filter_color) => Vec4::from(color)
.mul_add(
-Vec4::from(*filter_color) * Vec4::from([1., 1., 1., 0.]),
Vec4::from(color),
)
.into(),
PassThroughFilter::Rotating(filter_angle) => {
let mut hsla = color.as_hsla_f32();
hsla[0] = (hsla[0] + filter_angle) % 360.;
Color::hsla(hsla[0], hsla[1], hsla[2], hsla[3])
}
}
}
}
#[derive(Bundle)]
pub struct AbsorbingFilter {
pub color: FilterColor,
#[bundle]
pub mesh: ColorMesh2dBundle,
pub collider: Collider,
pub sensor: Sensor,
pub filter_type: PassThroughFilter,
pub filter: PassThroughFilter,
}
#[derive(Bundle)]
pub struct RotatingFilter {
#[bundle]
pub sprite: SpriteBundle,
pub collider: Collider,
pub sensor: Sensor,
pub filter: PassThroughFilter,
pub velocity: Velocity,
pub rigid_body: RigidBody,
}

View file

@ -184,7 +184,7 @@ fn collision_event_system(
&mut Handle<ColorMaterial>,
Option<&Player>,
)>,
pass_through_filter_query: Query<(&PassThroughFilter, &FilterColor)>,
pass_through_filter_query: Query<&PassThroughFilter>,
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
@ -231,21 +231,11 @@ fn collision_event_system(
audio.send(AudioMsg::Fusion).ok();
}
} else if *flags == CollisionEventFlags::SENSOR {
if let (
Ok((mut c_color, _c_transform, mut c_material, c_player)),
Ok((filter, filter_color)),
) = (
if let (Ok((mut c_color, _c_transform, mut c_material, c_player)), Ok(filter)) = (
character_query.get_mut(*e1),
pass_through_filter_query.get(*e2),
) {
c_color.0 = match filter {
PassThroughFilter::Absorbing => Vec4::from(c_color.0)
.mul_add(
-Vec4::from(filter_color.0) * Vec4::from([1., 1., 1., 0.]),
Vec4::from(c_color.0),
)
.into(),
};
c_color.0 = filter.apply(c_color.0);
*c_material = materials.add(ColorMaterial::from(c_color.0));
if c_player.is_some() {

View file

@ -56,6 +56,7 @@ pub fn post_setup_level(
&character_meshes,
&mut materials,
&audio,
&asset_server,
),
_ => game_over::setup(&mut commands, &asset_server),
}

View file

@ -9,6 +9,7 @@ pub fn setup(
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
asset_server: &Res<AssetServer>,
) {
commands
.spawn_bundle(ColorMesh2dBundle {
@ -53,7 +54,6 @@ pub fn setup(
commands
.spawn_bundle(AbsorbingFilter {
color: FilterColor(Color::RED),
mesh: ColorMesh2dBundle {
mesh: meshes
.add(Mesh::from(Quad {
@ -67,7 +67,22 @@ pub fn setup(
},
collider: Collider::cuboid(64., 8.),
sensor: Sensor,
filter_type: PassThroughFilter::Absorbing,
filter: PassThroughFilter::Absorbing(Color::RED),
})
.insert(Level);
commands
.spawn_bundle(RotatingFilter {
sprite: SpriteBundle {
texture: asset_server.get_handle("bevy.png"),
transform: Transform::from_xyz(256., -224., 2.),
..Default::default()
},
collider: Collider::ball(32.),
sensor: Sensor,
filter: PassThroughFilter::Rotating(45.),
velocity: Velocity::angular(1.),
rigid_body: RigidBody::KinematicVelocityBased,
})
.insert(Level);
}

View file

@ -49,12 +49,12 @@ fn setup(mut commands: Commands, mut windows: ResMut<Windows>, asset_server: Res
.unwrap()
.set_title(String::from("Bevyjam"));
#[cfg(not(target_arch = "wasm32"))]
let font: Handle<Font> = asset_server.load("UacariLegacy-Thin.ttf");
#[cfg(target_arch = "wasm32")]
let font: Handle<Font> = asset_server.load("UacariLegacy-Thin.ttf");
commands.insert_resource(font);
let bevy_icon: Handle<Image> = asset_server.load("bevy.png");
commands.insert_resource(bevy_icon);
commands.spawn_bundle(Camera2dBundle::default());
commands.insert_resource(AmbientLight {
color: Color::WHITE,