|
|
|
@ -4,68 +4,144 @@ pub(crate) struct Display3dPlugin;
|
|
|
|
|
|
|
|
|
|
|
|
impl Plugin for Display3dPlugin {
|
|
|
|
impl Plugin for Display3dPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.add_systems(
|
|
|
|
app.add_systems(Startup, initialize_camera)
|
|
|
|
Startup,
|
|
|
|
.add_systems(OnEnter(GameState::Loading), load_models)
|
|
|
|
(initialize_camera, load_models).run_if(in_state(GameState::Loading)),
|
|
|
|
.add_systems(OnExit(GameState::Loading), initialize_board)
|
|
|
|
)
|
|
|
|
.add_systems(
|
|
|
|
.add_systems(
|
|
|
|
Update,
|
|
|
|
Update,
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
initialize_board.run_if(resource_added::<ModelMap>()),
|
|
|
|
|
|
|
|
menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
|
|
|
|
menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.add_systems(Update, gizmo_system.run_if(in_state(GameState::Display3d)))
|
|
|
|
.add_systems(OnEnter(GameState::Display3d), activate);
|
|
|
|
.add_systems(OnEnter(GameState::Display3d), activate)
|
|
|
|
|
|
|
|
.add_systems(OnExit(GameState::Display3d), deactivate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
struct Board3d;
|
|
|
|
struct Board3d;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Resource)]
|
|
|
|
|
|
|
|
struct ModelMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn initialize_camera(mut commands: Commands) {
|
|
|
|
fn initialize_camera(mut commands: Commands) {
|
|
|
|
commands.spawn((
|
|
|
|
commands.spawn((
|
|
|
|
Camera3dBundle {
|
|
|
|
Camera3dBundle {
|
|
|
|
camera: Camera {
|
|
|
|
camera: Camera {
|
|
|
|
is_active: false,
|
|
|
|
is_active: false,
|
|
|
|
|
|
|
|
hdr: true,
|
|
|
|
..default()
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
..default()
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
UiCameraConfig { show_ui: true },
|
|
|
|
UiCameraConfig { show_ui: true },
|
|
|
|
));
|
|
|
|
));
|
|
|
|
|
|
|
|
// light
|
|
|
|
|
|
|
|
commands.spawn(PointLightBundle {
|
|
|
|
|
|
|
|
point_light: PointLight {
|
|
|
|
|
|
|
|
intensity: 1500.0,
|
|
|
|
|
|
|
|
shadows_enabled: true,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn load_models(server: Res<AssetServer>, mut commands: Commands) {
|
|
|
|
#[derive(Debug, Resource)]
|
|
|
|
warn!("TODO: Load models");
|
|
|
|
struct ModelsFile {
|
|
|
|
|
|
|
|
handle: Handle<Gltf>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commands.insert_resource(ModelMap);
|
|
|
|
/// Load 3d models
|
|
|
|
|
|
|
|
/// This is kind of pulling double duty.
|
|
|
|
|
|
|
|
/// Both loads the GLTF file _and_ populates the ModelMap once that is loaded.
|
|
|
|
|
|
|
|
fn load_models(server: Res<AssetServer>, mut commands: Commands) {
|
|
|
|
|
|
|
|
commands.insert_resource(ModelsFile {
|
|
|
|
|
|
|
|
handle: server.load("models/Martian Chess.glb"),
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn initialize_board(mut commands: Commands, model_map: Option<Res<ModelMap>>) {
|
|
|
|
/// Initialize the 3d board
|
|
|
|
if let Some(models) = model_map {
|
|
|
|
fn initialize_board(
|
|
|
|
warn!("TODO: Intialize 3D Board!");
|
|
|
|
mut commands: Commands,
|
|
|
|
|
|
|
|
model_file: Option<Res<ModelsFile>>,
|
|
|
|
|
|
|
|
gltfs: Res<Assets<Gltf>>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
info!("Initializing board");
|
|
|
|
|
|
|
|
if let Some(mf) = model_file {
|
|
|
|
|
|
|
|
let gltf = gltfs.get(&mf.handle).expect("Load GLTF content");
|
|
|
|
|
|
|
|
|
|
|
|
commands.spawn((
|
|
|
|
info!("Initializing root");
|
|
|
|
SpatialBundle {
|
|
|
|
commands
|
|
|
|
visibility: Visibility::Hidden,
|
|
|
|
.spawn((
|
|
|
|
..default()
|
|
|
|
SpatialBundle {
|
|
|
|
},
|
|
|
|
visibility: Visibility::Hidden,
|
|
|
|
Board3d,
|
|
|
|
..default()
|
|
|
|
));
|
|
|
|
},
|
|
|
|
|
|
|
|
Board3d,
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.with_children(|parent| {
|
|
|
|
|
|
|
|
info!("Initializing 3D lights!");
|
|
|
|
|
|
|
|
parent.spawn((
|
|
|
|
|
|
|
|
Board3d,
|
|
|
|
|
|
|
|
PointLightBundle {
|
|
|
|
|
|
|
|
point_light: PointLight {
|
|
|
|
|
|
|
|
intensity: 1500.0,
|
|
|
|
|
|
|
|
shadows_enabled: true,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
info!("Intializeing 3D Board!");
|
|
|
|
|
|
|
|
parent.spawn((
|
|
|
|
|
|
|
|
Board3d,
|
|
|
|
|
|
|
|
SceneBundle {
|
|
|
|
|
|
|
|
scene: gltf
|
|
|
|
|
|
|
|
.named_scenes
|
|
|
|
|
|
|
|
.get("Gameboard")
|
|
|
|
|
|
|
|
.expect("Game board model")
|
|
|
|
|
|
|
|
.clone(),
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Make this the active state
|
|
|
|
fn activate(
|
|
|
|
fn activate(
|
|
|
|
mut cameras: Query<&mut Camera, With<Camera3d>>,
|
|
|
|
mut cameras: Query<&mut Camera, With<Camera3d>>,
|
|
|
|
mut boards: Query<&mut Visibility, With<Board3d>>,
|
|
|
|
mut boards: Query<&mut Visibility, With<Board3d>>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
cameras.iter_mut().for_each(|mut camera| {
|
|
|
|
cameras.iter_mut().for_each(|mut camera| {
|
|
|
|
|
|
|
|
info!("Activating 3d camera");
|
|
|
|
camera.is_active = true;
|
|
|
|
camera.is_active = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
boards.iter_mut().for_each(|mut visibility| {
|
|
|
|
boards.iter_mut().for_each(|mut visibility| {
|
|
|
|
|
|
|
|
info!("Making entities visible");
|
|
|
|
*visibility = Visibility::Visible;
|
|
|
|
*visibility = Visibility::Visible;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Make this the non-active state
|
|
|
|
|
|
|
|
fn deactivate(
|
|
|
|
|
|
|
|
mut cameras: Query<&mut Camera, With<Camera3d>>,
|
|
|
|
|
|
|
|
mut boards: Query<&mut Visibility, With<Board3d>>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
cameras.iter_mut().for_each(|mut camera| {
|
|
|
|
|
|
|
|
info!("Deactivating 3d camera");
|
|
|
|
|
|
|
|
camera.is_active = false;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
boards.iter_mut().for_each(|mut visibility| {
|
|
|
|
|
|
|
|
info!("Making entities visible");
|
|
|
|
|
|
|
|
*visibility = Visibility::Hidden;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn gizmo_system(mut gizmos: Gizmos) {
|
|
|
|
|
|
|
|
gizmos.cuboid(
|
|
|
|
|
|
|
|
Transform::from_translation(Vec3::Y * 0.5).with_scale(Vec3::splat(1.)),
|
|
|
|
|
|
|
|
Color::WHITE,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|