aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.rs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-17 15:21:42 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-03-17 15:21:42 +0100
commite228208de62f8abf1744f389271168d996a10b25 (patch)
tree8dfd55a08608c2c9cd4286ee31dbf9bf38a7c222 /src/client.rs
parent7958bb7a82cf172766b2805a3c38aaa2e2a949fc (diff)
feat: add hotbar
Diffstat (limited to 'src/client.rs')
-rw-r--r--src/client.rs200
1 files changed, 198 insertions, 2 deletions
diff --git a/src/client.rs b/src/client.rs
index 3860366..1805cae 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -2,6 +2,7 @@ use crate::client::network::*;
use crate::protocol::*;
use crate::shared::ability::*;
use crate::shared::champion::*;
+use crate::shared::cooldown::*;
use crate::shared::health::*;
use crate::shared::imperative::*;
use crate::shared::projectile::*;
@@ -74,6 +75,13 @@ impl Plugin for ClientPlugin {
),
)
.add_systems(
+ Update,
+ (
+ (hotbar_hotbar_display, hotbar_hotbar_highlight),
+ hotbar_cooldown,
+ ),
+ )
+ .add_systems(
FixedPreUpdate,
buffer_input.in_set(InputSystemSet::BufferInputs),
)
@@ -81,8 +89,101 @@ impl Plugin for ClientPlugin {
}
}
+#[derive(Component, PartialEq, Eq, Debug)]
+pub enum Hotbar {
+ A,
+ Q,
+ W,
+ E,
+ R,
+ F,
+ G,
+}
+
+#[derive(Component, PartialEq, Eq, Debug)]
+pub enum HotbarCooldown {
+ A,
+ Q,
+ W,
+ E,
+ R,
+ F,
+ G,
+}
+
fn setup(mut client: ClientMut, mut commands: Commands, champion: Res<MyChampion>) {
commands.spawn(Camera2dBundle::default());
+ commands
+ .spawn(NodeBundle {
+ style: Style {
+ position_type: PositionType::Absolute,
+ bottom: Val::Px(4.),
+ width: Val::Percent(100.),
+ flex_direction: FlexDirection::Row,
+ justify_content: JustifyContent::Center,
+ column_gap: Val::Px(4.),
+ ..Default::default()
+ },
+ ..Default::default()
+ })
+ .with_children(|builder| {
+ let mut hotkey = |label, hotbar, hotbar_cooldown| {
+ builder
+ .spawn(NodeBundle {
+ style: Style {
+ align_items: AlignItems::Center,
+ justify_content: JustifyContent::Center,
+ width: Val::Px(24.),
+ height: Val::Px(24.),
+ border: UiRect {
+ top: Val::Px(0.5),
+ right: Val::Px(0.5),
+ bottom: Val::Px(0.5),
+ left: Val::Px(0.5),
+ },
+ ..Default::default()
+ },
+ //background_color: BackgroundColor(Color::GREEN),
+ border_color: BorderColor(Color::WHITE),
+ ..Default::default()
+ })
+ .with_children(|builder| {
+ builder.spawn((
+ TextBundle::from_section(
+ label,
+ TextStyle {
+ font_size: 12.,
+ color: Color::GRAY,
+ ..Default::default()
+ },
+ ),
+ hotbar,
+ ));
+ builder.spawn((
+ TextBundle::from_section(
+ "0.00",
+ TextStyle {
+ font_size: 10.,
+ color: Color::GRAY,
+ ..Default::default()
+ },
+ )
+ .with_style(Style {
+ display: Display::None,
+ ..Default::default()
+ }),
+ hotbar_cooldown,
+ ));
+ });
+ };
+ hotkey("A", Hotbar::A, HotbarCooldown::A);
+ hotkey("Q", Hotbar::Q, HotbarCooldown::Q);
+ hotkey("W", Hotbar::W, HotbarCooldown::W);
+ hotkey("E", Hotbar::E, HotbarCooldown::E);
+ hotkey("R", Hotbar::R, HotbarCooldown::R);
+ hotkey("F", Hotbar::F, HotbarCooldown::F);
+ hotkey("G", Hotbar::G, HotbarCooldown::G);
+ });
client.connect().unwrap();
client
.send_message::<Channel1, SelectChampion>(SelectChampion(champion.0))
@@ -219,10 +320,10 @@ fn choose_attack(keyboard_input: Res<ButtonInput<KeyCode>>, mut attack_key: ResM
attack_key.0 = Some(AttackKey::E);
} else if keyboard_input.just_pressed(KeyCode::KeyR) {
attack_key.0 = Some(AttackKey::R);
- } else if keyboard_input.just_pressed(KeyCode::KeyD) {
- attack_key.0 = Some(AttackKey::D);
} else if keyboard_input.just_pressed(KeyCode::KeyF) {
attack_key.0 = Some(AttackKey::F);
+ } else if keyboard_input.just_pressed(KeyCode::KeyG) {
+ attack_key.0 = Some(AttackKey::G);
} else if keyboard_input.just_pressed(KeyCode::Escape) {
attack_key.0 = None;
}
@@ -354,3 +455,98 @@ fn render_health(players: Query<(&Health, &PlayerPosition)>, mut gizmos: Gizmos)
gizmos.line_2d(start, start.lerp(end, health.0 / MAX_HEALTH), Color::RED);
}
}
+
+fn hotbar_cooldown(
+ client_id: Res<ClientId>,
+ cooldowns: Query<(&PlayerId, &Cooldown)>,
+ mut hotbar_cooldowns: Query<(&HotbarCooldown, &mut Text, &mut Style)>,
+) {
+ let Some(cooldown) = player_cooldown(&client_id, &cooldowns) else {
+ return;
+ };
+ for (hotbar_cooldown, mut text, mut style) in hotbar_cooldowns.iter_mut() {
+ let cooldown = (match hotbar_cooldown {
+ HotbarCooldown::A => cooldown.a_cooldown,
+ HotbarCooldown::Q => cooldown.q_cooldown,
+ HotbarCooldown::W => cooldown.w_cooldown,
+ HotbarCooldown::E => cooldown.e_cooldown,
+ HotbarCooldown::R => cooldown.r_cooldown,
+ HotbarCooldown::F => cooldown.f_cooldown,
+ HotbarCooldown::G => cooldown.g_cooldown,
+ })
+ .as_secs_f32();
+ if text.sections.len() <= 0 {
+ continue;
+ }
+ if cooldown <= 0. {
+ style.display = Display::None;
+ } else {
+ style.display = Display::Flex;
+ text.sections[0].value = format!("{cooldown:.1}");
+ }
+ }
+}
+
+fn player_cooldown(
+ client_id: &Res<ClientId>,
+ players: &Query<(&PlayerId, &Cooldown)>,
+) -> Option<Cooldown> {
+ for (id, cooldown) in players.iter() {
+ if id.0 == client_id.0 {
+ return Some(*cooldown);
+ }
+ }
+ None
+}
+
+fn hotbar_hotbar_display(
+ client_id: Res<ClientId>,
+ cooldowns: Query<(&PlayerId, &Cooldown)>,
+ mut hotbars: Query<(&Hotbar, &mut Style)>,
+) {
+ let Some(cooldown) = player_cooldown(&client_id, &cooldowns) else {
+ return;
+ };
+ for (hotbar, mut style) in hotbars.iter_mut() {
+ let cooldown = (match hotbar {
+ Hotbar::A => cooldown.a_cooldown,
+ Hotbar::Q => cooldown.q_cooldown,
+ Hotbar::W => cooldown.w_cooldown,
+ Hotbar::E => cooldown.e_cooldown,
+ Hotbar::R => cooldown.r_cooldown,
+ Hotbar::F => cooldown.f_cooldown,
+ Hotbar::G => cooldown.g_cooldown,
+ })
+ .as_secs_f32();
+ if cooldown <= 0. {
+ style.display = Display::Flex;
+ } else {
+ style.display = Display::None;
+ }
+ }
+}
+
+fn hotbar_hotbar_highlight(attack: Res<Attack>, mut hotbars: Query<(&Hotbar, &mut Text)>) {
+ let Some(attack_key) = attack.0 else {
+ return;
+ };
+ for (hotbar, mut text) in hotbars.iter_mut() {
+ let is_highlighted = match attack_key {
+ AttackKey::A => *hotbar == Hotbar::A,
+ AttackKey::Q => *hotbar == Hotbar::Q,
+ AttackKey::W => *hotbar == Hotbar::W,
+ AttackKey::E => *hotbar == Hotbar::E,
+ AttackKey::R => *hotbar == Hotbar::R,
+ AttackKey::F => *hotbar == Hotbar::F,
+ AttackKey::G => *hotbar == Hotbar::G,
+ };
+ if text.sections.len() <= 0 {
+ continue;
+ }
+ if is_highlighted {
+ text.sections[0].style.color = Color::WHITE;
+ } else {
+ text.sections[0].style.color = Color::GRAY;
+ }
+ }
+}