From 1c7568e60e46ba14ba0c97a5076b1eed62a36d8a Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Thu, 11 Apr 2024 17:35:22 +0200 Subject: [PATCH] Fix text color changing mid-frame --- src/main.rs | 50 ++++++++++++++++++++++++++++---------------------- src/player.rs | 2 +- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4b80158..8f1b163 100644 --- a/src/main.rs +++ b/src/main.rs @@ -170,6 +170,7 @@ fn player_panel(model: &Model, draw: &Draw, bounds: Rect, player: Player) { }; let castle_bounds = castle_bounds.top_part(0.7); + #[allow(clippy::cast_precision_loss)] let castle_rect = Rect::from_x_y_w_h( castle_bounds.x(), castle_bounds.y(), @@ -224,7 +225,7 @@ fn player_panel(model: &Model, draw: &Draw, bounds: Rect, player: Player) { .unwrap(); let character_width = character_size.width(); let character_height = character_size.height(); - + #[allow(clippy::cast_precision_loss)] let character_aspect_ratio = character_width as f32 / character_height as f32; let font_size = f32::min( @@ -259,7 +260,7 @@ fn hand( bounds: Rect, mouse_position: Option, window: &Window, - can_click: &mut bool, + just_clicked: &mut bool, ) { #[allow(clippy::cast_precision_loss)] let hand_rect = Rect::from_x_y_w_h( @@ -275,6 +276,9 @@ fn hand( .current_player .read() .expect("current player poisoned"); + + let mut card_clicked = false; + let mut damage = 0; for (idx, &rect) in hand_rect .split_horizontal::() .iter() @@ -338,18 +342,17 @@ fn hand( if matches!(mouse_position, Some(mouse_position) if rect.contains(mouse_position)) { window.set_cursor_icon(CursorIcon::Hand); - if *can_click { - *can_click = false; + if *just_clicked { + *just_clicked = false; - let damage = if matches!(card.effect.0, card::Stat::Attack) { - card.effect.1 + if matches!(card.effect.0, card::Stat::Attack) { + damage = card.effect.1; } else { model .stats_of(current_player) .write() .expect("player stats poisoned") .apply(card.effect); - 0 }; model @@ -357,22 +360,25 @@ fn hand( .write() .expect("hand poisoned")[idx] = random(); - let mut current_player = model - .current_player - .write() - .expect("current player poisoned"); - *current_player = (*current_player).next(); - - let mut other_player_stats = model - .stats_of(*current_player) - .write() - .expect("player stats poisoned"); - drop(current_player); - other_player_stats.damage(damage); - other_player_stats.apply_gains(); + card_clicked = true; } } } + + if card_clicked { + let mut current_player = model + .current_player + .write() + .expect("current player poisoned"); + *current_player = current_player.next(); + let mut player_stats = model + .stats_of(*current_player) + .write() + .expect("player stats poisoned"); + drop(current_player); + player_stats.damage(damage); + player_stats.apply_gains(); + } } fn view(app: &App, model: &Model, frame: Frame) { @@ -380,7 +386,7 @@ fn view(app: &App, model: &Model, frame: Frame) { window.set_cursor_icon(CursorIcon::Default); frame.clear(BLACK); - let mut can_click = *model.just_clicked.read().expect("click handler poisoned"); + let mut just_clicked = *model.just_clicked.read().expect("click handler poisoned"); let mouse = &app.mouse; @@ -407,7 +413,7 @@ fn view(app: &App, model: &Model, frame: Frame) { hand_bounds, mouse_position, &window, - &mut can_click, + &mut just_clicked, ); if cfg!(debug_assertions) { diff --git a/src/player.rs b/src/player.rs index 4f9d06d..cbe2ef4 100644 --- a/src/player.rs +++ b/src/player.rs @@ -47,7 +47,7 @@ impl Stats { self.fence = self.fence.saturating_sub(damage); self.castle = self.castle.saturating_sub(castle_damage); - if self.castle == 0 { + if self.castle >= 100 { println!("Game over!"); } }