Skip to content

Commit

Permalink
Implement deck rearranging in level up menu
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Jul 26, 2024
1 parent 51521e8 commit 7d6c288
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/game/card/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,28 @@ impl Deck {
}
}

fn peek_next(&self) -> Option<&String> {
self.card_keys.get(self.next())
}

fn next(&self) -> usize {
if !self.card_keys.is_empty() {
(self.active + 1) % self.card_keys.len()
} else {
0
pub fn advance(&mut self, step: isize) -> Option<&String> {
if self.card_keys.is_empty() {
return None;
}

self.active =
(self.active as isize + step).rem_euclid(self.card_keys.len() as isize) as usize;

Some(&self.card_keys[self.active])
}

fn advance(&mut self) {
if !self.card_keys.is_empty() {
self.active = self.next();
pub fn swap(&mut self, step: isize) -> Option<&String> {
if self.card_keys.is_empty() {
return None;
}

let old = self.active;
self.active =
(self.active as isize + step).rem_euclid(self.card_keys.len() as isize) as usize;
self.card_keys.swap(old, self.active);

Some(&self.card_keys[self.active])
}
}

Expand All @@ -84,12 +90,11 @@ fn advance_deck(
let config = r!(config.get());

for (entity, mut deck) in &mut deck_query {
let card_key = c!(deck.peek_next());
let card_key = c!(deck.advance(1));
let card_action = c!(config.card_map.get(card_key));
let action = card_action.action;
let action_config = card_action.action_config.clone();
commands.run_system_with_input(action.0, (entity, action_config));
deck.advance();
}
}

Expand Down
107 changes: 107 additions & 0 deletions src/screen/playing/level_up_menu.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use bevy::prelude::*;
use bevy_mod_picking::prelude::*;
use leafwing_input_manager::common_conditions::action_just_pressed;
use leafwing_input_manager::prelude::*;
use pyri_state::extra::entity_scope::StateScope;
use pyri_state::prelude::*;

use crate::core::pause::Pause;
use crate::core::UpdateSet;
use crate::game::actor::level::up::LevelUp;
use crate::game::card::deck::Deck;
use crate::game::card::deck::IsDeckDisplay;
use crate::screen::playing::PlayingMenu;
use crate::ui::prelude::*;
use crate::util::prelude::*;
Expand All @@ -25,6 +29,8 @@ pub(super) fn plugin(app: &mut App) {
.in_set(UpdateSet::SyncLate)
.run_if(on_event::<LevelUp>()),
);

app.configure::<LevelUpMenuAction>();
}

fn open_level_up_menu(mut commands: Commands, ui_root: Res<UiRoot>) {
Expand Down Expand Up @@ -106,3 +112,104 @@ fn ready_button(mut entity: EntityWorldMut) {
.add(widget::menu_button("Ready?"))
.insert(On::<Pointer<Click>>::run(PlayingMenu::disable));
}

#[derive(Actionlike, Reflect, Clone, Hash, PartialEq, Eq)]
pub enum LevelUpMenuAction {
// TODO: Discard action.
SelectLeft,
SelectRight,
SwapLeft,
SwapRight,
}

impl Configure for LevelUpMenuAction {
fn configure(app: &mut App) {
app.init_resource::<ActionState<Self>>();
app.insert_resource(
InputMap::default()
.insert(Self::SelectLeft, GamepadButtonType::DPadLeft)
.insert(Self::SelectLeft, GamepadButtonType::LeftTrigger)
.insert(Self::SelectLeft, KeyCode::KeyA)
.insert(Self::SelectLeft, KeyCode::ArrowLeft)
.insert(Self::SelectRight, GamepadButtonType::DPadRight)
.insert(Self::SelectRight, GamepadButtonType::RightTrigger)
.insert(Self::SelectRight, KeyCode::KeyD)
.insert(Self::SelectRight, KeyCode::ArrowRight)
.insert(Self::SwapLeft, GamepadButtonType::LeftTrigger2)
.insert_modified(Self::SwapLeft, Modifier::Shift, KeyCode::KeyA)
.insert_modified(Self::SwapLeft, Modifier::Shift, KeyCode::ArrowLeft)
.insert(Self::SwapRight, GamepadButtonType::RightTrigger2)
.insert_modified(Self::SwapRight, Modifier::Shift, KeyCode::KeyD)
.insert_modified(Self::SwapRight, Modifier::Shift, KeyCode::ArrowRight)
.build(),
);
app.add_plugins(InputManagerPlugin::<Self>::default());
// TODO: It'd be better to disable the action outside of `PlayingMenu::LevelUp`, but
// action disabling is buggy in LWIM 0.14. The fix is merged but not yet released.
app.add_systems(
Update,
(
select_card_left.in_set(UpdateSet::RecordInput).run_if(
PlayingMenu::LevelUp
.will_update()
.and_then(action_just_pressed(Self::SelectLeft)),
),
select_card_right.in_set(UpdateSet::RecordInput).run_if(
PlayingMenu::LevelUp
.will_update()
.and_then(action_just_pressed(Self::SelectRight)),
),
swap_card_left.in_set(UpdateSet::RecordInput).run_if(
PlayingMenu::LevelUp
.will_update()
.and_then(action_just_pressed(Self::SwapLeft)),
),
swap_card_right.in_set(UpdateSet::RecordInput).run_if(
PlayingMenu::LevelUp
.will_update()
.and_then(action_just_pressed(Self::SwapRight)),
),
),
);
}
}

fn select_card_left(
deck_display_query: Query<&Selection, With<IsDeckDisplay>>,
mut deck_query: Query<&mut Deck>,
) {
for selection in &deck_display_query {
let mut deck = c!(deck_query.get_mut(selection.0));
deck.advance(-1);
}
}

fn select_card_right(
deck_display_query: Query<&Selection, With<IsDeckDisplay>>,
mut deck_query: Query<&mut Deck>,
) {
for selection in &deck_display_query {
let mut deck = c!(deck_query.get_mut(selection.0));
deck.advance(1);
}
}

fn swap_card_left(
deck_display_query: Query<&Selection, With<IsDeckDisplay>>,
mut deck_query: Query<&mut Deck>,
) {
for selection in &deck_display_query {
let mut deck = c!(deck_query.get_mut(selection.0));
deck.swap(-1);
}
}

fn swap_card_right(
deck_display_query: Query<&Selection, With<IsDeckDisplay>>,
mut deck_query: Query<&mut Deck>,
) {
for selection in &deck_display_query {
let mut deck = c!(deck_query.get_mut(selection.0));
deck.swap(1);
}
}

0 comments on commit 7d6c288

Please sign in to comment.