aboutsummaryrefslogtreecommitdiff
path: root/src/classes/gameobject.rs
blob: fde042d55b086131883bf250da09d95c19de3c53 (plain)
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
use anyhow::Result;
use serde::Serialize;
use crate::object::Value;
use super::pptr::PPtr;

#[derive(Debug, Serialize)]
pub struct GameObject {
    pub components: Vec<PPtr>,
    pub layer: u32,
    pub tag: u16,
    pub name: String,
    pub is_active: bool,
}
impl GameObject {
    pub fn from_value(v: Value) -> Result<Self> {
        let mut fields = v.as_class("GameObject").unwrap();
        Ok(GameObject {
            components: fields
                .remove("m_Component")
                .unwrap()
                .as_array()
                .unwrap()
                .into_iter()
                .map(|e| {
                    PPtr::from_value(
                        e.as_class("ComponentPair")
                            .unwrap()
                            .remove("component")
                            .unwrap(),
                    )
                    .unwrap()
                })
                .collect(),
            layer: fields["m_Layer"].as_u32().unwrap(),
            tag: fields["m_Tag"].as_u16().unwrap(),
            name: fields["m_Name"].clone().as_string().unwrap(),
            is_active: fields["m_IsActive"].as_bool().unwrap(),
        })
    }
}