76 lines
2.3 KiB
Rust
76 lines
2.3 KiB
Rust
use godot::{
|
|
classes::{GridContainer, Label, PanelContainer},
|
|
prelude::*,
|
|
};
|
|
|
|
use crate::{game::Game, replay::StandaloneReplayManager};
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=PanelContainer, init)]
|
|
pub struct PlayerStatsPanel {
|
|
#[export]
|
|
player_name: Option<Gd<Label>>,
|
|
#[export]
|
|
stats_grid: Option<Gd<GridContainer>>,
|
|
|
|
base: Base<PanelContainer>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl PlayerStatsPanel {
|
|
#[func]
|
|
pub fn close(&mut self) {
|
|
self.base_mut().set_visible(false);
|
|
}
|
|
|
|
pub fn show(&mut self, player_id: u16) {
|
|
let stats = if let Some(game) = Game::singleton()
|
|
&& let Some(player) = game.bind().find_player(player_id)
|
|
{
|
|
Some((player.data.name.clone(), player.stats.clone()))
|
|
} else if let Some(playback) = &StandaloneReplayManager::singleton().bind().playback
|
|
&& let Some(player) = playback.bind().replay.players.get(&player_id)
|
|
{
|
|
Some((player.name.clone(), player.stats.clone()))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
if let Some(name_label) = &mut self.player_name
|
|
&& let Some((name, _)) = &stats
|
|
{
|
|
name_label.set_text(name);
|
|
}
|
|
|
|
if let Some(grid) = &mut self.stats_grid {
|
|
for child in grid.get_children().iter_shared() {
|
|
grid.remove_child(&child);
|
|
}
|
|
|
|
if let Some((_, stats)) = stats {
|
|
let stats = vec![
|
|
("Kills", stats.kills.to_string()),
|
|
("Deaths", stats.deaths.to_string()),
|
|
("Goals", stats.goals.to_string()),
|
|
("Own Goals", stats.own_goals.to_string()),
|
|
("Friendly Kills", stats.friendly_kills.to_string()),
|
|
("Friendly Deaths", stats.friendly_deaths.to_string()),
|
|
("Distance Moved", format!("{:.0}m", stats.distance_moved)),
|
|
];
|
|
|
|
for (stat_name, stat) in stats {
|
|
let mut label = Label::new_alloc();
|
|
label.set_text(&stat_name.to_owned());
|
|
grid.add_child(&label);
|
|
|
|
let mut label = Label::new_alloc();
|
|
label.set_text(&stat.to_owned());
|
|
grid.add_child(&label);
|
|
}
|
|
}
|
|
}
|
|
|
|
self.base_mut().set_visible(true);
|
|
}
|
|
}
|