|
|
|
@ -5,10 +5,24 @@ pub(crate) struct DicePlugin;
|
|
|
|
|
|
|
|
|
|
|
|
impl Plugin for DicePlugin {
|
|
|
|
impl Plugin for DicePlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
|
|
|
app.add_event::<DiceAction>();
|
|
|
|
app.add_systems(Startup, init_dice_ui);
|
|
|
|
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
|
|
|
|
/// Create UI for the Dice game at startup
|
|
|
|
fn init_dice_ui(mut commands: Commands) {
|
|
|
|
fn init_dice_ui(mut commands: Commands) {
|
|
|
|
commands
|
|
|
|
commands
|
|
|
|
@ -39,5 +53,104 @@ fn init_dice_ui(mut commands: Commands) {
|
|
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
|
|
..default()
|
|
|
|
..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]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|