implement upgrade system

This commit is contained in:
Vinzenz Schroeter 2025-07-05 23:23:07 +02:00
commit 3468fff882

View file

@ -1,49 +1,60 @@
use crate::{Currency, Progressable};
use servicepoint::{GridMut};
use servicepoint::{CharGridMutExt, GridMut, WindowMut};
use std::time::Duration;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct Bar {
name: &'static str,
pub(crate) enabled: bool,
progress: f64,
speed: f64,
factor: f64,
pub(crate) speed: f64,
pub(crate) productivity: f64,
}
impl Bar {
pub(crate) fn new(factor: f64, speed: f64) -> Self {
pub(crate) fn new(productivity: f64, speed: f64, name: &'static str) -> Self {
Self {
factor,
name,
enabled: false,
productivity,
progress: 0f64,
speed,
}
}
pub(crate) fn add_speed(&self, speed: f64) -> Self {
Self {
speed: self.speed + speed,
..*self
}
}
pub(crate) fn is_enabled(&self) -> bool {
self.speed > 0.0
}
}
impl Progressable for Bar {
fn progress(&self, delta: Duration) -> (Self, Currency) {
let extra_progress = delta.as_secs_f64() * self.speed;
impl Bar {
pub(crate) fn progress(&mut self, delta: Duration) -> Currency {
if !self.enabled {
return 0.0;
}
let extra_progress = delta.mul_f64(self.speed).as_secs_f64();
let progress = self.progress + extra_progress;
let completions = progress.floor();
let progress = progress - completions;
let currency = completions * self.factor;
self.progress = progress - completions;
(Self { progress, ..*self }, currency)
completions * self.productivity
}
}
impl Bar {
pub fn draw<P: GridMut<bool>>(&self, bitmap: &mut P) {
pub fn draw<C: GridMut<char>, P: GridMut<bool>>(&self, chars: &mut C, bitmap: &mut P) {
if !self.enabled {
return;
}
let mut bitmap = WindowMut::new(bitmap, 0, 0, bitmap.width() / 2, bitmap.height()).unwrap();
let mut chars = WindowMut::new(
chars,
chars.width() / 2 + 1,
0,
chars.width() / 2 - 1,
chars.height(),
)
.unwrap();
chars.set_row_str(0, self.name).unwrap();
bitmap.fill(false);
let margin = 1;