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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*
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::{BTreeSet, HashMap};
pub struct SceneTree {
pub objects: HashMap<Object, ObjectData>,
}
pub struct ObjectData {
pub pos: Vec3A,
pub rot: Vec3A,
pub parent: Object,
pub children: BTreeSet<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(),
children: BTreeSet::new(),
});
}
Packet::Remove(object) => {
if let Some(o) = self.objects.remove(&object) {
for c in o.children {
self.reparent(o.parent, c);
}
}
}
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) => {
self.reparent(*parent, *child);
}
_ => (),
}
}
pub fn reparent(&mut self, parent: Object, child: Object) {
if !self.objects.contains_key(&parent) || !self.objects.contains_key(&child) {
return;
}
if let Some(co) = self.objects.get(&child) {
let old_parent = co.parent;
if let Some(po) = self.objects.get_mut(&old_parent) {
po.children.remove(&child);
}
}
if let Some(po) = self.objects.get_mut(&parent) {
po.children.insert(child);
}
if let Some(co) = self.objects.get_mut(&child) {
co.parent = parent;
}
}
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()
}
}
|