camera
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/phos-neo.iml" filepath="$PROJECT_DIR$/.idea/phos-neo.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
13
.idea/phos-neo.iml
generated
Normal file
13
.idea/phos-neo.iml
generated
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/engine/world_generation/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/game/camera_system/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/game/main/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -1285,6 +1285,13 @@ dependencies = [
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "camera_system"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.90"
|
||||
@@ -2901,6 +2908,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bevy-inspector-egui",
|
||||
"camera_system",
|
||||
"iyes_perf_ui",
|
||||
"noise 0.8.2",
|
||||
"world_generation",
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
members = [
|
||||
"game/main",
|
||||
"engine/world_generation"
|
||||
]
|
||||
, "game/camera_system"]
|
||||
resolver = "2"
|
||||
|
||||
# Enable a small amount of optimization in debug mode
|
||||
|
||||
@@ -3,8 +3,7 @@ use bevy::prelude::*;
|
||||
pub const OUTER_RADIUS: f32 = 1.;
|
||||
pub const INNER_RADIUS: f32 = OUTER_RADIUS * 0.866025404;
|
||||
|
||||
|
||||
pub fn to_hex_pos(pos: Vec3) -> Vec3 {
|
||||
let x = (pos.x + pos.z * 0.5 - (pos.z / 2.).floor()) * (INNER_RADIUS * 2.);
|
||||
return Vec3::new(x, pos.y, pos.z * OUTER_RADIUS * 1.5);
|
||||
}
|
||||
}
|
||||
|
||||
9
game/camera_system/Cargo.toml
Normal file
9
game/camera_system/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "camera_system"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.13.1"
|
||||
113
game/camera_system/src/lib.rs
Normal file
113
game/camera_system/src/lib.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use crate::prelude::PhosCamera;
|
||||
use bevy::input::mouse::MouseMotion;
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::CursorGrabMode;
|
||||
|
||||
pub mod prelude {
|
||||
use bevy::prelude::Component;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct PhosCamera {
|
||||
pub min_height: f32,
|
||||
pub max_height: f32,
|
||||
pub speed: f32,
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PhosCameraPlugin;
|
||||
|
||||
impl Plugin for PhosCameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, setup)
|
||||
.add_systems(Update, (update_camera, grab_mouse));
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
Camera3dBundle {
|
||||
transform: Transform::from_xyz(-200., 300., -200.)
|
||||
.looking_at(Vec3::new(1000., 0., 1000.), Vec3::Y),
|
||||
..default()
|
||||
},
|
||||
PhosCamera {
|
||||
speed: 100.,
|
||||
..default()
|
||||
},
|
||||
));
|
||||
}
|
||||
fn update_camera(
|
||||
mut cam_query: Query<(&PhosCamera, &mut Transform)>,
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (cam, mut transform) = cam_query.single_mut();
|
||||
|
||||
let mut move_vec = Vec3::ZERO;
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
move_vec += Vec3::NEG_X;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
move_vec += Vec3::X;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
move_vec += Vec3::NEG_Z;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
move_vec += Vec3::Z;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ShiftLeft) {
|
||||
move_vec += Vec3::NEG_Y;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::Space) {
|
||||
move_vec += Vec3::Y;
|
||||
}
|
||||
if move_vec.length_squared() == 0. {
|
||||
return;
|
||||
}
|
||||
let rot = transform.rotation;
|
||||
transform.translation += (rot * move_vec.normalize()) * cam.speed * time.delta_seconds();
|
||||
}
|
||||
|
||||
fn update_camera_mouse(
|
||||
mut cam_query: Query<&mut Transform, With<PhosCamera>>,
|
||||
mut mouse_move: EventReader<MouseMotion>,
|
||||
time: Res<Time>,
|
||||
windows: Query<&Window>,
|
||||
) {
|
||||
let window = windows.single();
|
||||
if window.cursor.grab_mode != CursorGrabMode::Locked {
|
||||
return;
|
||||
}
|
||||
let mut transform = cam_query.single_mut();
|
||||
let cam_rot = mouse_move.read().map(|event| event.delta).sum::<Vec2>() * time.delta_seconds();
|
||||
|
||||
let (mut pitch, mut yaw, _) = transform.rotation.to_euler(EulerRot::XYZ);
|
||||
|
||||
pitch -= cam_rot.y.to_radians() ;
|
||||
yaw -= cam_rot.x.to_radians() ;
|
||||
pitch = pitch.clamp(-1.54, 1.54);
|
||||
// if rot_x > PI && cam_rot.x < 2. * PI {
|
||||
// rot_x = PI;
|
||||
// }
|
||||
|
||||
transform.rotation = Quat::from_axis_angle(Vec3::Y, yaw) * Quat::from_axis_angle(Vec3::X, pitch);
|
||||
}
|
||||
|
||||
fn grab_mouse(
|
||||
mut windows: Query<&mut Window>,
|
||||
mouse: Res<ButtonInput<MouseButton>>,
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
) {
|
||||
let mut window = windows.single_mut();
|
||||
|
||||
if mouse.just_pressed(MouseButton::Middle) {
|
||||
window.cursor.visible = false;
|
||||
window.cursor.grab_mode = CursorGrabMode::Locked;
|
||||
}
|
||||
|
||||
if key.just_pressed(KeyCode::Escape) {
|
||||
window.cursor.visible = true;
|
||||
window.cursor.grab_mode = CursorGrabMode::None;
|
||||
}
|
||||
}
|
||||
@@ -11,3 +11,4 @@ bevy-inspector-egui = "0.23.4"
|
||||
iyes_perf_ui = "0.2.3"
|
||||
noise = "0.8.2"
|
||||
world_generation ={path="../../engine/world_generation"}
|
||||
camera_system={path = "../camera_system"}
|
||||
|
||||
@@ -4,25 +4,25 @@ mod phos;
|
||||
use phos::PhosGamePlugin;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "Phos".into(),
|
||||
name: Some("phos".into()),
|
||||
resolution: (1920.0, 1080.0).into(),
|
||||
resizable: false,
|
||||
enabled_buttons: bevy::window::EnabledButtons {
|
||||
maximize: false,
|
||||
..Default::default()
|
||||
},
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
}),
|
||||
WireframePlugin,
|
||||
WorldInspectorPlugin::new(),
|
||||
PhosGamePlugin,
|
||||
))
|
||||
.run();
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "Phos".into(),
|
||||
name: Some("phos".into()),
|
||||
resolution: (1920.0, 1080.0).into(),
|
||||
resizable: false,
|
||||
enabled_buttons: bevy::window::EnabledButtons {
|
||||
maximize: false,
|
||||
..Default::default()
|
||||
},
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
}),
|
||||
WireframePlugin,
|
||||
WorldInspectorPlugin::new(),
|
||||
PhosGamePlugin,
|
||||
))
|
||||
.run();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use bevy::{pbr::CascadeShadowConfig, prelude::*};
|
||||
use camera_system::PhosCameraPlugin;
|
||||
use iyes_perf_ui::prelude::*;
|
||||
use world_generation::{
|
||||
heightmap::generate_heightmap, hex_utils::to_hex_pos, mesh_generator::generate_chunk_mesh,
|
||||
@@ -8,6 +9,7 @@ pub struct PhosGamePlugin;
|
||||
|
||||
impl Plugin for PhosGamePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(PhosCameraPlugin);
|
||||
app.add_systems(Startup, init_game)
|
||||
.add_systems(Startup, create_map);
|
||||
app.add_plugins(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
|
||||
@@ -18,12 +20,6 @@ impl Plugin for PhosGamePlugin {
|
||||
}
|
||||
|
||||
fn init_game(mut commands: Commands) {
|
||||
commands.spawn((Camera3dBundle {
|
||||
transform: Transform::from_xyz(-200., 300., -200.)
|
||||
.looking_at(Vec3::new(1000., 0., 1000.), Vec3::Y),
|
||||
..default()
|
||||
},));
|
||||
|
||||
commands.spawn((
|
||||
PerfUiRoot::default(),
|
||||
PerfUiEntryFPS::default(),
|
||||
|
||||
Reference in New Issue
Block a user