Improve grid appearance

This commit is contained in:
Pierre HUBERT 2022-10-15 16:27:22 +02:00
parent 4341bdc682
commit b832ef82ed
2 changed files with 39 additions and 6 deletions

View File

@ -220,7 +220,7 @@ impl GameScreen {
}
fn player_map(&self, map: &CurrentGameMapStatus, opponent_map: bool) -> GameMapWidget {
let mut map_widget = GameMapWidget::new(&self.game.rules);
let mut map_widget = GameMapWidget::new(&self.game.rules).set_default_empty_char(' ');
// Current shoot position
if opponent_map {
@ -241,8 +241,14 @@ impl GameScreen {
}
// Sunk boats
for b in &map.sunk_boats {
for c in b.all_coordinates() {
map_widget =
map_widget.set_char(c, b.len.to_string().chars().next().unwrap_or('9'));
}
}
let sunk_boats = ColoredCells {
color: Color::Gray,
color: Color::LightRed,
cells: map
.sunk_boats
.iter()
@ -251,18 +257,29 @@ impl GameScreen {
};
// Touched boats
for b in &map.successful_strikes {
map_widget = map_widget.set_char_no_overwrite(*b, 'T');
}
let touched_areas = ColoredCells {
color: Color::Red,
cells: map.successful_strikes.clone(),
};
// Failed strikes
for b in &map.failed_strikes {
map_widget = map_widget.set_char_no_overwrite(*b, '.');
}
let failed_strikes = ColoredCells {
color: Color::DarkGray,
color: Color::Black,
cells: map.failed_strikes.clone(),
};
// Boats
for b in &map.boats.0 {
for c in b.all_coordinates() {
map_widget = map_widget.set_char_no_overwrite(c, 'B');
}
}
let boats = ColoredCells {
color: Color::Blue,
cells: map

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Display;
use tui::buffer::Buffer;
@ -21,6 +22,7 @@ pub struct GameMapWidget<'a> {
title: Option<String>,
legend: Option<String>,
yield_coordinates: Option<Box<dyn 'a + FnMut(Coordinates, Rect)>>,
chars: HashMap<Coordinates, char>,
}
impl<'a> GameMapWidget<'a> {
@ -32,6 +34,7 @@ impl<'a> GameMapWidget<'a> {
title: None,
legend: None,
yield_coordinates: None,
chars: Default::default(),
}
}
@ -63,6 +66,16 @@ impl<'a> GameMapWidget<'a> {
self
}
pub fn set_char(mut self, coordinates: Coordinates, c: char) -> Self {
self.chars.insert(coordinates, c);
self
}
pub fn set_char_no_overwrite(mut self, coordinates: Coordinates, c: char) -> Self {
self.chars.entry(coordinates).or_insert(c);
self
}
pub fn grid_size(&self) -> (u16, u16) {
let w = self.rules.map_width as u16 * 2 + 1;
let h = self.rules.map_height as u16 * 2 + 1;
@ -157,9 +170,12 @@ impl<'a> Widget for GameMapWidget<'a> {
}
if x < self.rules.map_width && y < self.rules.map_height {
let cell = buf
.get_mut(o_x + 1, o_y + 1)
.set_char(self.default_empty_character);
let cell = buf.get_mut(o_x + 1, o_y + 1).set_char(
*self
.chars
.get(&coordinates)
.unwrap_or(&self.default_empty_character),
);
if let Some(c) = color {
cell.set_bg(c.color);