bevy-laser/src/main.rs

81 lines
1.4 KiB
Rust

use bevy::{
prelude::*,
};
#[derive(Component,Debug)]
struct Pos(Vec2);
#[derive(Component,Debug)]
struct Ray(Vec2);
#[derive(Component,Debug)]
struct Col(Color);
#[derive(Component,Debug)]
struct Size(f32);
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (animate, rand_color, gizmos))
.run()
}
fn setup(
mut commands: Commands,
) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(250.0, 0.0, 0.0),
..default()
});
commands.spawn((
Pos(Vec2::new(0.0, 0.0)),
Ray(Vec2::new(100.0, 5.0)),
Col(Color::rgba(1.0, 0.0, 1.0, 1.0)),
Size(10.0)
));
commands.spawn((
Pos(Vec2::new(0.0, 0.0)),
Ray(Vec2::new(200.0, 1.0)),
Col(Color::rgba(1.0, 1.0, 0.0, 1.0)),
Size(20.0)
));
}
fn animate(
time: Res<Time>,
mut query: Query<(&mut Pos, &Ray)>
) {
let t = time.elapsed_seconds();
for (mut pos, ray) in &mut query {
pos.0.x = (ray.0.y*t).cos() * ray.0.x;
pos.0.y = (ray.0.y*t).sin() * ray.0.x;
}
}
fn rand_color(
time: Res<Time>,
mut query: Query<&mut Col>
) {
let t = time.delta_seconds();
for mut col in &mut query {
if let Color::Rgba{ ref mut red, .. } = col.0 {
*red += t;
if *red > 1.0 {
*red = 0.0;
}
}
}
}
fn gizmos(
mut gizmos: Gizmos,
query: Query<(&Pos,&Col,&Size)>
) {
for (Pos(pos), Col(color), Size(size)) in &query {
gizmos.circle_2d(*pos, *size, *color);
}
}