Add Level and Exp component

This commit is contained in:
Piotr Korgól 2019-07-26 16:23:33 +02:00
parent 3c435c809c
commit b1aace0110
3 changed files with 70 additions and 13 deletions

View File

@ -22,5 +22,5 @@ pub use inputs::{
pub use inventory::{item, Inventory};
pub use phys::{ForceUpdate, Ori, Pos, Vel};
pub use player::Player;
pub use stats::{Dying, HealthSource, Stats};
pub use stats::{Dying, Exp, HealthSource, Level, Stats};
pub use visual::LightEmitter;

View File

@ -17,6 +17,17 @@ pub struct Health {
pub last_change: Option<(i32, f64, HealthSource)>,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Exp {
current: f64,
maximum: f64,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Level {
amount: u32,
}
impl Health {
pub fn get_current(&self) -> u32 {
self.current
@ -35,11 +46,56 @@ impl Health {
}
}
impl Exp {
pub fn get_current(&self) -> f64 {
self.current
}
pub fn get_maximum(&self) -> f64 {
self.maximum
}
pub fn set_current(&mut self, current: f64) {
if self.current < self.maximum {
self.current = current;
}
}
pub fn set_maximum(&mut self, maximum: f64) {
self.maximum = maximum;
}
pub fn change_current_by(&mut self, current: f64) {
if self.current < self.maximum {
self.current + current;
}
}
pub fn change_maximum_by(&mut self, maximum: f64) {
self.maximum + maximum;
}
}
impl Level {
pub fn set_level(&mut self, level: u32) {
self.amount = level;
}
pub fn get_level(&self) -> u32 {
self.amount
}
pub fn change_by(&mut self, level: u32) {
self.amount + level;
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Stats {
pub name: String,
pub health: Health,
pub xp: u32,
pub level: Level,
pub exp: Exp,
pub is_dead: bool,
}
@ -64,7 +120,11 @@ impl Stats {
maximum: 100,
last_change: None,
},
xp: 0,
level: Level { amount: 1 },
exp: Exp {
current: 0.0,
maximum: 50.0,
},
is_dead: false,
}
}

View File

@ -71,12 +71,12 @@ impl<'a> Widget for Skillbar<'a> {
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { state, ui, .. } = args;
// TODO: remove this
let level = (self.stats.xp as f64).log(4.0).trunc() as u32 + 1;
let start_level_xp = ((level - 1) as f64).powi(4);
let next_level_xp = (level as f64).powi(4) - start_level_xp;
let level = (self.stats.level.get_level()).to_string();
let next_level = self.stats.level.get_level() + 1;
// TODO: We need a max xp value
let xp_percentage = (self.stats.xp as f64 - start_level_xp) / next_level_xp;
let xp_percentage = self.stats.exp.get_current();
let max_xp = self.stats.exp.get_maximum();
let hp_percentage =
self.stats.health.get_current() as f64 / self.stats.health.get_maximum() as f64;
let mana_percentage = 1.0;
@ -167,16 +167,13 @@ impl<'a> Widget for Skillbar<'a> {
// Level Display
// TODO: don't construct a new string here
// TODO: Insert actual Level here.
Text::new(&level.to_string())
Text::new(&level)
.left_from(state.ids.xp_bar, -15.0)
.font_size(10)
.color(TEXT_COLOR)
.set(state.ids.level_text, ui);
// TODO: Insert next Level here.
Text::new(&(level + 1).to_string())
Text::new(&next_level.to_string())
.right_from(state.ids.xp_bar, -15.0)
.font_size(10)
.color(TEXT_COLOR)