2023-10-25 00:03:27 +00:00
|
|
|
use lyra_engine::{math::{self, Vec3}, ecs::{World, components::{transform::TransformComponent, camera::CameraComponent, model::ModelComponent}, EventQueue, SimpleSystem, Component}, math::Transform, input::{KeyCode, InputButtons, MouseMotion}, game::Game, plugin::Plugin, render::window::{CursorGrabMode, WindowOptions}, change_tracker::Ct};
|
2023-10-23 20:56:55 +00:00
|
|
|
use lyra_engine::assets::{ResourceManager, Model};
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
use tracing::debug;
|
|
|
|
|
2023-10-25 00:03:27 +00:00
|
|
|
mod free_fly_camera;
|
|
|
|
use free_fly_camera::{FreeFlyCameraController, FreeFlyCamera};
|
|
|
|
|
2023-10-05 15:42:24 +00:00
|
|
|
/* pub const VERTICES: &[Vertex] = &[
|
2023-09-29 18:57:22 +00:00
|
|
|
Vertex { position: [-0.0868241, 0.49240386, 0.0], tex_coords: [0.4131759, 0.00759614], }, // A
|
|
|
|
Vertex { position: [-0.49513406, 0.06958647, 0.0], tex_coords: [0.0048659444, 0.43041354], }, // B
|
|
|
|
Vertex { position: [-0.21918549, -0.44939706, 0.0], tex_coords: [0.28081453, 0.949397], }, // C
|
|
|
|
Vertex { position: [0.35966998, -0.3473291, 0.0], tex_coords: [0.85967, 0.84732914], }, // D
|
|
|
|
Vertex { position: [0.44147372, 0.2347359, 0.0], tex_coords: [0.9414737, 0.2652641], }, // E
|
2023-10-05 15:42:24 +00:00
|
|
|
]; */
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
pub const INDICES: &[u16] = &[
|
|
|
|
0, 1, 4,
|
|
|
|
1, 2, 4,
|
|
|
|
2, 3, 4,
|
|
|
|
];
|
|
|
|
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() {
|
|
|
|
let setup_sys = |world: &mut World| -> anyhow::Result<()> {
|
2023-10-26 01:49:38 +00:00
|
|
|
{
|
2023-09-29 18:57:22 +00:00
|
|
|
let mut window_options = world.get_resource_mut::<Ct<WindowOptions>>().unwrap();
|
|
|
|
window_options.cursor_grab = CursorGrabMode::Confined;
|
|
|
|
window_options.cursor_visible = false;
|
2023-10-26 01:49:38 +00:00
|
|
|
}
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
let mut resman = world.get_resource_mut::<ResourceManager>().unwrap();
|
2023-10-23 20:56:55 +00:00
|
|
|
//let diffuse_texture = resman.request::<Texture>("assets/happy-tree.png").unwrap();
|
2023-10-22 02:19:34 +00:00
|
|
|
let antique_camera_model = resman.request::<Model>("assets/AntiqueCamera.glb").unwrap();
|
2023-10-23 20:56:55 +00:00
|
|
|
//let cube_model = resman.request::<Model>("assets/texture-sep/texture-sep.gltf").unwrap();
|
2023-09-29 18:57:22 +00:00
|
|
|
drop(resman);
|
|
|
|
|
2023-10-22 02:19:34 +00:00
|
|
|
/* world.spawn((
|
|
|
|
ModelComponent(cube_model.clone()),
|
|
|
|
TransformComponent::from(Transform::from_xyz(3.0, 0.5, -2.2)),
|
2023-09-29 18:57:22 +00:00
|
|
|
)); */
|
|
|
|
|
|
|
|
world.spawn((
|
2023-10-22 02:19:34 +00:00
|
|
|
ModelComponent(antique_camera_model),
|
|
|
|
TransformComponent::from(Transform::from_xyz(0.0, -5.0, -10.0)),
|
2023-09-29 18:57:22 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
let mut camera = CameraComponent::new_3d();
|
2023-10-05 15:42:24 +00:00
|
|
|
camera.transform.translation += math::Vec3::new(0.0, 0.0, 7.5);
|
2023-09-29 18:57:22 +00:00
|
|
|
//camera.transform.rotate_y(Angle::Degrees(-25.0));
|
2023-10-25 00:03:27 +00:00
|
|
|
//camera.transform.rotate_z(math::Angle::Degrees(-90.0));
|
|
|
|
world.spawn(( camera, FreeFlyCamera::default() ));
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
2023-10-23 20:56:55 +00:00
|
|
|
/* let fps_system = |world: &mut World| -> anyhow::Result<()> {
|
2023-09-29 18:57:22 +00:00
|
|
|
let mut counter: RefMut<fps_counter::FPSCounter> = world.get_resource_mut().unwrap();
|
|
|
|
|
|
|
|
let fps = counter.tick();
|
|
|
|
|
|
|
|
debug!("FPS: {fps}");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
let fps_plugin = move |game: &mut Game| {
|
|
|
|
let world = game.world();
|
|
|
|
world.insert_resource(fps_counter::FPSCounter::new());
|
|
|
|
|
|
|
|
game.with_system("fps", fps_system, &["input"]);
|
2023-10-23 20:56:55 +00:00
|
|
|
}; */
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
let jiggle_system = |world: &mut World| -> anyhow::Result<()> {
|
2023-10-18 02:04:25 +00:00
|
|
|
let keys = world.get_resource::<InputButtons<KeyCode>>();
|
2023-09-29 18:57:22 +00:00
|
|
|
if keys.is_none() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2023-10-18 02:04:25 +00:00
|
|
|
let keys = keys.unwrap();
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-10-18 02:04:25 +00:00
|
|
|
let speed = 0.01;
|
2023-10-05 15:42:24 +00:00
|
|
|
let rot_speed = 1.0;
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
let mut dir_x = 0.0;
|
|
|
|
let mut dir_y = 0.0;
|
2023-10-18 02:04:25 +00:00
|
|
|
let mut dir_z = 0.0;
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-10-05 15:42:24 +00:00
|
|
|
let mut rot_x = 0.0;
|
|
|
|
let mut rot_y = 0.0;
|
|
|
|
|
2023-09-29 18:57:22 +00:00
|
|
|
if keys.is_pressed(KeyCode::A) {
|
|
|
|
dir_x += speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_pressed(KeyCode::D) {
|
|
|
|
dir_x -= speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_pressed(KeyCode::S) {
|
|
|
|
dir_y += speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_pressed(KeyCode::W) {
|
|
|
|
dir_y -= speed;
|
|
|
|
}
|
|
|
|
|
2023-10-18 02:04:25 +00:00
|
|
|
if keys.is_pressed(KeyCode::E) {
|
|
|
|
dir_z += speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_pressed(KeyCode::Q) {
|
|
|
|
dir_z -= speed;
|
|
|
|
}
|
|
|
|
|
2023-10-05 15:42:24 +00:00
|
|
|
if keys.is_pressed(KeyCode::Left) {
|
|
|
|
rot_y -= rot_speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_pressed(KeyCode::Right) {
|
|
|
|
rot_y += rot_speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_pressed(KeyCode::Up) {
|
|
|
|
rot_x -= rot_speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.is_pressed(KeyCode::Down) {
|
|
|
|
rot_x += rot_speed;
|
|
|
|
}
|
|
|
|
|
2023-09-29 18:57:22 +00:00
|
|
|
drop(keys);
|
|
|
|
|
2023-10-18 02:04:25 +00:00
|
|
|
if dir_x == 0.0 && dir_y == 0.0 && dir_z == 0.0 && rot_x == 0.0 && rot_y == 0.0 {
|
2023-09-29 18:57:22 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
//debug!("moving by ({}, {})", dir_x, dir_y);
|
|
|
|
|
|
|
|
for transform in world.query_mut::<(&mut TransformComponent,)>().iter_mut() {
|
|
|
|
let t = &mut transform.transform;
|
2023-10-05 15:42:24 +00:00
|
|
|
//debug!("Translation: {}", t.translation);
|
|
|
|
//debug!("Rotation: {}", t.rotation);
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
/* t.translation += glam::Vec3::new(0.0, 0.001, 0.0);
|
|
|
|
t.translation.x *= -1.0; */
|
|
|
|
t.translation.x += dir_x;
|
|
|
|
t.translation.y += dir_y;
|
2023-10-18 02:04:25 +00:00
|
|
|
t.translation.z += dir_z;
|
2023-10-05 15:42:24 +00:00
|
|
|
t.rotate_x(math::Angle::Degrees(rot_x));
|
|
|
|
t.rotate_y(math::Angle::Degrees(rot_y));
|
2023-09-29 18:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let events = world.get_resource_mut::<EventQueue>().unwrap();
|
|
|
|
if let Some(mm) = events.read_events::<MouseMotion>() {
|
|
|
|
debug!("Mouse motion: {:?}", mm);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
let jiggle_plugin = move |game: &mut Game| {
|
2023-10-25 00:03:27 +00:00
|
|
|
//game.with_system("jiggle", jiggle_system, &["input"]);
|
2023-09-29 18:57:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Game::initialize().await
|
|
|
|
.with_plugin(lyra_engine::DefaultPlugins)
|
|
|
|
.with_startup_system(setup_sys)
|
|
|
|
//.with_plugin(fps_plugin)
|
|
|
|
.with_plugin(jiggle_plugin)
|
2023-10-25 00:03:27 +00:00
|
|
|
.with_plugin(FreeFlyCameraController)
|
2023-09-29 18:57:22 +00:00
|
|
|
.run().await;
|
|
|
|
}
|