2023-09-29 18:57:22 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
|
|
|
use lyra_engine::{math, ecs::{World, components::{mesh::MeshComponent, transform::TransformComponent, camera::CameraComponent, model::ModelComponent}, atomicell::{Ref, RefMut}, EventQueue}, render::{mesh::Mesh, material::Material, vertex::Vertex, window::{CursorGrabMode, WindowOptions}}, math::Transform, input::{KeyCode, InputButtons, MouseMotion}, game::Game, change_tracker::Ct};
|
|
|
|
|
|
|
|
use lyra_engine::assets::{ResourceManager, Texture, Model};
|
|
|
|
|
|
|
|
use tracing::debug;
|
|
|
|
|
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,
|
|
|
|
];
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct Point2d {
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Point2d {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "(x={}, y={})", self.x, self.y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Point2d {
|
|
|
|
pub fn new(x: i32, y: i32) -> Self {
|
|
|
|
Self {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct Point3d {
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
z: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Point3d {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "(x={}, y={}, z={})", self.x, self.y, self.z)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Point3d {
|
|
|
|
pub fn new(x: i32, y: i32, z: i32) -> Self {
|
|
|
|
Self {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
z,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::main]
|
|
|
|
async fn main() {
|
|
|
|
let setup_sys = |world: &mut World| -> anyhow::Result<()> {
|
2023-10-05 15:42:24 +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-05 15:42:24 +00:00
|
|
|
} */
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
let mut resman = world.get_resource_mut::<ResourceManager>().unwrap();
|
|
|
|
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();
|
|
|
|
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));
|
|
|
|
camera.transform.rotate_z(math::Angle::Degrees(-90.0));
|
|
|
|
world.spawn((camera,));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
let fps_system = |world: &mut World| -> anyhow::Result<()> {
|
|
|
|
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"]);
|
|
|
|
};
|
|
|
|
|
|
|
|
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| {
|
|
|
|
game.with_system("jiggle", jiggle_system, &["input"]);
|
|
|
|
};
|
|
|
|
|
|
|
|
Game::initialize().await
|
|
|
|
.with_plugin(lyra_engine::DefaultPlugins)
|
|
|
|
.with_startup_system(setup_sys)
|
|
|
|
//.with_plugin(fps_plugin)
|
|
|
|
.with_plugin(jiggle_plugin)
|
|
|
|
.run().await;
|
|
|
|
}
|