chunkedge_boss_bar/
components.rs

1use std::borrow::Cow;
2
3use bevy_ecs::prelude::{Bundle, Component};
4use chunkedge_entity::EntityLayerId;
5use chunkedge_server::protocol::packets::play::boss_event_s2c::{
6    BossBarAction, BossBarColor, BossBarDivision, BossBarFlags,
7};
8use chunkedge_server::protocol::IntoTextComponent;
9use chunkedge_server::{Text, UniqueId};
10use derive_more::{Deref, DerefMut};
11
12/// The bundle of components that make up a boss bar.
13#[derive(Bundle, Default)]
14pub struct BossBarBundle {
15    pub id: UniqueId,
16    pub title: BossBarTitle,
17    pub health: BossBarHealth,
18    pub style: BossBarStyle,
19    pub flags: BossBarFlags,
20    pub layer: EntityLayerId,
21}
22
23/// The title of a boss bar.
24#[derive(Component, Clone, Default, Deref, DerefMut)]
25pub struct BossBarTitle(pub Text);
26
27impl ToPacketAction for BossBarTitle {
28    fn to_packet_action(&self) -> BossBarAction<'_> {
29        BossBarAction::UpdateTitle(Cow::Borrowed(&self.0).into_cow_text_component())
30    }
31}
32
33/// The health of a boss bar.
34#[derive(Component, Default, Deref, DerefMut)]
35pub struct BossBarHealth(pub f32);
36
37impl ToPacketAction for BossBarHealth {
38    fn to_packet_action(&self) -> BossBarAction<'_> {
39        BossBarAction::UpdateHealth(self.0)
40    }
41}
42
43/// The style of a boss bar. This includes the color and division of the boss
44/// bar.
45#[derive(Component, Default)]
46pub struct BossBarStyle {
47    pub color: BossBarColor,
48    pub division: BossBarDivision,
49}
50
51impl ToPacketAction for BossBarStyle {
52    fn to_packet_action(&self) -> BossBarAction<'_> {
53        BossBarAction::UpdateStyle(self.color, self.division)
54    }
55}
56
57impl ToPacketAction for BossBarFlags {
58    fn to_packet_action(&self) -> BossBarAction<'_> {
59        BossBarAction::UpdateFlags(*self)
60    }
61}
62
63/// Trait for converting a component to a boss bar action.
64pub(crate) trait ToPacketAction {
65    fn to_packet_action(&self) -> BossBarAction<'_>;
66}