From c9b2a22e68a60025ae48e97e9f4d70da0b234dd5 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Thu, 25 Jan 2024 21:15:31 -0800 Subject: [PATCH] Editor, plus better alignment for pieces on side of board The pieces are still not in the right place... --- src/display3d.rs | 19 +++------- src/editor.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 + 3 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 src/editor.rs diff --git a/src/display3d.rs b/src/display3d.rs index 9953990..df87a1e 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -491,21 +491,12 @@ fn board_translation(&BoardIndex { x, y }: &BoardIndex) -> Vec3 { fn capture_translation(side: &Side, num: usize) -> Vec3 { info!("Side: {:?} Num: {:?}", side, num); + let x = 5.0 - ((num % 4) as f32 * 1.3); + let y = -1.3; + let z = 4.0 + ((num / 4) as f32 * 1.3); match side { - Side::B => { - let x = -((num % 4) as f32 * 1.3) - 5.2; // mod(num, 4) - let z = ((num / 4) as f32 * 1.3) - 5.2; // floor(div(num, 4)) - let y = -1.3; - info!("Vec3({}, {}, {})", x, y, z); - Vec3::new(x, y, z) - }, - Side::A => { - let x = ((num % 4) as f32 * 1.3) + 5.2; // mod(num, 4) - let z = -((num / 4) as f32 * 1.3) + 5.2; // floor(div(num, 4)) - let y = -1.3; - info!("Vec3({}, {}, {})", x, y, z); - Vec3::new(x, y, z) - } + Side::B => Vec3::new(-x, y, z), + Side::A => Vec3::new(x, y, -z), } } diff --git a/src/editor.rs b/src/editor.rs new file mode 100644 index 0000000..7e26010 --- /dev/null +++ b/src/editor.rs @@ -0,0 +1,97 @@ +use bevy::input::keyboard::KeyboardInput; + +use crate::prelude::*; + +pub struct EditorPlugin; + +impl Plugin for EditorPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Startup, init_editor) + .insert_resource( + GizmoConfig { + depth_bias: -0.1, + ..default() + }, + ) + .add_systems(Update, ( + toggle_editor.run_if(on_event::()), + aabb_gizmo, + )) + // Systems that run in the editor mode + .add_systems(Update, ( + selected_gizmo.run_if(any_with_component::()), + selected_position.run_if(any_with_component::()), + ).run_if(resource_exists::())); + } +} + +#[derive(Debug, Resource)] +struct EditorActive; + +fn init_editor( + mut commands: Commands, +) { + info!("Starting editor"); +} + +fn toggle_editor( + mut events: EventReader, + active: Option>, + mut commands: Commands, +) { + events + .read() + .filter(|KeyboardInput { + state, key_code, .. + }| *state == ButtonState::Pressed && *key_code == Some(KeyCode::F3)) + .for_each(|_| match active { + Some(_) => commands.remove_resource::(), + None => commands.insert_resource(EditorActive), + }); +} + +fn aabb_gizmo( + added: Query>, + mut removed: RemovedComponents, + selected: Query>, + active: Option>, + mut commands: Commands, +) { + added.iter().for_each(|e| { + commands.entity(e).insert(AabbGizmo { color: Some(Color::RED) }); + }); + removed.read().for_each(|e| { + commands.entity(e).remove::(); + }); + match active { + Some(_) => selected.iter().for_each(|e| { + commands.entity(e).insert(AabbGizmo { color: Some(Color::RED) }); + }), + None => selected.iter().for_each(|e| { + commands.entity(e).remove::(); + }), + } +} + +/// Draw a gizmo showing cardinal directions for a selected object +fn selected_gizmo( + selected: Query<(Entity, &Transform, &GlobalTransform), With>, + mut gizmos: Gizmos, +) { + selected.iter().for_each(|(e, t, g)| { + let s = g.translation(); + gizmos.ray(s, Vec3::X, Color::RED); + gizmos.ray(s, Vec3::Y, Color::GREEN); + gizmos.ray(s, Vec3::Z, Color::BLUE); + }); +} + +fn selected_position( + selected: Query<(Entity, &GlobalTransform), With>, + mut debug_info: ResMut, +) { + let val = selected.iter().map(|(e, gt)| { + format!("\n{:?} {:?}", e, gt.translation()) + }).collect::>().join(""); + debug_info.set("Position".into(), val); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d86a553..529c5d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod credits; mod debug; mod display2d; mod display3d; +mod editor; mod game; mod hit; mod loading; @@ -61,6 +62,7 @@ fn main() { app.add_plugins(debug::DebugPlugin); app.add_plugins(display2d::Display2dPlugin); app.add_plugins(display3d::Display3dPlugin); + app.add_plugins(editor::EditorPlugin); app.add_plugins(game::GamePlugin); app.add_plugins(loading::LoadingPlugin); app.add_plugins(menu::MenuPlugin);