rts camera
center camera spawn basic water fix generator config set camera bounds
This commit is contained in:
@@ -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<Msaa>) {
|
||||
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<ButtonInput<MouseButto
|
||||
}
|
||||
|
||||
fn rts_camera_system(
|
||||
mut cam_query: Query<(&mut Transform, &PhosCamera)>,
|
||||
mut cam_query: Query<(&mut Transform, &PhosCamera, &mut PhosCameraTargets)>,
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mut wheel: EventReader<MouseWheel>,
|
||||
) {
|
||||
let (mut cam, cam_cfg) = cam_query.single_mut();
|
||||
let (mut cam, cam_cfg, mut cam_targets) = cam_query.single_mut();
|
||||
let mut cam_move = Vec3::ZERO;
|
||||
|
||||
if key.pressed(KeyCode::KeyA) {
|
||||
cam_move.x = -1.;
|
||||
} else if key.pressed(KeyCode::KeyD) {
|
||||
cam_move.x = 1.;
|
||||
} else if key.pressed(KeyCode::KeyD) {
|
||||
cam_move.x = -1.;
|
||||
}
|
||||
|
||||
if key.pressed(KeyCode::KeyW) {
|
||||
@@ -138,5 +147,30 @@ fn rts_camera_system(
|
||||
cam_move.z = -1.;
|
||||
}
|
||||
|
||||
cam.translation += cam_move.normalize() * cam_cfg.speed * time.delta_seconds();
|
||||
let fwd = cam.forward();
|
||||
let fwd_quat = Quat::from_rotation_arc(Vec3::Z, fwd.into());
|
||||
cam_move = fwd_quat.mul_vec3(cam_move.normalize_or_zero()) * cam_cfg.speed * time.delta_seconds();
|
||||
|
||||
for e in wheel.read() {
|
||||
match e.unit {
|
||||
MouseScrollUnit::Line => cam_targets.height -= e.y * 20.,
|
||||
MouseScrollUnit::Pixel => cam_targets.height -= e.y,
|
||||
}
|
||||
}
|
||||
|
||||
cam_targets.height = cam_targets.height.clamp(cam_cfg.min_height, cam_cfg.max_height);
|
||||
|
||||
cam.translation += cam_move;
|
||||
}
|
||||
|
||||
fn apply_camera_height(mut cam_query: Query<(&mut Transform, &PhosCamera, &mut PhosCameraTargets)>, time: Res<Time>) {
|
||||
let (mut cam_t, cam_cfg, targets) = cam_query.single_mut();
|
||||
|
||||
let movement = (cam_t.translation.y - targets.height) * time.delta_seconds();
|
||||
|
||||
cam_t.translation.y -= movement;
|
||||
}
|
||||
|
||||
fn limit_camera_bounds(mut cam_query: Query<(&mut Transform, &CameraBounds)>) {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,44 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
#[derive(Component)]
|
||||
pub struct PhosCamera {
|
||||
pub min_height: f32,
|
||||
pub max_height: f32,
|
||||
pub speed: f32,
|
||||
pub zoom_speed: f32,
|
||||
pub min_angle: f32,
|
||||
pub max_angle: f32,
|
||||
}
|
||||
|
||||
impl Default for PhosCamera {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
min_height: 10.,
|
||||
max_height: 100.,
|
||||
speed: 20.,
|
||||
zoom_speed: 20.,
|
||||
min_angle: 10.,
|
||||
max_angle: 80.,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct PhosCameraTargets {
|
||||
pub height: f32,
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct CameraBounds {
|
||||
pub min: Vec2,
|
||||
pub max: Vec2,
|
||||
}
|
||||
|
||||
impl CameraBounds {
|
||||
pub fn from_size(size: UVec2) -> Self {
|
||||
return Self {
|
||||
min: Vec2::ZERO,
|
||||
max: size.as_vec2(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use bevy::{asset::LoadState, pbr::ExtendedMaterial, prelude::*};
|
||||
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
|
||||
use bevy_rapier3d::geometry::{Collider, TriMeshFlags};
|
||||
use camera_system::prelude::PhosCamera;
|
||||
use camera_system::prelude::{CameraBounds, PhosCamera};
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
use world_generation::{
|
||||
biome_painter::*,
|
||||
@@ -29,7 +29,8 @@ impl Plugin for MapInitPlugin {
|
||||
ResourceInspectorPlugin::<GenerationConfig>::default(),
|
||||
));
|
||||
|
||||
app.add_systems(Startup, (load_textures, load_tiles, create_map));
|
||||
app.add_systems(Startup, (load_textures, load_tiles));
|
||||
app.add_systems(PostStartup, create_map);
|
||||
|
||||
app.add_systems(Update, finalize_texture);
|
||||
app.add_systems(PostUpdate, (despawn_map, spawn_map).chain());
|
||||
@@ -87,13 +88,13 @@ fn finalize_texture(
|
||||
map.regenerate = true;
|
||||
}
|
||||
|
||||
fn create_map(mut commands: Commands, mut cam: Query<&mut Transform, With<PhosCamera>>) {
|
||||
fn create_map(mut commands: Commands, mut cam: Query<(&mut Transform, Entity), With<PhosCamera>>) {
|
||||
let config = GenerationConfig {
|
||||
layers: vec![
|
||||
GeneratorLayer {
|
||||
base_roughness: 2.14,
|
||||
roughness: 0.87,
|
||||
strength: 2.93,
|
||||
strength: 8.3,
|
||||
min_value: -0.2,
|
||||
persistence: 0.77,
|
||||
is_rigid: false,
|
||||
@@ -117,7 +118,7 @@ fn create_map(mut commands: Commands, mut cam: Query<&mut Transform, With<PhosCa
|
||||
GeneratorLayer {
|
||||
base_roughness: 2.6,
|
||||
roughness: 4.,
|
||||
strength: 4.3,
|
||||
strength: 3.1,
|
||||
min_value: 0.,
|
||||
persistence: 1.57,
|
||||
is_rigid: true,
|
||||
@@ -148,7 +149,7 @@ fn create_map(mut commands: Commands, mut cam: Query<&mut Transform, With<PhosCa
|
||||
weight: 1.,
|
||||
weight_multi: 4.57,
|
||||
layers: 3,
|
||||
first_layer_mask: true,
|
||||
first_layer_mask: false,
|
||||
},
|
||||
],
|
||||
noise_scale: 450.,
|
||||
@@ -161,12 +162,14 @@ fn create_map(mut commands: Commands, mut cam: Query<&mut Transform, With<PhosCa
|
||||
|
||||
commands.insert_resource(heightmap);
|
||||
|
||||
// let mut cam_t = cam.single_mut();
|
||||
// cam_t.translation = Vec3::new(
|
||||
// tile_to_world_distance(config.size.x as i32 / 2),
|
||||
// cam_t.translation.y,
|
||||
// tile_to_world_distance(config.size.y as i32 / 2),
|
||||
// );
|
||||
let (mut cam_t, cam_entity) = cam.single_mut();
|
||||
cam_t.translation = Vec3::new(
|
||||
tile_to_world_distance((config.size.x as i32 * Chunk::SIZE as i32) / 2),
|
||||
cam_t.translation.y,
|
||||
tile_to_world_distance((config.size.y as i32 * Chunk::SIZE as i32) / 2),
|
||||
);
|
||||
|
||||
commands.entity(cam_entity).insert(CameraBounds::from_size(config.size));
|
||||
|
||||
commands.insert_resource(config);
|
||||
}
|
||||
@@ -174,7 +177,8 @@ fn create_map(mut commands: Commands, mut cam: Query<&mut Transform, With<PhosCa
|
||||
fn spawn_map(
|
||||
heightmap: Res<Map>,
|
||||
mut commands: Commands,
|
||||
mut materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
|
||||
mut chunk_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
|
||||
mut standard_materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
atlas: Res<ChunkAtlas>,
|
||||
mut map: ResMut<PhosMap>,
|
||||
@@ -188,7 +192,7 @@ fn spawn_map(
|
||||
}
|
||||
let b_painter = biome_painters.get(painter.0.clone());
|
||||
map.regenerate = false;
|
||||
let chunk_material = materials.add(ExtendedMaterial {
|
||||
let chunk_material = chunk_materials.add(ExtendedMaterial {
|
||||
base: StandardMaterial::default(),
|
||||
extension: ChunkMaterial {
|
||||
array_texture: atlas.handle.clone(),
|
||||
@@ -228,6 +232,17 @@ fn spawn_map(
|
||||
Collider::trimesh_with_flags(col_verts, col_indicies, TriMeshFlags::MERGE_DUPLICATE_VERTICES),
|
||||
));
|
||||
}
|
||||
|
||||
commands.spawn((PbrBundle {
|
||||
transform: Transform::from_translation(heightmap.get_center()),
|
||||
mesh: meshes.add(Plane3d::default().mesh().size(heightmap.get_world_width(), heightmap.get_world_height())),
|
||||
material: standard_materials.add(StandardMaterial {
|
||||
base_color: Color::AQUAMARINE.with_a(0.5),
|
||||
alpha_mode: AlphaMode::Blend,
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
},));
|
||||
}
|
||||
|
||||
fn despawn_map(
|
||||
|
||||
Reference in New Issue
Block a user