diff --git a/engine/world_generation/src/map/map.rs b/engine/world_generation/src/map/map.rs index a9ab300..99d189b 100644 --- a/engine/world_generation/src/map/map.rs +++ b/engine/world_generation/src/map/map.rs @@ -141,6 +141,14 @@ impl Map { return Vec3::new(w / 2., self.sealevel, h / 2.); } + pub fn get_center_with_height(&self) -> Vec3 { + let w = self.get_world_width(); + let h = self.get_world_height(); + let mut pos = Vec3::new(w / 2., self.sealevel, h / 2.); + pos.y = self.sample_height(&HexCoord::from_world_pos(pos)); + return pos; + } + pub fn get_world_width(&self) -> f32 { return (self.width * Chunk::SIZE) as f32 * SHORT_DIAGONAL; } diff --git a/engine/world_generation/src/map/mesh_chunk.rs b/engine/world_generation/src/map/mesh_chunk.rs index 0800976..7b6c7f2 100644 --- a/engine/world_generation/src/map/mesh_chunk.rs +++ b/engine/world_generation/src/map/mesh_chunk.rs @@ -1,3 +1,7 @@ +use std::collections::VecDeque; + +use bevy::math::IVec2; + use crate::hex_utils::HexCoord; use super::chunk::Chunk; @@ -42,4 +46,40 @@ impl MeshChunkData { } return (data, has_land); } + + pub fn caluclate_water_distances(data: &mut Vec, height: usize, width: usize, range: usize) { + let mut open: VecDeque<(HexCoord, f32, usize)> = VecDeque::new(); + let mut closed: Vec<(HexCoord, f32)> = Vec::new(); + for z in 0..height { + for x in 0..width { + let chunk = &mut data[z * height + x]; + chunk.prepare_chunk_open(x * Chunk::SIZE, z * Chunk::SIZE, &mut open); + } + } + } + + fn prepare_chunk_open(&mut self, offset_x: usize, offset_z: usize, open: &mut VecDeque<(HexCoord, f32, usize)>) { + for z in 0..Chunk::SIZE { + for x in 0..Chunk::SIZE { + let coord = HexCoord::from_grid_pos(x + offset_x, z + offset_z); + let idx = coord.to_chunk_local_index(); + let h = self.heights[idx]; + self.distance_to_land[idx] = if h > self.sealevel { 0.0 } else { 4.0 }; + if h > self.sealevel { + open.push_back((coord, h, 0)); + } + } + } + } + + fn fill_chunk_borders( + &mut self, + chunks: &Vec, + offset: IVec2, + open: &mut VecDeque<(HexCoord, f32, usize)>, + closed: &mut Vec<(HexCoord, f32)>, + ) { + self.prepare_chunk_open(offset.x as usize * Chunk::SIZE, offset.y as usize * Chunk::SIZE, open); + todo!("Fill closed list with bordering tiles") + } } diff --git a/game/main/src/camera_system/camera_plugin.rs b/game/main/src/camera_system/camera_plugin.rs index 549e2fe..3fed000 100644 --- a/game/main/src/camera_system/camera_plugin.rs +++ b/game/main/src/camera_system/camera_plugin.rs @@ -2,11 +2,11 @@ use bevy::core_pipeline::experimental::taa::{TemporalAntiAliasBundle, TemporalAn use bevy::core_pipeline::prepass::DepthPrepass; use bevy::input::mouse::{MouseMotion, MouseScrollUnit, MouseWheel}; use bevy::prelude::*; -use bevy::window::CursorGrabMode; -use shared::states::MenuState; +use shared::sets::GameplaySet; use shared::tags::MainCamera; use world_generation::hex_utils::HexCoord; use world_generation::prelude::Map; +use world_generation::states::GeneratorState; use super::components::*; @@ -15,11 +15,15 @@ pub struct PhosCameraPlugin; impl Plugin for PhosCameraPlugin { fn build(&self, app: &mut App) { app.register_type::(); + app.register_type::(); app.add_systems(PreStartup, setup); - app.add_systems(Update, rts_camera_system.run_if(in_state(MenuState::InGame))); - app.add_systems(PostUpdate, limit_camera_bounds.run_if(in_state(MenuState::InGame))); + // app.add_systems(Update, rts_camera_system.in_set(GameplaySet)); + // app.add_systems(PostUpdate, limit_camera_bounds.in_set(GameplaySet)); + app.add_systems(Update, orbit_camera_upate.in_set(GameplaySet)); + + app.add_systems(Update, init_bounds.run_if(in_state(GeneratorState::SpawnMap))); //Free Cam //app.add_systems(Update, (grab_mouse, (update_camera, update_camera_mouse).chain())); @@ -27,6 +31,22 @@ impl Plugin for PhosCameraPlugin { } } +fn init_bounds( + mut commands: Commands, + mut cam: Query<(&mut Transform, Entity), With>, + heightmap: Res, +) { + let (mut cam_t, cam_entity) = cam.single_mut(); + cam_t.translation = heightmap.get_center(); + commands + .entity(cam_entity) + .insert(CameraBounds::from_size(heightmap.get_world_size())) + .insert(PhosOrbitCamera { + target: heightmap.get_center_with_height(), + ..Default::default() + }); +} + fn setup(mut commands: Commands, mut msaa: ResMut) { commands .spawn(( @@ -37,93 +57,108 @@ fn setup(mut commands: Commands, mut msaa: ResMut) { PhosCamera::default(), MainCamera, DepthPrepass, - PhosCameraTargets::default(), + PhosOrbitCamera::default(), )) .insert(TemporalAntiAliasBundle::default()); *msaa = Msaa::Off; } -fn update_camera( - mut cam_query: Query<(&PhosCamera, &mut Transform)>, - keyboard_input: Res>, + +fn orbit_camera_upate( + mut cam_query: Query<(&mut Transform, &PhosCamera, &mut PhosOrbitCamera, &CameraBounds)>, + mut wheel: EventReader, + mut mouse_motion: EventReader, + mouse: Res>, + key: Res>, time: Res