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);
#[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<Vec2>,
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::<HAND_CARD_COUNT>()
.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) {

View file

@ -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!");
}
}