implement upgrade system

This commit is contained in:
Vinzenz Schroeter 2025-07-05 23:23:07 +02:00
commit 3468fff882
7 changed files with 558 additions and 100 deletions

View file

@ -1,92 +1,121 @@
use crate::{
bar::Bar,
Currency,
Progressable
};
use servicepoint::{Bitmap, CharGrid, CharGridMutExt, WindowMut, TILE_SIZE};
use crate::upgrades::{Upgrade, get_upgrades};
use crate::{Currency, Progressable, bar::Bar};
use log::log;
use servicepoint::{Bitmap, CharGrid, CharGridMutExt, TILE_SIZE, WindowMut};
use std::collections::VecDeque;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct Game<const STEP_COUNT:usize=11> {
#[derive(Debug)]
pub struct Game<const STEP_COUNT: usize = 11> {
pub(crate) currency: Currency,
pub(crate) bars: [Bar; STEP_COUNT],
pub(crate) names: [&'static str; STEP_COUNT],
unlocks: usize,
unlock_queue: VecDeque<Upgrade>,
total_currency: Currency,
pub(crate) global_speed: f64,
pub(crate) global_productivity: f64,
}
impl Game {
pub fn new() -> Self {
Self {
currency: 0f64,
total_currency: 0f64,
global_speed: 1f64,
global_productivity: 1f64,
unlocks: 0,
unlock_queue: get_upgrades(),
bars: [
Bar::new(1f64,1.0),
Bar::new(2f64, 0.5),
Bar::new(4f64, 0.25),
Bar::new(8f64,0.125),
Bar::new(16f64, 0.0625),
Bar::new(32f64, 0.03125),
Bar::new(64f64, 0.015625),
Bar::new(128f64, 0.0078125),
Bar::new(256f64, 0.00390625),
Bar::new(512f64, 0.001953125),
Bar::new(1024f64,0.000976562),
],
names: [
"Powering infrastructure",
"Dusting ServicePoint",
"Activating colorful lights",
"Dimming darkroom",
"Refilling Matemat",
"Pre-heating convectiomat",
"Resetting chair heights",
"Cleaning 'block chain'",
"Refilling sticker box",
"Setting room to public",
"Welcoming creatures",
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"),
],
}
}
}
impl Progressable for Game {
fn progress(&self, delta: Duration) -> (Self, Currency) {
let mut currency = self.currency;
let bars = self.bars.clone().map(|bar| {
let (bar, curr) = bar.progress(delta);
currency += curr;
bar
});
pub(crate) fn progress(&mut self, delta: Duration) {
let adjusted_delta = delta.mul_f64(self.global_speed);
(
Self {
currency,
bars,
names: self.names,
},
0f64,
)
self.currency += self.global_productivity
* self
.bars
.iter_mut()
.map(|bar| bar.progress(adjusted_delta))
.sum::<Currency>();
if let Some(next_upgrade) = self.unlock_queue.front() {
if next_upgrade.cost <= self.currency {
log::info!("Applying upgrade {:?}", next_upgrade);
let next_upgrade = self.unlock_queue.pop_front().unwrap();
self.currency -= next_upgrade.cost;
(next_upgrade.apply)(self);
self.unlocks += 1;
}
}
}
}
impl Game {
pub fn draw(&self, text_layer: &mut WindowMut<char, CharGrid>, pixel_layer: &mut WindowMut<bool, Bitmap>) {
text_layer.set_row_str(0, "Discordia Boot Procedure").unwrap();
pub(crate) fn draw(
&self,
text_layer: &mut WindowMut<char, CharGrid>,
pixel_layer: &mut WindowMut<bool, Bitmap>,
) {
text_layer
.set_row_str(0, "Discordia Boot Procedure")
.unwrap();
let middle = text_layer.width() / 2;
text_layer.window_mut( middle, 0, middle, 1)
text_layer
.window_mut(middle, 0, middle, 1)
.unwrap()
.set_row_str(0, &format!(" Cycles: {}", self.currency.floor()))
.set_row_str(0, &format!(" Completions: {}", self.currency.floor()))
.unwrap();
for (index, bar) in self.bars.iter().enumerate() {
let row = 1 + index;
let mut bar_window = pixel_layer.window_mut(0, row * TILE_SIZE, pixel_layer.width() / 2, TILE_SIZE).unwrap();
let mut label_window = text_layer.window_mut(middle, row, middle, 1).unwrap();
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);
}
if !bar.is_enabled() {
continue;
}
if let Some(next_upgrade) = self.unlock_queue.front() {
text_layer
.window_mut(0, text_layer.height() - 2, text_layer.width(), 1)
.unwrap()
.set_row_str(
0,
&format!("Next unlock: {} {}", next_upgrade.cost, next_upgrade.name),
)
.unwrap();
}
bar.draw(&mut bar_window);
label_window.set_row_str(0, self.names[index]).unwrap();
text_layer
.window_mut(0, text_layer.height() - 1, text_layer.width() / 2, 1)
.unwrap()
.set_row_str(0, &format!("Score: {}", self.total_currency))
.unwrap();
if self.unlocks > 0 {
text_layer
.window_mut(
text_layer.width() / 2,
text_layer.height() - 1,
text_layer.width() / 2,
1,
)
.unwrap()
.set_row_str(0, &format!(" Unlocks: {}", self.unlocks))
.unwrap();
}
}
}