Changed exp type from f64 to u32 and altered exp calculations and function signatures to adhere to the new types

Signed-off-by: Yashas Lokesh <yashas.lokesh@gmail.com>
This commit is contained in:
yashaslokesh 2019-08-27 22:33:14 +00:00 committed by Joshua Barretto
parent 1bd3f0ab28
commit be71aea20b
5 changed files with 25 additions and 24 deletions

View File

@ -35,8 +35,8 @@ pub struct Energy {
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Exp {
current: f64,
maximum: f64,
current: u32,
maximum: u32,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
@ -97,29 +97,29 @@ impl Energy {
}
impl Exp {
pub fn current(&self) -> f64 {
pub fn current(&self) -> u32 {
self.current
}
pub fn maximum(&self) -> f64 {
pub fn maximum(&self) -> u32 {
self.maximum
}
pub fn set_current(&mut self, current: f64) {
pub fn set_current(&mut self, current: u32) {
self.current = current;
}
// TODO: Uncomment when needed
// pub fn set_maximum(&mut self, maximum: f64) {
// pub fn set_maximum(&mut self, maximum: u32) {
// self.maximum = maximum;
// }
pub fn change_by(&mut self, current: f64) {
self.current = self.current + current;
pub fn change_by(&mut self, current: i64) {
self.current = ((self.current as i64) + current) as u32;
}
pub fn change_maximum_by(&mut self, maximum: f64) {
self.maximum = self.maximum + maximum;
pub fn change_maximum_by(&mut self, maximum: i64) {
self.maximum = ((self.maximum as i64) + maximum) as u32;
}
}
@ -171,8 +171,8 @@ impl Stats {
},
level: Level { amount: 1 },
exp: Exp {
current: 0.0,
maximum: 50.0,
current: 0,
maximum: 50,
},
energy: Energy {
current: 200,

View File

@ -40,8 +40,8 @@ impl<'a> System<'a> for Sys {
}
if stat.exp.current() >= stat.exp.maximum() {
stat.exp.change_by(-stat.exp.maximum());
stat.exp.change_maximum_by(25.0);
stat.exp.change_by(-(stat.exp.maximum() as i64));
stat.exp.change_maximum_by(25);
stat.level.change_by(1);
stat.health.set_maximum(stat.health.maximum() + 10);
stat.health

View File

@ -315,8 +315,9 @@ impl Server {
if let Some(attacker_stats) = stats.get_mut(attacker) {
// TODO: Discuss whether we should give EXP by Player Killing or not.
attacker_stats.exp.change_by(
entity_stats.health.maximum() as f64 / 10.0
+ entity_stats.level.level() as f64 * 10.0,
(entity_stats.health.maximum() as f64 / 10.0
+ entity_stats.level.level() as f64 * 10.0)
as i64,
);
}
});

View File

@ -113,8 +113,8 @@ impl<'a> Widget for CharacterWindow<'a> {
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { id, state, ui, .. } = args;
let xp_percentage = self.stats.exp.current() / self.stats.exp.maximum();
let xp_treshold = format!("{}/{}", self.stats.exp.current(), self.stats.exp.maximum());
let exp_percentage = (self.stats.exp.current() as f64) / (self.stats.exp.maximum() as f64);
let exp_treshold = format!("{}/{}", self.stats.exp.current(), self.stats.exp.maximum());
let level = (self.stats.level.level()).to_string();
// Frame
@ -354,7 +354,7 @@ impl<'a> Widget for CharacterWindow<'a> {
.set(state.charwindow_exp_rectangle, ui);
// Exp-Bar Progress
Rectangle::fill_with([170.0 * (xp_percentage), 6.0], XP_COLOR) // 0.8 = Experience percentage
Rectangle::fill_with([170.0 * (exp_percentage), 6.0], XP_COLOR) // 0.8 = Experience percentage
.mid_left_with_margin_on(state.charwindow_tab1_expbar, 1.0)
.set(state.charwindow_exp_progress_rectangle, ui);
@ -365,7 +365,7 @@ impl<'a> Widget for CharacterWindow<'a> {
.set(state.charwindow_tab1_expbar, ui);
// Exp-Text
Text::new(&xp_treshold)
Text::new(&exp_treshold)
.mid_top_with_margin_on(state.charwindow_tab1_expbar, 10.0)
.font_id(self.fonts.opensans)
.font_size(15)

View File

@ -116,7 +116,7 @@ impl<'a> Skillbar<'a> {
pub struct State {
ids: Ids,
last_xp_value: f64,
last_xp_value: u32,
last_level: u32,
last_update_xp: Instant,
last_update_level: Instant,
@ -131,7 +131,7 @@ impl<'a> Widget for Skillbar<'a> {
State {
ids: Ids::new(id_gen),
last_xp_value: 0.0,
last_xp_value: 0,
last_level: 1,
last_update_xp: Instant::now(),
last_update_level: Instant::now(),
@ -148,7 +148,7 @@ impl<'a> Widget for Skillbar<'a> {
let level = (self.stats.level.level()).to_string();
let next_level = (self.stats.level.level() + 1).to_string();
let exp_percentage = self.stats.exp.current() / self.stats.exp.maximum();
let exp_percentage = (self.stats.exp.current() as f64) / (self.stats.exp.maximum() as f64);
let hp_percentage =
self.stats.health.current() as f64 / self.stats.health.maximum() as f64 * 100.0;
@ -281,7 +281,7 @@ impl<'a> Widget for Skillbar<'a> {
}
let seconds_xp = state.last_update_xp.elapsed().as_secs_f32();
let fade_xp = if current_xp == 0.0 {
let fade_xp = if current_xp == 0 {
0.0
} else if seconds_xp < FADE_IN_XP {
seconds_xp / FADE_IN_XP