use nightshade_api::prelude::*;
struct Planet {
entity: Entity,
moon: Entity,
radius: f32,
speed: f32,
}
fn main() {
let mut app = open();
show_grid(&mut app.world, false);
set_background(&mut app.world, Background::Space);
set_bloom(&mut app.world, true);
let sun = spawn_sphere(&mut app.world, vec3(0.0, 2.0, 0.0));
set_scale(&mut app.world, sun, vec3(2.0, 2.0, 2.0));
set_emissive(&mut app.world, sun, [1.0, 0.8, 0.3], 12.0);
set_unlit(&mut app.world, sun, true);
let bodies = [(3.5, 0.9, ORANGE), (5.5, 0.6, BLUE), (8.0, 0.35, GOLD)];
let orbit_segments = 96;
let orbit_lines = bodies
.iter()
.flat_map(|&(radius, _, color)| {
(0..orbit_segments).map(move |segment| {
let start_angle = segment as f32 / orbit_segments as f32 * std::f32::consts::TAU;
let end_angle =
(segment + 1) as f32 / orbit_segments as f32 * std::f32::consts::TAU;
Line {
start: vec3(start_angle.cos() * radius, 2.0, start_angle.sin() * radius),
end: vec3(end_angle.cos() * radius, 2.0, end_angle.sin() * radius),
color: vec4(color[0], color[1], color[2], color[3]),
}
})
})
.collect();
spawn_lines(&mut app.world, orbit_lines);
let planets: Vec<Planet> = bodies
.into_iter()
.map(|(radius, speed, color)| {
let entity = spawn_sphere(&mut app.world, vec3(radius, 2.0, 0.0));
set_scale(&mut app.world, entity, vec3(0.6, 0.6, 0.6));
set_color(&mut app.world, entity, color);
let moon = spawn_sphere(&mut app.world, vec3(radius + 1.2, 2.0, 0.0));
set_scale(&mut app.world, moon, vec3(0.2, 0.2, 0.2));
set_color(&mut app.world, moon, GRAY);
set_parent(&mut app.world, moon, Some(entity));
Planet {
entity,
moon,
radius,
speed,
}
})
.collect();
orbit_camera(&mut app.world, vec3(0.0, 2.0, 0.0), 18.0);
while frame(&mut app) {
let time = elapsed_seconds(&app.world);
let step = delta_time(&app.world);
for planet in &planets {
let angle = time * planet.speed;
set_position(
&mut app.world,
planet.entity,
vec3(
angle.cos() * planet.radius,
2.0,
angle.sin() * planet.radius,
),
);
rotate(&mut app.world, planet.entity, Vec3::y(), step * 0.8);
rotate(&mut app.world, planet.moon, Vec3::y(), step * 0.5);
}
}
}