split game struct

This commit is contained in:
Vinzenz Schroeter 2025-07-06 00:17:52 +02:00
commit e07fac4211
5 changed files with 218 additions and 188 deletions

View file

@ -1,65 +1,62 @@
use crate::upgrades::{Upgrade, get_upgrades};
use crate::{Currency, Progressable, bar::Bar};
use log::log;
use crate::unlocks::UnlockSystem;
use crate::{Currency, bar::Bar};
use servicepoint::{Bitmap, CharGrid, CharGridMutExt, TILE_SIZE, WindowMut};
use std::collections::VecDeque;
use std::time::Duration;
#[derive(Debug)]
pub struct Game<const STEP_COUNT: usize = 11> {
pub(crate) currency: Currency,
pub(crate) bars: [Bar; STEP_COUNT],
unlocks: usize,
unlock_queue: VecDeque<Upgrade>,
pub struct Game {
total_currency: Currency,
pub(crate) global_speed: f64,
pub(crate) global_productivity: f64,
unlocks: UnlockSystem,
state: State,
}
#[derive(Debug)]
pub struct State<const STEP_COUNT: usize = 11> {
pub(crate) currency: Currency,
pub(crate) speed: f64,
pub(crate) productivity: f64,
pub(crate) bars: [Bar; STEP_COUNT],
}
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, "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 {
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"),
],
},
}
}
pub(crate) fn progress(&mut self, delta: Duration) {
let adjusted_delta = delta.mul_f64(self.global_speed);
let adjusted_delta = delta.mul_f64(self.state.speed);
self.currency += self.global_productivity
let extra_currency = self.state.productivity
* self
.state
.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;
}
}
self.state.currency += extra_currency;
self.total_currency += extra_currency;
self.unlocks.check_next(&mut self.state);
}
pub(crate) fn draw(
@ -74,10 +71,10 @@ impl Game {
text_layer
.window_mut(middle, 0, middle, 1)
.unwrap()
.set_row_str(0, &format!(" Completions: {}", self.currency.floor()))
.set_row_str(0, &format!(" Hacks: {}", self.state.currency.floor()))
.unwrap();
for (index, bar) in self.bars.iter().enumerate() {
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)
@ -88,24 +85,21 @@ impl Game {
bar.draw(&mut label_window, &mut bar_window);
}
if let Some(next_upgrade) = self.unlock_queue.front() {
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: {} {}", next_upgrade.cost, next_upgrade.name),
)
.set_row_str(0, &format!("Next unlock for {} Hacks", next_upgrade.cost))
.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))
.set_row_str(0, &format!("Hack Score: {:.2}", self.total_currency))
.unwrap();
if self.unlocks > 0 {
if self.unlocks.bought() > 0 {
text_layer
.window_mut(
text_layer.width() / 2,
@ -114,7 +108,7 @@ impl Game {
1,
)
.unwrap()
.set_row_str(0, &format!(" Unlocks: {}", self.unlocks))
.set_row_str(0, &format!(" Unlocks: {}", self.unlocks.bought()))
.unwrap();
}
}