Merge branch 'qutrin/level-up' into 'master'

Levels and XP

See merge request veloren/veloren!364
This commit is contained in:
Marcel 2019-07-27 13:16:59 +00:00
commit bacee7750f
7 changed files with 113 additions and 24 deletions

1
Cargo.lock generated
View File

@ -2769,6 +2769,7 @@ dependencies = [
name = "veloren-server"
version = "0.2.0"
dependencies = [
"chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",

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,54 @@ 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) {
self.current = current;
}
// TODO: Uncomment when needed
// pub fn set_maximum(&mut self, maximum: f64) {
// self.maximum = maximum;
// }
pub fn change_current_by(&mut self, current: f64) {
self.current = self.current + current;
}
pub fn change_maximum_by(&mut self, maximum: f64) {
self.maximum = self.maximum + maximum;
}
}
impl Level {
// TODO: Uncomment when needed
// 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 = 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 +118,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

@ -710,7 +710,7 @@ impl Server {
self.clients
.notify_registered(ServerMsg::EcsSync(self.state.ecs_mut().next_sync_package()));
// Sync deaths.
// Handle deaths.
let ecs = &self.state.ecs();
let clients = &mut self.clients;
let todo_kill = (&ecs.entities(), &ecs.read_storage::<comp::Dying>())
@ -737,6 +737,30 @@ impl Server {
clients.notify_registered(ServerMsg::chat(msg));
}
// Give EXP to the client
if let Some(_enemy) = ecs.read_storage::<comp::Body>().get(entity) {
if let comp::HealthSource::Attack { by } = dying.cause {
ecs.entity_from_uid(by.into()).and_then(|attacker| {
let mut stats = ecs.write_storage::<comp::Stats>();
let attacker_stats = stats.get_mut(attacker).unwrap();
// TODO: Discuss whether we should give EXP by Player Killing or not.
if attacker_stats.exp.get_current() >= attacker_stats.exp.get_maximum()
{
attacker_stats.exp.change_maximum_by(25.0);
attacker_stats.exp.set_current(0.0);
attacker_stats.level.change_by(1);
} else {
// TODO: Don't make this a single value and make it depend on
// slayed entity's level
attacker_stats.exp.change_current_by(1.0);
};
ecs.read_storage::<comp::Player>().get(attacker).cloned()
});
}
}
entity
})
.collect::<Vec<_>>();

View File

@ -1,4 +1,5 @@
use super::{img_ids::Imgs, Fonts, TEXT_COLOR, XP_COLOR};
use common::comp::Stats;
use conrod_core::{
color,
widget::{self, Button, Image, Rectangle, Text},
@ -69,16 +70,18 @@ widget_ids! {
pub struct CharacterWindow<'a> {
imgs: &'a Imgs,
fonts: &'a Fonts,
stats: &'a Stats,
#[conrod(common_builder)]
common: widget::CommonBuilder,
}
impl<'a> CharacterWindow<'a> {
pub fn new(imgs: &'a Imgs, fonts: &'a Fonts) -> Self {
pub fn new(imgs: &'a Imgs, fonts: &'a Fonts, stats: &'a Stats) -> Self {
Self {
imgs,
fonts,
stats,
common: widget::CommonBuilder::default(),
}
}
@ -104,8 +107,13 @@ impl<'a> Widget for CharacterWindow<'a> {
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { id, state, ui, .. } = args;
// TODO: Read from parameter/character struct.
let xp_percentage = 0.4;
let xp_percentage = self.stats.exp.get_current() / self.stats.exp.get_maximum();
let xp_treshold = format!(
"{}/{}",
self.stats.exp.get_current(),
self.stats.exp.get_maximum()
);
let level = (self.stats.level.get_level()).to_string();
// Frame
Image::new(self.imgs.window_3)
@ -330,8 +338,8 @@ impl<'a> Widget for CharacterWindow<'a> {
//.label_font_size(14)
//.set(state.charwindow_tab1, ui);
// TODO: Use an actual character level.
Text::new("1")
// Level
Text::new(&level)
.mid_top_with_margin_on(state.charwindow_rectangle, 10.0)
.font_id(self.fonts.opensans)
.font_size(30)
@ -355,8 +363,7 @@ impl<'a> Widget for CharacterWindow<'a> {
.set(state.charwindow_tab1_expbar, ui);
// Exp-Text
// TODO: Shows current Exp over the next threshold Exp.
Text::new("120/170")
Text::new(&xp_treshold)
.mid_top_with_margin_on(state.charwindow_tab1_expbar, 10.0)
.font_id(self.fonts.opensans)
.font_size(15)

View File

@ -690,7 +690,10 @@ impl Hud {
// Character Window
if let Windows::CharacterAnd(small) = self.show.open_windows {
match CharacterWindow::new(&self.imgs, &self.fonts)
let ecs = client.state().ecs();
let stats = ecs.read_storage::<comp::Stats>();
let player_stats = stats.get(client.entity()).unwrap();
match CharacterWindow::new(&self.imgs, &self.fonts, &player_stats)
.set(self.ids.character_window, ui_widgets)
{
Some(character_window::Event::Close) => {

View File

@ -71,12 +71,11 @@ 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;
// TODO: We need a max xp value
let xp_percentage = (self.stats.xp as f64 - start_level_xp) / next_level_xp;
let level = (self.stats.level.get_level()).to_string();
let next_level = (self.stats.level.get_level() + 1).to_string();
let exp_percentage = self.stats.exp.get_current() / 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;
@ -94,7 +93,7 @@ impl<'a> Widget for Skillbar<'a> {
.mid_bottom_of(ui.window)
.set(state.ids.xp_bar, ui);
Rectangle::fill_with([406.0 * (xp_percentage), 5.0], XP_COLOR) // "W=406*[Exp. %]"
Rectangle::fill_with([406.0 * (exp_percentage), 5.0], XP_COLOR) // "W=406*[Exp. %]"
.top_left_with_margins_on(state.ids.xp_bar, 5.0, 21.0)
.set(state.ids.xp_bar_progress, ui);
@ -167,16 +166,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)
.right_from(state.ids.xp_bar, -15.0)
.font_size(10)
.color(TEXT_COLOR)