354 lines
13 KiB
Rust
354 lines
13 KiB
Rust
use std::{ptr::NonNull, thread, time::Duration};
|
|
|
|
use lyra_engine::{assets::gltf::Gltf, change_tracker::Ct, ecs::{query::{Res, View}, system::{Criteria, CriteriaSchedule, IntoSystem}, Component, World}, game::Game, input::{Action, ActionHandler, ActionKind, ActionMapping, ActionMappingId, ActionSource, InputActionPlugin, KeyCode, LayoutId, MouseAxis, MouseInput}, math::{self, Quat, Transform, Vec3}, render::{light::{directional::DirectionalLight, PointLight, SpotLight}, window::{CursorGrabMode, WindowOptions}}, scene::{self, CameraComponent, FreeFlyCamera, FreeFlyCameraPlugin, WorldTransform, ACTLBL_LOOK_LEFT_RIGHT, ACTLBL_LOOK_ROLL, ACTLBL_LOOK_UP_DOWN, ACTLBL_MOVE_FORWARD_BACKWARD, ACTLBL_MOVE_LEFT_RIGHT, ACTLBL_MOVE_UP_DOWN}, DeltaTime};
|
|
use lyra_engine::assets::ResourceManager;
|
|
|
|
struct FixedTimestep {
|
|
max_tps: u32,
|
|
fixed_time: f32,
|
|
accumulator: f32,
|
|
old_dt: Option<DeltaTime>,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
impl FixedTimestep {
|
|
pub fn new(max_tps: u32) -> Self {
|
|
Self {
|
|
max_tps,
|
|
fixed_time: Self::calc_fixed_time(max_tps),
|
|
accumulator: 0.0,
|
|
old_dt: None,
|
|
}
|
|
}
|
|
|
|
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, mut world: NonNull<World>, check_count: u32) -> CriteriaSchedule {
|
|
let world = unsafe { world.as_mut() };
|
|
if check_count == 0 {
|
|
let delta_time = world.get_resource::<DeltaTime>();
|
|
self.accumulator += **delta_time;
|
|
}
|
|
|
|
if self.accumulator >= self.fixed_time {
|
|
self.accumulator -= self.fixed_time;
|
|
return CriteriaSchedule::YesAndLoop;
|
|
}
|
|
|
|
CriteriaSchedule::No
|
|
}
|
|
|
|
fn modify_world(&mut self, mut world: NonNull<World>) {
|
|
let world = unsafe { world.as_mut() };
|
|
self.old_dt = world.try_get_resource().map(|r| *r);
|
|
|
|
world.add_resource(DeltaTime::from(self.fixed_time));
|
|
}
|
|
|
|
fn undo_world_modifications(&mut self, mut world: NonNull<World>) {
|
|
let world = unsafe { world.as_mut() };
|
|
world.add_resource(
|
|
self.old_dt
|
|
.expect("DeltaTime resource was somehow never got from the world"),
|
|
);
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct TpsAccumulator(f32);
|
|
|
|
#[derive(Component)]
|
|
struct CubeFlag;
|
|
|
|
#[async_std::main]
|
|
async fn main() {
|
|
let setup_sys = |world: &mut World| -> anyhow::Result<()> {
|
|
{
|
|
/* let mut window_options = world.get_resource_mut::<Ct<WindowOptions>>();
|
|
window_options.cursor_grab = CursorGrabMode::Confined;
|
|
window_options.cursor_visible = false; */
|
|
}
|
|
|
|
let resman = world.get_resource_mut::<ResourceManager>();
|
|
//let diffuse_texture = resman.request::<Texture>("assets/happy-tree.png").unwrap();
|
|
//let antique_camera_model = resman.request::<Model>("assets/AntiqueCamera.glb").unwrap();
|
|
//let cube_model = resman.request::<Model>("assets/cube-texture-bin.glb").unwrap();
|
|
let cube_gltf = resman.request::<Gltf>("../assets/texture-sep/texture-sep.gltf").unwrap();
|
|
/*let crate_gltf = resman.request::<Gltf>("assets/crate/crate.gltf").unwrap();
|
|
|
|
let separate_gltf = resman.request::<Gltf>("assets/pos-testing/child-node-cubes.glb").unwrap(); */
|
|
//drop(resman);
|
|
|
|
cube_gltf.wait_recurse_dependencies_load();
|
|
let cube_mesh = &cube_gltf.data_ref()
|
|
.unwrap().meshes[0];
|
|
/* let crate_mesh = &crate_gltf.data_ref()
|
|
.unwrap().meshes[0];
|
|
|
|
let separate_scene = &separate_gltf.data_ref()
|
|
.unwrap().scenes[0]; */
|
|
|
|
let sponza_model = resman.request::<Gltf>("../assets/sponza/Sponza.gltf").unwrap();
|
|
drop(resman);
|
|
|
|
sponza_model.wait_recurse_dependencies_load();
|
|
let sponza_scene = &sponza_model.data_ref()
|
|
.unwrap().scenes[0];
|
|
|
|
world.spawn((
|
|
sponza_scene.clone(),
|
|
WorldTransform::default(),
|
|
Transform::from_xyz(0.0, 0.0, 0.0),
|
|
));
|
|
|
|
{
|
|
let mut light_tran = Transform::from_xyz(1.5, 2.5, 0.0);
|
|
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
|
light_tran.rotate_x(math::Angle::Degrees(-45.0));
|
|
light_tran.rotate_y(math::Angle::Degrees(25.0));
|
|
world.spawn((
|
|
DirectionalLight {
|
|
enabled: true,
|
|
color: Vec3::ONE,
|
|
intensity: 0.35
|
|
//..Default::default()
|
|
},
|
|
light_tran,
|
|
));
|
|
}
|
|
|
|
{
|
|
let t = Transform::new(
|
|
//Vec3::new(-5.0, 1.0, -1.28),
|
|
Vec3::new(-5.0, 1.0, -0.0),
|
|
//Vec3::new(-10.0, 0.94, -0.28),
|
|
|
|
Quat::IDENTITY,
|
|
Vec3::new(0.25, 0.25, 0.25),
|
|
);
|
|
|
|
world.spawn((
|
|
PointLight {
|
|
enabled: true,
|
|
color: Vec3::new(0.0, 0.0, 1.0),
|
|
intensity: 1.0,
|
|
range: 2.0,
|
|
..Default::default()
|
|
},
|
|
WorldTransform::from(t),
|
|
t,
|
|
cube_mesh.clone(),
|
|
));
|
|
|
|
let t = Transform::new(
|
|
Vec3::new(-3.0, 0.2, -1.5),
|
|
//Vec3::new(-5.0, 1.0, -0.28),
|
|
//Vec3::new(-10.0, 0.94, -0.28),
|
|
|
|
Quat::IDENTITY,
|
|
Vec3::new(0.15, 0.15, 0.15),
|
|
);
|
|
|
|
world.spawn((
|
|
PointLight {
|
|
enabled: true,
|
|
color: Vec3::new(0.0, 0.5, 1.0),
|
|
intensity: 1.0,
|
|
range: 1.0,
|
|
..Default::default()
|
|
},
|
|
WorldTransform::from(t),
|
|
t,
|
|
cube_mesh.clone(),
|
|
));
|
|
|
|
let t = Transform::new(
|
|
Vec3::new(0.0, 0.2, -1.5),
|
|
//Vec3::new(-5.0, 1.0, -0.28),
|
|
//Vec3::new(-10.0, 0.94, -0.28),
|
|
|
|
Quat::IDENTITY,
|
|
Vec3::new(0.15, 0.15, 0.15),
|
|
);
|
|
|
|
world.spawn((
|
|
SpotLight {
|
|
enabled: true,
|
|
color: Vec3::new(1.0, 0.0, 0.0),
|
|
intensity: 1.0,
|
|
range: 1.5,
|
|
//cutoff: math::Angle::Degrees(45.0),
|
|
..Default::default()
|
|
},
|
|
WorldTransform::from(t),
|
|
t,
|
|
cube_mesh.clone(),
|
|
));
|
|
}
|
|
|
|
/* {
|
|
let mut light_tran = Transform::from_xyz(2.0, 2.5, -9.5);
|
|
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
|
world.spawn((
|
|
PointLight {
|
|
color: Vec3::new(0.0, 0.0, 1.0),
|
|
|
|
intensity: 3.3,
|
|
|
|
constant: 1.0,
|
|
linear: 0.09,
|
|
quadratic: 0.032,
|
|
|
|
ambient: 0.2,
|
|
diffuse: 1.0,
|
|
specular: 1.3,
|
|
},
|
|
Transform::from(light_tran),
|
|
cube_mesh.clone(),
|
|
));
|
|
} */
|
|
|
|
/* {
|
|
let mut light_tran = Transform::from_xyz(-5.0, 2.5, -9.5);
|
|
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
|
world.spawn((
|
|
PointLight {
|
|
color: Vec3::new(1.0, 1.0, 1.0),
|
|
|
|
intensity: 1.0,
|
|
|
|
constant: 1.0,
|
|
linear: 0.045,
|
|
quadratic: 0.0075,
|
|
|
|
ambient: 0.1,
|
|
diffuse: 1.0,
|
|
specular: 1.3,
|
|
},
|
|
TransformComponent::from(light_tran),
|
|
ModelComponent(cube_model.clone()),
|
|
));
|
|
} */
|
|
|
|
let mut camera = CameraComponent::new_3d();
|
|
// these values were taken by manually positioning the camera in the scene.
|
|
camera.transform = Transform::new(
|
|
Vec3::new(-10.0, 0.94, -0.28),
|
|
Quat::from_xyzw(0.03375484, -0.7116095, 0.0342693, 0.70092666),
|
|
Vec3::ONE
|
|
);
|
|
//camera.transform.translation += math::Vec3::new(0.0, 0.0, 5.5);
|
|
world.spawn(( camera, FreeFlyCamera::default() ));
|
|
|
|
Ok(())
|
|
};
|
|
|
|
let camera_debug_plugin = move |app: &mut App| {
|
|
let sys = |handler: Res<ActionHandler>, view: View<&mut CameraComponent>| -> anyhow::Result<()> {
|
|
if handler.was_action_just_pressed("Debug") {
|
|
for mut cam in view.into_iter() {
|
|
cam.debug = !cam.debug;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
};
|
|
|
|
game.with_system("camera_debug_trigger", sys, &[]);
|
|
game.with_system("update_world_transforms", scene::system_update_world_transforms, &[]);
|
|
};
|
|
|
|
let action_handler_plugin = |app: &mut App| {
|
|
let action_handler = ActionHandler::builder()
|
|
.add_layout(LayoutId::from(0))
|
|
|
|
.add_action(ACTLBL_MOVE_FORWARD_BACKWARD, Action::new(ActionKind::Axis))
|
|
.add_action(ACTLBL_MOVE_LEFT_RIGHT, Action::new(ActionKind::Axis))
|
|
.add_action(ACTLBL_MOVE_UP_DOWN, Action::new(ActionKind::Axis))
|
|
.add_action(ACTLBL_LOOK_LEFT_RIGHT, Action::new(ActionKind::Axis))
|
|
.add_action(ACTLBL_LOOK_UP_DOWN, Action::new(ActionKind::Axis))
|
|
.add_action(ACTLBL_LOOK_ROLL, Action::new(ActionKind::Axis))
|
|
.add_action("Debug", Action::new(ActionKind::Button))
|
|
|
|
.add_mapping(ActionMapping::builder(LayoutId::from(0), ActionMappingId::from(0))
|
|
.bind(ACTLBL_MOVE_FORWARD_BACKWARD, &[
|
|
ActionSource::Keyboard(KeyCode::W).into_binding_modifier(1.0),
|
|
ActionSource::Keyboard(KeyCode::S).into_binding_modifier(-1.0)
|
|
])
|
|
.bind(ACTLBL_MOVE_LEFT_RIGHT, &[
|
|
ActionSource::Keyboard(KeyCode::A).into_binding_modifier(-1.0),
|
|
ActionSource::Keyboard(KeyCode::D).into_binding_modifier(1.0)
|
|
])
|
|
.bind(ACTLBL_MOVE_UP_DOWN, &[
|
|
ActionSource::Keyboard(KeyCode::C).into_binding_modifier(1.0),
|
|
ActionSource::Keyboard(KeyCode::Z).into_binding_modifier(-1.0)
|
|
])
|
|
.bind(ACTLBL_LOOK_LEFT_RIGHT, &[
|
|
ActionSource::Mouse(MouseInput::Axis(MouseAxis::X)).into_binding(),
|
|
ActionSource::Keyboard(KeyCode::Left).into_binding_modifier(-1.0),
|
|
ActionSource::Keyboard(KeyCode::Right).into_binding_modifier(1.0),
|
|
//ActionSource::Gamepad(GamepadFormat::DualAxis, GamepadInput::Axis(GamepadAxis::RThumbstickX)).into_binding(),
|
|
])
|
|
.bind(ACTLBL_LOOK_UP_DOWN, &[
|
|
ActionSource::Mouse(MouseInput::Axis(MouseAxis::Y)).into_binding(),
|
|
ActionSource::Keyboard(KeyCode::Up).into_binding_modifier(-1.0),
|
|
ActionSource::Keyboard(KeyCode::Down).into_binding_modifier(1.0),
|
|
//ActionSource::Gamepad(GamepadFormat::DualAxis, GamepadInput::Axis(GamepadAxis::RThumbstickY)).into_binding(),
|
|
])
|
|
.bind(ACTLBL_LOOK_ROLL, &[
|
|
ActionSource::Keyboard(KeyCode::E).into_binding_modifier(-1.0),
|
|
ActionSource::Keyboard(KeyCode::Q).into_binding_modifier(1.0),
|
|
])
|
|
.bind("Debug", &[
|
|
ActionSource::Keyboard(KeyCode::B).into_binding(),
|
|
])
|
|
.finish()
|
|
).finish();
|
|
|
|
let world = game.world_mut();
|
|
world.add_resource(action_handler);
|
|
game.with_plugin(InputActionPlugin);
|
|
};
|
|
|
|
/* let script_test_plugin = |app: &mut App| {
|
|
game.with_plugin(LuaScriptingPlugin);
|
|
|
|
let world = game.world_mut();
|
|
let mut res_man = world.get_resource_mut::<ResourceManager>();
|
|
let script = res_man.request::<LuaScript>("scripts/test.lua").unwrap();
|
|
res_man.watch("scripts/test.lua", false).unwrap();
|
|
drop(res_man);
|
|
|
|
let script = Script::new("test.lua", script);
|
|
let scripts = ScriptList::new(vec![script]);
|
|
world.spawn((scripts,));
|
|
|
|
}; */
|
|
|
|
Game::initialize().await
|
|
.with_plugin(lyra_engine::DefaultPlugins)
|
|
.with_startup_system(setup_sys.into_system())
|
|
.with_plugin(action_handler_plugin)
|
|
//.with_plugin(script_test_plugin)
|
|
//.with_plugin(fps_plugin)
|
|
.with_plugin(camera_debug_plugin)
|
|
.with_plugin(FreeFlyCameraPlugin)
|
|
.run().await;
|
|
}
|