From a8409e5720c7149b1432ab9d5e7346515e90aeff Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Tue, 6 Aug 2024 20:49:47 -0400 Subject: [PATCH] finally realized that bevy uses right handed coords --- engine/world_generation/src/heightmap.rs | 1 - engine/world_generation/src/hex_utils.rs | 8 +++--- engine/world_generation/src/map/mesh_chunk.rs | 2 +- game/buildings/src/building_plugin.rs | 3 +++ game/main/src/camera_system/camera_plugin.rs | 19 +++++++------ game/main/src/camera_system/components.rs | 2 +- game/main/src/map_rendering/map_init.rs | 2 +- .../src/map_rendering/terraforming_test.rs | 3 +++ game/main/src/utlis/debug_plugin.rs | 27 ++++++++++++------- 9 files changed, 42 insertions(+), 25 deletions(-) diff --git a/engine/world_generation/src/heightmap.rs b/engine/world_generation/src/heightmap.rs index 49e9458..4312ba6 100644 --- a/engine/world_generation/src/heightmap.rs +++ b/engine/world_generation/src/heightmap.rs @@ -2,7 +2,6 @@ use core::f32; use bevy::math::{IVec2, UVec2}; use bevy::prelude::{FloatExt, Vec2}; -use bevy::render::render_resource::encase::internal::BufferMut; use bevy::utils::default; use noise::{NoiseFn, SuperSimplex}; use rayon::iter::{IntoParallelIterator, ParallelIterator}; diff --git a/engine/world_generation/src/hex_utils.rs b/engine/world_generation/src/hex_utils.rs index 5cacc23..ba20e2c 100644 --- a/engine/world_generation/src/hex_utils.rs +++ b/engine/world_generation/src/hex_utils.rs @@ -72,12 +72,12 @@ impl Display for HexCoord { impl HexCoord { pub const DIRECTIONS: [IVec3; 6] = [ - IVec3::new(1, -1, 0), - IVec3::new(1, 0, -1), IVec3::new(0, 1, -1), - IVec3::new(-1, 1, 0), - IVec3::new(-1, 0, 1), + IVec3::new(1, 0, -1), + IVec3::new(1, -1, 0), IVec3::new(0, -1, 1), + IVec3::new(-1, 0, 1), + IVec3::new(-1, 1, 0), ]; pub const ZERO: HexCoord = HexCoord { hex: IVec3::ZERO }; diff --git a/engine/world_generation/src/map/mesh_chunk.rs b/engine/world_generation/src/map/mesh_chunk.rs index be87f78..3a09423 100644 --- a/engine/world_generation/src/map/mesh_chunk.rs +++ b/engine/world_generation/src/map/mesh_chunk.rs @@ -11,7 +11,7 @@ impl MeshChunkData { pub fn get_neighbors(&self, coord: &HexCoord) -> [f32; 6] { let mut data = [0.; 6]; let n_tiles = coord.get_neighbors(); - for i in 6..0 { + for i in 0..6 { let n = n_tiles[i]; if !n.is_in_bounds(Chunk::SIZE, Chunk::SIZE) { continue; diff --git a/game/buildings/src/building_plugin.rs b/game/buildings/src/building_plugin.rs index 9ca45d9..cd18a07 100644 --- a/game/buildings/src/building_plugin.rs +++ b/game/buildings/src/building_plugin.rs @@ -106,6 +106,9 @@ fn hq_placement( if let Some((_e, dist)) = collision { let contact_point = cam_ray.get_point(dist); let contact_coord = HexCoord::from_world_pos(contact_point); + if !map.is_in_bounds(&contact_coord) { + return; + } let positions = map.hex_select(&contact_coord, 3, true, |pos, h, _| pos.to_world(h)); show_indicators(positions, &mut commands, &indicator); diff --git a/game/main/src/camera_system/camera_plugin.rs b/game/main/src/camera_system/camera_plugin.rs index 9a947ab..43b08bf 100644 --- a/game/main/src/camera_system/camera_plugin.rs +++ b/game/main/src/camera_system/camera_plugin.rs @@ -31,7 +31,7 @@ fn setup(mut commands: Commands, mut msaa: ResMut) { commands .spawn(( Camera3dBundle { - transform: Transform::from_xyz(0., 30., 0.).looking_to(Vec3::Z, Vec3::Y), + transform: Transform::from_xyz(0., 30., 0.).looking_to(Vec3::NEG_Z, Vec3::Y), ..default() }, PhosCamera::default(), @@ -138,15 +138,15 @@ fn rts_camera_system( let mut cam_pos = cam.translation; if key.pressed(KeyCode::KeyA) { - cam_move.x = 1.; - } else if key.pressed(KeyCode::KeyD) { cam_move.x = -1.; + } else if key.pressed(KeyCode::KeyD) { + cam_move.x = 1.; } if key.pressed(KeyCode::KeyW) { - cam_move.z = 1.; - } else if key.pressed(KeyCode::KeyS) { cam_move.z = -1.; + } else if key.pressed(KeyCode::KeyS) { + cam_move.z = 1.; } let move_speed = if key.pressed(KeyCode::ShiftLeft) { @@ -156,7 +156,7 @@ fn rts_camera_system( }; cam_move = cam_move.normalize_or_zero() * move_speed * time.delta_seconds(); - cam_pos -= cam_move; + cam_pos += cam_move; let mut scroll = 0.0; for e in wheel.read() { @@ -209,8 +209,11 @@ fn rts_camera_system( cam_targets.rotate_time = cam_targets.rotate_time.min(1.); } let angle = cam_cfg.min_angle.lerp(cam_cfg.max_angle, t); - let rot = Quat::from_axis_angle(Vec3::X, -angle); - cam.rotation = rot; + let mut rot = cam.rotation.to_euler(EulerRot::XYZ); + rot.0 = -angle; + cam.rotation = Quat::from_euler(EulerRot::XYZ, rot.0, rot.1, rot.2); + // let rot = Quat::from_axis_angle(Vec3::X, -angle); + // cam.rotation = rot; cam.translation = cam_pos; } diff --git a/game/main/src/camera_system/components.rs b/game/main/src/camera_system/components.rs index ec83e77..28510d2 100644 --- a/game/main/src/camera_system/components.rs +++ b/game/main/src/camera_system/components.rs @@ -38,7 +38,7 @@ impl Default for PhosCameraTargets { fn default() -> Self { Self { height: Default::default(), - forward: Vec3::Z, + forward: Vec3::NEG_Z, last_height: Default::default(), anim_time: Default::default(), rotate_time: Default::default(), diff --git a/game/main/src/map_rendering/map_init.rs b/game/main/src/map_rendering/map_init.rs index 0a7fcd8..07b2606 100644 --- a/game/main/src/map_rendering/map_init.rs +++ b/game/main/src/map_rendering/map_init.rs @@ -55,7 +55,7 @@ impl Plugin for MapInitPlugin { app.register_asset_reflect::>(); app.add_plugins(( ChunkRebuildPlugin, - TerraFormingTestPlugin, + // TerraFormingTestPlugin, MaterialPlugin::>::default(), MaterialPlugin::> { prepass_enabled: false, diff --git a/game/main/src/map_rendering/terraforming_test.rs b/game/main/src/map_rendering/terraforming_test.rs index 382a804..53edbcd 100644 --- a/game/main/src/map_rendering/terraforming_test.rs +++ b/game/main/src/map_rendering/terraforming_test.rs @@ -69,6 +69,9 @@ fn deform( let span = info_span!("Deform Mesh").entered(); let contact_point = cam_ray.get_point(dist); let contact_coord = HexCoord::from_world_pos(contact_point); + if !heightmap.is_in_bounds(&contact_coord) { + return; + } let modified_tiles = heightmap.create_crater(&contact_coord, 5, 5. * multi); let mut chunk_set: HashSet = HashSet::new(); for (tile, height) in modified_tiles { diff --git a/game/main/src/utlis/debug_plugin.rs b/game/main/src/utlis/debug_plugin.rs index 1558b1e..9437b75 100644 --- a/game/main/src/utlis/debug_plugin.rs +++ b/game/main/src/utlis/debug_plugin.rs @@ -81,25 +81,34 @@ fn show_tile_heights( if let Some((_e, dist)) = collision { let contact_point = cam_ray.get_point(dist); let contact_coord = HexCoord::from_world_pos(contact_point); - if map.is_in_bounds(&contact_coord) { - let height = map.sample_height(&contact_coord); - gizmos.primitive_3d( - &shape.0, - contact_coord.to_world(height + 0.01), - Quat::IDENTITY, - Color::WHITE, - ); + if !map.is_in_bounds(&contact_coord) { + return; } + let height = map.sample_height(&contact_coord); + gizmos.primitive_3d( + &shape.0, + contact_coord.to_world(height + 0.01), + Quat::IDENTITY, + Color::WHITE, + ); let nbors = map.get_neighbors(&contact_coord); + let contact_tile_pos = contact_coord.to_world(map.sample_height(&contact_coord)); + for i in 0..6 { if let Some(s) = nbors[i] { let coord = contact_coord.get_neighbor(i); let p = coord.to_world(s); gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), Color::WHITE); } + + let p = HEX_CORNERS[i] + contact_tile_pos; + gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), LinearRgba::rgb(1.0, 0.0, 0.5)); } - gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, Srgba::rgb(1., 0., 0.5)); + gizmos.line(contact_point, contact_point + Vec3::X, LinearRgba::RED); + gizmos.line(contact_point, contact_point + Vec3::Y, LinearRgba::GREEN); + gizmos.line(contact_point, contact_point + Vec3::Z, LinearRgba::BLUE); + //gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, LinearRgba::rgb(1., 0., 0.5)); } }