lyra-engine/examples/testbed/src/main.rs

200 lines
5.8 KiB
Rust
Raw Normal View History

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;
pub const VERTICES: &[Vertex] = &[
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
];
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<()> {
{
let mut window_options = world.get_resource_mut::<Ct<WindowOptions>>().unwrap();
window_options.cursor_grab = CursorGrabMode::Confined;
window_options.cursor_visible = false;
}
let mut resman = world.get_resource_mut::<ResourceManager>().unwrap();
let diffuse_texture = resman.request::<Texture>("assets/happy-tree.png").unwrap();
let cube_model = resman.request::<Model>("assets/cube-embedded.gltf").unwrap();
drop(resman);
/* world.spawn((MeshComponent::new(
Mesh {
vertices: VERTICES.to_vec(),
indices: Some(INDICES.to_vec())
}, Material {
shader_id: 0,
texture: diffuse_texture.clone()
}),
TransformComponent::from(Transform::from_xyz(0.005, 0.0, -2.0)),
));
world.spawn((MeshComponent::new(
Mesh {
vertices: VERTICES.to_vec(),
indices: Some(INDICES.to_vec())
}, Material {
shader_id: 0,
texture: diffuse_texture
}),
TransformComponent::from(Transform::from_xyz(0.005, 0.7, -0.5)),
)); */
world.spawn((
ModelComponent(cube_model),
TransformComponent::from(Transform::from_xyz(0.005, 0.5, -1.2)),
));
let mut camera = CameraComponent::new_3d();
camera.transform.translation += math::Vec3::new(0.0, 0.0, 2.0);
//camera.transform.rotate_y(Angle::Degrees(-25.0));
camera.transform.rotate_z(math::Angle::Degrees(-90.0));
world.spawn((camera,));
Ok(())
};
//world.insert_resource(fps_counter::FPSCounter::new());
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<()> {
let keys = world.get_resource();
if keys.is_none() {
return Ok(());
}
let keys: Ref<InputButtons<KeyCode>> = keys.unwrap();
let speed = 0.001;
let mut dir_x = 0.0;
let mut dir_y = 0.0;
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;
}
drop(keys);
if dir_x == 0.0 && dir_y == 0.0 {
return Ok(());
}
//debug!("moving by ({}, {})", dir_x, dir_y);
for transform in world.query_mut::<(&mut TransformComponent,)>().iter_mut() {
let t = &mut transform.transform;
debug!("Translation: {}", t.translation);
/* 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;
}
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;
}