Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for bevy master #24

Merged
merged 18 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default = [

[dependencies]
winit = {version = "0.24.0", features = ["web-sys"]}
bevy = {version = "0.4.0", default-features = false}
bevy = { git = "https://github.com/bevyengine/bevy/", rev="110ff77db9cdbc2906d0bde0763a8527d79ff243", default-features=false }
regex = "1.4"
cfg-if = "1.0.0"
js-sys = "0.3.45"
Expand All @@ -46,7 +46,8 @@ web-sys = {version = "0.3.45", features = [

[dev-dependencies]
compile-time-run = "0.2"
rand = "0.7"
rand = "0.8"
getrandom = { version = "0.1", features = ["wasm-bindgen"] }

[profile.release]
opt-level = 's'
Expand All @@ -63,7 +64,7 @@ name = "sprite_sheet"
path = "examples/2d/sprite_sheet.rs"

# disabled as it do not compile on github Actions
# [[example]]
#[[example]]
# name = "contributors"
# path = "examples/2d/contributors.rs"

Expand Down
71 changes: 48 additions & 23 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use bevy::prelude::*;
use rand::{prelude::SliceRandom, Rng};
use std::collections::BTreeSet;
use std::{
collections::BTreeSet,
io::{BufRead, BufReader},
process::Stdio,
};

fn main() {
App::build()
Expand Down Expand Up @@ -36,8 +40,8 @@ struct Velocity {
const GRAVITY: f32 = -9.821 * 100.0;
const SPRITE_SIZE: f32 = 75.0;

const COL_DESELECTED: Color = Color::rgb_linear(0.03, 0.03, 0.03);
const COL_SELECTED: Color = Color::rgb_linear(5.0, 5.0, 5.0);
const COL_DESELECTED: Color = Color::rgba_linear(0.03, 0.03, 0.03, 0.92);
const COL_SELECTED: Color = Color::WHITE;

const SHOWCASE_TIMER_SECS: f32 = 3.0;

Expand All @@ -47,11 +51,12 @@ fn setup(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let contribs = contributors();

let texture_handle = asset_server.load("branding/icon.png");

commands
.spawn(Camera2dBundle::default())
.spawn(CameraUiBundle::default());
.spawn(OrthographicCameraBundle::new_2d())
.spawn(UiCameraBundle::default());

let mut sel = ContributorSelection {
order: vec![],
Expand All @@ -61,15 +66,15 @@ fn setup(
let mut rnd = rand::thread_rng();

for name in contribs {
let pos = (rnd.gen_range(-400.0, 400.0), rnd.gen_range(0.0, 400.0));
let dir = rnd.gen_range(-1.0, 1.0);
let pos = (rnd.gen_range(-400.0..400.0), rnd.gen_range(0.0..400.0));
let dir = rnd.gen_range(-1.0..1.0);
let velocity = Vec3::new(dir * 500.0, 0.0, 0.0);
let col = gen_color(&mut rnd);

// some sprites should be flipped
let flipped = rnd.gen_bool(0.5);

let mut transform = Transform::from_translation(Vec3::new(pos.0, pos.1, 0.0));
let mut transform = Transform::from_xyz(pos.0, pos.1, 0.0);
transform.scale.x *= if flipped { -1.0 } else { 1.0 };

commands
Expand Down Expand Up @@ -108,13 +113,25 @@ fn setup(
..Default::default()
},
text: Text {
value: "Contributor showcase".to_string(),
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
style: TextStyle {
font_size: 60.0,
color: Color::WHITE,
..Default::default()
},
sections: vec![
TextSection {
value: "Contributor showcase".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 60.0,
color: Color::WHITE,
},
},
TextSection {
value: "".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 60.0,
color: Color::WHITE,
},
},
],
..Default::default()
},
..Default::default()
});
Expand All @@ -127,9 +144,9 @@ fn select_system(
mut materials: ResMut<Assets<ColorMaterial>>,
mut sel: ResMut<ContributorSelection>,
mut dq: Query<Mut<Text>, With<ContributorDisplay>>,
time: Res<Time>,
mut tq: Query<Mut<Timer>, With<SelectTimer>>,
mut q: Query<(&Contributor, &Handle<ColorMaterial>, &mut Transform)>,
time: Res<Time>,
) {
let mut timer_fired = false;
for mut t in tq.iter_mut() {
Expand Down Expand Up @@ -190,7 +207,9 @@ fn select(

trans.translation.z = 100.0;

text.value = format!("Contributor: {}", name);
text.sections[0].value = "Contributor: ".to_string();
text.sections[1].value = name.to_string();
text.sections[1].style.color = mat.color;

Some(())
}
Expand Down Expand Up @@ -249,7 +268,7 @@ fn collision_system(
if bottom < ground {
t.translation.y = ground + SPRITE_SIZE / 2.0;
// apply an impulse upwards
v.translation.y = rnd.gen_range(700.0, 1000.0);
v.translation.y = rnd.gen_range(700.0..1000.0);
}
if top > ceiling {
t.translation.y = ceiling - SPRITE_SIZE / 2.0;
Expand Down Expand Up @@ -291,6 +310,7 @@ fn contributors() -> Contributors {
"log",
"--pretty=format:%an"
);

stdout.split("\n").map(|x| x.to_string()).collect()
}

Expand All @@ -299,9 +319,14 @@ fn contributors() -> Contributors {
/// Because there is no `Mul<Color> for Color` instead `[f32; 3]` is
/// used.
fn gen_color(rng: &mut impl Rng) -> [f32; 3] {
let r = rng.gen_range(0.2, 1.0);
let g = rng.gen_range(0.2, 1.0);
let b = rng.gen_range(0.2, 1.0);
let v = Vec3::new(r, g, b);
v.normalize().into()
loop {
let rgb = rng.gen();
if luminance(rgb) >= 0.6 {
break rgb;
}
}
}

fn luminance([r, g, b]: [f32; 3]) -> f32 {
0.299 * r + 0.587 * g + 0.114 * b
}
2 changes: 1 addition & 1 deletion examples/2d/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn setup(
) {
let texture_handle = asset_server.load("branding/icon.png");
commands
.spawn(Camera2dBundle::default())
.spawn(OrthographicCameraBundle::new_2d())
.spawn(SpriteBundle {
material: materials.add(texture_handle.into()),
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn setup(
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1);
let texture_atlas_handle = texture_atlases.add(texture_atlas);
commands
.spawn(Camera2dBundle::default())
.spawn(OrthographicCameraBundle::new_2d())
.spawn(SpriteSheetBundle {
texture_atlas: texture_atlas_handle,
transform: Transform::from_scale(Vec3::splat(6.0)),
Expand Down
10 changes: 5 additions & 5 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.insert_resource(Msaa { samples: 4 })
.add_plugins(bevy_webgl2::DefaultPlugins)
.add_startup_system(setup.system())
.run();
Expand All @@ -26,17 +26,17 @@ fn setup(
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_translation(Vec3::new(0.0, 0.5, 0.0)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(-2.0, 2.5, 5.0))
.spawn(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
10 changes: 5 additions & 5 deletions examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ use bevy::prelude::*;

fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.insert_resource(Msaa { samples: 4 })
.add_plugins(bevy_webgl2::DefaultPlugins)
.add_startup_system(setup.system())
.run();
}

fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
commands
.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf"))
.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"))
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 5.0, 4.0)),
transform: Transform::from_xyz(4.0, 5.0, 4.0),
..Default::default()
})
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.7, 0.7, 1.0))
.spawn(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0)
.looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::unit_y()),
..Default::default()
});
Expand Down
12 changes: 6 additions & 6 deletions examples/3d/parenting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::prelude::*;
/// are propagated to their descendants
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.insert_resource(Msaa { samples: 4 })
.add_plugins(bevy_webgl2::DefaultPlugins)
.add_startup_system(setup.system())
.add_system(rotator_system.system())
Expand Down Expand Up @@ -38,7 +38,7 @@ fn setup(
.spawn(PbrBundle {
mesh: cube_handle.clone(),
material: cube_material_handle.clone(),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
transform: Transform::from_xyz(0.0, 0.0, 1.0),
..Default::default()
})
.with(Rotator)
Expand All @@ -47,18 +47,18 @@ fn setup(
parent.spawn(PbrBundle {
mesh: cube_handle,
material: cube_material_handle,
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 3.0)),
transform: Transform::from_xyz(0.0, 0.0, 3.0),
..Default::default()
});
})
// light
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 5.0, -4.0)),
transform: Transform::from_xyz(4.0, 5.0, -4.0),
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(5.0, 10.0, 10.0))
.spawn(PerspectiveCameraBundle {
transform: Transform::from_xyz(5.0, 10.0, 10.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
10 changes: 5 additions & 5 deletions examples/3d/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ fn setup(
// this material renders the texture normally
let material_handle = materials.add(StandardMaterial {
albedo_texture: Some(texture_handle.clone()),
shaded: false,
unlit: true,
..Default::default()
});

// this material modulates the texture to make it red (and slightly transparent)
let red_material_handle = materials.add(StandardMaterial {
albedo: Color::rgba(1.0, 0.0, 0.0, 0.5),
albedo_texture: Some(texture_handle.clone()),
shaded: false,
unlit: true,
});

// and lets make this one blue! (and also slightly transparent)
let blue_material_handle = materials.add(StandardMaterial {
albedo: Color::rgba(0.0, 0.0, 1.0, 0.5),
albedo_texture: Some(texture_handle),
shaded: false,
unlit: true,
});

// add entities to the world
Expand Down Expand Up @@ -95,8 +95,8 @@ fn setup(
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(3.0, 5.0, 8.0))
.spawn(PerspectiveCameraBundle {
transform: Transform::from_xyz(3.0, 5.0, 8.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
16 changes: 8 additions & 8 deletions examples/3d/z_sort_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ fn setup(
.spawn(PbrBundle {
mesh: cube_handle.clone(),
material: materials.add(StandardMaterial {
shaded: false,
unlit: true,
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
transform: Transform::from_xyz(0.0, 0.0, 1.0),
..Default::default()
})
.with(Rotator)
Expand All @@ -65,25 +65,25 @@ fn setup(
.spawn(PbrBundle {
mesh: cube_handle.clone(),
material: materials.add(StandardMaterial {
shaded: false,
unlit: true,
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(0.0, 3.0, 0.0)),
transform: Transform::from_xyz(0.0, 3.0, 0.0),
..Default::default()
})
.spawn(PbrBundle {
mesh: cube_handle,
material: materials.add(StandardMaterial {
shaded: false,
unlit: true,
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(0.0, -3.0, 0.0)),
transform: Transform::from_xyz(0.0, -3.0, 0.0),
..Default::default()
});
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(5.0, 10.0, 10.0))
.spawn(PerspectiveCameraBundle {
transform: Transform::from_xyz(5.0, 10.0, 10.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
Expand Down
Loading