119 lines
4.3 KiB
Rust
119 lines
4.3 KiB
Rust
use godot::{
|
|
classes::{HBoxContainer, IHBoxContainer, Label, Texture2D, TextureRect},
|
|
prelude::*,
|
|
};
|
|
|
|
use crate::{game_manager::Game, replay::StandaloneReplayManager};
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=HBoxContainer, init)]
|
|
pub struct PlayerListing {
|
|
#[export]
|
|
name_label: Option<Gd<Label>>,
|
|
#[export]
|
|
kills_label: Option<Gd<Label>>,
|
|
#[export]
|
|
deaths_label: Option<Gd<Label>>,
|
|
#[export]
|
|
ping_label: Option<Gd<Label>>,
|
|
#[export]
|
|
ping_low: Option<Gd<Texture2D>>,
|
|
#[export]
|
|
ping_low_color: Color,
|
|
#[export]
|
|
ping_medium: Option<Gd<Texture2D>>,
|
|
#[export]
|
|
ping_medium_color: Color,
|
|
#[export]
|
|
ping_high: Option<Gd<Texture2D>>,
|
|
#[export]
|
|
ping_high_color: Color,
|
|
#[export]
|
|
ping_icon: Option<Gd<TextureRect>>,
|
|
|
|
pub player_id: Option<u16>,
|
|
|
|
base: Base<HBoxContainer>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl IHBoxContainer for PlayerListing {
|
|
fn ready(&mut self) {
|
|
self.update();
|
|
if let Some(game) = Game::singleton() {
|
|
game.signals()
|
|
.on_players_updated()
|
|
.connect_other(self, |s| s.update());
|
|
game.signals()
|
|
.on_player_killed()
|
|
.connect_other(self, |s, _| s.update());
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PlayerListing {
|
|
pub fn update(&mut self) {
|
|
if let Some(player_id) = self.player_id {
|
|
if let Some(game) = Game::singleton() {
|
|
if let Some(player) = game.bind().find_player(player_id) {
|
|
if let Some(name_label) = &mut self.name_label {
|
|
name_label.set_text(&player.data.name);
|
|
}
|
|
if let Some(kills_label) = &mut self.kills_label {
|
|
kills_label.set_text(&player.data.kills.to_string());
|
|
}
|
|
if let Some(deaths_label) = &mut self.deaths_label {
|
|
deaths_label.set_text(&player.data.deaths.to_string());
|
|
}
|
|
if let Some(ping_label) = &mut self.ping_label {
|
|
ping_label.set_text(&player.data.ping.to_string());
|
|
}
|
|
if let Some(ping_icon) = &mut self.ping_icon {
|
|
let (icon, color) = match player.data.ping {
|
|
..100 => (self.ping_low.clone(), self.ping_low_color),
|
|
100..200 => (self.ping_medium.clone(), self.ping_medium_color),
|
|
200.. => (self.ping_high.clone(), self.ping_high_color),
|
|
};
|
|
if let Some(icon) = icon {
|
|
ping_icon.set_texture(&icon);
|
|
}
|
|
ping_icon.set_modulate(color);
|
|
}
|
|
} else {
|
|
if let Some(name_label) = &mut self.name_label {
|
|
name_label.set_text("Name");
|
|
}
|
|
if let Some(kills_label) = &mut self.kills_label {
|
|
kills_label.set_text("Kills");
|
|
}
|
|
if let Some(deaths_label) = &mut self.deaths_label {
|
|
deaths_label.set_text("Deaths");
|
|
}
|
|
if let Some(ping_label) = &mut self.ping_label {
|
|
ping_label.set_text("Ping");
|
|
}
|
|
}
|
|
} else if let Some(playback) = &StandaloneReplayManager::singleton().bind().playback {
|
|
if let Some(player) = playback.bind().replay.players.get(&player_id) {
|
|
if let Some(name_label) = &mut self.name_label {
|
|
name_label.set_text(&player.name);
|
|
}
|
|
} else {
|
|
if let Some(name_label) = &mut self.name_label {
|
|
name_label.set_text("Name");
|
|
}
|
|
if let Some(kills_label) = &mut self.kills_label {
|
|
kills_label.set_text("Kills");
|
|
}
|
|
if let Some(deaths_label) = &mut self.deaths_label {
|
|
deaths_label.set_text("Deaths");
|
|
}
|
|
if let Some(ping_label) = &mut self.ping_label {
|
|
ping_label.set_text("Ping");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|