generated upgrades and bars

This commit is contained in:
Vinzenz Schroeter 2025-07-06 11:46:32 +02:00
commit 74d146242b
4 changed files with 155 additions and 169 deletions

View file

@ -1,4 +1,4 @@
use crate::unlocks::UnlockSystem;
use crate::unlocks::{Unlock, UnlockSystem};
use crate::{Currency, bar::Bar};
use servicepoint::{Bitmap, CharGrid, CharGridMutExt, TILE_SIZE, WindowMut};
use std::time::Duration;
@ -11,36 +11,50 @@ pub struct Game {
}
#[derive(Debug)]
pub struct State<const STEP_COUNT: usize = 11> {
pub struct State {
pub(crate) currency: Currency,
pub(crate) speed: f64,
pub(crate) productivity: f64,
pub(crate) bars: [Bar; STEP_COUNT],
pub(crate) bars: Vec<Bar>,
}
impl Game {
const BAR_NAMES: [&'static str; 11] = [
"Powering infrastructure",
"Dusting ServicePoint",
"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",
];
pub fn new() -> Self {
let bars = Self::BAR_NAMES
.iter()
.enumerate()
.map(|(index, name)| {
Bar::new(
2usize.pow(index as u32) as f64,
1.0 * (0.5f64.powi(index as i32)),
*name,
)
})
.collect();
let state = State {
bars,
currency: 1f64,
speed: 1f64,
productivity: 1f64,
};
Self {
total_currency: 0f64,
unlocks: UnlockSystem::new(),
state: State {
currency: 0f64,
speed: 1f64,
productivity: 1f64,
bars: [
Bar::new(1f64, 1.0, "Powering infrastructure"),
Bar::new(2f64, 0.5, "Dusting ServicePoint"),
Bar::new(4f64, 0.25, "Activating colorful lights"),
Bar::new(8f64, 0.125, "Dimming darkroom"),
Bar::new(16f64, 0.0625, "Refilling Matemat"),
Bar::new(32f64, 0.03125, "Pre-heating convectiomat"),
Bar::new(64f64, 0.015625, "Resetting chair heights"),
Bar::new(128f64, 0.0078125, "Untangling 'block chain'"),
Bar::new(256f64, 0.00390625, "Refilling sticker box"),
Bar::new(512f64, 0.001953125, "Setting room to public"),
Bar::new(1024f64, 0.000976562, "Welcoming creatures"),
],
},
unlocks: UnlockSystem::new(&state),
state,
}
}
@ -67,34 +81,33 @@ impl Game {
text_layer
.set_row_str(0, "Discordia Boot Procedure")
.unwrap();
let middle = text_layer.width() / 2;
text_layer
.window_mut(middle, 0, middle, 1)
.unwrap()
.set_row_str(0, &format!(" Hacks: {}", self.state.currency.floor()))
.unwrap();
self.draw_bars(text_layer, pixel_layer, 1);
for (index, bar) in self.state.bars.iter().enumerate() {
let row = 2 + index;
let mut bar_window = pixel_layer
.window_mut(0, row * TILE_SIZE, pixel_layer.width(), TILE_SIZE)
.unwrap();
let mut label_window = text_layer
.window_mut(0, row, text_layer.width(), 1)
.unwrap();
bar.draw(&mut label_window, &mut bar_window);
}
let mut row = text_layer.height() - 1;
self.draw_stats(text_layer, row);
row -= 1;
if let Some(next_upgrade) = self.unlocks.peek_next() {
text_layer
.window_mut(0, text_layer.height() - 2, text_layer.width(), 1)
.unwrap()
.set_row_str(0, &format!("Next unlock for {} Hacks", next_upgrade.cost))
.unwrap();
self.draw_next_upgrade(text_layer, row, next_upgrade);
row -= 1;
}
if let Some(prev_unlock) = self.unlocks.previous() {
self.draw_prev_unlock(text_layer, row, prev_unlock);
}
}
fn draw_next_upgrade(&self, text_layer: &mut WindowMut<char, CharGrid>, row: usize, next_upgrade: &Unlock) {
text_layer
.window_mut(0, text_layer.height() - 1, text_layer.width() / 2, 1)
.window_mut(0, row, text_layer.width(), 1)
.unwrap()
.set_row_str(0, &format!("Next unlock: {:.0}/{} Hacks", self.state.currency, next_upgrade.cost))
.unwrap();
}
fn draw_stats(&self, text_layer: &mut WindowMut<char, CharGrid>, row: usize) {
text_layer
.window_mut(0, row, text_layer.width() / 2, 1)
.unwrap()
.set_row_str(0, &format!("Hack Score: {:.2}", self.total_currency))
.unwrap();
@ -103,7 +116,7 @@ impl Game {
text_layer
.window_mut(
text_layer.width() / 2,
text_layer.height() - 1,
row,
text_layer.width() / 2,
1,
)
@ -112,4 +125,26 @@ impl Game {
.unwrap();
}
}
fn draw_bars(&self, text_layer: &mut WindowMut<char, CharGrid>, pixel_layer: &mut WindowMut<bool, Bitmap>, mut row: usize) {
for bar in self.state.bars.iter() {
let mut bar_window = pixel_layer
.window_mut(0, row * TILE_SIZE, pixel_layer.width(), TILE_SIZE)
.unwrap();
let mut label_window = text_layer
.window_mut(0, row, text_layer.width(), 1)
.unwrap();
bar.draw(&mut label_window, &mut bar_window);
row += 1;
}
}
fn draw_prev_unlock(&self, chars: &mut WindowMut<char, CharGrid>, row: usize, prev: &String) {
chars
.window_mut(0, row, chars.width(), 1)
.unwrap()
.set_row_str(0, &format!("Unlocked: {}", prev))
.unwrap();
}
}