Add level up network event

This commit is contained in:
scott-c 2020-06-01 17:21:33 +08:00
parent 730399b5f4
commit 7eb1b2c263
5 changed files with 33 additions and 18 deletions

View File

@ -762,22 +762,19 @@ impl Client {
},
ServerMsg::PlayerListUpdate(PlayerListUpdate::LevelChange(uid, next_level)) => {
if let Some(player_info) = self.player_list.get_mut(&uid) {
if let Some(character) = &player_info.character {
// TODO: assign next value
// &character.level = next_level;
} else {
warn!(
"Received msg to update character level info to {} for uid \
{}, but this player's character is None.",
next_level, uid
);
}
} else {
warn!(
"Received msg to update character level info to {} for uid {}, \
but they were not in the list.",
next_level, uid
);
player_info.character = match &player_info.character {
Some(character) => Some(common::msg::CharacterInfo {
name: character.name.to_string(),
level: next_level,
}),
None => {
warn!(
"Received msg to update character level info to {} for \
uid {}, but this player's character is None.",
next_level, uid
);
},
};
}
},
ServerMsg::PlayerListUpdate(PlayerListUpdate::Remove(uid)) => {

View File

@ -90,6 +90,7 @@ pub enum ServerEvent {
Mount(EcsEntity, EcsEntity),
Unmount(EcsEntity),
Possess(Uid, Uid),
LevelUp(EcsEntity, u32),
SelectCharacter {
entity: EcsEntity,
character_id: i32,

View File

@ -66,6 +66,7 @@ impl<'a> System<'a> for Sys {
stat.exp.change_by(-(stat.exp.maximum() as i64));
stat.level.change_by(1);
stat.exp.update_maximum(stat.level.level());
server_event_emitter.emit(ServerEvent::LevelUp(entity, stat.level.level()));
}
stat.update_max_hp();

View File

@ -2,7 +2,7 @@ use crate::{client::Client, Server, SpawnPoint, StateExt};
use common::{
assets,
comp::{self, object, Body, HealthChange, HealthSource, Item, Player, Stats},
msg::ServerMsg,
msg::{PlayerListUpdate, ServerMsg},
state::BlockChange,
sync::{Uid, WorldSyncExt},
sys::combat::{BLOCK_ANGLE, BLOCK_EFFICIENCY},
@ -295,3 +295,17 @@ pub fn handle_explosion(server: &Server, pos: Vec3<f32>, power: f32, owner: Opti
.cast();
}
}
pub fn handle_level_up(server: &mut Server, entity: EcsEntity, new_level: u32) {
let uids = server.state.ecs().read_storage::<Uid>();
let uid = uids
.get(entity)
.expect("Failed to fetch uid component for entity.")
.0;
server
.state
.notify_registered_clients(ServerMsg::PlayerListUpdate(PlayerListUpdate::LevelChange(
uid, new_level,
)));
}

View File

@ -4,7 +4,8 @@ use entity_creation::{
handle_create_character, handle_create_npc, handle_create_waypoint, handle_shoot,
};
use entity_manipulation::{
handle_damage, handle_destroy, handle_explosion, handle_land_on_ground, handle_respawn,
handle_damage, handle_destroy, handle_explosion, handle_land_on_ground, handle_level_up,
handle_respawn,
};
use interaction::{handle_lantern, handle_mount, handle_possess, handle_unmount};
use inventory_manip::handle_inventory;
@ -75,6 +76,7 @@ impl Server {
body,
main,
} => handle_create_character(self, entity, character_id, body, main),
ServerEvent::LevelUp(entity, new_level) => handle_level_up(self, entity, new_level),
ServerEvent::ExitIngame { entity } => handle_exit_ingame(self, entity),
ServerEvent::CreateNpc {
pos,