units testing
This commit is contained in:
@@ -107,6 +107,7 @@ fn setup_materials(
|
||||
base: StandardMaterial {
|
||||
base_color: Color::srgb(0., 0.878, 1.),
|
||||
alpha_mode: AlphaMode::Blend,
|
||||
metallic: 1.0,
|
||||
..Default::default()
|
||||
},
|
||||
extension: WaterMaterial {
|
||||
|
||||
@@ -2,6 +2,7 @@ use bevy::{prelude::*, utils::hashbrown::HashSet, window::PrimaryWindow};
|
||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
||||
use shared::{
|
||||
events::{ChunkModifiedEvent, TileModifiedEvent},
|
||||
resources::TileUnderCursor,
|
||||
states::GameplayState,
|
||||
};
|
||||
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
||||
@@ -25,13 +26,11 @@ impl Plugin for TerraFormingTestPlugin {
|
||||
}
|
||||
|
||||
fn deform(
|
||||
cam_query: Query<(&GlobalTransform, &Camera), With<PhosCamera>>,
|
||||
mut commands: Commands,
|
||||
window: Query<&Window, With<PrimaryWindow>>,
|
||||
mouse: Res<ButtonInput<MouseButton>>,
|
||||
rapier_context: Res<RapierContext>,
|
||||
mut heightmap: ResMut<Map>,
|
||||
chunks: Res<PhosChunkRegistry>,
|
||||
tile_under_cursor: Res<TileUnderCursor>,
|
||||
mut chunk_modified: EventWriter<ChunkModifiedEvent>,
|
||||
mut tile_modified: EventWriter<TileModifiedEvent>,
|
||||
) {
|
||||
@@ -46,33 +45,10 @@ 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 = rapier_context.cast_ray(
|
||||
cam_ray.origin,
|
||||
cam_ray.direction.into(),
|
||||
500.,
|
||||
true,
|
||||
QueryFilter::only_fixed(),
|
||||
);
|
||||
|
||||
if let Some((_, dist)) = collision {
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
#[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);
|
||||
if !heightmap.is_in_bounds(&contact_coord) {
|
||||
return;
|
||||
}
|
||||
let modified_tiles = heightmap.create_crater(&contact_coord, 5, 5. * multi);
|
||||
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);
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::camera_system::components::PhosCamera;
|
||||
use crate::map_rendering::map_init::MapInitPlugin;
|
||||
use crate::utlis::editor_plugin::EditorPlugin;
|
||||
use crate::utlis::render_distance_system::RenderDistancePlugin;
|
||||
use crate::utlis::tile_selection_plugin::TileSelectionPlugin;
|
||||
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
|
||||
use bevy::{
|
||||
pbr::{wireframe::WireframeConfig, CascadeShadowConfig},
|
||||
@@ -13,8 +14,11 @@ use bevy_rapier3d::geometry::Collider;
|
||||
use bevy_rapier3d::plugin::{NoUserData, RapierPhysicsPlugin};
|
||||
use buildings::BuildingPugin;
|
||||
use iyes_perf_ui::prelude::*;
|
||||
use shared::sets::GameplaySet;
|
||||
use shared::states::{GameplayState, MenuState};
|
||||
use shared::{despawn::DespawnPuglin, states::AssetLoadState};
|
||||
use units::units_plugin::UnitsPlugin;
|
||||
use world_generation::states::GeneratorState;
|
||||
|
||||
pub struct PhosGamePlugin;
|
||||
|
||||
@@ -33,13 +37,17 @@ impl Plugin for PhosGamePlugin {
|
||||
MapInitPlugin,
|
||||
RenderDistancePlugin,
|
||||
BuildingPugin,
|
||||
UnitsPlugin,
|
||||
DespawnPuglin,
|
||||
TileSelectionPlugin,
|
||||
#[cfg(debug_assertions)]
|
||||
EditorPlugin,
|
||||
#[cfg(debug_assertions)]
|
||||
DebugPlugin,
|
||||
));
|
||||
|
||||
configure_gameplay_set(app);
|
||||
|
||||
//Systems - Startup
|
||||
app.add_systems(Startup, init_game);
|
||||
|
||||
@@ -63,6 +71,34 @@ impl Plugin for PhosGamePlugin {
|
||||
}
|
||||
}
|
||||
|
||||
fn configure_gameplay_set(app: &mut App) {
|
||||
app.configure_sets(
|
||||
Update,
|
||||
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||
);
|
||||
app.configure_sets(
|
||||
PreUpdate,
|
||||
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||
);
|
||||
app.configure_sets(
|
||||
PostUpdate,
|
||||
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||
);
|
||||
|
||||
app.configure_sets(
|
||||
FixedUpdate,
|
||||
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||
);
|
||||
app.configure_sets(
|
||||
FixedPreUpdate,
|
||||
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||
);
|
||||
app.configure_sets(
|
||||
FixedPostUpdate,
|
||||
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||
);
|
||||
}
|
||||
|
||||
fn init_game(mut commands: Commands, mut materials: ResMut<Assets<StandardMaterial>>) {
|
||||
commands.spawn((
|
||||
PerfUiRoot::default(),
|
||||
|
||||
@@ -2,6 +2,7 @@ use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy_inspector_egui::bevy_egui::{systems::InputEvents, EguiContexts};
|
||||
use bevy_inspector_egui::egui;
|
||||
use bevy_rapier3d::prelude::*;
|
||||
use shared::resources::TileUnderCursor;
|
||||
use shared::states::GameplayState;
|
||||
use shared::tags::MainCamera;
|
||||
use world_generation::{
|
||||
@@ -66,62 +67,32 @@ fn regenerate_map(
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
let height = map.sample_height(&contact_coord);
|
||||
fn show_tile_heights(map: Res<Map>, mut gizmos: Gizmos, shape: Res<Shape>, tile_under_cursor: Res<TileUnderCursor>) {
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
let height = map.sample_height(&contact.tile);
|
||||
gizmos.primitive_3d(
|
||||
&shape.0,
|
||||
contact_coord.to_world(height + 0.01),
|
||||
contact.tile.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));
|
||||
let nbors = map.get_neighbors(&contact.tile);
|
||||
let contact_tile_pos = contact.tile.to_world(map.sample_height(&contact.tile));
|
||||
|
||||
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);
|
||||
}
|
||||
// for i in 0..6 {
|
||||
// if let Some(s) = nbors[i] {
|
||||
// let coord = contact.tile.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));
|
||||
}
|
||||
// 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.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.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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,3 +2,4 @@ pub mod chunk_utils;
|
||||
pub mod render_distance_system;
|
||||
pub mod debug_plugin;
|
||||
pub mod editor_plugin;
|
||||
pub mod tile_selection_plugin;
|
||||
|
||||
62
game/main/src/utlis/tile_selection_plugin.rs
Normal file
62
game/main/src/utlis/tile_selection_plugin.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy_rapier3d::{plugin::RapierContext, prelude::QueryFilter};
|
||||
use shared::{
|
||||
resources::{TileContact, TileUnderCursor},
|
||||
tags::MainCamera,
|
||||
};
|
||||
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
||||
pub struct TileSelectionPlugin;
|
||||
|
||||
impl Plugin for TileSelectionPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<TileUnderCursor>();
|
||||
app.add_systems(
|
||||
PreUpdate,
|
||||
update_tile_under_cursor.run_if(in_state(GeneratorState::Idle)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn update_tile_under_cursor(
|
||||
cam_query: Query<(&GlobalTransform, &Camera), With<MainCamera>>,
|
||||
window: Query<&Window, With<PrimaryWindow>>,
|
||||
rapier_context: Res<RapierContext>,
|
||||
map: Res<Map>,
|
||||
mut tile_under_cursor: ResMut<TileUnderCursor>,
|
||||
) {
|
||||
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);
|
||||
//todo: handle correct tile detection when contacting a tile from the side
|
||||
if !map.is_in_bounds(&contact_coord) {
|
||||
tile_under_cursor.0 = None;
|
||||
return;
|
||||
}
|
||||
let surface = map.sample_height(&contact_coord);
|
||||
tile_under_cursor.0 = Some(TileContact::new(
|
||||
contact_coord,
|
||||
contact_point,
|
||||
contact_coord.to_world(surface),
|
||||
));
|
||||
} else {
|
||||
tile_under_cursor.0 = None;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user