1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use bevy::prelude::*;
use bones_bevy_renderer::*;
use bones_lib::prelude as bones;

#[derive(Deref, DerefMut, Resource)]
struct Session(pub bones_lib::prelude::World);

impl HasBonesWorld for Session {
    fn world(&mut self) -> &mut bones::World {
        &mut self.0
    }
}

pub fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(BonesRendererPlugin::<Session>::new())
        .add_startup_system(setup)
        .add_system(spin)
        .run();
}

/// Setup the game, loading the metadata and starting the game session.
fn setup(mut commands: Commands) {
    let mut world = bones::World::new();

    commands.spawn(Camera2dBundle::default());

    world
        .run_system(
            |mut entities: bones::ResMut<bones::Entities>,
             mut path2ds: bones::CompMut<bones::Path2d>,
             mut transforms: bones::CompMut<bones::Transform>| {
                const SIZE: f32 = 100.0;

                // Draw a red square
                let ent = entities.create();
                path2ds.insert(
                    ent,
                    bones::Path2d {
                        color: bones::Color::RED,
                        points: vec![
                            glam::vec2(-SIZE, -SIZE),
                            glam::vec2(SIZE, -SIZE),
                            glam::vec2(SIZE, SIZE),
                            glam::vec2(-SIZE, SIZE),
                            glam::vec2(-SIZE, -SIZE),
                        ],
                        thickness: 2.0,
                        ..default()
                    },
                );
                transforms.insert(ent, default());

                const SIZE2: f32 = SIZE / 2.0;

                // Draw two blue lines
                let ent = entities.create();
                path2ds.insert(
                    ent,
                    bones::Path2d {
                        color: bones::Color::BLUE,
                        points: vec![
                            // The first line
                            glam::vec2(-SIZE2, -SIZE2),
                            glam::vec2(SIZE2, -SIZE2),
                            // The second line
                            glam::vec2(SIZE2, SIZE2),
                            glam::vec2(-SIZE2, SIZE2),
                        ],
                        thickness: 4.0,
                        // This means that it won't connect points with indexes 2 and 3, so that
                        // they will be separate lines.
                        line_breaks: vec![2],
                    },
                );
                transforms.insert(ent, default());
            },
        )
        .unwrap();

    commands.insert_resource(Session(world));
}

fn spin(session: ResMut<Session>) {
    session
        .run_initialized_system(|mut transforms: bones::CompMut<bones::Transform>| {
            transforms.iter_mut().for_each(|trans| {
                trans.rotation *= Quat::from_axis_angle(Vec3::Z, f32::to_radians(0.1))
            });
        })
        .unwrap();
}