matrix_engine::engine::components

Struct Components

source
pub struct Components<C: Component> { /* private fields */ }

Implementations§

source§

impl<C: Component> Components<C>

source

pub fn new() -> Self

source

pub fn iter(&self) -> Iter<'_, C>

source

pub fn iter_mut(&mut self) -> IterMut<'_, C>

source

pub fn insert(&mut self, e: Entity, c: C)

Examples found in repository?
examples/run/main.rs (line 59)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    fn build(&self, scene: &mut matrix_engine::engine::scene::Scene<CustomEvents>) {
        let mut latest = Instant::now();
        let mut v = Vec::<f32>::new();
        let mut latest_second = Instant::now();
        scene.add_send_system(
            move |(events, write_events, id): &mut (
                ReadE<CustomEvents>,
                WriteE<CustomEvents>,
                ReadSystemID,
            )| {
                let now = Instant::now();

                v.push(1.0 / (now - latest).as_secs_f32());

                if (now - latest_second).as_secs() > 0 {
                    let fps = v.iter().sum::<f32>() / v.len() as f32;
                    println!("fps: {:10.5}, {:10.5}", fps, 1.0 / fps);
                    latest_second = now;
                }
                latest = now;

                if events.is_just_pressed(KeyCode::KeyW) {
                    write_events.send(MatrixEvent::DestroySystem(**id)).unwrap();
                }
            },
        );
        scene.add_send_startup_system(
            |render_objs: &mut WriteC<RenderObject>,
             transforms: &mut WriteC<Transform>,
             camera: &mut WriteR<Camera, CustomEvents>| {
                for i in 0..100 {
                    for y in 0..100 {
                        for z in 0..100 {
                            let e = Entity::new();
                            render_objs
                                .insert(e, RenderObject::new(Cube, "./img.jpg".to_string()));
                            transforms.insert(
                                e,
                                Transform::new_position(Vector3::new(
                                    5.0 * i as f32,
                                    5.0 * y as f32,
                                    5.0 * z as f32,
                                )),
                            );
                        }
                    }
                }

                camera.insert_and_notify(Camera {
                    eye: Vector3::new(0.0, 0.0, -1.),
                    dir: Vector3::new(1., 0., 0.),
                    up: Vector3::new(0., 1., 0.),
                    aspect: 1.,
                    fovy: PI / 4.,
                    znear: 0.1,
                    zfar: 1000.,
                });
            },
        );

        let mut yaw = 0.0; // Horizontal rotation around the y-axis
        let mut pitch = 0.0; // Vertical rotation
        scene.add_send_system(
            move |camera: &mut WriteR<Camera, CustomEvents>, events: &mut ReadE<CustomEvents>| {
                if let Some(camera) = camera.get_mut() {
                    let dt = events.dt();
                    let move_speed = dt * 3.;
                    let rotation_speed = dt * camera.fovy / PI;

                    // Get forward (z-axis), right (x-axis), and up (y-axis) direction vectors
                    let forward = camera.dir.normalized();
                    let right = forward.cross(&Vector3::unit_y()).normalized();
                    let up = Vector3::unit_y();

                    if events.is_pressed(KeyCode::KeyW) {
                        camera.eye += &forward * move_speed;
                    }
                    if events.is_pressed(KeyCode::KeyS) {
                        camera.eye -= &forward * move_speed;
                    }
                    if events.is_pressed(KeyCode::KeyA) {
                        camera.eye -= &right * move_speed;
                    }
                    if events.is_pressed(KeyCode::KeyD) {
                        camera.eye += &right * move_speed;
                    }
                    if events.is_pressed(KeyCode::Space) {
                        camera.eye += &up * move_speed;
                    }
                    if events.is_pressed(KeyCode::ControlLeft) {
                        camera.eye -= &up * move_speed;
                    }

                    if events.is_pressed(KeyCode::ArrowLeft) {
                        yaw -= rotation_speed; // Rotate left
                    }
                    if events.is_pressed(KeyCode::ArrowRight) {
                        yaw += rotation_speed; // Rotate right
                    }
                    if events.is_pressed(KeyCode::ArrowUp) {
                        pitch += rotation_speed; // Look up
                    }
                    if events.is_pressed(KeyCode::ArrowDown) {
                        pitch -= rotation_speed; // Look down
                    }

                    if events.is_just_pressed(KeyCode::KeyE) {
                        camera.fovy *= 2.0;
                    }
                    if events.is_just_pressed(KeyCode::KeyQ) {
                        camera.fovy /= 2.0;
                    }

                    // Update the camera's direction (yaw and pitch)
                    let (sin_yaw, cos_yaw) = yaw.sin_cos();
                    let (sin_pitch, cos_pitch) = pitch.sin_cos();

                    // Calculate the new forward direction after applying yaw and pitch
                    let new_forward =
                        Vector3::new(cos_pitch * cos_yaw, sin_pitch, cos_pitch * sin_yaw);

                    camera.dir = new_forward.normalized();
                }
            },
        );
    }
source

pub fn get(&self, e: &Entity) -> Option<&C>

source

pub fn get_mut(&mut self, e: &Entity) -> Option<&mut C>

Trait Implementations§

source§

impl<C: Debug + Component> Debug for Components<C>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<C: Component> Default for Components<C>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C> Freeze for Components<C>

§

impl<C> RefUnwindSafe for Components<C>
where C: RefUnwindSafe,

§

impl<C> Send for Components<C>

§

impl<C> Sync for Components<C>

§

impl<C> Unpin for Components<C>

§

impl<C> UnwindSafe for Components<C>
where C: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Downcast<T> for T

source§

fn downcast(&self) -> &T

source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> TypeIDable for T
where T: 'static,

source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> Component for T
where T: Send + Sync + Any,

source§

impl<T> Resource for T
where T: Send + 'static + ?Sized,

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSendSync for T

source§

impl<T> WasmNotSync for T
where T: Sync,