got display 3d color changing working

selection-refactor
Elijah Voigt 2 years ago
parent 9da2109dd9
commit c987ad10b1

BIN
assets/models/Martian Chess.glb (Stored with Git LFS)

Binary file not shown.

@ -17,39 +17,33 @@ impl Plugin for Display3dPlugin {
OnExit(GameState::Loading),
(initialize, fix_skybox.before(initialize)),
)
.add_systems(
Update,
menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
)
.add_systems(
Update,
(
set_piece_position.run_if(any_component_changed::<BoardIndex>),
set_piece_model.run_if(any_component_changed::<Side>),
menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
set_piece_model.run_if(any_component_added::<Piece3d>),
set_board_model.run_if(any_component_added::<Board3d>),
set_piece_position.run_if(any_component_changed::<BoardIndex>),
set_piece_texture.run_if(any_component_changed::<Side>),
),
)
.add_systems(
Update,
gizmo_system
.run_if(in_state(GameState::Display3d))
.run_if(resource_exists::<debug::DebugEnabled>()),
)
.add_systems(
Update,
(
move_camera
.run_if(in_state(GameState::Display3d))
.run_if(on_event::<MouseMotion>())
.run_if(resource_exists::<debug::DebugEnabled>()),
)
.add_systems(
Update,
.run_if(on_event::<MouseMotion>()),
gizmo_system.run_if(in_state(GameState::Display3d)),
mouse_zoom
.run_if(in_state(GameState::Display3d))
.run_if(on_event::<MouseWheel>())
.run_if(on_event::<MouseWheel>()),
)
.run_if(resource_exists::<debug::DebugEnabled>()),
)
.add_systems(OnEnter(GameState::Display3d), activate::<Display3d>)
.add_systems(
OnEnter(GameState::Display3d),
(activate::<Display3d>, set_piece_texture),
)
.add_systems(OnExit(GameState::Display3d), deactivate::<Display3d>);
}
}
@ -80,7 +74,7 @@ fn load_assets(server: Res<AssetServer>, mut commands: Commands) {
}
/// Initialize the 3d board
fn initialize(mut commands: Commands, board: Option<Res<game::Board>>, assets: Res<AssetsMap>) {
fn initialize(mut commands: Commands, board: Res<game::Board>, assets: Res<AssetsMap>) {
info!("Initialize 3d camera");
// let handle = server.load("images/mars.hdr");
commands.spawn((
@ -130,7 +124,7 @@ fn initialize(mut commands: Commands, board: Option<Res<game::Board>>, assets: R
parent
.spawn((Display3d, Board3d, SceneBundle { ..default() }))
.with_children(|parent| {
board.unwrap().pieces().iter().for_each(|(index, piece)| {
board.pieces().iter().for_each(|(index, piece)| {
let side = Board::side(index).expect("Spawn valid side");
parent.spawn((
@ -161,14 +155,14 @@ fn fix_skybox(mut images: ResMut<Assets<Image>>, assets: Res<AssetsMap>) {
}
}
/// Set the model for each piece based on the game::Piece::* marker
fn set_piece_model(
mut events: Query<(&mut Handle<Scene>, &Piece), (With<Piece3d>, Changed<Side>)>,
assets_map: Option<Res<AssetsMap>>,
mut events: Query<(&mut Handle<Scene>, &Piece), Added<Piece3d>>,
assets_map: Res<AssetsMap>,
gltfs: Res<Assets<Gltf>>,
) {
let mf = assets_map.expect("Models file");
events.iter_mut().for_each(|(mut handle, piece)| {
let gltf = gltfs.get(&mf.models).expect("Load GLTF content");
let gltf = gltfs.get(&assets_map.models).expect("Load GLTF content");
*handle = match piece {
game::Piece::Pawn => gltf.named_scenes.get("Pawn"),
game::Piece::Drone => gltf.named_scenes.get("Drone"),
@ -181,12 +175,11 @@ fn set_piece_model(
fn set_board_model(
mut events: Query<&mut Handle<Scene>, With<Board3d>>,
assets_map: Option<Res<AssetsMap>>,
assets_map: Res<AssetsMap>,
gltfs: Res<Assets<Gltf>>,
) {
let mf = assets_map.expect("Models file");
events.iter_mut().for_each(|mut handle| {
let gltf = gltfs.get(&mf.models).expect("Load GLTF content");
let gltf = gltfs.get(&assets_map.models).expect("Load GLTF content");
*handle = gltf
.named_scenes
.get("Gameboard")
@ -259,3 +252,118 @@ fn mouse_zoom(
});
});
}
/// Set the Texture for a piece given it's position (left or right) on the bord.
/// Executed when Side is changed or upon entry to Display3d state
/// Getting this to run _after_ GLTF is loaded is a pain.
/// PERF: We are saving what to work on in a Vector which is bad.
/// CAVEAT: We are only exeucting this when a piece changes or state is changed.
fn set_piece_texture(
events: Query<(Entity, &Piece, &Side), (With<Piece3d>, Changed<Side>)>,
all: Query<(Entity, &Piece, &Side), With<Piece3d>>,
gltfs: Res<Assets<Gltf>>,
assets_map: Res<AssetsMap>,
children: Query<&Children>,
mut models: Query<(&Name, &mut Handle<StandardMaterial>)>,
) {
let pieces = if events.is_empty() {
all.iter().collect::<Vec<(Entity, &Piece, &Side)>>()
} else {
events.iter().collect::<Vec<(Entity, &Piece, &Side)>>()
};
pieces.iter().for_each(|(entity, piece, side)| {
if let Some(gltf) = gltfs.get(&assets_map.models) {
children.iter_descendants(*entity).for_each(|child| {
if let Ok((n, mut m)) = models.get_mut(child) {
match (*piece, *side, n.as_str()) {
(Piece::Queen, Side::A, "Queen.0") => {
*m = gltf
.named_materials
.get("Queen")
.expect("Load Red Queen texture")
.clone()
}
(Piece::Queen, Side::A, "Queen.1") => {
*m = gltf
.named_materials
.get("Dots")
.expect("Load Red Dots texture")
.clone()
}
(Piece::Queen, Side::B, "Queen.0") => {
*m = gltf
.named_materials
.get("QueenBlue")
.expect("Load Blue Queen texture")
.clone()
}
(Piece::Queen, Side::B, "Queen.1") => {
*m = gltf
.named_materials
.get("DotsBlue")
.expect("Load Red Dots texture")
.clone()
}
(Piece::Drone, Side::A, "Drone.0") => {
*m = gltf
.named_materials
.get("Drone")
.expect("Load Red Drone texture")
.clone()
}
(Piece::Drone, Side::A, "Drone.1") => {
*m = gltf
.named_materials
.get("Dots")
.expect("Load Red Dots texture")
.clone()
}
(Piece::Drone, Side::B, "Drone.0") => {
*m = gltf
.named_materials
.get("DroneBlue")
.expect("Load Blue Drone texture")
.clone()
}
(Piece::Drone, Side::B, "Drone.1") => {
*m = gltf
.named_materials
.get("DotsBlue")
.expect("Load Blue Dots texture")
.clone()
}
(Piece::Pawn, Side::A, "Pawn.0") => {
*m = gltf
.named_materials
.get("Pawn")
.expect("Load Red Pawn texture")
.clone()
}
(Piece::Pawn, Side::A, "Pawn.1") => {
*m = gltf
.named_materials
.get("Dots")
.expect("Load Red Dots texture")
.clone()
}
(Piece::Pawn, Side::B, "Pawn.0") => {
*m = gltf
.named_materials
.get("DroneBlue") // TODO: FIX
.expect("Load Blue Pawn texture")
.clone()
}
(Piece::Pawn, Side::B, "Pawn.1") => {
*m = gltf
.named_materials
.get("DotsBlue")
.expect("Load Blue Dots texture")
.clone()
}
_ => warn!("???"),
}
}
});
}
})
}

@ -159,3 +159,9 @@ pub(crate) fn any_component_changed<C: Component>(q: Query<Entity, Changed<C>>)
pub(crate) fn any_component_added<C: Component>(q: Query<Entity, Added<C>>) -> bool {
!q.is_empty()
}
pub(crate) fn any_component_added_or_changed<C: Component>(
q: Query<Entity, Or<(Added<C>, Changed<C>)>>,
) -> bool {
!q.is_empty()
}

Loading…
Cancel
Save