diff --git a/src/app/app.rs b/src/app/app.rs index 6c8d217..8b714b5 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -25,10 +25,13 @@ pub struct App { impl App { pub fn new(args: Cli) -> Self { + let mut states: GameStates = GameStates::new(&args); + states.skirmish.init_board(); + Self { exit: false, view: args.view, - states: GameStates::new(args), + states, } } diff --git a/src/app/state.rs b/src/app/state.rs index b9edb8c..66c871f 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -15,7 +15,7 @@ pub struct GameStates { } impl GameStates { - pub fn new(args: Cli) -> Self { + pub fn new(args: &Cli) -> Self { Self { main_menu: MainMenuState { id: 0, @@ -27,6 +27,9 @@ impl GameStates { name: "Skirmish", vertical_offset: Offset::new(), horizontal_offset: Offset::new(), + map_width: args.map_width as usize, + map_height: args.map_height as usize, + board_cells: Vec::new(), }, perk_decks: PerkDecksState { id: 2, @@ -39,16 +42,14 @@ impl GameStates { selected_skill: 0, }, settings: SettingsState { - username: args.username, - game_mode: args.game_mode, - map_width: args.map_width, - map_height: args.map_height, - perk_deck: args.perk_deck, - starting_wood: args.starting_wood, - starting_iron: args.starting_iron, - supply_limit: args.supply_limit, - xp_modifier: args.xp_modifier, - skill_points_limit: args.skill_points_limit, + username: args.username.clone(), + game_mode: args.game_mode.clone(), + perk_deck: args.perk_deck.clone(), + starting_wood: args.starting_wood.clone(), + starting_iron: args.starting_iron.clone(), + supply_limit: args.supply_limit.clone(), + xp_modifier: args.xp_modifier.clone(), + skill_points_limit: args.skill_points_limit.clone(), }, } } diff --git a/src/app/states/settings.rs b/src/app/states/settings.rs index cea07de..6f31b5f 100644 --- a/src/app/states/settings.rs +++ b/src/app/states/settings.rs @@ -4,8 +4,6 @@ use crate::app::states::{GameMode, PerkDecks}; pub struct SettingsState { pub username: String, pub game_mode: GameMode, - pub map_width: u8, - pub map_height: u8, pub perk_deck: PerkDecks, pub starting_wood: u16, pub starting_iron: u16, diff --git a/src/app/states/skirmish.rs b/src/app/states/skirmish.rs index 05f9df9..d1efe75 100644 --- a/src/app/states/skirmish.rs +++ b/src/app/states/skirmish.rs @@ -1,3 +1,4 @@ +use crate::app::widgets::CellWidget; use clap::ValueEnum; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -16,6 +17,10 @@ impl Offset { } pub fn set_max(&mut self, max: usize) { + if self.max != 0 { + return; + } + self.max = max; } @@ -28,12 +33,29 @@ impl Offset { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct SkirmishState { pub id: usize, pub name: &'static str, + pub map_width: usize, + pub map_height: usize, pub vertical_offset: Offset, pub horizontal_offset: Offset, + pub board_cells: Vec, +} + +impl SkirmishState { + pub fn init_board(&mut self) { + if !self.board_cells.is_empty() { + return; + } + + for row in 0..self.map_height { + for col in 0..self.map_width { + self.board_cells.push(CellWidget::new(row, col)); + } + } + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)] diff --git a/src/app/widgets/board.rs b/src/app/widgets/board.rs index fc25a75..180568e 100644 --- a/src/app/widgets/board.rs +++ b/src/app/widgets/board.rs @@ -6,31 +6,33 @@ use ratatui::{ }; use std::rc::Rc; -pub struct BoardWidget { +pub struct BoardWidget<'a> { + map_width: usize, cell_width: u16, cell_height: u16, cols: u16, rows: u16, h_offset: usize, v_offset: usize, + cells: &'a Vec, } -impl BoardWidget { - pub fn new(app: &mut App, area_width: u16, area_height: u16) -> Self { - const CELL_HIGHT: u16 = 8; // 4 - const CELL_WIDTH: u16 = 14; // 7 +impl<'a> BoardWidget<'a> { + pub fn new(app: &'a mut App, area_width: u16, area_height: u16) -> Self { + const CELL_HIGHT: u16 = 5; // 4 + const CELL_WIDTH: u16 = 9; // 7 let rows: u16 = area_height / CELL_HIGHT; let cols: u16 = area_width / CELL_WIDTH; - let v_max_offset: usize = if app.states.settings.map_height as u16 > rows { - app.states.settings.map_height as u16 - rows + let v_max_offset: usize = if app.states.skirmish.map_height as u16 > rows { + app.states.skirmish.map_height as u16 - rows } else { 0 } as usize; - let h_max_offset: usize = if app.states.settings.map_width as u16 > cols { - app.states.settings.map_width as u16 - cols + let h_max_offset: usize = if app.states.skirmish.map_width as u16 > cols { + app.states.skirmish.map_width as u16 - cols } else { 0 } as usize; @@ -39,17 +41,19 @@ impl BoardWidget { app.states.skirmish.vertical_offset.set_max(v_max_offset); Self { + map_width: app.states.skirmish.map_width as usize, cell_width: CELL_WIDTH, cell_height: CELL_HIGHT, cols, rows, h_offset: app.states.skirmish.horizontal_offset.get_value(), v_offset: app.states.skirmish.vertical_offset.get_value(), + cells: &app.states.skirmish.board_cells, } } } -impl Widget for BoardWidget { +impl Widget for BoardWidget<'_> { fn render(self, area: Rect, buf: &mut Buffer) { let horizontal: Rc<[Rect]> = Layout::horizontal(vec![ Constraint::Length(self.cell_width); @@ -65,10 +69,12 @@ impl Widget for BoardWidget { .split(*col_area); for (row_idx, cell_area) in vertical.iter().enumerate() { - let cell: CellWidget = - CellWidget::new(row_idx + self.v_offset, col_idx + self.h_offset); - - cell.render(*cell_area, buf); + if let Some(cell) = self + .cells + .get((row_idx + self.v_offset) * self.map_width + (col_idx + self.h_offset)) + { + cell.render(*cell_area, buf); + } } } } diff --git a/src/app/widgets/cell.rs b/src/app/widgets/cell.rs index c2d8822..c8ac0ad 100644 --- a/src/app/widgets/cell.rs +++ b/src/app/widgets/cell.rs @@ -13,6 +13,7 @@ use ratatui::{ // Base, // } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct CellWidget { pub row: usize, pub col: usize, diff --git a/src/cli.rs b/src/cli.rs index 37c2b69..91b50ee 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -37,7 +37,7 @@ pub struct Cli { long, help = "Map width", value_name = "Positive integer [20; 100]", - default_value = "27" + default_value = "50" )] pub map_width: u8, @@ -45,7 +45,7 @@ pub struct Cli { long, help = "Map height", value_name = "Positive integer [11; 50]", - default_value = "11" + default_value = "25" )] pub map_height: u8,