2023-12-27 04:48:46 +00:00
|
|
|
use std::ptr::NonNull;
|
|
|
|
|
2024-01-13 16:52:20 +00:00
|
|
|
use lyra_engine::{math::{self, Vec3}, math::Transform, input::{KeyCode, ActionHandler, Action, ActionKind, LayoutId, ActionMapping, ActionSource, ActionMappingId, InputActionPlugin, MouseInput, MouseAxis, CommonActionLabel}, game::Game, render::{window::{CursorGrabMode, WindowOptions}, light::{PointLight, directional::DirectionalLight, SpotLight}}, change_tracker::Ct, ecs::{system::{Criteria, CriteriaSchedule, BatchedSystem, IntoSystem}, world::World, Component}, DeltaTime, scene::{ModelComponent, CameraComponent}, lua::{LuaScriptingPlugin, LuaScript}, Script, ScriptList};
|
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};
|
|
|
|
|
2024-01-07 02:38:21 +00:00
|
|
|
#[derive(Clone, Copy, Hash, Debug)]
|
|
|
|
pub enum ActionLabel {
|
|
|
|
MoveForwardBackward,
|
|
|
|
MoveLeftRight,
|
|
|
|
MoveUpDown,
|
|
|
|
LookLeftRight,
|
|
|
|
LookUpDown,
|
|
|
|
LookRoll,
|
|
|
|
}
|
|
|
|
|
2023-10-29 21:54:04 +00:00
|
|
|
struct FixedTimestep {
|
|
|
|
max_tps: u32,
|
|
|
|
fixed_time: f32,
|
|
|
|
accumulator: f32,
|
|
|
|
}
|
|
|
|
|
2024-01-06 20:40:13 +00:00
|
|
|
#[allow(dead_code)]
|
2023-10-29 21:54:04 +00:00
|
|
|
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 {
|
2023-12-27 04:48:46 +00:00
|
|
|
fn can_run(&mut self, mut world: NonNull<World>, check_count: u32) -> CriteriaSchedule {
|
|
|
|
let world = unsafe { world.as_mut() };
|
2023-10-29 21:54:04 +00:00
|
|
|
if check_count == 0 {
|
2023-12-26 19:12:53 +00:00
|
|
|
let delta_time = world.get_resource::<DeltaTime>();
|
2023-10-29 21:54:04 +00:00
|
|
|
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
|
|
|
{
|
2024-01-13 16:52:20 +00:00
|
|
|
/* let mut window_options = world.get_resource_mut::<Ct<WindowOptions>>();
|
2023-09-29 18:57:22 +00:00
|
|
|
window_options.cursor_grab = CursorGrabMode::Confined;
|
2024-01-13 16:52:20 +00:00
|
|
|
window_options.cursor_visible = false; */
|
2023-10-26 01:49:38 +00:00
|
|
|
}
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-12-26 19:12:53 +00:00
|
|
|
let mut resman = world.get_resource_mut::<ResourceManager>();
|
2023-10-23 20:56:55 +00:00
|
|
|
//let diffuse_texture = resman.request::<Texture>("assets/happy-tree.png").unwrap();
|
2024-02-17 19:27:16 +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);
|
|
|
|
|
2024-02-17 19:27:16 +00:00
|
|
|
/* world.spawn((
|
2023-10-22 02:19:34 +00:00
|
|
|
ModelComponent(antique_camera_model),
|
2024-01-13 16:52:20 +00:00
|
|
|
Transform::from_xyz(0.0, -5.0, -10.0),
|
2024-02-17 19:27:16 +00:00
|
|
|
)); */
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2023-11-19 18:19:35 +00:00
|
|
|
{
|
2023-12-28 03:53:58 +00:00
|
|
|
let cube_tran = Transform::from_xyz(-3.5, 0.0, -8.0);
|
2023-11-19 18:19:35 +00:00
|
|
|
//cube_tran.rotate_y(math::Angle::Degrees(180.0));
|
|
|
|
world.spawn((
|
2024-01-13 16:52:20 +00:00
|
|
|
cube_tran,
|
2023-11-19 18:19:35 +00:00
|
|
|
ModelComponent(crate_model.clone()),
|
|
|
|
CubeFlag,
|
|
|
|
));
|
|
|
|
}
|
2023-11-10 22:52:11 +00:00
|
|
|
|
2023-11-19 18:19:35 +00:00
|
|
|
{
|
|
|
|
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 {
|
|
|
|
color: Vec3::new(1.0, 1.0, 1.0),
|
|
|
|
ambient: 0.3,
|
|
|
|
diffuse: 1.0,
|
|
|
|
specular: 1.3,
|
|
|
|
},
|
2024-01-13 16:52:20 +00:00
|
|
|
light_tran,
|
2023-11-19 18:19:35 +00:00
|
|
|
ModelComponent(cube_model.clone()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut light_tran = Transform::from_xyz(-3.5, 0.2, -4.5);
|
|
|
|
light_tran.scale = Vec3::new(0.5, 0.5, 0.5);
|
|
|
|
world.spawn((
|
|
|
|
SpotLight {
|
|
|
|
color: Vec3::new(1.0, 0.2, 0.2),
|
|
|
|
cutoff: math::Angle::Degrees(12.5),
|
|
|
|
outer_cutoff: math::Angle::Degrees(17.5),
|
|
|
|
|
|
|
|
constant: 1.0,
|
|
|
|
linear: 0.007,
|
|
|
|
quadratic: 0.0002,
|
|
|
|
|
|
|
|
ambient: 0.0,
|
|
|
|
diffuse: 7.0,
|
|
|
|
specular: 1.0,
|
|
|
|
},
|
2024-01-13 16:52:20 +00:00
|
|
|
Transform::from(light_tran),
|
2023-11-19 18:19:35 +00:00
|
|
|
ModelComponent(cube_model.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 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,
|
|
|
|
},
|
2024-01-13 16:52:20 +00:00
|
|
|
Transform::from(light_tran),
|
2023-11-19 18:19:35 +00:00
|
|
|
ModelComponent(cube_model),
|
|
|
|
));
|
|
|
|
}
|
2023-11-10 17:28:17 +00:00
|
|
|
|
2023-09-29 18:57:22 +00:00
|
|
|
let mut camera = CameraComponent::new_3d();
|
2023-11-19 18:19:35 +00:00
|
|
|
camera.transform.translation += math::Vec3::new(0.0, 0.0, 5.5);
|
2023-10-25 00:03:27 +00:00
|
|
|
world.spawn(( camera, FreeFlyCamera::default() ));
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
2024-01-06 20:40:13 +00:00
|
|
|
#[allow(unused_variables)]
|
2023-10-29 21:54:04 +00:00
|
|
|
let fps_system = |world: &mut World| -> anyhow::Result<()> {
|
2023-12-26 19:12:53 +00:00
|
|
|
let mut counter = world.get_resource_mut::<fps_counter::FPSCounter>();
|
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| {
|
2024-02-23 21:43:23 +00:00
|
|
|
let world = game.world_mut();
|
2023-12-26 19:12:53 +00:00
|
|
|
world.add_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-19 18:19:35 +00:00
|
|
|
const SPEED: f32 = 4.0;
|
2023-12-26 19:12:53 +00:00
|
|
|
let delta_time = **world.get_resource::<DeltaTime>();
|
2023-09-29 18:57:22 +00:00
|
|
|
|
2024-01-13 16:52:20 +00:00
|
|
|
for (mut transform, _) in world.view_iter::<(&mut Transform, &CubeFlag)>() {
|
|
|
|
let t = &mut 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
|
|
|
}
|
|
|
|
|
2024-01-13 16:52:20 +00:00
|
|
|
for (mut transform, _s) in world.view_iter::<(&mut Transform, &mut SpotLight)>() {
|
|
|
|
let t = &mut transform;
|
2023-11-19 18:19:35 +00:00
|
|
|
t.rotate_x(math::Angle::Degrees(SPEED * delta_time));
|
2023-12-26 19:12:53 +00:00
|
|
|
}
|
2023-11-19 18:19:35 +00:00
|
|
|
|
2023-09-29 18:57:22 +00:00
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
let jiggle_plugin = move |game: &mut Game| {
|
2024-02-23 21:43:23 +00:00
|
|
|
game.world_mut().add_resource(TpsAccumulator(0.0));
|
2023-10-29 21:54:04 +00:00
|
|
|
|
|
|
|
let mut sys = BatchedSystem::new();
|
2023-11-03 23:50:00 +00:00
|
|
|
sys.with_criteria(FixedTimestep::new(45));
|
2023-12-27 04:48:46 +00:00
|
|
|
sys.with_system(spin_system.into_system());
|
2023-11-17 19:52:38 +00:00
|
|
|
//sys.with_system(fps_system);
|
2023-10-29 21:54:04 +00:00
|
|
|
|
2024-01-13 16:52:20 +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| {
|
2024-02-25 22:06:53 +00:00
|
|
|
/* let action_handler = ActionHandler::builder()
|
2023-11-06 03:50:57 +00:00
|
|
|
.add_layout(LayoutId::from(0))
|
2024-01-07 01:15:54 +00:00
|
|
|
|
2024-01-07 02:38:21 +00:00
|
|
|
.add_action(CommonActionLabel::MoveForwardBackward, Action::new(ActionKind::Axis))
|
|
|
|
.add_action(CommonActionLabel::MoveLeftRight, Action::new(ActionKind::Axis))
|
|
|
|
.add_action(CommonActionLabel::MoveUpDown, Action::new(ActionKind::Axis))
|
|
|
|
.add_action(CommonActionLabel::LookLeftRight, Action::new(ActionKind::Axis))
|
|
|
|
.add_action(CommonActionLabel::LookUpDown, Action::new(ActionKind::Axis))
|
|
|
|
.add_action(CommonActionLabel::LookRoll, Action::new(ActionKind::Axis))
|
2024-01-07 01:15:54 +00:00
|
|
|
|
2024-02-25 22:06:53 +00:00
|
|
|
.add_mapping(ActionMapping::builder(LayoutId::from(0), ActionMappingId::from(0))
|
2024-01-07 02:38:21 +00:00
|
|
|
.bind(CommonActionLabel::MoveForwardBackward, &[
|
2023-11-06 03:50:57 +00:00
|
|
|
ActionSource::Keyboard(KeyCode::W).into_binding_modifier(1.0),
|
|
|
|
ActionSource::Keyboard(KeyCode::S).into_binding_modifier(-1.0)
|
|
|
|
])
|
2024-01-07 02:38:21 +00:00
|
|
|
.bind(CommonActionLabel::MoveLeftRight, &[
|
2024-01-07 01:15:54 +00:00
|
|
|
ActionSource::Keyboard(KeyCode::A).into_binding_modifier(-1.0),
|
|
|
|
ActionSource::Keyboard(KeyCode::D).into_binding_modifier(1.0)
|
|
|
|
])
|
2024-01-07 02:38:21 +00:00
|
|
|
.bind(CommonActionLabel::MoveUpDown, &[
|
2024-01-07 01:15:54 +00:00
|
|
|
ActionSource::Keyboard(KeyCode::C).into_binding_modifier(1.0),
|
|
|
|
ActionSource::Keyboard(KeyCode::Z).into_binding_modifier(-1.0)
|
|
|
|
])
|
2024-01-07 02:38:21 +00:00
|
|
|
.bind(CommonActionLabel::LookLeftRight, &[
|
2024-01-07 01:15:54 +00:00
|
|
|
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(),
|
|
|
|
])
|
2024-01-07 02:38:21 +00:00
|
|
|
.bind(CommonActionLabel::LookUpDown, &[
|
2024-01-07 01:15:54 +00:00
|
|
|
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(),
|
|
|
|
])
|
2024-01-07 02:38:21 +00:00
|
|
|
.bind(CommonActionLabel::LookRoll, &[
|
2024-01-07 01:15:54 +00:00
|
|
|
ActionSource::Keyboard(KeyCode::E).into_binding_modifier(-1.0),
|
|
|
|
ActionSource::Keyboard(KeyCode::Q).into_binding_modifier(1.0),
|
2023-11-06 03:50:57 +00:00
|
|
|
])
|
|
|
|
.finish()
|
|
|
|
);
|
|
|
|
|
2024-02-23 21:43:23 +00:00
|
|
|
let world = game.world_mut();
|
2024-01-13 16:52:20 +00:00
|
|
|
world.add_resource(action_handler);
|
2024-02-25 22:06:53 +00:00
|
|
|
game.with_plugin(InputActionPlugin); */
|
2023-11-06 03:50:57 +00:00
|
|
|
};
|
2024-01-07 04:06:00 +00:00
|
|
|
|
|
|
|
let script_test_plugin = |game: &mut Game| {
|
|
|
|
game.with_plugin(LuaScriptingPlugin);
|
|
|
|
|
2024-02-23 21:43:23 +00:00
|
|
|
let world = game.world_mut();
|
2024-01-07 04:06:00 +00:00
|
|
|
let mut res_man = world.get_resource_mut::<ResourceManager>();
|
|
|
|
let script = res_man.request::<LuaScript>("scripts/test.lua").unwrap();
|
2024-01-16 04:22:21 +00:00
|
|
|
res_man.watch("scripts/test.lua", false).unwrap();
|
2024-01-07 04:06:00 +00:00
|
|
|
drop(res_man);
|
|
|
|
|
|
|
|
let script = Script::new("test.lua", script);
|
|
|
|
let scripts = ScriptList::new(vec![script]);
|
|
|
|
world.spawn((scripts,));
|
|
|
|
|
|
|
|
};
|
2023-09-29 18:57:22 +00:00
|
|
|
|
|
|
|
Game::initialize().await
|
|
|
|
.with_plugin(lyra_engine::DefaultPlugins)
|
2023-12-27 04:48:46 +00:00
|
|
|
.with_startup_system(setup_sys.into_system())
|
2023-11-06 03:50:57 +00:00
|
|
|
.with_plugin(action_handler_plugin)
|
2024-01-07 04:06:00 +00:00
|
|
|
.with_plugin(script_test_plugin)
|
2023-09-29 18:57:22 +00:00
|
|
|
//.with_plugin(fps_plugin)
|
|
|
|
.with_plugin(jiggle_plugin)
|
2024-02-25 22:06:53 +00:00
|
|
|
//.with_plugin(FreeFlyCameraPlugin)
|
2023-09-29 18:57:22 +00:00
|
|
|
.run().await;
|
|
|
|
}
|