everything is broken
This commit is contained in:
@@ -18,20 +18,22 @@ pub struct PhosCameraPlugin;
|
||||
|
||||
impl Plugin for PhosCameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, setup)
|
||||
.add_systems(Update, (update_camera, grab_mouse, update_camera_mouse));
|
||||
app.add_systems(Startup, setup).add_systems(
|
||||
Update,
|
||||
(grab_mouse, (update_camera, update_camera_mouse).chain()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Camera3dBundle {
|
||||
transform: Transform::from_xyz(-200., 300., -200.)
|
||||
transform: Transform::from_xyz(0., 30., 0.)
|
||||
.looking_at(Vec3::new(1000., 0., 1000.), Vec3::Y),
|
||||
..default()
|
||||
},
|
||||
PhosCamera {
|
||||
speed: 100.,
|
||||
speed: 50.,
|
||||
..default()
|
||||
},
|
||||
));
|
||||
@@ -56,17 +58,18 @@ fn update_camera(
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
move_vec += Vec3::Z;
|
||||
}
|
||||
|
||||
let rot = transform.rotation;
|
||||
move_vec = (rot * move_vec.normalize_or_zero()) * cam.speed * time.delta_seconds();
|
||||
|
||||
if keyboard_input.pressed(KeyCode::ShiftLeft) {
|
||||
move_vec += Vec3::NEG_Y;
|
||||
move_vec += Vec3::from(transform.down());
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::Space) {
|
||||
move_vec += Vec3::Y;
|
||||
move_vec += Vec3::from(transform.up());
|
||||
}
|
||||
if move_vec.length_squared() == 0. {
|
||||
return;
|
||||
}
|
||||
let rot = transform.rotation;
|
||||
transform.translation += (rot * move_vec.normalize()) * cam.speed * time.delta_seconds();
|
||||
|
||||
transform.translation += move_vec.normalize_or_zero() * cam.speed * time.delta_seconds();
|
||||
}
|
||||
|
||||
fn update_camera_mouse(
|
||||
@@ -80,19 +83,25 @@ fn update_camera_mouse(
|
||||
return;
|
||||
}
|
||||
let mut transform = cam_query.single_mut();
|
||||
let cam_rot = mouse_move.read().map(|event| event.delta).sum::<Vec2>() * time.delta_seconds();
|
||||
let window_scale = window.height().min(window.width());
|
||||
|
||||
let (mut pitch, mut yaw, _) = transform.rotation.to_euler(EulerRot::XYZ);
|
||||
for ev in mouse_move.read() {
|
||||
let (mut yaw, mut pitch, _) = transform.rotation.to_euler(EulerRot::YXZ);
|
||||
match window.cursor.grab_mode {
|
||||
CursorGrabMode::None => (),
|
||||
_ => {
|
||||
// Using smallest of height or width ensures equal vertical and horizontal sensitivity
|
||||
pitch -= (ev.delta.y * time.delta_seconds() * 5.).to_radians();
|
||||
yaw -= (ev.delta.x * time.delta_seconds() * 5.).to_radians();
|
||||
}
|
||||
}
|
||||
|
||||
pitch -= cam_rot.y.to_radians();
|
||||
yaw -= cam_rot.x.to_radians();
|
||||
pitch = pitch.clamp(-1.54, 1.54);
|
||||
// if rot_x > PI && cam_rot.x < 2. * PI {
|
||||
// rot_x = PI;
|
||||
// }
|
||||
pitch = pitch.clamp(-1.54, 1.54);
|
||||
|
||||
transform.rotation =
|
||||
Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
|
||||
// Order is important to prevent unintended roll
|
||||
transform.rotation =
|
||||
Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
|
||||
}
|
||||
}
|
||||
|
||||
fn grab_mouse(
|
||||
|
||||
@@ -20,7 +20,6 @@ fn main() {
|
||||
}),
|
||||
..default()
|
||||
}),
|
||||
WireframePlugin,
|
||||
WorldInspectorPlugin::new(),
|
||||
PhosGamePlugin,
|
||||
))
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use bevy::asset::io::memory::Value::Vec;
|
||||
use bevy::pbr::wireframe::{WireframeConfig, WireframePlugin};
|
||||
use bevy::{pbr::CascadeShadowConfig, prelude::*};
|
||||
use camera_system::PhosCameraPlugin;
|
||||
use iyes_perf_ui::prelude::*;
|
||||
use world_generation::hex_utils::offset_to_world;
|
||||
use world_generation::hex_utils::{offset_to_world, HexCoord};
|
||||
use world_generation::{
|
||||
heightmap::generate_heightmap, hex_utils::offset3d_to_world,
|
||||
mesh_generator::generate_chunk_mesh, prelude::*,
|
||||
@@ -13,11 +15,17 @@ impl Plugin for PhosGamePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(PhosCameraPlugin);
|
||||
app.add_systems(Startup, init_game)
|
||||
.add_systems(Startup, create_map);
|
||||
.add_systems(Startup, create_map)
|
||||
.add_systems(Update, draw_gizmos);
|
||||
app.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
|
||||
.add_plugins(bevy::diagnostic::EntityCountDiagnosticsPlugin)
|
||||
.add_plugins(bevy::diagnostic::SystemInformationDiagnosticsPlugin)
|
||||
.add_plugins(PerfUiPlugin);
|
||||
app.add_plugins(WireframePlugin);
|
||||
// app.insert_resource(WireframeConfig {
|
||||
// global: true,
|
||||
// default_color: Color::CYAN,
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,76 +38,103 @@ fn init_game(mut commands: Commands) {
|
||||
|
||||
commands.spawn(DirectionalLightBundle {
|
||||
directional_light: DirectionalLight {
|
||||
shadows_enabled: false,
|
||||
shadows_enabled: true,
|
||||
..default()
|
||||
},
|
||||
cascade_shadow_config: CascadeShadowConfig {
|
||||
bounds: vec![20., 40., 80., 1000., 5000., 19000., 20000.],
|
||||
bounds: vec![500., 1000., 5000., 10000.],
|
||||
..default()
|
||||
},
|
||||
transform: Transform::from_xyz(0.0, 16.0, 5.).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
transform: Transform::from_xyz(500., 160.0, 500.).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
..default()
|
||||
});
|
||||
}
|
||||
|
||||
fn draw_gizmos(mut gizmos: Gizmos, hm: Res<Map>) {
|
||||
gizmos.arrow(Vec3::ZERO, Vec3::Y * 1.5, Color::GREEN);
|
||||
gizmos.arrow(Vec3::ZERO, Vec3::Z * 1.5, Color::BLUE);
|
||||
gizmos.arrow(Vec3::ZERO, Vec3::X * 1.5, Color::RED);
|
||||
|
||||
let ch = &hm.chunks[0];
|
||||
let coord = HexCoord::new(16, 16);
|
||||
let h = ch.points[coord.to_chunk_local_index() as usize];
|
||||
gizmos.ray(coord.to_world(h), Vec3::Y, Color::RED);
|
||||
gizmos.ray(coord.to_world(h), Vec3::Z * 1.5, Color::BLUE);
|
||||
|
||||
// let t = coord.get_neighbor(5);
|
||||
// let h = ch.points[t.to_chunk_local_index() as usize];
|
||||
// gizmos.ray(t.to_world(h), Vec3::Y * 1., Color::PINK);
|
||||
let n = coord.get_neighbors();
|
||||
for i in 0..6 {
|
||||
let t = n[i];
|
||||
let h = ch.points[t.to_chunk_local_index() as usize];
|
||||
gizmos.ray(t.to_world(h), Vec3::Y * (i + 1) as f32, Color::CYAN);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_map(
|
||||
mut commands: Commands,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
) {
|
||||
let heightmap = generate_heightmap(
|
||||
32,
|
||||
32,
|
||||
1,
|
||||
1,
|
||||
&GenerationConfig {
|
||||
layers: vec![GeneratorLayer {
|
||||
base_roughness: 2.14,
|
||||
roughness: 0.87,
|
||||
strength: 2.93,
|
||||
min_value: -0.2,
|
||||
persistence: 0.77,
|
||||
is_rigid: false,
|
||||
weight: 0.,
|
||||
weight_multi: 0.,
|
||||
layers: 4,
|
||||
first_layer_mask: false,
|
||||
},GeneratorLayer {
|
||||
base_roughness: 2.85,
|
||||
roughness: 2.,
|
||||
strength: -0.23,
|
||||
min_value: -0.,
|
||||
persistence: 1.,
|
||||
is_rigid: false,
|
||||
weight: 0.,
|
||||
weight_multi: 0.,
|
||||
layers: 4,
|
||||
first_layer_mask: false,
|
||||
},GeneratorLayer {
|
||||
base_roughness: 2.6,
|
||||
roughness: 4.,
|
||||
strength: 10.44,
|
||||
min_value: 0.,
|
||||
persistence: 1.57,
|
||||
is_rigid: true,
|
||||
weight: 1.,
|
||||
weight_multi: 0.35,
|
||||
layers: 4,
|
||||
first_layer_mask: true,
|
||||
},GeneratorLayer {
|
||||
base_roughness: 3.87,
|
||||
roughness: 5.8,
|
||||
strength: -1.,
|
||||
min_value: 0.,
|
||||
persistence: 0.,
|
||||
is_rigid: true,
|
||||
weight: 1.,
|
||||
weight_multi: 4.57,
|
||||
layers: 3,
|
||||
first_layer_mask: true,
|
||||
}],
|
||||
layers: vec![
|
||||
GeneratorLayer {
|
||||
base_roughness: 2.14,
|
||||
roughness: 0.87,
|
||||
strength: 2.93,
|
||||
min_value: -0.2,
|
||||
persistence: 0.77,
|
||||
is_rigid: false,
|
||||
weight: 0.,
|
||||
weight_multi: 0.,
|
||||
layers: 4,
|
||||
first_layer_mask: false,
|
||||
},
|
||||
GeneratorLayer {
|
||||
base_roughness: 2.85,
|
||||
roughness: 2.,
|
||||
strength: -0.23,
|
||||
min_value: -0.,
|
||||
persistence: 1.,
|
||||
is_rigid: false,
|
||||
weight: 0.,
|
||||
weight_multi: 0.,
|
||||
layers: 4,
|
||||
first_layer_mask: false,
|
||||
},
|
||||
GeneratorLayer {
|
||||
base_roughness: 2.6,
|
||||
roughness: 4.,
|
||||
strength: 10.44,
|
||||
min_value: 0.,
|
||||
persistence: 1.57,
|
||||
is_rigid: true,
|
||||
weight: 1.,
|
||||
weight_multi: 0.35,
|
||||
layers: 4,
|
||||
first_layer_mask: true,
|
||||
},
|
||||
GeneratorLayer {
|
||||
base_roughness: 3.87,
|
||||
roughness: 5.8,
|
||||
strength: -1.,
|
||||
min_value: 0.,
|
||||
persistence: 0.,
|
||||
is_rigid: true,
|
||||
weight: 1.,
|
||||
weight_multi: 4.57,
|
||||
layers: 3,
|
||||
first_layer_mask: true,
|
||||
},
|
||||
],
|
||||
noise_scale: 350.,
|
||||
sea_level: 4.,
|
||||
},
|
||||
1,
|
||||
2,
|
||||
);
|
||||
|
||||
let debug_material = materials.add(StandardMaterial {
|
||||
@@ -107,9 +142,9 @@ fn create_map(
|
||||
..default()
|
||||
});
|
||||
|
||||
for chunk in heightmap.chunks {
|
||||
let mesh = generate_chunk_mesh(&chunk);
|
||||
let pos = offset_to_world(chunk.chunk_offset * Chunk::SIZE as i32);
|
||||
for chunk in &heightmap.chunks {
|
||||
let mesh = generate_chunk_mesh(&chunk, &heightmap);
|
||||
let pos = offset_to_world(chunk.chunk_offset * Chunk::SIZE as i32, 0.);
|
||||
commands.spawn(PbrBundle {
|
||||
mesh: meshes.add(mesh),
|
||||
material: debug_material.clone(),
|
||||
@@ -117,4 +152,6 @@ fn create_map(
|
||||
..default()
|
||||
});
|
||||
}
|
||||
|
||||
commands.insert_resource(heightmap);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user