From 3fa480082695a354ddafacb1593a7f58a23fd4d5 Mon Sep 17 00:00:00 2001 From: Sofia Date: Wed, 22 Jul 2026 20:29:14 +0300 Subject: [PATCH] Add simple score view --- godot/scenes/player/local_player.tscn | 11 ++++++ rust/src/game_manager.rs | 5 ++- rust/src/ui/mod.rs | 1 + rust/src/ui/simple_score_view.rs | 55 +++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 rust/src/ui/simple_score_view.rs diff --git a/godot/scenes/player/local_player.tscn b/godot/scenes/player/local_player.tscn index 5708b29..bc7fd8e 100644 --- a/godot/scenes/player/local_player.tscn +++ b/godot/scenes/player/local_player.tscn @@ -57,3 +57,14 @@ offset_bottom = 11.5 grow_horizontal = 2 grow_vertical = 2 horizontal_alignment = 1 + +[node name="score_view" type="SimpleScoreView" parent="." unique_id=1128041371] +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -20.0 +offset_top = 18.0 +offset_right = 20.0 +offset_bottom = 58.0 +grow_horizontal = 2 +alignment = 1 diff --git a/rust/src/game_manager.rs b/rust/src/game_manager.rs index 05f21a6..af0f7e9 100644 --- a/rust/src/game_manager.rs +++ b/rust/src/game_manager.rs @@ -252,6 +252,8 @@ impl Game { pub fn on_map_changed(id: u8); #[signal] pub fn options_changed(); + #[signal] + pub fn on_goal(); pub fn singleton() -> Option> { GameManager::singleton() @@ -522,7 +524,8 @@ impl Game { self.goals .insert(*team, self.goals.get(&team).copied().unwrap_or(0) + 1); } - self.run_deferred(move |_| { + self.run_deferred(move |s| { + s.signals().on_goal().emit(); if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer { if let PeerKind::Server(peer, _) = peer { peer.broadcast_reliable(Package::Goal(teams)); diff --git a/rust/src/ui/mod.rs b/rust/src/ui/mod.rs index d65354e..8b00937 100644 --- a/rust/src/ui/mod.rs +++ b/rust/src/ui/mod.rs @@ -2,3 +2,4 @@ pub mod cli; pub mod lobby; pub mod net_stats; pub mod player_listing; +pub mod simple_score_view; diff --git a/rust/src/ui/simple_score_view.rs b/rust/src/ui/simple_score_view.rs new file mode 100644 index 0000000..da4aae3 --- /dev/null +++ b/rust/src/ui/simple_score_view.rs @@ -0,0 +1,55 @@ +use std::collections::HashMap; + +use godot::{ + classes::{HBoxContainer, IHBoxContainer, Label}, + prelude::*, +}; + +use crate::game_manager::Game; + +#[derive(GodotClass)] +#[class(base=HBoxContainer, init)] +pub struct SimpleScoreView { + team_goals: HashMap>, + base: Base, +} + +#[godot_api] +impl IHBoxContainer for SimpleScoreView { + fn ready(&mut self) { + self.run_deferred(|s| { + if let Some(game) = Game::singleton() { + for (id, team) in game.bind().get_teams().iter_shared().enumerate() { + let mut goals_label = Label::new_alloc(); + goals_label.set_modulate(team.bind().color); + goals_label.set_text("0"); + if !s.team_goals.is_empty() { + let mut dash = Label::new_alloc(); + dash.set_text(" - "); + s.base_mut().add_child(&dash); + } + s.base_mut().add_child(&goals_label); + s.team_goals.insert(id as u8, goals_label); + } + + game.signals() + .on_goal() + .connect_other(s, |s| s.update_goals()); + } + }); + } +} + +impl SimpleScoreView { + pub fn update_goals(&mut self) { + if let Some(game) = Game::singleton() { + for (id, _) in game.bind().get_teams().iter_shared().enumerate() { + if let Some(goals) = game.bind().goals.get(&(id as u8)) + && let Some(label) = self.team_goals.get_mut(&(id as u8)) + { + label.set_text(&goals.to_string()); + } + } + } + } +}