-
-
Save tgecho/a88a27825f2cb8b404cc0c4699e5bb5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::{ | |
math::Vec3, | |
pbr::{PbrBundle, PointLight, PointLightBundle, StandardMaterial}, | |
prelude::{ | |
shape, App, Assets, Color, Commands, Mesh, Msaa, PerspectiveCameraBundle, Plugin, ResMut, | |
Transform, | |
}, | |
window::WindowDescriptor, | |
DefaultPlugins, | |
}; | |
use bevy_mod_picking::{ | |
DebugCursorPickingPlugin, DebugEventsPickingPlugin, DefaultPickingPlugins, PickableBundle, | |
PickingCameraBundle, | |
}; | |
fn setup( | |
mut commands: Commands, | |
mut meshes: ResMut<Assets<Mesh>>, | |
mut materials: ResMut<Assets<StandardMaterial>>, | |
) { | |
commands | |
.spawn_bundle(PerspectiveCameraBundle { | |
transform: Transform::from_xyz(-6.0, 5.0, 3.5) | |
// change z from 3.5 to 3.499 (or less) fixes it | |
.looking_at(Vec3::new(4.0, 0.0, 3.5), Vec3::Y), | |
..Default::default() | |
}) | |
.insert_bundle(PickingCameraBundle::default()); | |
commands.spawn_bundle(PointLightBundle { | |
point_light: PointLight { | |
intensity: 1500.0, | |
shadows_enabled: true, | |
..Default::default() | |
}, | |
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)), | |
..Default::default() | |
}); | |
} | |
fn create_board( | |
mut commands: Commands, | |
mut meshes: ResMut<Assets<Mesh>>, | |
mut materials: ResMut<Assets<StandardMaterial>>, | |
) { | |
let mesh = meshes.add(Mesh::from(shape::Plane { size: 1. })); | |
let white_material = materials.add(Color::rgb(1., 0.9, 0.9).into()); | |
let black_material = materials.add(Color::rgb(0., 0.1, 0.1).into()); | |
for i in 0..8 { | |
for j in 0..8 { | |
commands | |
.spawn_bundle(PbrBundle { | |
mesh: mesh.clone(), | |
material: if (i + j + 1) % 2 == 0 { | |
white_material.clone() | |
} else { | |
black_material.clone() | |
}, | |
transform: Transform::from_translation(Vec3::new(i as f32, 0., j as f32)), | |
..Default::default() | |
}) | |
.insert_bundle(PickableBundle::default()); | |
} | |
} | |
} | |
pub struct ChessPlugin; | |
impl Plugin for ChessPlugin { | |
fn build(&self, app: &mut App) { | |
app.add_startup_system(setup) | |
.add_startup_system(create_board); | |
} | |
} | |
fn main() { | |
App::new() | |
.insert_resource(Msaa { samples: 4 }) | |
.insert_resource(WindowDescriptor { | |
title: "Chess!".to_string(), | |
width: 1400., | |
height: 1000., | |
..Default::default() | |
}) | |
.add_plugins(DefaultPlugins) | |
.add_plugins(DefaultPickingPlugins) | |
.add_plugin(DebugCursorPickingPlugin) | |
.add_plugin(DebugEventsPickingPlugin) | |
.add_plugin(ChessPlugin) | |
.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment