skullball/rust/src/ui/replay_finished_screen.rs

91 lines
2.8 KiB
Rust

use godot::{
classes::{Button, HBoxContainer, IPanel, Label, Panel},
prelude::*,
};
use crate::{
game::GameManager, replay::StandaloneReplayManager, ui::player_listings::PlayerListings,
};
#[derive(GodotClass)]
#[class(base=Panel, init)]
pub struct ReplayFinishedScreen {
#[export]
title_container: Option<Gd<HBoxContainer>>,
#[export]
back_button: Option<Gd<Button>>,
#[export]
player_listings: Option<Gd<PlayerListings>>,
base: Base<Panel>,
}
#[godot_api]
impl IPanel for ReplayFinishedScreen {
fn ready(&mut self) {
self.produce_title();
if let Some(listings) = &mut self.player_listings {
listings.bind_mut().update_player_listings();
}
if let Some(back) = &self.back_button {
back.signals().pressed().connect_other(self, |_| {
StandaloneReplayManager::singleton().bind_mut().clear();
GameManager::singleton().bind().go_to_main_menu();
});
}
if let Some(focus) = &mut self.base_mut().find_next_valid_focus() {
focus.grab_focus();
}
}
}
#[godot_api]
impl ReplayFinishedScreen {
fn produce_title(&mut self) {
if let Some(playback) = &StandaloneReplayManager::singleton().bind().playback {
let playback = playback.bind();
let max_goals = playback
.replay
.goals
.iter()
.map(|(_, v)| v)
.max()
.copied()
.unwrap_or(0);
let winning_team_ids = playback
.replay
.goals
.iter()
.filter(|(_, goals)| **goals == max_goals)
.map(|(team, _)| *team)
.collect::<Vec<_>>();
let winning_teams = playback
.replay
.teams
.iter()
.enumerate()
.filter(|(id, _)| winning_team_ids.contains(&((*id) as u8)))
.map(|(_, team)| team)
.collect::<Vec<_>>();
if let Some(container) = &mut self.title_container {
for (idx, team) in winning_teams.iter().enumerate() {
let mut label = Label::new_alloc();
label.set_text(&team.name);
label.set_modulate(team.color.clone().into());
if idx > 0 {
let mut and_label = Label::new_alloc();
and_label.set_text(" and ");
container.add_child(&and_label);
}
container.add_child(&label);
}
let mut end = Label::new_alloc();
end.set_text(" victory!");
container.add_child(&end);
}
}
}
}