Buildings Processing

Tile/Chunk updates events
This commit is contained in:
2024-08-03 18:48:47 -04:00
parent 59090fd3fb
commit bde25435ec
18 changed files with 318 additions and 52 deletions

View File

@@ -4,6 +4,8 @@ use bevy::tasks::*;
use bevy::utils::futures;
use bevy_rapier3d::geometry::Collider;
use bevy_rapier3d::geometry::TriMeshFlags;
use shared::events::ChunkModifiedEvent;
use shared::events::TileModifiedEvent;
use world_generation::prelude::Map;
use world_generation::states::GeneratorState;
@@ -17,18 +19,14 @@ pub struct ChunkRebuildPlugin;
impl Plugin for ChunkRebuildPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(ChunkRebuildQueue::default());
app.init_resource::<PhosChunkRegistry>();
app.add_systems(PreUpdate, chunk_rebuilder.run_if(in_state(GeneratorState::SpawnMap)));
app.add_event::<ChunkModifiedEvent>();
app.add_event::<TileModifiedEvent>();
app.add_systems(PreUpdate, chunk_rebuilder.run_if(in_state(GeneratorState::Idle)));
app.add_systems(PostUpdate, collider_task_resolver);
}
}
#[derive(Resource, Default)]
pub struct ChunkRebuildQueue {
pub queue: Vec<usize>,
}
fn chunk_rebuilder(
mut commands: Commands,
chunk_query: Query<(Entity, &PhosChunk), (With<RebuildChunk>, Without<ChunkRebuildTask>)>,
@@ -39,7 +37,7 @@ fn chunk_rebuilder(
for (chunk_entity, idx) in &chunk_query {
#[cfg(feature = "tracing")]
let _spawn_span = info_span!("Rebuild Chunk").entered();
println!("Rebuilding Chunk");
let chunk_index = idx.index;
let chunk_data = heightmap.get_chunk_mesh_data(chunk_index);
let chunk_offset = heightmap.chunks[chunk_index].chunk_offset;

View File

@@ -1,5 +1,9 @@
use bevy::{prelude::*, window::PrimaryWindow};
use bevy::{prelude::*, utils::hashbrown::HashSet, window::PrimaryWindow};
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
use shared::{
events::{ChunkModifiedEvent, TileModifiedEvent},
states::GameplayState,
};
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
use crate::{
@@ -11,7 +15,12 @@ pub struct TerraFormingTestPlugin;
impl Plugin for TerraFormingTestPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, deform.run_if(in_state(GeneratorState::Idle)));
app.add_systems(
Update,
deform
.run_if(in_state(GeneratorState::Idle))
.run_if(in_state(GameplayState::Playing)),
);
}
}
@@ -23,6 +32,8 @@ fn deform(
rapier_context: Res<RapierContext>,
mut heightmap: ResMut<Map>,
chunks: Res<PhosChunkRegistry>,
mut chunk_modified: EventWriter<ChunkModifiedEvent>,
mut tile_modified: EventWriter<TileModifiedEvent>,
) {
let mut multi = 0.;
if mouse.just_pressed(MouseButton::Left) {
@@ -53,15 +64,22 @@ fn deform(
QueryFilter::only_fixed(),
);
if let Some((e, dist)) = collision {
if let Some((_, dist)) = collision {
#[cfg(feature = "tracing")]
let span = info_span!("Deform Mesh").entered();
let contact_point = cam_ray.get_point(dist);
let contact_coord = HexCoord::from_world_pos(contact_point);
let modified_chunks = heightmap.create_crater(&contact_coord, 5, 5. * multi);
for c in modified_chunks {
commands.entity(chunks.chunks[c]).insert(RebuildChunk);
let modified_tiles = heightmap.create_crater(&contact_coord, 5, 5. * multi);
let mut chunk_set: HashSet<usize> = HashSet::new();
for (tile, height) in modified_tiles {
let chunk = tile.to_chunk_index(heightmap.width);
if !chunk_set.contains(&chunk) {
chunk_modified.send(ChunkModifiedEvent { index: chunk });
chunk_set.insert(chunk);
commands.entity(chunks.chunks[chunk]).insert(RebuildChunk);
}
tile_modified.send(TileModifiedEvent::HeightChanged(tile, height));
}
commands.entity(e).insert(RebuildChunk);
// commands.entity(e).insert(RebuildChunk);
}
}

View File

@@ -1,7 +1,7 @@
use crate::camera_system::camera_plugin::PhosCameraPlugin;
use crate::camera_system::components::PhosCamera;
use crate::map_rendering::map_init::MapInitPlugin;
use crate::utlis::render_distance_system::RenderDistancePlugin;
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
use bevy::{
pbr::{wireframe::WireframeConfig, CascadeShadowConfig},
prelude::*,
@@ -33,6 +33,8 @@ impl Plugin for PhosGamePlugin {
RenderDistancePlugin,
BuildingPugin,
DespawnPuglin,
#[cfg(debug_assertions)]
DebugPlugin,
));
//Systems - Startup

View File

@@ -0,0 +1,97 @@
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_inspector_egui::bevy_egui::EguiContexts;
use bevy_inspector_egui::egui;
use bevy_rapier3d::prelude::*;
use shared::tags::MainCamera;
use world_generation::{
consts::HEX_CORNERS,
hex_utils::{HexCoord, INNER_RADIUS},
prelude::Map,
states::GeneratorState,
};
pub struct DebugPlugin;
impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) {
app.insert_state(DebugState::Base);
app.add_systems(
Update,
show_tile_heights
.run_if(in_state(GeneratorState::Idle))
.run_if(not(in_state(DebugState::None))),
);
app.add_systems(
Update,
verbose_data
.run_if(in_state(GeneratorState::Idle))
.run_if(in_state(DebugState::Verbose)),
);
app.insert_resource(Shape(Polyline3d::new([
HEX_CORNERS[0],
HEX_CORNERS[1],
HEX_CORNERS[2],
HEX_CORNERS[3],
HEX_CORNERS[4],
HEX_CORNERS[5],
HEX_CORNERS[0],
])));
}
}
#[derive(Resource)]
struct Shape(pub Polyline3d<7>);
#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
pub enum DebugState {
Base,
None,
Verbose,
}
fn show_tile_heights(
cam_query: Query<(&GlobalTransform, &Camera), With<MainCamera>>,
window: Query<&Window, With<PrimaryWindow>>,
rapier_context: Res<RapierContext>,
map: Res<Map>,
mut gizmos: Gizmos,
shape: Res<Shape>,
) {
let win = window.single();
let (cam_transform, camera) = cam_query.single();
let Some(cursor_pos) = win.cursor_position() else {
return;
};
let Some(cam_ray) = camera.viewport_to_world(cam_transform, cursor_pos) else {
return;
};
let collision = rapier_context.cast_ray(
cam_ray.origin,
cam_ray.direction.into(),
500.,
true,
QueryFilter::only_fixed(),
);
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,
);
}
gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, Srgba::rgb(1., 0., 0.5));
}
}
fn verbose_data() {}

View File

@@ -1,2 +1,3 @@
pub mod chunk_utils;
pub mod render_distance_system;
pub mod debug_plugin;