Added demo scene with 2d/3d camera controls and gizmos to help with initial setup
parent
97c58f9a6f
commit
05274b68cd
|
|
@ -364,6 +364,7 @@ dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"bevy-inspector-egui",
|
"bevy-inspector-egui",
|
||||||
"bevy_mod_debugdump",
|
"bevy_mod_debugdump",
|
||||||
|
"bevy_prototype_debug_lines",
|
||||||
"bevy_rapier2d",
|
"bevy_rapier2d",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
]
|
]
|
||||||
|
|
@ -833,6 +834,13 @@ dependencies = [
|
||||||
"thread_local",
|
"thread_local",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bevy_prototype_debug_lines"
|
||||||
|
version = "0.11.1"
|
||||||
|
dependencies = [
|
||||||
|
"bevy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bevy_ptr"
|
name = "bevy_ptr"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,9 @@ bevy-inspector-egui = "0.23.2"
|
||||||
bevy_mod_debugdump = "0.10.0"
|
bevy_mod_debugdump = "0.10.0"
|
||||||
bevy_rapier2d = "0.25.0"
|
bevy_rapier2d = "0.25.0"
|
||||||
num-traits = "0.2.18"
|
num-traits = "0.2.18"
|
||||||
|
bevy_prototype_debug_lines = { path = "../bevy_debug_lines", features = [
|
||||||
|
# "3d"
|
||||||
|
] } # NOTE: Local version until registry updates to 0.13
|
||||||
|
|
||||||
# Enable a small amount of optimization in debug mode
|
# Enable a small amount of optimization in debug mode
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,9 @@ impl Plugin for DebugPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.configure_sets(Last, DebugSet.run_if(is_debug_enabled));
|
app.configure_sets(Last, DebugSet.run_if(is_debug_enabled));
|
||||||
|
|
||||||
app.insert_resource(DebugMode::off())
|
app.insert_resource(DebugMode::on())
|
||||||
.add_plugins((
|
.add_plugins((
|
||||||
|
bevy_prototype_debug_lines::DebugLinesPlugin::default(),
|
||||||
bevy_inspector_egui::quick::WorldInspectorPlugin::new().run_if(is_debug_enabled),
|
bevy_inspector_egui::quick::WorldInspectorPlugin::new().run_if(is_debug_enabled),
|
||||||
bevy_rapier2d::prelude::RapierDebugRenderPlugin::default(),
|
bevy_rapier2d::prelude::RapierDebugRenderPlugin::default(),
|
||||||
))
|
))
|
||||||
|
|
@ -34,7 +35,7 @@ impl DebugMode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_debug_enabled(debug_mode: Res<DebugMode>) -> bool {
|
pub fn is_debug_enabled(debug_mode: Res<DebugMode>) -> bool {
|
||||||
debug_mode.enabled
|
debug_mode.enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
132
src/game.rs
132
src/game.rs
|
|
@ -2,15 +2,137 @@ use crate::{debug, game_setup};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub fn init(app: &mut App) {
|
pub fn init(app: &mut App) {
|
||||||
app.add_plugins((
|
let mut app = app.add_plugins((
|
||||||
game_setup::GameSetupPlugin,
|
game_setup::GameSetupPlugin,
|
||||||
bevy_rapier2d::prelude::RapierPhysicsPlugin::<bevy_rapier2d::prelude::NoUserData>::default(
|
bevy_rapier2d::prelude::RapierPhysicsPlugin::<bevy_rapier2d::prelude::NoUserData>::default(
|
||||||
),
|
),
|
||||||
debug::DebugPlugin,
|
debug::DebugPlugin,
|
||||||
))
|
));
|
||||||
.add_systems(Startup, setup);
|
|
||||||
|
app.add_systems(Startup, setup_2d)
|
||||||
|
.add_systems(Update, demo_2d);
|
||||||
|
|
||||||
|
// NOTE: If using 3D, make sure you have '3d' feature enabled for the
|
||||||
|
// 'bevy_prototype_debug_lines' crate
|
||||||
|
// app.add_systems(Startup, setup_3d)
|
||||||
|
// .add_systems(Update, demo_3d);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(mut commands: Commands) {
|
fn setup_2d(mut commands: Commands) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2dBundle {
|
||||||
|
transform: Transform::from_xyz(0.0, 0.0, 100.0),
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn demo_2d(
|
||||||
|
mut camera_query: Query<(&mut Transform, &mut OrthographicProjection)>,
|
||||||
|
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
||||||
|
mut scroll_events: EventReader<bevy::input::mouse::MouseWheel>,
|
||||||
|
mut debug_lines: ResMut<bevy_prototype_debug_lines::DebugLines>,
|
||||||
|
mut debug_shapes: ResMut<bevy_prototype_debug_lines::DebugShapes>,
|
||||||
|
mouse_input: Res<ButtonInput<MouseButton>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
let raw_mouse_motion: Vec2 = mouse_events.read().map(|e| e.delta).sum();
|
||||||
|
let raw_scroll_motion: f32 = scroll_events
|
||||||
|
.read()
|
||||||
|
.map(|event| match event.unit {
|
||||||
|
bevy::input::mouse::MouseScrollUnit::Line => event.y * -0.1,
|
||||||
|
bevy::input::mouse::MouseScrollUnit::Pixel => event.y * -0.05,
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
for (mut transform, mut projection) in camera_query.iter_mut() {
|
||||||
|
projection.scale += raw_scroll_motion * projection.scale;
|
||||||
|
let mouse_motion = raw_mouse_motion * projection.scale * Vec2::new(-1.0, 1.0);
|
||||||
|
if mouse_input.pressed(MouseButton::Middle) {
|
||||||
|
transform.translation += mouse_motion.extend(0.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug_lines.line_colored(Vec3::ZERO, Vec3::X * 50.0, 0.0, Color::RED);
|
||||||
|
debug_lines.line_colored(Vec3::ZERO, Vec3::Y * 50.0, 0.0, Color::GREEN);
|
||||||
|
|
||||||
|
debug_shapes
|
||||||
|
.rect()
|
||||||
|
.size(Vec2::splat(200.0))
|
||||||
|
.rotation(Quat::from_euler(
|
||||||
|
EulerRot::YXZ,
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
time.elapsed_seconds(),
|
||||||
|
))
|
||||||
|
.color(Color::WHITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_3d(mut commands: Commands) {
|
||||||
|
commands.spawn(Camera3dBundle {
|
||||||
|
transform: Transform::from_xyz(0.0, 0.0, 10.0),
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn demo_3d(
|
||||||
|
mut camera_query: Query<&mut Transform>,
|
||||||
|
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
||||||
|
mut debug_lines: ResMut<bevy_prototype_debug_lines::DebugLines>,
|
||||||
|
mut debug_shapes: ResMut<bevy_prototype_debug_lines::DebugShapes>,
|
||||||
|
mouse_input: Res<ButtonInput<MouseButton>>,
|
||||||
|
key_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
let raw_mouse_motion: Vec2 = mouse_events.read().map(|e| e.delta).sum();
|
||||||
|
|
||||||
|
if mouse_input.pressed(MouseButton::Right) {
|
||||||
|
let move_forward = key_input.pressed(KeyCode::KeyW);
|
||||||
|
let move_back = key_input.pressed(KeyCode::KeyS);
|
||||||
|
let move_left = key_input.pressed(KeyCode::KeyA);
|
||||||
|
let move_right = key_input.pressed(KeyCode::KeyD);
|
||||||
|
let move_up = key_input.pressed(KeyCode::Space) || key_input.pressed(KeyCode::KeyE);
|
||||||
|
let move_down = key_input.pressed(KeyCode::ControlLeft) || key_input.pressed(KeyCode::KeyQ);
|
||||||
|
|
||||||
|
let raw_movement = Vec3 {
|
||||||
|
x: match (move_right, move_left) {
|
||||||
|
(true, false) => 1.0,
|
||||||
|
(false, true) => -1.0,
|
||||||
|
_ => 0.0,
|
||||||
|
},
|
||||||
|
y: match (move_up, move_down) {
|
||||||
|
(true, false) => 1.0,
|
||||||
|
(false, true) => -1.0,
|
||||||
|
_ => 0.0,
|
||||||
|
},
|
||||||
|
z: match (move_back, move_forward) {
|
||||||
|
(true, false) => 1.0,
|
||||||
|
(false, true) => -1.0,
|
||||||
|
_ => 0.0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
for mut transform in camera_query.iter_mut() {
|
||||||
|
let mouse_motion = raw_mouse_motion * Vec2::new(-1.0, -1.0) * 0.002;
|
||||||
|
transform.rotate_axis(Vec3::Y, mouse_motion.x);
|
||||||
|
transform.rotate_local_x(mouse_motion.y);
|
||||||
|
|
||||||
|
let local_movement = raw_movement * time.delta_seconds() * 10.0;
|
||||||
|
let movement = transform.rotation * local_movement;
|
||||||
|
transform.translation += movement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debug_lines.line_colored(Vec3::ZERO, Vec3::X, 0.0, Color::RED);
|
||||||
|
debug_lines.line_colored(Vec3::ZERO, Vec3::Y, 0.0, Color::GREEN);
|
||||||
|
debug_lines.line_colored(Vec3::ZERO, Vec3::Z, 0.0, Color::BLUE);
|
||||||
|
|
||||||
|
debug_shapes
|
||||||
|
.cuboid()
|
||||||
|
.size(Vec3::splat(3.0))
|
||||||
|
.rotation(Quat::from_euler(
|
||||||
|
EulerRot::YXZ,
|
||||||
|
time.elapsed_seconds(),
|
||||||
|
0.0,
|
||||||
|
0.0,
|
||||||
|
))
|
||||||
|
.color(Color::WHITE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue