Skip to content

Commit

Permalink
fix: add avatar components to primary player (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
leanmendoza authored Nov 20, 2023
1 parent 4eeadfd commit 57a30cd
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
4 changes: 4 additions & 0 deletions godot/src/logic/player/player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ func _ready():

floor_snap_length = 0.2

Global.avatars.update_primary_player_profile(Global.config.avatar_profile)
Global.comms.update_profile_avatar(Global.config.avatar_profile)
# TODO: check this, the comms method already emit the signal of profile changed
# so, maybe we don't need to call here, only wait the signal
avatar.update_avatar(Global.config.avatar_profile)

Global.config.param_changed.connect(self._on_param_changed)
Expand All @@ -81,6 +84,7 @@ func _ready():

func _on_player_profile_changed(new_profile: Dictionary):
avatar.update_avatar(new_profile)
Global.avatars.update_primary_player_profile(new_profile)


func _on_param_changed(_param):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ layout_mode = 2
size_flags_horizontal = 3
tooltip_text = "Select scene to load"
focus_mode = 0
item_count = 8
item_count = 10
popup/item_0/text = "mannakia.dcl.eth"
popup/item_0/id = 0
popup/item_1/text = "http://127.0.0.1:8000"
Expand All @@ -114,6 +114,10 @@ popup/item_6/text = "https://peer.decentraland.org"
popup/item_6/id = 6
popup/item_7/text = "shibu.dcl.eth"
popup/item_7/id = 7
popup/item_8/text = "https://leanmendoza.github.io/mannakia-dcl-scene/mannakia-dcl-scene"
popup/item_8/id = 8
popup/item_9/text = "https://sdilauro.github.io/dae-unit-tests/dae-unit-tests"
popup/item_9/id = 9

[node name="HSeparator4" type="HSeparator" parent="VBoxContainer/ColorRect_Background/HBoxContainer/VBoxContainer_General"]
layout_mode = 2
Expand Down
63 changes: 58 additions & 5 deletions rust/decentraland-godot-lib/src/avatars/avatar_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ macro_rules! sync_crdt_lww_component {

#[godot_api]
impl AvatarScene {
#[func]
pub fn update_primary_player_profile(&mut self, profile: Dictionary) {
let mut serialized_profile = SerializedProfile::default();
serialized_profile.copy_from_godot_dictionary(&profile);
let base_url = profile
.get("base_url")
.map(|v| v.to_string())
.unwrap_or("https://peer.decentraland.org/content".into());

self.update_avatar(
SceneEntityId::PLAYER,
&serialized_profile,
base_url.as_str(),
);
}

#[func]
pub fn update_avatar_profile(&mut self, alias: u32, profile: Dictionary) {
let entity_id = if let Some(entity_id) = self.avatar_entity.get(&alias) {
Expand Down Expand Up @@ -325,14 +341,28 @@ impl AvatarScene {
self._update_avatar_transform(&entity_id, dcl_transform);
}

pub fn update_avatar(&mut self, alias: u32, profile: &SerializedProfile, base_url: &str) {
pub fn update_avatar_by_alias(
&mut self,
alias: u32,
profile: &SerializedProfile,
base_url: &str,
) {
let entity_id = if let Some(entity_id) = self.avatar_entity.get(&alias) {
*entity_id
} else {
// TODO: handle this condition
return;
};

self.update_avatar(entity_id, profile, base_url);
}

pub fn update_avatar(
&mut self,
entity_id: SceneEntityId,
profile: &SerializedProfile,
base_url: &str,
) {
// Avoid updating avatar with the same data
if let Some(val) = self.last_updated_profile.get(&entity_id) {
if profile.eq(val) {
Expand All @@ -341,10 +371,12 @@ impl AvatarScene {
}
self.last_updated_profile.insert(entity_id, profile.clone());

self.avatar_godot_scene.get_mut(&entity_id).unwrap().call(
"update_avatar".into(),
&[profile.to_godot_dictionary(base_url).to_variant()],
);
if let Some(avatar_scene) = self.avatar_godot_scene.get_mut(&entity_id) {
avatar_scene.call(
"update_avatar".into(),
&[profile.to_godot_dictionary(base_url).to_variant()],
);
}

let new_avatar_base = Some(profile.to_pb_avatar_base());
let avatar_base_component =
Expand Down Expand Up @@ -528,5 +560,26 @@ impl AvatarScene {
local_avatar_equipped_data
);
}

let entity_id = &SceneEntityId::PLAYER;
let target_player_identity_data =
SceneCrdtStateProtoComponents::get_player_identity_data_mut(target_crdt_state);
sync_crdt_lww_component!(
entity_id,
target_player_identity_data,
local_player_identity_data
);

let target_avatar_base =
SceneCrdtStateProtoComponents::get_avatar_base_mut(target_crdt_state);
sync_crdt_lww_component!(entity_id, target_avatar_base, local_avatar_base);

let target_avatar_equipped_data =
SceneCrdtStateProtoComponents::get_avatar_equipped_data_mut(target_crdt_state);
sync_crdt_lww_component!(
entity_id,
target_avatar_equipped_data,
local_avatar_equipped_data
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ impl CommunicationManager {
match &mut self.current_adapter {
Adapter::None | Adapter::SignedLogin(_) => {}
Adapter::Livekit(_livekit_room) => {
// TODO: implement
// livekit_room.change_profile(self.player_identity.clone());
}
Adapter::WsRoom(ws_room) => {
Expand Down
2 changes: 1 addition & 1 deletion rust/decentraland-godot-lib/src/comms/livekit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl LivekitRoom {
continue;
}

avatar_scene.update_avatar(
avatar_scene.update_avatar_by_alias(
peer.alias,
&serialized_profile,
&profile_response.base_url,
Expand Down
2 changes: 1 addition & 1 deletion rust/decentraland-godot-lib/src/comms/ws_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ impl WebSocketRoom {
continue;
}

self.avatars.bind_mut().update_avatar(
self.avatars.bind_mut().update_avatar_by_alias(
update.from_alias,
&serialized_profile,
&profile_response.base_url,
Expand Down

0 comments on commit 57a30cd

Please sign in to comment.