I wouldn't call it a die yet, but we're getting there

main
Elijah C. Voigt 1 year ago
parent 59b767a48e
commit 3fe21d45e9

16
Cargo.lock generated

@ -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"

@ -5,10 +5,24 @@ pub(crate) struct DicePlugin;
impl Plugin for DicePlugin {
fn build(&self, app: &mut App) {
app.add_event::<DiceAction>();
app.add_systems(Startup, init_dice_ui);
app.add_systems(Startup, init_dice);
app.add_systems(
Update,
button_emit_event::<DiceAction>.run_if(any_component_changed::<Interaction>),
);
app.add_systems(Update, draw_die.run_if(any_component_changed::<Die>));
app.add_systems(Update, roll_die.run_if(on_event::<DiceAction>()));
app.add_systems(Update, move_die.run_if(on_event::<KeyboardInput>()));
}
}
#[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<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
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<Die>>) {
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<DiceAction>, mut q: Query<&mut Die>, time: Res<Time>) {
r.read().for_each(|msg| match msg {
DiceAction::Roll => {
q.iter_mut().for_each(|mut d| {
d.roll(time.elapsed_seconds() as usize);
});
}
});
}
fn move_die(mut e: EventReader<KeyboardInput>, mut q: Query<&mut Transform, With<Die>>, time: Res<Time>) {
e.read().for_each(|KeyboardInput { key_code, state, .. }| {
match state {
ButtonState::Pressed => {
q.iter_mut().for_each(|mut t| {
match key_code {
KeyCode::ArrowLeft => t.translation -= Vec3::X * time.delta_seconds() * 1000.0,
KeyCode::ArrowRight => t.translation += Vec3::X * time.delta_seconds() * 1000.0,
KeyCode::ArrowDown => t.translation -= Vec3::Y * time.delta_seconds() * 1000.0,
KeyCode::ArrowUp => t.translation += Vec3::Y * time.delta_seconds() * 1000.0,
_ => ()
}
info!("Moved die {:?}", t.translation);
});
},
_ => ()
}
})
}
/// Component for each dice
#[derive(Component, Debug)]
struct Die {
sides: [&'static str; 6],
top: usize,
}
impl Die {
/// Initialize all dice values
fn new(sides: [&'static str; 6]) -> Die {
Die { sides, top: 0 }
}
/// Roll the dice
fn roll(&mut self, seed: usize) -> &'static str {
let idx = seed % 6;
self.top = idx;
self.get()
}
/// Get the current value of the dice
fn get(&self) -> &'static str {
self.sides[self.top]
}
}

@ -1,7 +1,12 @@
pub(crate) use std::fmt::Debug;
/// Bevy imports
pub(crate) use bevy::{ecs::system::EntityCommand, prelude::*};
pub(crate) use bevy::prelude::*;
pub(crate) use bevy::ecs::system::EntityCommand;
pub(crate) use bevy::input::keyboard::KeyboardInput;
pub(crate) use bevy::input::ButtonState;
pub(crate) use bevy::sprite::MaterialMesh2dBundle;
/// Intra-project imports
pub(crate) use crate::ecs::schedule::common_conditions::*;
@ -9,8 +14,10 @@ pub(crate) use crate::game::GameChoice;
pub(crate) use crate::manage_visibility;
pub(crate) use crate::menu::MenuState;
pub(crate) use crate::ui::button::UiButton;
pub(crate) use crate::ui::button_emit_event;
pub(crate) use crate::ui::button_state_action;
pub(crate) use crate::ui::container::UiContainer;
pub(crate) use crate::ui::style::UiStyle;
pub(crate) use crate::ui::title::UiTitle;
pub(crate) use crate::ui::EmitEvent;
pub(crate) use crate::ui::SetState;

@ -37,6 +37,22 @@ pub(crate) fn button_state_action<S: States + Clone + Debug>(
});
}
#[derive(Component)]
pub(crate) struct EmitEvent<E: Event + Clone>(pub E);
pub(crate) fn button_emit_event<E: Event + Clone>(
q: Query<(&EmitEvent<E>, &Interaction), Changed<Interaction>>,
mut w: EventWriter<E>,
) {
q.iter()
.for_each(|(EmitEvent(e), interaction)| match interaction {
Interaction::Pressed => {
w.send(e.clone());
}
_ => (),
})
}
pub(crate) mod button {
use super::*;
@ -84,7 +100,7 @@ pub(crate) mod title {
fn apply(self, id: Entity, world: &mut World) {
let title_text_style = TextStyle {
color: Color::BLACK,
font_size: 24.0,
font_size: 32.0,
..default()
};
world.entity_mut(id).insert(TextBundle {
@ -119,6 +135,7 @@ pub(crate) mod container {
..default()
},
border_color: BorderColor(Color::BLACK),
z_index: ZIndex::Local(-1),
..default()
});
}

Loading…
Cancel
Save