@@ -50,6 +50,10 @@ pub struct UiValueAnimator {
5050 pub negative: AnimationNodeIndex,
5151}
5252
53+ #[derive(Reflect, Resource, Deref)]
54+ #[reflect(Resource)]
55+ pub struct PlayerUiAnimationInfo(pub AnimationInfo);
56+
5357#[derive(Component, Default, Reflect, Serialize, Deserialize, Debug)]
5458#[require(Text, UiValueAnimator)]
5559pub struct PlayerUiValue(pub i32);
@@ -121,47 +125,46 @@ impl Plugin for PlayerUiPlugin {
121125 Update,
122126 (update_created_player_ui, update_player_ui).run_if(in_state(GameState::Game)),
123127 )
128+ .register_type::<PlayerUiAnimationInfo>()
129+ .add_systems(PostStartup, create_player_anim)
124130 .add_systems(
125131 OnEnter(GameState::Game),
126132 (setup_player_ui).after(game::init_players),
127133 );
128134 }
129135}
130136
137+ fn create_player_anim(
138+ mut animation_graphs: ResMut<Assets<AnimationGraph>>,
139+ mut animation_clips: ResMut<Assets<AnimationClip>>,
140+ mut commands: Commands,
141+ ) {
142+ let anim = AnimationInfo::create(&mut animation_graphs, &mut animation_clips, "UiTarget");
143+ commands.insert_resource(PlayerUiAnimationInfo(anim));
144+ }
145+
131146#[derive(Component, Reflect, Serialize, Deserialize, Deref, Default, Debug)]
132147#[require(AnimationPlayer)]
133148pub struct PlayerUi(pub PlayerNumber);
134149
135150fn update_created_player_ui(
136151 mut ui_query: Query<TextUiPlayerElements, Added<PlayerTextInterface>>,
137152 player_query: Players,
138- mut animation_graphs: ResMut<Assets<AnimationGraph>>,
139- mut animation_clips: ResMut<Assets<AnimationClip>>,
153+ anim_info: Res<PlayerUiAnimationInfo>,
140154 mut commands: Commands,
141155) {
142156 for mut el in ui_query.iter_mut() {
143157 if let Some(player) = player_query.get_player(el.target.0) {
144- let AnimationInfo {
145- target_name: animation_target_name,
146- target_id: animation_target_id,
147- graph: animation_graph,
148- pos_node_index,
149- neg_node_index,
150- } = AnimationInfo::create(
151- &mut animation_graphs,
152- &mut animation_clips,
153- format!("Ui{:?}", el.element),
154- );
155158 el.update(&player);
156- el.anim_config.positive = pos_node_index;
157- el.anim_config.negative = neg_node_index;
159+ el.anim_config.positive = anim_info. pos_node_index;
160+ el.anim_config.negative = anim_info. neg_node_index;
158161 commands.entity(el.entity).insert((
159- AnimationGraphHandle(animation_graph ),
162+ AnimationGraphHandle(anim_info.graph.clone() ),
160163 AnimationTarget {
161- id: animation_target_id ,
164+ id: anim_info.target_id ,
162165 player: el.entity,
163166 },
164- animation_target_name ,
167+ anim_info.target_name.clone() ,
165168 ));
166169 }
167170 }
@@ -220,11 +223,20 @@ fn setup_player_ui(mut commands: Commands, asset_server: Res<AssetServer>, playe
220223 ] {
221224 let player_info = player_query.get_player(player).expect("ERROR");
222225 let avatar_path = player_info.details.avatar_path();
226+ let order = if player == PlayerNumber::First {
227+ FlexDirection::Row
228+ } else {
229+ FlexDirection::RowReverse
230+ };
223231 commands
224232 .spawn((style.clone(), GameObject))
225233 .insert(Name::new(format!("Ui{:?}", player)))
226234 .with_children(|p| {
227- p.spawn((
235+ p.spawn(Node {
236+ flex_direction: order,
237+ ..default()
238+ })
239+ .with_child((
228240 ImageNode {
229241 image: asset_server.load(avatar_path),
230242 ..default()
@@ -236,33 +248,24 @@ fn setup_player_ui(mut commands: Commands, asset_server: Res<AssetServer>, playe
236248 ..default()
237249 },
238250 ))
239- .with_children(|p| {
240- p.spawn((
241- ImageNode {
242- image: asset_server.load("img/player_frame_name.png"),
243- flip_x: right_align,
244- ..default()
245- },
246- Node {
247- padding: UiRect::all(Val::Px(8.0)),
248- position_type: PositionType::Absolute,
249- bottom: Val::Px(0.0),
250- ..default()
251- },
252- ))
253- .with_children(|name| {
254- name.spawn((
255- Text::new(player_info.details.name.clone()),
256- header_style.clone(),
257- TextLayout::new_with_justify(if right_align {
258- JustifyText::Right
259- } else {
260- JustifyText::Left
261- }),
262- ))
263- .insert(PlayerUi(player));
264- });
265- });
251+ .with_child((
252+ ImageNode {
253+ image: asset_server.load("img/player_frame_name.png"),
254+ flip_x: right_align,
255+ ..default()
256+ },
257+ Node {
258+ padding: UiRect::axes(Val::Px(12.0), Val::Px(0.0)),
259+ height: Val::Px(64.0),
260+ align_items: AlignItems::Center,
261+ ..default()
262+ },
263+ children![(
264+ Text::new(player_info.details.name.clone()),
265+ header_style.clone(),
266+ PlayerUi(player)
267+ )],
268+ ));
266269 p.spawn((
267270 ImageNode::new(asset_server.load("img/player_frame_resources.png")),
268271 Node {
@@ -303,9 +306,9 @@ fn setup_player_ui(mut commands: Commands, asset_server: Res<AssetServer>, playe
303306 margin: UiRect::bottom(Val::Px(5.0)),
304307 ..default()
305308 },
309+ children![ImageNode::new(asset_server.load("img/resource_frame.png"))],
306310 ))
307311 .with_children(|p| {
308- p.spawn(ImageNode::new(asset_server.load("img/resource_frame.png")));
309312 p.spawn(Node {
310313 position_type: PositionType::Absolute,
311314 top: Val::Px(0.0),
0 commit comments