From 9352c9d4a3e602c3542668ecadf39ecef3bb32ec Mon Sep 17 00:00:00 2001 From: skye Date: Wed, 10 May 2023 13:58:01 +0300 Subject: [PATCH] feat(client): sorta wip hand plugin --- client/src/plugins/game/hand/mod.rs | 114 ++++++++++++++++++++++++++++ client/src/plugins/game/mod.rs | 2 + 2 files changed, 116 insertions(+) create mode 100644 client/src/plugins/game/hand/mod.rs diff --git a/client/src/plugins/game/hand/mod.rs b/client/src/plugins/game/hand/mod.rs new file mode 100644 index 0000000..1bc3dd5 --- /dev/null +++ b/client/src/plugins/game/hand/mod.rs @@ -0,0 +1,114 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use bevy::prelude::*; +use bevy_rapier3d::prelude::*; + +use crate::{plugins::GameActionCreateCallEvent, api::game::{Action, Command}, Global}; + +use super::{ + card::{visual_card_kind, VisualCard, VisualCardBundle, ClickedCard}, + GameData, +}; + +pub struct HandPlugin; + +impl Plugin for HandPlugin { + fn build(&self, app: &mut App) { + app.add_event::() + .add_event::() + .add_system(spawn_hand.run_if(on_event::())) + .add_system(position_hand.run_if(on_event::())) + ;//.add_system(handle_clicked_hand_card); + } +} + +pub struct SpawnHandEvent; + +fn spawn_hand( + mut commands: Commands, + game_data: Res, + global: Res, + mut ph_ev_w: EventWriter, + mut hand_query: Query>, +) { + let Some(status) = &game_data.game_status else { + warn!("game_status was none"); + return; + }; + + // despawn possible existing supply piles + for entity in hand_query.iter() { + commands.entity(entity).despawn_recursive(); + } + + for (index, card) in status.players + .get(&global.user.as_ref().unwrap().id).unwrap() + .hand.iter().enumerate() { + commands + .spawn(VisualCardBundle { + visual_card: VisualCard { + card: card.clone(), + }, + rigid_body: RigidBody::Fixed, + ..Default::default() + }) + .insert(visual_card_kind::Hand(index)); + } + + ph_ev_w.send(PositionHandEvent); +} + +pub struct PositionHandEvent; + +fn position_hand( + mut card_query: Query<(&VisualCard, &mut Transform), With>, +) { + let mut cards: Vec<(&VisualCard, Mut)> = card_query.iter_mut().collect::>(); + + cards.sort_by_key(|(vc, _t)| vc.card.name.clone()); + cards.sort_by_key(|(vc, _t)| vc.card.cost); + + // keep track of offset between cards + let mut offset = 0.; + + for (_, mut t) in cards { + t.translation.z += 1.5 * 3.; // leave room for 3 cards from z0.0 + t.translation.x += offset; + offset += 1.0; + } +} + +/* +fn handle_clicked_hand_card( + mut commands: Commands, + mut card_query: Query<(Entity, &VisualCard, &visual_card_kind::Supply), (With, With)>, + mut gac_ev_w: EventWriter, + game_data: Res, + global: Res, +) { + let Ok((entity, card, card_kind)) = card_query.get_single() else { + return; + }; + + commands.entity(entity).remove::(); + + 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), + ), + }); +} +*/ diff --git a/client/src/plugins/game/mod.rs b/client/src/plugins/game/mod.rs index 877a980..2dd597f 100644 --- a/client/src/plugins/game/mod.rs +++ b/client/src/plugins/game/mod.rs @@ -16,6 +16,7 @@ use self::card::VisualCardBundle; use super::GameDetailsCallEvent; mod card; +mod hand; mod supply; mod ui; @@ -27,6 +28,7 @@ impl Plugin for GamePlugin { .add_plugin(ui::GameUIPlugin) .add_plugin(card::CardPlugin) .add_plugin(supply::SupplyPlugin) + .add_plugin(hand::HandPlugin) .add_system(game_setup.in_schedule(OnEnter(AppState::InGame))); } } -- 2.44.1