cyber_rider/src/lights.rs

210 lines
6.4 KiB
Rust
Raw Normal View History

use bevy::prelude::*;
use crate::geometry::PLANET_RADIUS;
pub const LIGHT_RANGE: f32 = PLANET_RADIUS * 0.6;
pub const LIGHT_DIST: f32 = PLANET_RADIUS * 1.2;
#[derive(Component)]
struct Animate;
2022-01-14 00:14:08 +00:00
fn spawn_lights(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let red_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::RED,
radius: 1.0,
..Default::default()
};
let blue_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::BLUE,
radius: 1.0,
..Default::default()
};
let purple_light = PointLight {
intensity: 1_000.0,
range: LIGHT_RANGE,
color: Color::PURPLE,
radius: 1.0,
..Default::default()
};
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.32,
});
// east light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(LIGHT_DIST, 0.0, 0.0),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
// west light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(-LIGHT_DIST, 0.0, 0.0),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
// north light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, LIGHT_DIST),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
// south light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, 0.0, -LIGHT_DIST),
point_light: purple_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::UVSphere {
radius: 10.0,
..Default::default()
})),
material: materials.add(StandardMaterial {
base_color: Color::PURPLE,
emissive: Color::rgba_linear(50.0, 0.0, 50.0, 0.0),
..Default::default()
}),
..Default::default()
});
})
.insert(Animate);
// up light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, LIGHT_DIST, 0.0),
point_light: red_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 10.0,
subdivisions: 2,
})),
material: materials.add(StandardMaterial {
base_color: Color::RED,
emissive: Color::rgba_linear(100.0, 0.0, 0.0, 0.0),
..Default::default()
}),
..Default::default()
});
});
// down light
commands
.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(0.0, -LIGHT_DIST, 0.0),
point_light: blue_light,
..Default::default()
})
.with_children(|builder| {
builder.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 10.0,
subdivisions: 2,
})),
material: materials.add(StandardMaterial {
base_color: Color::BLUE,
emissive: Color::rgba_linear(0.0, 0.0, 100.0, 0.0),
..Default::default()
}),
..Default::default()
});
});
}
fn animate_lights(
time: Res<Time>,
mut query: Query<&mut Transform, (With<PointLight>, With<Animate>)>,
) {
let dt = time.delta_seconds();
for mut transform in query.iter_mut() {
let translation = &transform.translation;
let x = translation.x;
let y = translation.y;
let z = if translation.z.abs() < 0.1 {
translation.z + 0.15
} else {
translation.z
};
let rads = 10.0f32.to_radians();
let anim_rate = rads * dt;
let theta = z.atan2(x) + anim_rate;
let x_new = LIGHT_DIST * theta.cos();
let z_new = LIGHT_DIST * theta.sin();
*transform = Transform::from_xyz(x_new, y, z_new);
}
}
2022-01-14 00:14:08 +00:00
pub struct CyberSpaceLightsPlugin;
impl Plugin for CyberSpaceLightsPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(spawn_lights)
.add_system(animate_lights);
}
}