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

168
src/unlocks.rs Normal file
View file

@ -0,0 +1,168 @@
use crate::Currency;
use crate::game::{Game, State};
use std::collections::VecDeque;
use std::fmt::{Debug, Formatter};
#[derive(Debug)]
pub(crate) struct UnlockSystem {
bought: usize,
unlock_queue: VecDeque<Unlock>,
}
pub(crate) struct Unlock {
pub(crate) name: &'static str,
pub(crate) cost: Currency,
pub(crate) apply: fn(&mut State),
}
impl Debug for Unlock {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"Upgrade {{ name: {:?}, cost: {:?} }}",
self.name, self.cost
))
}
}
impl UnlockSystem {
pub(crate) fn new() -> Self {
Self {
bought: 0,
unlock_queue: vec![
Unlock {
name: "Start Powering infrastructure",
cost: 0f64,
apply: |game| {
game.bars[0].enabled = true;
},
},
Unlock {
name: "More power",
cost: 10f64,
apply: |game| {
game.bars[0].productivity *= 2.0;
},
},
Unlock {
name: "",
cost: 23f64,
apply: |game| {
game.productivity *= 1.1;
},
},
Unlock {
name: "The answer",
cost: 42f64,
apply: |game| {
game.speed *= 1.1;
},
},
Unlock {
name: "Start Dusting ServicePoint",
cost: 64f64,
apply: |game| {
game.bars[1].enabled = true;
},
},
Unlock {
name: "Dust filters",
cost: 100f64,
apply: |game| {
game.bars[1].productivity *= 2.0;
},
},
Unlock {
name: "HASS automation",
cost: 128f64,
apply: |game| {
game.bars[0].speed *= 1.5;
},
},
Unlock {
name: "Start Activating colorful lights",
cost: 256f64,
apply: |game| {
game.bars[2].enabled = true;
},
},
Unlock {
name: "Start Dimming darkroom",
cost: 1024f64,
apply: |game| {
game.bars[3].enabled = true;
},
},
Unlock {
name: "Start Refilling Matemat",
cost: 4096f64,
apply: |game| {
game.bars[4].enabled = true;
},
},
Unlock {
name: "Start Pre-heating convectiomat",
cost: 16384f64,
apply: |game| {
game.bars[5].enabled = true;
},
},
Unlock {
name: "Start Resetting chair heights",
cost: 65536f64,
apply: |game| {
game.bars[6].enabled = true;
},
},
Unlock {
name: "Start Untangling 'block chain'",
cost: 262144f64,
apply: |game| {
game.bars[7].enabled = true;
},
},
Unlock {
name: "Start Refilling sticker box",
cost: 1048576f64,
apply: |game| {
game.bars[8].enabled = true;
},
},
Unlock {
name: "Start Setting room to public",
cost: 4194304f64,
apply: |game| {
game.bars[9].enabled = true;
},
},
Unlock {
name: "Start Welcoming creatures",
cost: 16777216f64,
apply: |game| {
game.bars[10].enabled = true;
},
},
]
.into(),
}
}
pub(crate) fn peek_next(&self) -> Option<&Unlock> {
self.unlock_queue.front()
}
pub(crate) fn check_next(&mut self, state: &mut State) {
if let Some(next_upgrade) = self.peek_next() {
if next_upgrade.cost <= state.currency {
log::info!("Applying upgrade {:?}", next_upgrade);
let next_upgrade = self.unlock_queue.pop_front().unwrap();
state.currency -= next_upgrade.cost;
(next_upgrade.apply)(state);
self.bought += 1;
}
}
}
pub(crate) fn bought(&self) -> usize {
self.bought
}
}