Compare commits

..

No commits in common. 'a787e0e6c3cb55127b0d6a727d072567e1567993' and '1e42519a312d2a5ba28ee337933263906e4c2064' have entirely different histories.

@ -1,14 +1,12 @@
use std::f32::consts::PI; use std::f32::consts::PI;
use bevy::{ use bevy::{
input::common_conditions::input_just_pressed, input::common_conditions::input_just_pressed, prelude::*, render::{
prelude::*,
render::{
camera::RenderTarget, camera::RenderTarget,
render_resource::{ render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
}, },
}, }
}; };
fn main() { fn main() {
@ -17,7 +15,8 @@ fn main() {
.add_systems(Startup, add_texture) .add_systems(Startup, add_texture)
.add_systems( .add_systems(
Update, Update,
rotate_mesh.run_if(input_just_pressed(KeyCode::Space)), rotate_mesh
.run_if(input_just_pressed(KeyCode::Space)),
) )
.run(); .run();
} }

@ -1,3 +1,5 @@
use bevy::input::mouse::MouseButtonInput;
use crate::prelude::*; use crate::prelude::*;
/// Menu Plugin; empty struct for Plugin impl /// Menu Plugin; empty struct for Plugin impl
@ -5,14 +7,7 @@ pub(crate) struct CameraPlugin;
impl Plugin for CameraPlugin { impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems( app.add_systems(Update, editor_fly_camera);
Update,
move_editor_fly_camera.run_if(any_with_component::<FlyCamera>),
);
app.add_systems(
Update,
rotate_editor_fly_camera.run_if(any_with_component::<FlyCamera>),
);
} }
} }
@ -21,22 +16,19 @@ pub(crate) struct FlyCamera;
/// Fly camera system for moving around like a drone /// Fly camera system for moving around like a drone
/// TODO: Only if key is pressed! /// TODO: Only if key is pressed!
fn move_editor_fly_camera( fn editor_fly_camera(
mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>, mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>,
windows: Query<&Window>, windows: Query<&Window>,
primary_window: Query<Entity, With<PrimaryWindow>>, primary_window: Query<Entity, With<PrimaryWindow>>,
keys: Res<ButtonInput<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
mouse: Res<ButtonInput<MouseButton>>,
mut cursor_events: EventReader<CursorMoved>,
time: Res<Time>, time: Res<Time>,
) { ) {
(keys.any_pressed([ let any_keys_pressed = keys.any_pressed([ KeyCode::KeyW, KeyCode::KeyS, KeyCode::KeyA, KeyCode::KeyD, KeyCode::KeyQ, KeyCode::KeyE ]);
KeyCode::KeyW, let cursor_movement = cursor_events.len() > 0;
KeyCode::KeyS,
KeyCode::KeyA, (any_keys_pressed || cursor_movement).then(|| {
KeyCode::KeyD,
KeyCode::KeyQ,
KeyCode::KeyE,
]))
.then(|| {
// Iterate over all cameras // Iterate over all cameras
cameras.iter_mut().for_each(|(c, mut t)| { cameras.iter_mut().for_each(|(c, mut t)| {
// Determine which window this camera is attached to // Determine which window this camera is attached to
@ -73,36 +65,11 @@ fn move_editor_fly_camera(
} }
t.translation += delta; t.translation += delta;
}); });
});
});
}
fn rotate_editor_fly_camera(
mut cameras: Query<(&Camera, &mut Transform), With<FlyCamera>>,
windows: Query<&Window>,
primary_window: Query<Entity, With<PrimaryWindow>>,
mouse: Res<ButtonInput<MouseButton>>,
mut cursor_events: EventReader<CursorMoved>,
) {
(cursor_events.len() > 0).then(|| {
// Iterate over all cameras
cameras.iter_mut().for_each(|(c, mut t)| {
// Determine which window this camera is attached to
let target_window = match c.target {
RenderTarget::Window(wr) => match wr {
WindowRef::Entity(e) => Some(e),
WindowRef::Primary => Some(primary_window.get_single().unwrap()),
},
_ => None,
};
let window = windows.get(target_window.unwrap()).unwrap();
if mouse.pressed(MouseButton::Middle) { if mouse.pressed(MouseButton::Middle) {
cursor_events cursor_events.read().filter_map(|CursorMoved { delta, window, .. }| {
.read()
.filter_map(|CursorMoved { delta, window, .. }| {
(*window == target_window.unwrap()).then_some(delta) (*window == target_window.unwrap()).then_some(delta)
}) }).for_each(|delta| {
.for_each(|delta| {
if let Some(Vec2 { x, y }) = delta { if let Some(Vec2 { x, y }) = delta {
// Cribbing from bevy_flycam // Cribbing from bevy_flycam
// Link: https://github.com/sburris0/bevy_flycam/blob/baffe50e0961ad1491d467fa6ab5551f9f21db8f/src/lib.rs#L145-L151 // Link: https://github.com/sburris0/bevy_flycam/blob/baffe50e0961ad1491d467fa6ab5551f9f21db8f/src/lib.rs#L145-L151
@ -111,8 +78,7 @@ fn rotate_editor_fly_camera(
let sensitivity = 0.00012; let sensitivity = 0.00012;
pitch -= (sensitivity * y * window_scale).to_radians(); pitch -= (sensitivity * y * window_scale).to_radians();
yaw -= (sensitivity * x * window_scale).to_radians(); yaw -= (sensitivity * x * window_scale).to_radians();
t.rotation = Quat::from_axis_angle(Vec3::Y, yaw) t.rotation = Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
* Quat::from_axis_angle(Vec3::X, pitch);
} }
}); });
} else { } else {

@ -7,16 +7,10 @@ impl Plugin for EditorPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_state::<EditorState>(); app.init_state::<EditorState>();
app.add_systems(Startup, init_editor); app.add_systems(Startup, init_editor);
app.add_systems( app.add_systems(Update, toggle_editor.run_if(input_just_pressed(KeyCode::F3)));
Update,
toggle_editor.run_if(input_just_pressed(KeyCode::F3)),
);
app.add_systems(OnEnter(EditorState::Open), open_editor); app.add_systems(OnEnter(EditorState::Open), open_editor);
app.add_systems(OnExit(EditorState::Open), close_editor); app.add_systems(OnExit(EditorState::Open), close_editor);
app.add_systems( app.add_systems(Update, origin_directions.run_if(in_state(EditorState::Open)));
Update,
origin_directions.run_if(in_state(EditorState::Open)),
);
app.add_systems(Update, world_plane.run_if(in_state(EditorState::Open))); app.add_systems(Update, world_plane.run_if(in_state(EditorState::Open)));
} }
} }
@ -33,13 +27,14 @@ enum EditorState {
struct Editor; struct Editor;
/// Spawns all base editor entities including window, camera, and UI elements /// Spawns all base editor entities including window, camera, and UI elements
fn init_editor(mut commands: Commands) { fn init_editor(
mut commands: Commands
) {
// Spawn root editor entity hierarchy // Spawn root editor entity hierarchy
commands commands.spawn(SpatialBundle { ..default() })
.spawn(SpatialBundle { ..default() })
.with_children(|parent| { .with_children(|parent| {
let editor_window = parent
.spawn(( let editor_window = parent.spawn((
Editor, Editor,
Window { Window {
title: "Editor".into(), title: "Editor".into(),
@ -47,12 +42,10 @@ fn init_editor(mut commands: Commands) {
visible: false, visible: false,
..default() ..default()
}, },
)) )).id();
.id();
// Spawn editor camera // Spawn editor camera
let _editor_camera = parent let _editor_camera = parent.spawn((
.spawn((
Editor, Editor,
FlyCamera, FlyCamera,
Camera3dBundle { Camera3dBundle {
@ -60,22 +53,24 @@ fn init_editor(mut commands: Commands) {
target: RenderTarget::Window(WindowRef::Entity(editor_window)), target: RenderTarget::Window(WindowRef::Entity(editor_window)),
..default() ..default()
}, },
transform: Transform::from_xyz(1.0, 1.0, 1.0) transform: Transform::from_xyz(1.0, 1.0, 1.0).looking_at(Vec3::ZERO, Vec3::Y),
.looking_at(Vec3::ZERO, Vec3::Y),
..default() ..default()
}, },
)) )).id();
.id();
}); });
} }
fn open_editor(mut ws: Query<&mut Window, With<Editor>>) { fn open_editor(
mut ws: Query<&mut Window, With<Editor>>,
) {
ws.iter_mut().for_each(|mut w| { ws.iter_mut().for_each(|mut w| {
w.visible = true; w.visible = true;
}); });
} }
fn close_editor(mut ws: Query<&mut Window, With<Editor>>) { fn close_editor(
mut ws: Query<&mut Window, With<Editor>>,
) {
ws.iter_mut().for_each(|mut w| { ws.iter_mut().for_each(|mut w| {
w.visible = false; w.visible = false;
}); });
@ -98,13 +93,17 @@ fn toggle_editor(
} }
} }
fn origin_directions(mut gizmos: Gizmos) { fn origin_directions(
mut gizmos: Gizmos
) {
gizmos.arrow(Vec3::ZERO, Vec3::X, Color::RED); gizmos.arrow(Vec3::ZERO, Vec3::X, Color::RED);
gizmos.arrow(Vec3::ZERO, Vec3::Y, Color::GREEN); gizmos.arrow(Vec3::ZERO, Vec3::Y, Color::GREEN);
gizmos.arrow(Vec3::ZERO, Vec3::Z, Color::BLUE); gizmos.arrow(Vec3::ZERO, Vec3::Z, Color::BLUE);
} }
fn world_plane(mut gizmos: Gizmos) { fn world_plane(
mut gizmos: Gizmos
) {
(-10..=10).into_iter().for_each(|x| { (-10..=10).into_iter().for_each(|x| {
(-10..=10).into_iter().for_each(|z| { (-10..=10).into_iter().for_each(|z| {
{ {

@ -141,7 +141,8 @@ fn move_die(
}| { }| {
match state { match state {
ButtonState::Pressed => { ButtonState::Pressed => {
q.iter_mut().for_each(|mut t| match key_code { q.iter_mut().for_each(|mut t| {
match key_code {
KeyCode::ArrowLeft => { KeyCode::ArrowLeft => {
t.translation -= Vec3::X * time.delta_seconds() * 1000.0 t.translation -= Vec3::X * time.delta_seconds() * 1000.0
} }
@ -155,6 +156,7 @@ fn move_die(
t.translation += Vec3::Y * time.delta_seconds() * 1000.0 t.translation += Vec3::Y * time.delta_seconds() * 1000.0
} }
_ => (), _ => (),
}
}); });
} }
_ => (), _ => (),

@ -29,14 +29,13 @@ use crate::prelude::*;
fn main() { fn main() {
let mut app = App::new(); let mut app = App::new();
app.add_plugins( app.add_plugins(bevy::DefaultPlugins
bevy::DefaultPlugins
.set(low_latency_window_plugin()) .set(low_latency_window_plugin())
.set(WindowPlugin { .set(WindowPlugin {
exit_condition: ExitCondition::OnPrimaryClosed, exit_condition: ExitCondition::OnPrimaryClosed,
close_when_requested: false, close_when_requested: false,
..default() ..default()
}), })
); );
app.add_plugins(bevy_mod_picking::DefaultPickingPlugins); app.add_plugins(bevy_mod_picking::DefaultPickingPlugins);
app.add_plugins(menu::MenuPlugin); app.add_plugins(menu::MenuPlugin);
@ -44,9 +43,8 @@ fn main() {
app.add_plugins(game::GamePlugin); app.add_plugins(game::GamePlugin);
app.add_plugins(editor::EditorPlugin); app.add_plugins(editor::EditorPlugin);
app.add_plugins(camera::CameraPlugin); app.add_plugins(camera::CameraPlugin);
app.add_systems( app.add_systems(Update,
Update, handle_window_close.run_if(on_event::<WindowCloseRequested>())
handle_window_close.run_if(on_event::<WindowCloseRequested>()),
); );
app.run(); app.run();
} }
@ -77,7 +75,10 @@ fn handle_window_close(
if primary.contains(*window) { if primary.contains(*window) {
commands.entity(*window).remove::<Window>(); commands.entity(*window).remove::<Window>();
} else { } else {
secondary.get_mut(*window).iter_mut().for_each(|w| { secondary
.get_mut(*window)
.iter_mut()
.for_each(|w| {
w.visible = false; w.visible = false;
}); });
} }

@ -2,7 +2,6 @@ pub(crate) use std::fmt::Debug;
/// Bevy imports /// Bevy imports
pub(crate) use bevy::ecs::system::EntityCommand; pub(crate) use bevy::ecs::system::EntityCommand;
pub(crate) use bevy::input::common_conditions::input_just_pressed;
pub(crate) use bevy::input::keyboard::KeyboardInput; pub(crate) use bevy::input::keyboard::KeyboardInput;
pub(crate) use bevy::input::ButtonState; pub(crate) use bevy::input::ButtonState;
pub(crate) use bevy::prelude::*; pub(crate) use bevy::prelude::*;
@ -10,14 +9,14 @@ pub(crate) use bevy::render::camera::RenderTarget;
pub(crate) use bevy::render::render_resource::{ pub(crate) use bevy::render::render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
}; };
pub(crate) use bevy::window::ExitCondition;
pub(crate) use bevy::window::WindowRef; pub(crate) use bevy::window::WindowRef;
pub(crate) use bevy::window::{PrimaryWindow, WindowCloseRequested}; pub(crate) use bevy::window::{PrimaryWindow, WindowCloseRequested};
pub(crate) use bevy::window::ExitCondition;
pub(crate) use bevy::input::common_conditions::input_just_pressed;
/// Bevy Plugins /// Bevy Plugins
pub(crate) use bevy_mod_picking::prelude::*; pub(crate) use bevy_mod_picking::prelude::*;
pub(crate) use crate::camera::FlyCamera;
/// Intra-project imports /// Intra-project imports
pub(crate) use crate::ecs::schedule::common_conditions::*; pub(crate) use crate::ecs::schedule::common_conditions::*;
pub(crate) use crate::game::GameChoice; pub(crate) use crate::game::GameChoice;
@ -31,3 +30,4 @@ pub(crate) use crate::ui::style::UiStyle;
pub(crate) use crate::ui::title::UiTitle; pub(crate) use crate::ui::title::UiTitle;
pub(crate) use crate::ui::EmitEvent; pub(crate) use crate::ui::EmitEvent;
pub(crate) use crate::ui::SetState; pub(crate) use crate::ui::SetState;
pub(crate) use crate::camera::FlyCamera;

Loading…
Cancel
Save