137 lines
4.8 KiB
Rust
137 lines
4.8 KiB
Rust
pub use bevy::prelude::*;
|
|
|
|
use crate::game::prefab::{DemoCamera2d, DemoCamera3d};
|
|
|
|
pub fn setup_demo_2d(mut commands: Commands) {
|
|
commands.spawn((
|
|
Name::from("2D Demo Scene"),
|
|
Transform::default(),
|
|
Visibility::default(),
|
|
children![
|
|
DemoCamera2d,
|
|
(Name::from("2D sprite"), Sprite::sized(Vec2::splat(256.0)),)
|
|
],
|
|
));
|
|
}
|
|
|
|
pub fn demo_2d(
|
|
mut camera_query: Query<(&mut Transform, &mut Projection)>,
|
|
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
|
mut scroll_events: EventReader<bevy::input::mouse::MouseWheel>,
|
|
mouse_input: Res<ButtonInput<MouseButton>>,
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
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() {
|
|
let projection = match projection.as_mut() {
|
|
Projection::Orthographic(projection) => projection,
|
|
_ => continue,
|
|
};
|
|
if keyboard_input.pressed(KeyCode::ControlLeft) {
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn setup_demo_3d(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
commands.spawn((
|
|
Name::from("3D Demo Scene"),
|
|
Transform::from_xyz(0., 300.0, 0.),
|
|
Visibility::default(),
|
|
children![
|
|
(
|
|
DemoCamera3d,
|
|
Transform::from_xyz(-6.225, 2.197, -10.470).looking_at(Vec3::ZERO, Dir3::Y),
|
|
),
|
|
(
|
|
Name::from("Cuboid"),
|
|
Transform::from_xyz(0., 0.5, 0.),
|
|
Mesh3d(meshes.add(Cuboid::from_length(1.0))),
|
|
MeshMaterial3d(materials.add(Color::from(bevy::color::palettes::css::GREY))),
|
|
),
|
|
(
|
|
Name::from("Floor"),
|
|
Transform::from_xyz(0., -0.25, 0.),
|
|
Mesh3d(meshes.add(Cuboid::from_size(Vec3::new(10., 0.5, 10.)))),
|
|
MeshMaterial3d(materials.add(Color::from(bevy::color::palettes::css::GREY))),
|
|
),
|
|
(
|
|
Name::from("Sun"),
|
|
Transform::from_rotation(Quat::from_euler(
|
|
EulerRot::default(),
|
|
f32::to_radians(160.0),
|
|
f32::to_radians(-20.0),
|
|
0.0,
|
|
)),
|
|
DirectionalLight {
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
),
|
|
],
|
|
));
|
|
}
|
|
|
|
pub fn demo_3d(
|
|
mut camera_query: Query<&mut Transform, With<Camera3d>>,
|
|
mut mouse_events: EventReader<bevy::input::mouse::MouseMotion>,
|
|
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(Dir3::Y, mouse_motion.x);
|
|
transform.rotate_local_x(mouse_motion.y);
|
|
|
|
let local_movement = raw_movement * time.delta_secs() * 10.0;
|
|
let movement = transform.rotation * local_movement;
|
|
transform.translation += movement;
|
|
}
|
|
}
|
|
}
|