clean up ui, more upgrades

This commit is contained in:
Vinzenz Schroeter 2025-07-06 22:03:11 +02:00
commit 2a4f92c9b3
6 changed files with 132 additions and 175 deletions

View file

@ -1,11 +1,10 @@
use crate::border_panel::{INNER_BORDER, draw_border_panel};
use crate::{Currency, bar::Bar, game::State, row::Row};
use servicepoint::{CharGridMutExt, GridMut, TILE_SIZE, WindowMut};
use servicepoint::{Bitmap, CharGrid, CharGridMutExt, TILE_SIZE, WindowMut};
use std::{
collections::VecDeque,
fmt::{Debug, Formatter},
iter::Map,
ops::{Mul, Range},
ops::Mul,
};
#[derive(Debug)]
@ -15,6 +14,7 @@ pub struct UnlockSystem {
unlock_queue: VecDeque<Unlock>,
all_unlocks_bar: Bar,
next_unlock_bar: Bar,
total_unlock_count: usize,
}
pub struct Unlock {
@ -34,13 +34,16 @@ impl Debug for Unlock {
impl UnlockSystem {
pub fn new(start_state: &State) -> Self {
let mut upgrades = Self::gen_bar_upgrades(&start_state.rows)
.chain(Self::gen_speed_upgrades())
.chain(Self::gen_productivity_upgrades())
let mut upgrades = Self::gen_bar_enable_unlocks(&start_state.rows)
.chain(Self::gen_global_speed_unlocks())
.chain(Self::gen_global_productivity_unlocks())
.chain(Self::gen_bar_speed_unlocks(&start_state.rows))
.chain(Self::gen_bar_productivity_unlocks(&start_state.rows))
.collect::<Vec<_>>();
upgrades.sort_by(|l, r| l.cost.total_cmp(&r.cost));
Self {
total_unlock_count: upgrades.len(),
unlock_queue: upgrades.into(),
bought: 0,
last_unlock: None,
@ -64,52 +67,25 @@ impl UnlockSystem {
}
}
pub fn bought(&self) -> usize {
self.bought
}
pub fn previous(&self) -> Option<&String> {
self.last_unlock.as_ref()
}
pub fn draw<C: GridMut<char>, P: GridMut<bool>>(
&self,
chars: WindowMut<char, C>,
bitmap: WindowMut<bool, P>,
) {
let (mut chars, mut bitmap) = draw_border_panel(chars, bitmap, " Unlocks ", INNER_BORDER);
let mut free_row_bottom = chars.height() - 1;
pub fn draw(&self, chars: WindowMut<char, CharGrid>, bitmap: WindowMut<bool, Bitmap>) {
let title = format!(" Stage {}/{} ", self.bought, self.total_unlock_count);
let pixel_width = bitmap.width();
self.all_unlocks_bar.draw(
&mut WindowMut::new(
&mut bitmap,
0,
free_row_bottom * TILE_SIZE,
pixel_width,
TILE_SIZE,
)
.unwrap(),
);
free_row_bottom -= 1;
let (mut chars, mut bitmap) = draw_border_panel(chars, bitmap, &title, INNER_BORDER);
self.next_unlock_bar.draw(
&mut WindowMut::new(
&mut bitmap,
0,
free_row_bottom * TILE_SIZE,
pixel_width,
TILE_SIZE,
)
.unwrap(),
);
free_row_bottom -= 1;
let (all_unlocks_pixels, bitmap) = bitmap.split_vertical_mut(TILE_SIZE).unwrap();
self.all_unlocks_bar.draw(all_unlocks_pixels);
if let Some(prev_unlock) = self.previous() {
let char_width = chars.width();
WindowMut::new(&mut chars, 0, free_row_bottom, char_width, 1)
.unwrap()
.set_row_str(0, &format!("Unlocked: {}", prev_unlock))
let (next_unlocks_pixels, _) = bitmap.split_vertical_mut(TILE_SIZE).unwrap();
self.next_unlock_bar.draw(next_unlocks_pixels);
if let Some(next) = self.peek_next() {
let (_, mut next_unlock_chars) = chars.split_vertical_mut(2).unwrap();
next_unlock_chars
.set_row_str(0, &format!("Loading: {}", next.name))
.unwrap();
}
}
@ -124,56 +100,62 @@ impl UnlockSystem {
self.last_unlock = Some(next_upgrade.name);
self.bought += 1;
let total = self.bought + self.unlock_queue.len();
self.all_unlocks_bar.progress = self.bought as f64 / total as f64;
self.all_unlocks_bar.progress = self.bought as f64 / self.total_unlock_count as f64;
}
fn gen_bar_upgrades(bars: &Vec<Row>) -> impl Iterator<Item = Unlock> {
bars.iter().enumerate().flat_map(|(index, bar)| {
let initial_cost = 10usize.pow(index as u32);
[
Unlock {
name: format!("Start {}", bar.name()),
cost: initial_cost as f64,
apply: Box::new(move |game| {
game.rows[index].enabled = true;
}),
},
Unlock {
name: format!("{} productivity", bar.name()),
cost: initial_cost.mul(2) as f64,
apply: Box::new(move |game| {
game.rows[index].productivity *= 2.0;
}),
},
Unlock {
name: format!("{} speed", bar.name()),
cost: initial_cost.mul(20) as f64,
apply: Box::new(move |game| {
game.rows[index].speed *= 1.5;
}),
},
]
})
}
fn gen_speed_upgrades() -> impl Iterator<Item = Unlock> {
(1..10).map(|level| Unlock {
name: format!("The answer {}", level),
cost: (42usize * 10usize.pow(level)) as f64,
fn gen_bar_enable_unlocks(bars: &Vec<Row>) -> impl Iterator<Item = Unlock> {
bars.iter().enumerate().map(|(index, bar)| Unlock {
name: format!("Start {}", bar.name()),
cost: 10f64.powi(index as i32),
apply: Box::new(move |game| {
game.speed *= 1.1;
game.rows[index].enabled = true;
}),
})
}
fn gen_productivity_upgrades() -> Map<Range<u32>, fn(u32) -> Unlock> {
(1..10).map(|level| Unlock {
fn gen_global_speed_unlocks() -> impl Iterator<Item = Unlock> {
(1..32).map_while(|level| {
Some(Unlock {
name: format!("The answer {}", level),
cost: 42f64.mul(2f64.powi(level)),
apply: Box::new(move |game| {
game.speed *= 1.15;
}),
})
})
}
fn gen_global_productivity_unlocks() -> impl Iterator<Item = Unlock> {
(1..32).map(|level| Unlock {
name: format!("??? {}", level),
cost: (23usize * 10usize.pow(level)) as f64,
cost: 23f64.mul(2f64.powi(level)),
apply: Box::new(move |game| {
game.productivity *= 1.1;
game.productivity *= 1.15;
}),
})
}
fn gen_bar_productivity_unlocks(bars: &Vec<Row>) -> impl Iterator<Item = Unlock> {
bars.iter().enumerate().flat_map(|(index, bar)| {
(1..=20).map(move |level| Unlock {
name: format!("{} productivity {level}", bar.name()),
cost: 10f64.powi(index as i32).mul(3f64.powi(level)),
apply: Box::new(move |game| {
game.rows[index].productivity *= 1.5;
}),
})
})
}
fn gen_bar_speed_unlocks(bars: &Vec<Row>) -> impl Iterator<Item = Unlock> {
bars.iter().enumerate().flat_map(|(index, bar)| {
(1..=15).map(move |level| Unlock {
name: format!("{} speed {level}", bar.name()),
cost: 10f64.powi(index as i32).mul(5f64.powi(level)),
apply: Box::new(move |game| {
game.rows[index].speed *= 1.5;
}),
})
})
}
}