use crate::border_panel::{INNER_BORDER, OUTER_BORDER, draw_border_panel}; use crate::row::Row; use crate::{Currency, unlocks::UnlockSystem}; use servicepoint::{Bitmap, CharGrid, TILE_SIZE, WindowMut}; use std::time::Duration; #[derive(Debug)] pub struct Game { total_currency: Currency, unlocks: UnlockSystem, state: State, } #[derive(Debug)] pub struct State { pub currency: Currency, pub speed: f64, pub productivity: f64, pub rows: Vec, } impl Game { const BAR_NAMES: [&'static str; 11] = [ "Powering infrastructure", "Activating colorful lights", "Dimming darkroom", "Refilling Matemat", "Pre-heating convectiomat", "Resetting chair heights", "Untangling 'block chain'", "Refilling sticker box", "Setting room to public", "Welcoming creatures", "Making Discordia proud", ]; pub fn new() -> Self { let bars = Self::BAR_NAMES .iter() .enumerate() .map(|(index, name)| { Row::new( 5usize.pow(index as u32) as f64, 1.0 * (0.5f64.powi(index as i32)), name, ) }) .collect(); let state = State { rows: bars, currency: 1f64, speed: 1f64, productivity: 1f64, }; Self { total_currency: 0f64, unlocks: UnlockSystem::new(&state), state, } } pub fn progress(&mut self, delta: Duration) { let adjusted_delta = delta.mul_f64(self.state.speed); let extra_currency = self.state.productivity * self .state .rows .iter_mut() .map(|bar| bar.progress(adjusted_delta)) .sum::(); self.state.currency += extra_currency; self.total_currency += extra_currency; self.unlocks.progress(&mut self.state); } pub fn draw( &self, mut text_layer: WindowMut, mut pixel_layer: WindowMut, ) { let (mut text_layer, mut pixel_layer) = draw_border_panel( &mut text_layer, &mut pixel_layer, " Discordia Boot Procedure ", OUTER_BORDER, ); let unlocks_height = 3 + 2; let (mut unlocks_text, bars_text) = text_layer.split_vertical_mut(unlocks_height).unwrap(); let (mut unlocks_pixel, bars_pixel) = pixel_layer .split_vertical_mut(unlocks_height * TILE_SIZE) .unwrap(); self.unlocks.draw(&mut unlocks_text, &mut unlocks_pixel); self.draw_bars(bars_text, bars_pixel); } fn draw_bars( &self, mut text_layer: WindowMut, mut pixel_layer: WindowMut, ) { let (mut text_layer, mut pixel_layer) = draw_border_panel( &mut text_layer, &mut pixel_layer, " Processes ", INNER_BORDER, ); for (index, row) in self.state.rows.iter().enumerate() { let mut bar_window = pixel_layer .window_mut(.., index * TILE_SIZE..index * TILE_SIZE + TILE_SIZE) .unwrap(); let mut label_window = text_layer.window_mut(.., index..index + 1).unwrap(); row.draw(&mut label_window, &mut bar_window); } } }