Merge branch 'master' into avian
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
use avian3d::prelude::*;
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy::{prelude::*, utils::hashbrown::HashSet, window::PrimaryWindow};
|
||||
use shared::{
|
||||
events::{ChunkModifiedEvent, TileModifiedEvent},
|
||||
resources::TileUnderCursor,
|
||||
states::GameplayState,
|
||||
};
|
||||
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
||||
|
||||
use crate::{
|
||||
@@ -11,18 +16,24 @@ 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)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn deform(
|
||||
cam_query: Query<(&GlobalTransform, &Camera), With<PhosCamera>>,
|
||||
mut commands: Commands,
|
||||
window: Query<&Window, With<PrimaryWindow>>,
|
||||
mouse: Res<ButtonInput<MouseButton>>,
|
||||
spatial_query: SpatialQuery,
|
||||
mut heightmap: ResMut<Map>,
|
||||
chunks: Res<PhosChunkRegistry>,
|
||||
tile_under_cursor: Res<TileUnderCursor>,
|
||||
mut chunk_modified: EventWriter<ChunkModifiedEvent>,
|
||||
mut tile_modified: EventWriter<TileModifiedEvent>,
|
||||
) {
|
||||
let mut multi = 0.;
|
||||
if mouse.just_pressed(MouseButton::Left) {
|
||||
@@ -35,36 +46,20 @@ fn deform(
|
||||
return;
|
||||
}
|
||||
|
||||
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 = spatial_query.cast_ray(
|
||||
cam_ray.origin,
|
||||
cam_ray.direction.into(),
|
||||
500.,
|
||||
true,
|
||||
SpatialQueryFilter::default(),
|
||||
);
|
||||
|
||||
if let Some(hit) = collision {
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
#[cfg(feature = "tracing")]
|
||||
let span = info_span!("Deform Mesh").entered();
|
||||
|
||||
let e = hit.entity;
|
||||
let dist = hit.time_of_impact;
|
||||
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.tile, 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user