Created
September 12, 2021 22:19
-
-
Save afonsolage/445fdc0b66b2765bec21411ec99b3f81 to your computer and use it in GitHub Desktop.
drawing test
This file contains hidden or 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::prelude::shape::{Cube, UVSphere}; | |
use bevy::prelude::*; | |
use rand::Rng; | |
fn main() { | |
App::new() | |
.add_plugins(DefaultPlugins) | |
.add_startup_system(spawn_test_world) | |
.add_startup_system(spawn_character) | |
.run(); | |
} | |
fn spawn_test_world( | |
mut commands: Commands, | |
mut materials: ResMut<Assets<StandardMaterial>>, | |
mut meshes: ResMut<Assets<Mesh>>, | |
) { | |
let cube = meshes.add(Mesh::from(Cube { size: 1.0 })); | |
let sphere = meshes.add(Mesh::from(UVSphere::default())); | |
#[derive(Debug, Bundle, Default)] | |
struct LightBundle { | |
directional_light: DirectionalLight, | |
transform: Transform, | |
global_transform: GlobalTransform, | |
} | |
//spawn a light | |
commands.spawn_bundle(LightBundle { | |
directional_light: DirectionalLight::default(), | |
transform: Transform::from_xyz(0.0, 0.0, 0.0), | |
global_transform: GlobalTransform::identity(), | |
}); | |
// a floor | |
let grey = materials.add(Color::hex("2B2D2E").unwrap().into()); | |
let orange = materials.add(Color::rgb(251.0 / 255.0, 99.0 / 255.0, 21.0 / 255.0).into()); | |
let blue = materials.add(Color::rgb(182.0 / 255.0, 226.0 / 255.0, 245.0 / 255.0).into()); | |
commands.spawn_bundle(PbrBundle { | |
material: grey, | |
mesh: cube.clone(), | |
transform: Transform::from_matrix(Mat4::from_scale_rotation_translation( | |
Vec3::new(20.0, 1.0, 20.0), | |
Quat::IDENTITY, | |
-Vec3::Y, | |
)), | |
..Default::default() | |
}); | |
// some shapes for reference | |
let scale = 0.5; | |
let mut rng = rand::thread_rng(); | |
for _ in 0..20 { | |
let x = rng.gen_range(-10.0..10.0); | |
let z = rng.gen_range(-10.0..10.0); | |
commands.spawn_bundle(PbrBundle { | |
material: orange.clone(), | |
mesh: cube.clone(), | |
transform: Transform::from_matrix(Mat4::from_scale_rotation_translation( | |
Vec3::splat(scale), | |
Quat::IDENTITY, | |
Vec3::new(x, 0.5 * (scale - 1.0), z), | |
)), | |
..Default::default() | |
}); | |
let x = rng.gen_range(-10.0..10.0); | |
let z = rng.gen_range(-10.0..10.0); | |
commands.spawn_bundle(PbrBundle { | |
material: blue.clone(), | |
mesh: sphere.clone(), | |
transform: Transform::from_matrix(Mat4::from_scale_rotation_translation( | |
Vec3::splat(scale), | |
Quat::IDENTITY, | |
Vec3::new(x, 0.5 * (scale - 1.0), z), | |
)), | |
..Default::default() | |
}); | |
} | |
} | |
pub fn spawn_character(mut commands: Commands) { | |
commands | |
.spawn_bundle(PerspectiveCameraBundle { | |
transform: Transform::from_matrix(Mat4::face_toward( | |
Vec3::new(0.0, 4.0, 8.0), | |
Vec3::ZERO, | |
Vec3::Y, | |
)), | |
..Default::default() | |
}) | |
.id(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment