Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/net/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,39 @@ pub fn handle_game_packet(
saturation: p.saturation,
});
}
ClientboundGamePacket::SetExperience(p) => {
let _ = event_tx.try_send(NetworkEvent::PlayerExperience {
progress: p.experience_progress,
level: p.experience_level as i32,
});
}
ClientboundGamePacket::UpdateAttributes(p) => {
use azalea_core::attribute_modifier_operation::AttributeModifierOperation;
use azalea_registry::builtin::Attribute;
for snapshot in &p.values {
if snapshot.attribute != Attribute::Armor {
continue;
}
let base = snapshot.base;
let mut add = 0.0f64;
let mut mul_base = 0.0f64;
let mut mul_total = 0.0f64;
for m in &snapshot.modifiers {
match m.operation {
AttributeModifierOperation::AddValue => add += m.amount,
AttributeModifierOperation::AddMultipliedBase => mul_base += m.amount,
AttributeModifierOperation::AddMultipliedTotal => mul_total += m.amount,
}
}
let value = (base + add) * (1.0 + mul_base) * (1.0 + mul_total);
let armor = value.clamp(0.0, 30.0).round() as u32;
let _ = event_tx.try_send(NetworkEvent::EntityArmorUpdate {
entity_id: p.entity_id.0,
armor,
});
break;
}
}
ClientboundGamePacket::SystemChat(p) if !p.overlay => {
send_chat(event_tx, p.content.to_string());
}
Expand Down
8 changes: 8 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ pub enum NetworkEvent {
food: u32,
saturation: f32,
},
PlayerExperience {
progress: f32,
level: i32,
},
EntityArmorUpdate {
entity_id: i32,
armor: u32,
},
InventoryContent {
items: Vec<ItemStack>,
},
Expand Down
6 changes: 6 additions & 0 deletions src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct LocalPlayer {
pub on_ground: bool,
pub health: f32,
pub food: u32,
pub armor: u32,
pub saturation: f32,
pub inventory: Inventory,
pub sprinting: bool,
Expand All @@ -46,6 +47,8 @@ pub struct LocalPlayer {
pub game_mode: u8,
pub score: i32,
pub entity_id: i32,
pub experience_level: i32,
pub experience_progress: f32,
}

impl LocalPlayer {
Expand All @@ -58,6 +61,7 @@ impl LocalPlayer {
on_ground: false,
health: 20.0,
food: 20,
armor: 0,
saturation: 5.0,
inventory: Inventory::new(),
sprinting: false,
Expand All @@ -71,6 +75,8 @@ impl LocalPlayer {
game_mode: 0,
score: 0,
entity_id: -1,
experience_level: 0,
experience_progress: 0.0,
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/renderer/pipelines/menu_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,11 @@ pub enum SpriteId {
AirFull,
AirBursting,
AirEmpty,
ArmorEmpty,
ArmorHalf,
ArmorFull,
ExperienceBarBackground,
ExperienceBarProgress,
InventoryBackground,
EmptyHelmet,
EmptyChestplate,
Expand Down Expand Up @@ -1436,6 +1441,31 @@ fn build_sprite_atlas(
"minecraft/textures/gui/sprites/hud/air_empty.png",
0.0,
),
(
SpriteId::ArmorEmpty,
"minecraft/textures/gui/sprites/hud/armor_empty.png",
0.0,
),
(
SpriteId::ArmorHalf,
"minecraft/textures/gui/sprites/hud/armor_half.png",
0.0,
),
(
SpriteId::ArmorFull,
"minecraft/textures/gui/sprites/hud/armor_full.png",
0.0,
),
(
SpriteId::ExperienceBarBackground,
"minecraft/textures/gui/sprites/hud/experience_bar_background.png",
0.0,
),
(
SpriteId::ExperienceBarProgress,
"minecraft/textures/gui/sprites/hud/experience_bar_progress.png",
0.0,
),
(
SpriteId::EmptyHelmet,
"minecraft/textures/gui/sprites/container/slot/helmet.png",
Expand Down
128 changes: 105 additions & 23 deletions src/ui/hud.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use azalea_core::position::BlockPos;
use azalea_inventory::ItemStack;

use super::common::{WHITE, push_item_count};
use super::common::{FONT_SIZE, WHITE, push_item_count};
use crate::player::inventory::item_resource_name;
use crate::renderer::pipelines::menu_overlay::{MenuElement, SpriteId};

Expand Down Expand Up @@ -38,6 +38,8 @@ const SELECTION_H: f32 = 24.0;
const SLOT_STRIDE: f32 = 20.0;
const ICON_SIZE: f32 = 9.0;
const ICON_STRIDE: f32 = 8.0;
const XP_BAR_W: f32 = 182.0;
const XP_BAR_H: f32 = 5.0;

pub fn max_gui_scale(screen_w: f32, screen_h: f32) -> u32 {
let mut scale = 1;
Expand All @@ -64,8 +66,12 @@ pub fn build_hud(
selected_slot: u8,
health: f32,
food: u32,
armor: u32,
air_supply: i32,
eyes_in_water: bool,
experience_level: i32,
experience_progress: f32,
game_mode: u8,
hotbar: &[ItemStack],
first_person: bool,
debug: Option<&DebugInfo<'_>>,
Expand Down Expand Up @@ -129,10 +135,11 @@ pub fn build_hud(
}
}

let status_bar_y = hotbar_y - (XP_BAR_H + 1.0 + 2.0) * gs;
build_status_bar(
elements,
hotbar_x,
hotbar_y - 2.0 * gs,
status_bar_y,
health,
false,
SpriteId::HeartContainer,
Expand All @@ -143,7 +150,7 @@ pub fn build_hud(
build_status_bar(
elements,
hotbar_x + hotbar_w,
hotbar_y - 2.0 * gs,
status_bar_y,
food as f32,
true,
SpriteId::FoodEmpty,
Expand All @@ -152,6 +159,92 @@ pub fn build_hud(
gs,
);

if armor > 0 {
let armor_y = status_bar_y - (ICON_SIZE + 1.0) * gs;
build_status_bar(
elements,
hotbar_x,
armor_y,
armor as f32,
false,
SpriteId::ArmorEmpty,
SpriteId::ArmorFull,
SpriteId::ArmorHalf,
gs,
);
}

if game_mode == 0 || game_mode == 2 {
let xp_w = XP_BAR_W * gs;
let xp_h = XP_BAR_H * gs;
let xp_x = cx - xp_w / 2.0;
let xp_y = hotbar_y - xp_h - 2.0 * gs;

elements.push(MenuElement::Image {
x: xp_x,
y: xp_y,
w: xp_w,
h: xp_h,
sprite: SpriteId::ExperienceBarBackground,
tint: WHITE,
});

let fill_px = (experience_progress.clamp(0.0, 1.0) * XP_BAR_W).ceil() as i32;
if fill_px > 0 {
let fill_w = fill_px as f32 * gs;
elements.push(MenuElement::ScissorPush {
x: xp_x,
y: xp_y,
w: fill_w,
h: xp_h,
});
elements.push(MenuElement::Image {
x: xp_x,
y: xp_y,
w: xp_w,
h: xp_h,
sprite: SpriteId::ExperienceBarProgress,
tint: WHITE,
});
elements.push(MenuElement::ScissorPop);
}

if experience_level > 0 {
let text = experience_level.to_string();
let fs = FONT_SIZE * gs;
let ty = xp_y - 6.0 * gs;
let shadow = [0.0, 0.0, 0.0, 1.0];
let main = [0.5, 1.0, 0.125, 1.0];
for (dx, dy) in [
(1.0, 0.0),
(-1.0, 0.0),
(0.0, 1.0),
(0.0, -1.0),
(1.0, 1.0),
(1.0, -1.0),
(-1.0, 1.0),
(-1.0, -1.0),
] {
elements.push(MenuElement::Text {
x: cx + dx * gs,
y: ty + dy * gs,
text: text.clone(),
scale: fs,
color: shadow,
centered: true,
});
}
elements.push(MenuElement::Text {
x: cx,
y: ty,
text,
scale: fs,
color: main,
centered: true,
});
}
}

let max_air = crate::player::MAX_AIR_SUPPLY;
if eyes_in_water || air_supply < max_air {
let air = air_supply.clamp(0, max_air);
Expand All @@ -163,7 +256,7 @@ pub fn build_hud(
let empty_bubbles = 10 - bubble_count(empty_delay);
let is_popping = full_bubbles != popping_pos;

let bubble_y = hotbar_y - 2.0 * gs - ICON_SIZE * gs - (ICON_SIZE + 1.0) * gs;
let bubble_y = status_bar_y - ICON_SIZE * gs - 1.0 * gs;
let stride = ICON_STRIDE * gs;
let icon_size = ICON_SIZE * gs;
for b in 1..=10i32 {
Expand Down Expand Up @@ -233,32 +326,21 @@ fn build_status_bar(
};
let iy = y - icon_size;

let sprite = if i < full_icons {
full
} else if i == full_icons && has_half {
half
} else {
bg
};
elements.push(MenuElement::Image {
x,
y: iy,
w: icon_size,
h: icon_size,
sprite: bg,
sprite,
tint: WHITE,
});

let icon = if i < full_icons {
Some(full)
} else if i == full_icons && has_half {
Some(half)
} else {
None
};
if let Some(sprite) = icon {
elements.push(MenuElement::Image {
x,
y: iy,
w: icon_size,
h: icon_size,
sprite,
tint: WHITE,
});
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,15 @@ impl App {
}
}
}
NetworkEvent::PlayerExperience { progress, level } => {
self.player.experience_progress = progress;
self.player.experience_level = level;
}
NetworkEvent::EntityArmorUpdate { entity_id, armor } => {
if entity_id == self.player.entity_id {
self.player.armor = armor;
}
}
NetworkEvent::InventoryContent { items } => {
self.player.inventory.set_contents(items);
}
Expand Down Expand Up @@ -1579,8 +1588,12 @@ impl ApplicationHandler for App {
self.input.selected_slot(),
self.player.health,
self.player.food,
self.player.armor,
self.player.air_supply,
self.player.eyes_in_water,
self.player.experience_level,
self.player.experience_progress,
self.player.game_mode,
self.player.inventory.hotbar_slots(),
renderer.is_first_person(),
debug.as_ref(),
Expand Down
Loading