Fix text color changing mid-frame

This commit is contained in:
Tobias Berger 2024-04-11 17:35:22 +02:00
parent e64742a159
commit 1c7568e60e
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
2 changed files with 29 additions and 23 deletions

View file

@ -170,6 +170,7 @@ fn player_panel(model: &Model, draw: &Draw, bounds: Rect, player: Player) {
}; };
let castle_bounds = castle_bounds.top_part(0.7); let castle_bounds = castle_bounds.top_part(0.7);
#[allow(clippy::cast_precision_loss)]
let castle_rect = Rect::from_x_y_w_h( let castle_rect = Rect::from_x_y_w_h(
castle_bounds.x(), castle_bounds.x(),
castle_bounds.y(), castle_bounds.y(),
@ -224,7 +225,7 @@ fn player_panel(model: &Model, draw: &Draw, bounds: Rect, player: Player) {
.unwrap(); .unwrap();
let character_width = character_size.width(); let character_width = character_size.width();
let character_height = character_size.height(); let character_height = character_size.height();
#[allow(clippy::cast_precision_loss)]
let character_aspect_ratio = character_width as f32 / character_height as f32; let character_aspect_ratio = character_width as f32 / character_height as f32;
let font_size = f32::min( let font_size = f32::min(
@ -259,7 +260,7 @@ fn hand(
bounds: Rect, bounds: Rect,
mouse_position: Option<Vec2>, mouse_position: Option<Vec2>,
window: &Window, window: &Window,
can_click: &mut bool, just_clicked: &mut bool,
) { ) {
#[allow(clippy::cast_precision_loss)] #[allow(clippy::cast_precision_loss)]
let hand_rect = Rect::from_x_y_w_h( let hand_rect = Rect::from_x_y_w_h(
@ -275,6 +276,9 @@ fn hand(
.current_player .current_player
.read() .read()
.expect("current player poisoned"); .expect("current player poisoned");
let mut card_clicked = false;
let mut damage = 0;
for (idx, &rect) in hand_rect for (idx, &rect) in hand_rect
.split_horizontal::<HAND_CARD_COUNT>() .split_horizontal::<HAND_CARD_COUNT>()
.iter() .iter()
@ -338,18 +342,17 @@ fn hand(
if matches!(mouse_position, Some(mouse_position) if rect.contains(mouse_position)) { if matches!(mouse_position, Some(mouse_position) if rect.contains(mouse_position)) {
window.set_cursor_icon(CursorIcon::Hand); window.set_cursor_icon(CursorIcon::Hand);
if *can_click { if *just_clicked {
*can_click = false; *just_clicked = false;
let damage = if matches!(card.effect.0, card::Stat::Attack) { if matches!(card.effect.0, card::Stat::Attack) {
card.effect.1 damage = card.effect.1;
} else { } else {
model model
.stats_of(current_player) .stats_of(current_player)
.write() .write()
.expect("player stats poisoned") .expect("player stats poisoned")
.apply(card.effect); .apply(card.effect);
0
}; };
model model
@ -357,22 +360,25 @@ fn hand(
.write() .write()
.expect("hand poisoned")[idx] = random(); .expect("hand poisoned")[idx] = random();
let mut current_player = model card_clicked = true;
.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();
} }
} }
} }
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) { 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); window.set_cursor_icon(CursorIcon::Default);
frame.clear(BLACK); 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; let mouse = &app.mouse;
@ -407,7 +413,7 @@ fn view(app: &App, model: &Model, frame: Frame) {
hand_bounds, hand_bounds,
mouse_position, mouse_position,
&window, &window,
&mut can_click, &mut just_clicked,
); );
if cfg!(debug_assertions) { if cfg!(debug_assertions) {

View file

@ -47,7 +47,7 @@ impl Stats {
self.fence = self.fence.saturating_sub(damage); self.fence = self.fence.saturating_sub(damage);
self.castle = self.castle.saturating_sub(castle_damage); self.castle = self.castle.saturating_sub(castle_damage);
if self.castle == 0 { if self.castle >= 100 {
println!("Game over!"); println!("Game over!");
} }
} }