From 3fe21d45e91189e0fa2d0c8257ef7f541f46e146 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Fri, 7 Jun 2024 21:01:17 -0700 Subject: [PATCH] I wouldn't call it a die yet, but we're getting there --- Cargo.lock | 16 +++---- src/game/dice.rs | 113 +++++++++++++++++++++++++++++++++++++++++++++++ src/prelude.rs | 9 +++- src/ui.rs | 19 +++++++- 4 files changed, 147 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f66c862..6da7548 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1209,9 +1209,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.98" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" +checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" dependencies = [ "jobserver", "libc", @@ -3276,9 +3276,9 @@ checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" [[package]] name = "unicode-width" -version = "0.1.12" +version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f5e5f3158ecfd4b8ff6fe086db7c8467a2dfdac97fe420f2b7c4aa97af66d6" +checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" [[package]] name = "unicode-xid" @@ -3646,9 +3646,9 @@ dependencies = [ [[package]] name = "windows-result" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "749f0da9cc72d82e600d8d2e44cadd0b9eedb9038f71a1c58556ac1c5791813b" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" dependencies = [ "windows-targets 0.52.5", ] @@ -3960,9 +3960,9 @@ dependencies = [ [[package]] name = "xkeysym" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "054a8e68b76250b253f671d1268cb7f1ae089ec35e195b2efb2a4e9a836d0621" +checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" [[package]] name = "xml-rs" diff --git a/src/game/dice.rs b/src/game/dice.rs index 474e779..1dc24bf 100644 --- a/src/game/dice.rs +++ b/src/game/dice.rs @@ -5,10 +5,24 @@ pub(crate) struct DicePlugin; impl Plugin for DicePlugin { fn build(&self, app: &mut App) { + app.add_event::(); app.add_systems(Startup, init_dice_ui); + app.add_systems(Startup, init_dice); + app.add_systems( + Update, + button_emit_event::.run_if(any_component_changed::), + ); + app.add_systems(Update, draw_die.run_if(any_component_changed::)); + app.add_systems(Update, roll_die.run_if(on_event::())); + app.add_systems(Update, move_die.run_if(on_event::())); } } +#[derive(Event, Clone)] +enum DiceAction { + Roll, +} + /// Create UI for the Dice game at startup fn init_dice_ui(mut commands: Commands) { commands @@ -39,5 +53,104 @@ fn init_dice_ui(mut commands: Commands) { border: UiRect::all(Val::Px(1.0)), ..default() })); + + parent + .spawn(EmitEvent(DiceAction::Roll)) + .add(UiButton { label: "Roll" }) + .add(UiStyle(Style { + position_type: PositionType::Absolute, + margin: UiRect::all(Val::Px(5.0)), + padding: UiRect::all(Val::Px(5.0)), + border: UiRect::all(Val::Px(1.0)), + ..default() + })); }); } + +fn init_dice( + mut meshes: ResMut>, + mut materials: ResMut>, + mut commands: Commands +) { + commands.spawn(( + GameChoice::Dice, MenuState::Closed, + Die::new(["a", "b", "c", "d", "e", "f"]), + )).insert(Text2dBundle { + text: Text::from_section( + "", + TextStyle { + color: Color::BLACK, + font_size: 32.0, + ..default() + }, + ), + ..default() + }).insert(MaterialMesh2dBundle { + mesh: meshes.add(Rectangle { half_size: Vec2::splat(32.0) }).into(), + material: materials.add(Color::PINK), + ..default() + }); +} + +fn draw_die(mut q: Query<(&Die, &mut Text), Changed>) { + q.iter_mut().for_each(|(d, mut t)| { + info!("Dice currently reads {:?}", d.get()); + t.sections[0].value.replace_range(.., d.get()); + }); +} + +fn roll_die(mut r: EventReader, mut q: Query<&mut Die>, time: Res