summaryrefslogtreecommitdiff
path: root/shared/src/tree.rs
blob: 18d55b3b42c77acb9d8d88d756a96ca3c16c1eb5 (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
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
/*
    wearechat - generic multiplayer game with voip
    Copyright (C) 2025 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
use crate::{
    packets::{Object, Packet, Resource},
    resources::Prefab,
};
use glam::Vec3A;
use std::collections::HashMap;

pub struct SceneTree {
    pub objects: HashMap<Object, ObjectData>,
}
pub struct ObjectData {
    pub pos: Vec3A,
    pub rot: Vec3A,
    pub parent: Object,
    pub pose: Vec<f32>,
    pub res: Resource<Prefab>,
}
impl Default for SceneTree {
    fn default() -> Self {
        Self {
            objects: Default::default(),
        }
    }
}
impl SceneTree {
    pub fn packet(&mut self, p: &Packet) {
        match p {
            Packet::Add(object, res) => {
                self.objects.insert(*object, ObjectData {
                    parent: Object(0),
                    pos: Vec3A::ZERO,
                    rot: Vec3A::ZERO,
                    pose: Vec::new(),
                    res: res.clone(),
                });
            }
            Packet::Remove(object) => {
                self.objects.remove(&object);
            }
            Packet::Position(object, pos, rot) => {
                if let Some(o) = self.objects.get_mut(&object) {
                    o.pos = *pos;
                    o.rot = *rot;
                }
            }
            Packet::Pose(object, pose) => {
                if let Some(o) = self.objects.get_mut(&object) {
                    o.pose = pose.to_vec();
                }
            }
            Packet::Parent(parent, child) => {
                if let Some(o) = self.objects.get_mut(&parent) {
                    o.parent = *child
                }
            }
            _ => (),
        }
    }

    pub fn prime_client(&self) -> impl Iterator<Item = Packet> {
        self.objects
            .iter()
            .map(|(object, data)| {
                [
                    Packet::Add(*object, data.res.clone()),
                    Packet::Parent(*object, data.parent),
                    Packet::Position(*object, data.pos, data.rot),
                ]
                .into_iter()
                .chain(if data.pose.is_empty() {
                    None
                } else {
                    Some(Packet::Pose(*object, data.pose.clone()))
                })
            })
            .flatten()
    }
}