aboutsummaryrefslogtreecommitdiffstats
path: root/src/client.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/client.rs')
-rw-r--r--src/client.rs98
1 files changed, 27 insertions, 71 deletions
diff --git a/src/client.rs b/src/client.rs
index e3c4594..7ccced7 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -47,7 +47,7 @@ struct ClientId(pub u64);
struct MyChampion(pub Champion);
#[derive(Resource)]
-struct Attack(Option<AttackKey>);
+struct Attack(Option<AbilitySlot>);
struct ClientPlugin {
pub server_addr: Option<SocketAddr>,
@@ -100,26 +100,10 @@ impl Plugin for ClientPlugin {
}
#[derive(Component, PartialEq, Eq, Debug)]
-pub enum Hotbar {
- A,
- Q,
- W,
- E,
- R,
- F,
- G,
-}
+pub struct Hotbar(AbilitySlot);
#[derive(Component, PartialEq, Eq, Debug)]
-pub enum HotbarCooldown {
- A,
- Q,
- W,
- E,
- R,
- F,
- G,
-}
+pub struct HotbarCooldown(AbilitySlot);
fn setup(mut client: ClientMut, mut commands: Commands, champion: Res<MyChampion>) {
commands.spawn(Camera2dBundle::default());
@@ -137,7 +121,10 @@ fn setup(mut client: ClientMut, mut commands: Commands, champion: Res<MyChampion
..Default::default()
})
.with_children(|builder| {
- let mut hotkey = |label, hotbar, hotbar_cooldown| {
+ let mut hotkey = |ability_slot: AbilitySlot| {
+ let label = ability_slot.to_label();
+ let hotbar = Hotbar(ability_slot);
+ let hotbar_cooldown = HotbarCooldown(ability_slot);
builder
.spawn(NodeBundle {
style: Style {
@@ -186,13 +173,9 @@ fn setup(mut client: ClientMut, mut commands: Commands, champion: Res<MyChampion
));
});
};
- 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);
+ for ability_slot in AbilitySlot::all() {
+ hotkey(ability_slot);
+ }
});
client.connect().unwrap();
client
@@ -273,7 +256,7 @@ fn buffer_input(
if mouse_input.just_pressed(MouseButton::Left) {
match attack.0 {
Some(attack_key) => match champion.0.to_ability(attack_key) {
- Ability::Directional(ability) => {
+ Ability::Directional(_) => {
let Some(world_position) = cursor_world_position(&windows, &cameras) else {
return;
};
@@ -284,18 +267,18 @@ fn buffer_input(
return;
};
client.add_input(Inputs::Imperative(Imperative::AttackDirection(
- ability, direction,
+ attack_key, direction,
)));
attack.0 = None;
}
- Ability::Targeted(ability) => {
+ Ability::Targeted(_) => {
let Some((target_player, _)) =
hovered_other_player(&cameras, &client_id, &players, &windows)
else {
return;
};
client.add_input(Inputs::Imperative(Imperative::AttackTarget(
- ability,
+ attack_key,
target_player,
)));
attack.0 = None;
@@ -306,7 +289,7 @@ fn buffer_input(
hovered_other_player(&cameras, &client_id, &players, &windows)
{
client.add_input(Inputs::Imperative(Imperative::AttackTarget(
- TargetedAbility::MeeleAttack,
+ AbilitySlot::A,
target_player,
)));
} else {
@@ -321,19 +304,19 @@ fn buffer_input(
fn choose_attack(keyboard_input: Res<ButtonInput<KeyCode>>, mut attack_key: ResMut<Attack>) {
if keyboard_input.just_pressed(KeyCode::KeyA) {
- attack_key.0 = Some(AttackKey::A);
+ attack_key.0 = Some(AbilitySlot::A);
} else if keyboard_input.just_pressed(KeyCode::KeyQ) {
- attack_key.0 = Some(AttackKey::Q);
+ attack_key.0 = Some(AbilitySlot::Q);
} else if keyboard_input.just_pressed(KeyCode::KeyW) {
- attack_key.0 = Some(AttackKey::W);
+ attack_key.0 = Some(AbilitySlot::W);
} else if keyboard_input.just_pressed(KeyCode::KeyE) {
- attack_key.0 = Some(AttackKey::E);
+ attack_key.0 = Some(AbilitySlot::E);
} else if keyboard_input.just_pressed(KeyCode::KeyR) {
- attack_key.0 = Some(AttackKey::R);
+ attack_key.0 = Some(AbilitySlot::R);
} else if keyboard_input.just_pressed(KeyCode::KeyF) {
- attack_key.0 = Some(AttackKey::F);
+ attack_key.0 = Some(AbilitySlot::F);
} else if keyboard_input.just_pressed(KeyCode::KeyG) {
- attack_key.0 = Some(AttackKey::G);
+ attack_key.0 = Some(AbilitySlot::G);
} else if keyboard_input.just_pressed(KeyCode::Escape) {
attack_key.0 = None;
}
@@ -413,7 +396,7 @@ fn gizmos_attack_indicator(
return;
};
let Some(attack_key) = attack.0.or_else(|| {
- hovered_other_player(&cameras, &client_id, &hoverables, &windows).map(|_| AttackKey::A)
+ hovered_other_player(&cameras, &client_id, &hoverables, &windows).map(|_| AbilitySlot::A)
}) else {
return;
};
@@ -480,16 +463,7 @@ fn hotbar_cooldown(
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();
+ let cooldown = cooldown.0[hotbar_cooldown.0].as_secs_f32();
if text.sections.len() <= 0 {
continue;
}
@@ -523,17 +497,7 @@ fn hotbar_hotbar_display(
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. {
+ if cooldown.0[hotbar.0].as_secs_f32() <= 0. {
style.display = Display::Flex;
} else {
style.display = Display::None;
@@ -542,19 +506,11 @@ fn hotbar_hotbar_display(
}
fn hotbar_hotbar_highlight(attack: Res<Attack>, mut hotbars: Query<(&Hotbar, &mut Text)>) {
- let Some(attack_key) = attack.0 else {
+ let Some(ability_slot) = 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,
- };
+ let is_highlighted = ability_slot == hotbar.0;
if text.sections.len() <= 0 {
continue;
}