From ed94f77323183a091e4fdabf53b002b6b44d81ad Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Thu, 2 May 2024 22:11:40 -0400 Subject: [PATCH] rts camera center camera spawn basic water fix generator config set camera bounds --- engine/world_generation/src/heightmap.rs | 14 ++----- engine/world_generation/src/lib.rs | 22 +++++++++- game/camera_system/src/lib.rs | 52 ++++++++++++++++++++---- game/camera_system/src/prelude.rs | 32 ++++++++++++++- game/main/src/map_init.rs | 43 +++++++++++++------- 5 files changed, 127 insertions(+), 36 deletions(-) diff --git a/engine/world_generation/src/heightmap.rs b/engine/world_generation/src/heightmap.rs index 16b95e2..7e56568 100644 --- a/engine/world_generation/src/heightmap.rs +++ b/engine/world_generation/src/heightmap.rs @@ -19,6 +19,7 @@ pub fn generate_heightmap(cfg: &GenerationConfig, seed: u32) -> Map { chunks, height: cfg.size.y as usize, width: cfg.size.x as usize, + sea_level: cfg.sea_level as f32, }; } @@ -40,12 +41,8 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed: (x as f64 + chunk_x * Chunk::SIZE as f64) / &cfg.noise_scale, (z as f64 + chunk_z * Chunk::SIZE as f64) / &cfg.noise_scale, ]) as f32; - temp[x + z * Chunk::SIZE] = sample_tempurature( - z as f32 + chunk_z as f32 * Chunk::SIZE as f32, - sample, - &cfg, - 100., - ); + temp[x + z * Chunk::SIZE] = + sample_tempurature(z as f32 + chunk_z as f32 * Chunk::SIZE as f32, sample, &cfg, 100.); } } return Chunk { @@ -95,10 +92,7 @@ fn sample_point(x: f64, z: f64, cfg: &GenerationConfig, noise: &impl NoiseFn, pub height: usize, pub width: usize, + pub sea_level: f32, } impl Map { @@ -105,7 +106,7 @@ pub mod prelude { return results; } - pub fn get_height(&self, pos: &HexCoord) -> f32 { + pub fn sample_height(&self, pos: &HexCoord) -> f32 { let chunk = &self.chunks[pos.to_chunk_index(self.width)]; return chunk.heights[pos.to_chunk_local_index()]; } @@ -119,6 +120,23 @@ pub mod prelude { let chunk = &self.chunks[pos.to_chunk_index(self.width)]; return chunk.temperature[pos.to_chunk_local_index()]; } + + pub fn get_center(&self) -> Vec3 { + let w = self.width * Chunk::SIZE; + let h = self.height * Chunk::SIZE; + return Vec3::new( + tile_to_world_distance(w as i32 / 2), + self.sea_level, + tile_to_world_distance(h as i32 / 2), + ); + } + + pub fn get_world_width(&self) -> f32 { + return tile_to_world_distance((self.width * Chunk::SIZE) as i32); + } + pub fn get_world_height(&self) -> f32 { + return tile_to_world_distance((self.height * Chunk::SIZE) as i32); + } } pub const ATTRIBUTE_PACKED_VERTEX_DATA: MeshVertexAttribute = MeshVertexAttribute::new("PackedVertexData", 988540817, VertexFormat::Uint32); diff --git a/game/camera_system/src/lib.rs b/game/camera_system/src/lib.rs index 4cb20f3..97a3eff 100644 --- a/game/camera_system/src/lib.rs +++ b/game/camera_system/src/lib.rs @@ -1,9 +1,10 @@ use crate::prelude::PhosCamera; use bevy::core_pipeline::experimental::taa::{TemporalAntiAliasBundle, TemporalAntiAliasPlugin}; -use bevy::input::mouse::MouseMotion; +use bevy::input::mouse::{MouseMotion, MouseScrollUnit, MouseWheel}; use bevy::pbr::ScreenSpaceAmbientOcclusionBundle; use bevy::prelude::*; use bevy::window::CursorGrabMode; +use prelude::{CameraBounds, PhosCameraTargets}; pub mod prelude; @@ -11,8 +12,12 @@ pub struct PhosCameraPlugin; impl Plugin for PhosCameraPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, setup) - .add_systems(Update, (grab_mouse, (update_camera, update_camera_mouse).chain())); + app.add_systems(PreStartup, setup); + + app.add_systems(Update, (rts_camera_system, apply_camera_height).chain()); + app.add_systems(PostUpdate, limit_camera_bounds); + //Free Cam + //app.add_systems(Update, (grab_mouse, (update_camera, update_camera_mouse).chain())); app.add_plugins(TemporalAntiAliasPlugin); } @@ -22,13 +27,16 @@ fn setup(mut commands: Commands, mut msaa: ResMut) { commands .spawn(( Camera3dBundle { - transform: Transform::from_xyz(0., 30., 0.).looking_at(Vec3::new(1000., 0., 1000.), Vec3::Y), + transform: Transform::from_xyz(0., 30., 0.) + .with_rotation(Quat::from_axis_angle(Vec3::Y, (-90 as f32).to_radians())), ..default() }, PhosCamera { speed: 100., + zoom_speed: 20., ..default() }, + PhosCameraTargets::default(), )) .insert(ScreenSpaceAmbientOcclusionBundle::default()) .insert(TemporalAntiAliasBundle::default()); @@ -119,17 +127,18 @@ fn grab_mouse(mut windows: Query<&mut Window>, mouse: Res, + mut cam_query: Query<(&mut Transform, &PhosCamera, &mut PhosCameraTargets)>, key: Res>, time: Res