Skip to main content

cube/
cube.rs

1//! The smallest scene worth looking at: a lit cube turning on a grid, and a
2//! camera to fly around it.
3//!
4//! ```text
5//! cargo run --example cube
6//! ```
7use codecraft::ecs::Entity;
8use codecraft::glam::Vec3;
9use codecraft::prelude::*;
10use codecraft::sceneobjects::lights::default_lights;
11use codecraft::{Light, OrbitCamera, Transform, gizmos, primitives};
12
13struct CubeScene {
14    cube: Entity,
15}
16
17impl CubeScene {
18    fn new(app: &mut AppState) -> Self {
19        app.spawn(gizmos::grid());
20
21        let [key, rim] = default_lights();
22        app.spawn_entity((key, Light::object("Key Light")));
23        app.spawn_entity((rim, Light::object("Rim Light")));
24
25        let cube = app.spawn_entity(
26            primitives::Box::cube(1.0)
27                .at(0.0, 0.5, 0.0)
28                .color(Color::srgb(0.9, 0.45, 0.2)),
29        );
30        app.spawn_entity(
31            primitives::Sphere::new(0.4)
32                .at(1.8, 0.4, -0.6)
33                .color(Color::srgb(0.3, 0.6, 0.9)),
34        );
35
36        app.spawn_entity((
37            OrbitCamera::new(Vec3::new(0.0, 0.5, 0.0), 5.0).touchpad(),
38            OrbitCamera::object("Orbit Camera"),
39        ));
40
41        CubeScene { cube }
42    }
43}
44
45impl Scene for CubeScene {
46    fn update(&mut self, app: &mut AppState) {
47        let turn = app.time().elapsed * 0.6;
48        app.edit::<Transform>(self.cube, |transform| transform.set_yaw(turn));
49    }
50}
51
52fn main() {
53    App::new("cube").scene(CubeScene::new).run();
54}