2023-11-10 17:28:17 +00:00
|
|
|
use lyra_engine::{math::{self, Vec3}, ecs::{World, components::{transform::TransformComponent, camera::CameraComponent, model::ModelComponent, DeltaTime}, EventQueue, SimpleSystem, Component, Criteria, CriteriaSchedule, BatchedSystem}, math::Transform, input::{KeyCode, InputButtons, MouseMotion, ActionHandler, Layout, Action, ActionKind, LayoutId, ActionMapping, Binding, ActionSource, ActionMappingId, InputActionPlugin, ActionState}, game::Game, plugin::Plugin, render::{window::{CursorGrabMode, WindowOptions}, light::PointLight}, 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
|
|
|
|
2023-10-25 00:03:27 +00:00
|
|
|
mod free_fly_camera;
|
2023-10-29 21:54:04 +00:00
|
|
|
use free_fly_camera::{FreeFlyCameraPlugin, FreeFlyCamera};
|
2023-11-06 03:50:57 +00:00
|
|
|
use tracing::debug;
|
2023-10-29 21:54:04 +00:00
|
|
|
|
|
|
|
struct FixedTimestep {
|
|
|
|
max_tps: u32,
|
|
|
|
fixed_time: f32,
|
|
|
|
accumulator: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FixedTimestep {
|
|
|
|
pub fn new(max_tps: u32) -> Self {
|
|
|
|
Self {
|
|
|
|
max_tps,
|
|
|
|
fixed_time: Self::calc_fixed_time(max_tps),
|
|
|
|
accumulator: 0.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calc_fixed_time(max_tps: u32) -> f32 {
|
|
|
|
1.0 / max_tps as f32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_tps(&mut self, tps: u32) {
|
|
|
|
self.max_tps = tps;
|
|
|
|
self.fixed_time = Self::calc_fixed_time(tps);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tps(&self) -> u32 {
|
|
|
|
self.max_tps
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fixed_time(&self) -> f32 {
|
|
|
|
self.fixed_time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Criteria for FixedTimestep {
|
|
|
|
fn can_run(&mut self, world: &mut edict::World, check_count: u32) -> CriteriaSchedule {
|
|
|
|
if check_count == 0 {
|
|
|
|
let delta_time = world.get_resource::<DeltaTime>().unwrap();
|
|
|
|
self.accumulator += **delta_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.accumulator >= self.fixed_time {
|
|
|
|
self.accumulator -= self.fixed_time;
|
|
|
|
return CriteriaSchedule::YesAndLoop;
|
|
|
|
}
|
|
|
|
|
|
|
|
CriteriaSchedule::No
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct TpsAccumulator(f32);
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-11-17 19:52:38 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
struct CubeFlag;
|
|
|
|
|
2023-09-29 18:57:22 +00:00
|
|
|
#[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-11-18 16:36:33 +00:00
|
|
|
let antique_camera_model = resman.request::<Model>("assets/AntiqueCamera.glb").unwrap();
|
2023-11-16 00:47:22 +00:00
|
|
|
//let cube_model = resman.request::<Model>("assets/cube-texture-bin.glb").unwrap();
|
|
|
|
let cube_model = resman.request::<Model>("assets/texture-sep/texture-sep.gltf").unwrap();
|
2023-11-18 16:36:33 +00:00
|
|
|
let crate_model = resman.request::<Model>("assets/crate/crate.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
|
|
|
)); */
|
|
|
|
|
2023-11-18 16:36:33 +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-11-18 16:36:33 +00:00
|
|
|
));
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-11-10 17:28:17 +00:00
|
|
|
/* let light = PointLight {
|
|
|
|
color: Vec3::new(1.0, 1.0, 1.0),
|
|
|
|
position: Vec3::new(0.0, -5.0, -8.0),
|
|
|
|
constant: 1.0,
|
|
|
|
linear: 0.09,
|
|
|
|
quadratic: 0.032,
|
|
|
|
};
|
|
|
|
world.spawn((light,)); */
|
2023-11-12 19:56:59 +00:00
|
|
|
let mut cube_tran = Transform::from_xyz(-3.5, 0.0, -7.0);
|
|
|
|
cube_tran.rotate_y(math::Angle::Degrees(180.0));
|
2023-11-10 17:28:17 +00:00
|
|
|
world.spawn((
|
2023-11-12 19:56:59 +00:00
|
|
|
/* PointLight {
|
2023-11-10 17:28:17 +00:00
|
|
|
color: Vec3::new(1.0, 1.0, 1.0),
|
2023-11-10 22:52:11 +00:00
|
|
|
intensity: 1.0,
|
2023-11-10 17:28:17 +00:00
|
|
|
constant: 1.0,
|
|
|
|
linear: 0.045,
|
|
|
|
quadratic: 0.0075,
|
2023-11-12 19:56:59 +00:00
|
|
|
}, */
|
|
|
|
TransformComponent::from(cube_tran),
|
2023-11-18 16:36:33 +00:00
|
|
|
ModelComponent(crate_model.clone()),
|
2023-11-17 19:52:38 +00:00
|
|
|
CubeFlag,
|
2023-11-10 22:52:11 +00:00
|
|
|
));
|
|
|
|
|
2023-11-17 19:52:38 +00:00
|
|
|
let mut light_tran = Transform::from_xyz(1.5, 2.5, -8.5);
|
2023-11-12 19:56:59 +00:00
|
|
|
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
2023-11-10 22:52:11 +00:00
|
|
|
world.spawn((
|
|
|
|
PointLight {
|
2023-11-17 19:52:38 +00:00
|
|
|
color: Vec3::new(1.0, 1.0, 1.0), //Vec3::new(0.361, 0.984, 0.0),
|
2023-11-18 16:36:33 +00:00
|
|
|
intensity: 1.2,
|
2023-11-17 19:52:38 +00:00
|
|
|
/* constant: 1.0,
|
2023-11-10 22:52:11 +00:00
|
|
|
linear: 0.045,
|
2023-11-17 19:52:38 +00:00
|
|
|
quadratic: 0.0075, */
|
|
|
|
|
|
|
|
constant: 1.0,
|
|
|
|
linear: 0.09,
|
|
|
|
quadratic: 0.032,
|
2023-11-16 00:47:22 +00:00
|
|
|
|
2023-11-18 16:36:33 +00:00
|
|
|
ambient: 0.3,
|
|
|
|
diffuse: 1.0,
|
|
|
|
specular: 1.3,
|
2023-11-10 22:52:11 +00:00
|
|
|
},
|
2023-11-12 19:56:59 +00:00
|
|
|
TransformComponent::from(light_tran),
|
2023-11-10 17:28:17 +00:00
|
|
|
ModelComponent(cube_model),
|
|
|
|
));
|
|
|
|
|
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-29 21:54:04 +00:00
|
|
|
let fps_system = |world: &mut World| -> anyhow::Result<()> {
|
|
|
|
let mut counter = world.get_resource_mut::<fps_counter::FPSCounter>().unwrap();
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
let fps = counter.tick();
|
|
|
|
|
2023-10-29 21:54:04 +00:00
|
|
|
println!("FPS: {fps}");
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
let fps_plugin = move |game: &mut Game| {
|
|
|
|
let world = game.world();
|
|
|
|
world.insert_resource(fps_counter::FPSCounter::new());
|
2023-10-29 21:54:04 +00:00
|
|
|
};
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-10-29 21:54:04 +00:00
|
|
|
let spin_system = |world: &mut World| -> anyhow::Result<()> {
|
2023-11-17 19:52:38 +00:00
|
|
|
const SPEED: f32 = 7.0;
|
2023-10-29 21:54:04 +00:00
|
|
|
let delta_time = **world.get_resource::<DeltaTime>().unwrap();
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-11-17 19:52:38 +00:00
|
|
|
for (transform, _) in world.query_mut::<(&mut TransformComponent, &CubeFlag)>().iter_mut() {
|
2023-09-29 18:57:22 +00:00
|
|
|
let t = &mut transform.transform;
|
2023-10-29 21:54:04 +00:00
|
|
|
t.rotate_y(math::Angle::Degrees(SPEED * delta_time));
|
2023-09-29 18:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
let jiggle_plugin = move |game: &mut Game| {
|
2023-10-29 21:54:04 +00:00
|
|
|
game.world().insert_resource(TpsAccumulator(0.0));
|
|
|
|
|
|
|
|
let mut sys = BatchedSystem::new();
|
2023-11-03 23:50:00 +00:00
|
|
|
sys.with_criteria(FixedTimestep::new(45));
|
2023-11-12 19:56:59 +00:00
|
|
|
//sys.with_system(spin_system);
|
2023-11-17 19:52:38 +00:00
|
|
|
//sys.with_system(fps_system);
|
2023-10-29 21:54:04 +00:00
|
|
|
|
|
|
|
game.with_system("fixed", sys, &[]);
|
|
|
|
fps_plugin(game);
|
2023-09-29 18:57:22 +00:00
|
|
|
};
|
2023-11-06 03:50:57 +00:00
|
|
|
|
|
|
|
let action_handler_plugin = |game: &mut Game| {
|
|
|
|
let action_handler = ActionHandler::new()
|
|
|
|
.add_layout(LayoutId::from(0))
|
|
|
|
.add_action("forward_backward", Action::new(ActionKind::Button))
|
|
|
|
.add_action("left_right", Action::new(ActionKind::Button))
|
|
|
|
.add_mapping(ActionMapping::new(LayoutId::from(0), ActionMappingId::from(0))
|
|
|
|
.bind("forward_backward", &[
|
|
|
|
ActionSource::Keyboard(KeyCode::W).into_binding_modifier(1.0),
|
|
|
|
ActionSource::Keyboard(KeyCode::S).into_binding_modifier(-1.0)
|
|
|
|
])
|
|
|
|
.bind("left_right", &[
|
|
|
|
ActionSource::Keyboard(KeyCode::A).into_binding_modifier(1.0),
|
|
|
|
ActionSource::Keyboard(KeyCode::D).into_binding_modifier(-1.0)
|
|
|
|
])
|
|
|
|
.finish()
|
|
|
|
);
|
|
|
|
|
|
|
|
let test_system = |world: &mut World| -> anyhow::Result<()> {
|
|
|
|
let handler = world.get_resource::<ActionHandler>().unwrap();
|
|
|
|
|
|
|
|
if let Some(alpha) = handler.get_pressed_modifier("forward_backward") {
|
|
|
|
debug!("'forward_backward': {alpha}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(alpha) = handler.get_pressed_modifier("left_right") {
|
|
|
|
debug!("'left_right': {alpha}");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
game.world().insert_resource(action_handler);
|
|
|
|
game.with_plugin(InputActionPlugin);
|
2023-11-17 19:52:38 +00:00
|
|
|
//game.with_system("test_actions", test_system, &[]);
|
2023-11-06 03:50:57 +00:00
|
|
|
};
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
Game::initialize().await
|
|
|
|
.with_plugin(lyra_engine::DefaultPlugins)
|
|
|
|
.with_startup_system(setup_sys)
|
2023-11-06 03:50:57 +00:00
|
|
|
.with_plugin(action_handler_plugin)
|
2023-09-29 18:57:22 +00:00
|
|
|
//.with_plugin(fps_plugin)
|
|
|
|
.with_plugin(jiggle_plugin)
|
2023-10-29 21:54:04 +00:00
|
|
|
.with_plugin(FreeFlyCameraPlugin)
|
2023-09-29 18:57:22 +00:00
|
|
|
.run().await;
|
|
|
|
}
|