Lazy init game states and extract board module

App now stores CLI arguments and an optional GameStates, initializing
the
states lazily on the first window resize. Skirmish board logic is moved
to
a new BoardState module with dedicated helpers (cells_area_helper and
updated cell size handling). Views and keybindings are updated to use
the
optional state accessors.
This commit is contained in:
2026-04-02 00:45:57 +02:00
parent c4e28255b6
commit 923af91aeb
15 changed files with 228 additions and 178 deletions
+15 -5
View File
@@ -22,22 +22,29 @@ pub struct App {
pub exit: bool,
pub view: View,
pub window_area: Rect,
pub states: GameStates,
pub args: Cli,
pub states: Option<GameStates>,
}
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,
window_area: Rect::default(),
states,
args: args,
states: None,
}
}
pub fn states(&self) -> Option<&GameStates> {
self.states.as_ref()
}
pub fn states_mut(&mut self) -> Option<&mut GameStates> {
self.states.as_mut()
}
pub fn run(&mut self, terminal: &mut DefaultTerminal, rx: Receiver<Event>) -> Result<()> {
while !self.exit {
terminal.draw(|frame: &mut Frame<'_>| self.draw(frame))?;
@@ -65,6 +72,9 @@ impl App {
|| window_area.height != self.window_area.height
{
self.window_area = window_area;
if self.states.is_none() {
self.states = Some(GameStates::new(&self.args, &self.window_area))
}
}
frame.render_widget(self, window_area);