-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathavatar_shape.rs
More file actions
189 lines (167 loc) · 7.13 KB
/
Copy pathavatar_shape.rs
File metadata and controls
189 lines (167 loc) · 7.13 KB
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::{
avatars::avatar_type::DclAvatarWireFormat,
comms::profile::{AvatarColor, AvatarColor3, AvatarEmote, AvatarWireFormat},
dcl::{
components::{proto_components::common::Color3, SceneComponentId},
crdt::{
grow_only_set::GenericGrowOnlySetComponentOperation,
last_write_wins::LastWriteWinsComponentOperation, SceneCrdtState,
SceneCrdtStateProtoComponents,
},
},
scene_runner::scene::Scene,
};
use godot::prelude::*;
#[allow(dead_code)]
trait ToDictionaryColorObject {
fn to_dictionary_color_object(&self) -> Dictionary;
}
impl ToDictionaryColorObject for crate::dcl::components::proto_components::common::Color3 {
fn to_dictionary_color_object(&self) -> Dictionary {
let mut dictionary = Dictionary::new();
dictionary.set("r", self.r);
dictionary.set("g", self.g);
dictionary.set("b", self.b);
dictionary.set("a", 1.0);
let mut ret_dictionary = Dictionary::new();
ret_dictionary.set("color", dictionary);
ret_dictionary
}
}
impl ToDictionaryColorObject for crate::dcl::components::proto_components::common::Color4 {
fn to_dictionary_color_object(&self) -> Dictionary {
let mut dictionary = Dictionary::new();
dictionary.set("r", self.r);
dictionary.set("g", self.g);
dictionary.set("b", self.b);
dictionary.set("a", self.a);
let mut ret_dictionary = Dictionary::new();
ret_dictionary.set("color", dictionary);
ret_dictionary
}
}
fn color3_to_avatar_color(color: Color3) -> AvatarColor {
AvatarColor {
color: AvatarColor3 {
r: color.r,
g: color.g,
b: color.b,
},
}
}
pub fn update_avatar_shape(scene: &mut Scene, crdt_state: &mut SceneCrdtState) {
let godot_dcl_scene = &mut scene.godot_dcl_scene;
let dirty_lww_components = &scene.current_dirty.lww_components;
let avatar_shape_component = SceneCrdtStateProtoComponents::get_avatar_shape(crdt_state);
if let Some(avatar_shape_dirty) = dirty_lww_components.get(&SceneComponentId::AVATAR_SHAPE) {
for entity in avatar_shape_dirty {
let new_value = avatar_shape_component.get(entity);
if new_value.is_none() {
continue;
}
let new_value = new_value.unwrap();
let (_godot_entity_node, mut node_3d) = godot_dcl_scene.ensure_node_3d(entity);
let new_value = new_value.value.clone();
let existing = node_3d.try_get_node_as::<Node>(NodePath::from("AvatarShape"));
if new_value.is_none() {
if let Some(mut avatar_node) = existing {
avatar_node.queue_free();
node_3d.remove_child(avatar_node);
}
} else if let Some(new_value) = new_value {
let avatar_name = new_value.name.unwrap_or("NPC".into());
let mut new_avatar_data = AvatarWireFormat {
wearables: new_value.wearables,
emotes: Some(
new_value
.emotes
.iter()
.enumerate()
.map(|(index, urn)| AvatarEmote {
slot: index as u32,
urn: urn.clone(),
})
.collect(),
),
..Default::default()
};
if let Some(body_shape) = new_value.body_shape {
new_avatar_data.body_shape = Some(body_shape);
}
if let Some(eye_color) = new_value.eye_color {
new_avatar_data.eyes = Some(color3_to_avatar_color(eye_color));
}
if let Some(hair_color) = new_value.hair_color {
new_avatar_data.hair = Some(color3_to_avatar_color(hair_color));
}
if let Some(skin_color) = new_value.skin_color {
new_avatar_data.skin = Some(color3_to_avatar_color(skin_color));
}
let new_avatar_data = DclAvatarWireFormat::from_gd(new_avatar_data);
if let Some(mut avatar_node) = existing {
avatar_node.call_deferred(
"async_update_avatar".into(),
&[new_avatar_data.to_variant(), avatar_name.to_variant()],
);
} else {
let mut new_avatar_shape = godot::engine::load::<PackedScene>(
"res://src/decentraland_components/avatar/avatar.tscn",
)
.instantiate()
.unwrap();
new_avatar_shape.set("skip_process".into(), true.to_variant());
new_avatar_shape.set_name(GString::from("AvatarShape"));
node_3d.add_child(new_avatar_shape.clone().upcast());
// Remove trigger detection for AvatarShapes - scene NPCs should not trigger areas
new_avatar_shape.call("remove_trigger_detection".into(), &[]);
new_avatar_shape.call_deferred(
"async_update_avatar".into(),
&[new_avatar_data.to_variant(), avatar_name.to_variant()],
);
}
}
}
}
}
pub fn update_avatar_shape_emote_command(scene: &mut Scene, crdt_state: &mut SceneCrdtState) {
let godot_dcl_scene = &mut scene.godot_dcl_scene;
let dirty_gos_components = &scene.current_dirty.gos_components;
let avatar_shape_component =
SceneCrdtStateProtoComponents::get_avatar_emote_command(crdt_state);
if let Some(avatar_emote_command_dirty) =
dirty_gos_components.get(&SceneComponentId::AVATAR_EMOTE_COMMAND)
{
for entity in avatar_emote_command_dirty.keys() {
let Some(emotes) = avatar_shape_component.get(entity) else {
continue;
};
if emotes.is_empty() {
continue;
}
let Some(node_3d) = godot_dcl_scene.get_node_or_null_3d(entity) else {
continue;
};
let Some(mut avatar_node) =
node_3d.try_get_node_as::<Node>(NodePath::from("AvatarShape"))
else {
continue;
};
let emote = emotes
.back()
.expect("emotes should have at least one element");
let local_emote = emote.emote_urn.contains(".glb") || emote.emote_urn.contains(".gltf");
let urn = if local_emote {
let Some(file_hash) = scene.content_mapping.get_hash(&emote.emote_urn) else {
continue;
};
format!(
"urn:decentraland:off-chain:scene-emote:{file_hash}-{}",
emote.r#loop
)
} else {
emote.emote_urn.clone()
};
avatar_node.call_deferred("async_play_emote".into(), &[urn.to_variant()]);
}
}
}