DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

66fe22c89e03dfe8124c9703f1f3187b2d867f50 — Jonni Liljamo 1 year, 5 months ago 07abbbd
feat(client): play cards
M client/src/api/game/mod.rs => client/src/api/game/mod.rs +4 -0
@@ 42,6 42,10 @@ pub enum Command {
        index: usize,
        for_cost: usize,
    },
    /// play card from hand index N
    PlayCard {
        index: usize,
    },
    /// draw N amount of cards from deck
    Draw {
        amount: usize,

M client/src/game_status/parser.rs => client/src/game_status/parser.rs +24 -0
@@ 135,6 135,30 @@ fn parse_action(action: &Action, game: &Game, game_status: &mut GameStatus) {

            target.discard.push(pile.card.clone());
        }
        Command::PlayCard { index } => {
            // index should be within range
            assert!(*index <= target.hand.len());

            // player should have plays
            assert!(target.plays > 0);
            target.plays -= 1;

            let card = target.hand.remove(*index);
            target.discard.push(card.clone());

            for card_action in &card.actions {
                let action = &Action::new(
                    &game.id,
                    &action.invoker,
                    &action.target,
                    &card_action.command,
                    current_seed!(action),
                );
                game_status.actions.insert(action_pos + 1, action.clone());

                parse_action(action, game, game_status);
            }
        }
        Command::Draw { amount } => {
            for _ in 0..*amount {
                if target.deck.is_empty() {

M client/src/plugins/game/hand/mod.rs => client/src/plugins/game/hand/mod.rs +39 -13
@@ 9,10 9,15 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;

use crate::Global;
use crate::{
    api::game::{Action, Command},
    game_status::PlayerState,
    plugins::GameActionCreateCallEvent,
    seed_gen, Global,
};

use super::{
    card::{visual_card_kind, VisualCard, VisualCardBundle},
    card::{visual_card_kind, ClickedCard, VisualCard, VisualCardBundle},
    GameData,
};



@@ 23,7 28,8 @@ impl Plugin for HandPlugin {
        app.add_event::<SpawnHandEvent>()
            .add_event::<PositionHandEvent>()
            .add_system(spawn_hand.run_if(on_event::<SpawnHandEvent>()))
            .add_system(position_hand.run_if(on_event::<PositionHandEvent>())); //.add_system(handle_clicked_hand_card);
            .add_system(position_hand.run_if(on_event::<PositionHandEvent>()))
            .add_system(handle_clicked_hand_card);
    }
}



@@ 86,31 92,51 @@ fn position_hand(
    }
}

/*
fn handle_clicked_hand_card(
    mut commands: Commands,
    mut card_query: Query<(Entity, &VisualCard, &visual_card_kind::Supply), (With<visual_card_kind::Supply>, With<ClickedCard>)>,
    card_query: Query<
        (Entity, &visual_card_kind::Hand),
        (With<visual_card_kind::Hand>, With<ClickedCard>),
    >,
    mut gac_ev_w: EventWriter<GameActionCreateCallEvent>,
    game_data: Res<GameData>,
    mut game_data: ResMut<GameData>,
    global: Res<Global>,
) {
    let Ok((entity, card, card_kind)) = card_query.get_single() else {
    if game_data.locked {
        return;
    }

    let Ok((entity, card_kind)) = card_query.get_single() else {
        return;
    };

    commands.entity(entity).remove::<ClickedCard>();

    let player = game_data
        .game_status
        .as_ref()
        .unwrap()
        .players
        .get(&global.user.as_ref().unwrap().id)
        .unwrap();

    #[allow(clippy::if_same_then_else)]
    if player.state != PlayerState::PlayPhase {
        // we ain't playing rn
        return;
    } else if player.plays == 0 {
        // not enough plays
        return;
    }

    game_data.locked = true;
    gac_ev_w.send(GameActionCreateCallEvent {
        action: Action::new(
            &game_data.game.as_ref().unwrap().id,
            &global.user.as_ref().unwrap().id,
            &global.user.as_ref().unwrap().id,
            &Command::TakeFromPile {
                index: card_kind.0,
                for_cost: card.card.cost,
            },
            fastrand::u64(u64::MIN..=u64::MAX),
            &Command::PlayCard { index: card_kind.0 },
            seed_gen!(),
        ),
    });
}
*/