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 ecs v2 #31

Merged
merged 17 commits into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from 16 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
11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ default = [

[dependencies]
winit = {version = "0.24.0", features = ["web-sys"]}
# bevy = { git = "https://github.com/bevyengine/bevy/", rev="c2a427f1a38db6b1d9798e631a7da7a8507fe18c", default-features=false }
bevy = {path = "../bevy", default-features=false}
bevy = { git = "https://github.com/bevyengine/bevy/", branch = "main", default-features=false }
regex = "1.4"
cfg-if = "1.0.0"
js-sys = "0.3.45"
parking_lot = "0.11.0"
wasm-bindgen = "0.2.59"
wasm-bindgen = "=0.2.69"
web-sys = {version = "0.3.45", features = [
'Document',
'Element',
Expand Down Expand Up @@ -70,9 +69,9 @@ name = "sprite_sheet"
path = "examples/2d/sprite_sheet.rs"

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

[[example]]
name = "3d_scene"
Expand Down
Binary file added assets/fonts/FiraMono-Medium.ttf
Binary file not shown.
49 changes: 22 additions & 27 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use bevy::prelude::*;
use rand::{prelude::SliceRandom, Rng};
use std::{
collections::BTreeSet,
io::{BufRead, BufReader},
process::Stdio,
};
use std::collections::BTreeSet;

fn main() {
App::build()
Expand Down Expand Up @@ -46,17 +42,16 @@ const COL_SELECTED: Color = Color::WHITE;
const SHOWCASE_TIMER_SECS: f32 = 3.0;

fn setup(
commands: &mut Commands,
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let contribs = contributors();

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

commands
.spawn(OrthographicCameraBundle::new_2d())
.spawn(UiCameraBundle::default());
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());

let mut sel = ContributorSelection {
order: vec![],
Expand All @@ -74,40 +69,40 @@ fn setup(
// some sprites should be flipped
let flipped = rnd.gen_bool(0.5);

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

commands
.spawn((Contributor { color: col },))
.with(Velocity {
let e = commands
.spawn_bundle((Contributor { color: col },))
.insert(Velocity {
translation: velocity,
rotation: -dir * 5.0,
})
.with_bundle(SpriteBundle {
.insert_bundle(SpriteBundle {
sprite: Sprite {
size: Vec2::new(1.0, 1.0) * SPRITE_SIZE,
resize_mode: SpriteResizeMode::Manual,
flip_x: flipped,
..Default::default()
},
material: materials.add(ColorMaterial {
color: COL_DESELECTED * col,
texture: Some(texture_handle.clone()),
}),
..Default::default()
})
.with(transform);

let e = commands.current_entity().unwrap();
.insert(transform)
.id();

sel.order.push((name, e));
}

sel.order.shuffle(&mut rnd);

commands.spawn((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true)));
commands.spawn_bundle((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true)));

commands
.spawn((ContributorDisplay,))
.with_bundle(TextBundle {
.spawn_bundle((ContributorDisplay,))
.insert_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
..Default::default()
Expand Down Expand Up @@ -143,14 +138,14 @@ fn setup(
fn select_system(
mut materials: ResMut<Assets<ColorMaterial>>,
mut sel: ResMut<ContributorSelection>,
mut dq: Query<Mut<Text>, With<ContributorDisplay>>,
mut tq: Query<Mut<Timer>, With<SelectTimer>>,
mut dq: Query<&mut Text, With<ContributorDisplay>>,
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() {
if !t.tick(time.delta_seconds()).just_finished() {
if !t.tick(time.delta()).just_finished() {
continue;
}
t.reset();
Expand Down Expand Up @@ -231,7 +226,7 @@ fn deselect(
}

/// Applies gravity to all entities with velocity
fn velocity_system(time: Res<Time>, mut q: Query<Mut<Velocity>>) {
fn velocity_system(time: Res<Time>, mut q: Query<&mut Velocity>) {
let delta = time.delta_seconds();

for mut v in q.iter_mut() {
Expand All @@ -246,7 +241,7 @@ fn velocity_system(time: Res<Time>, mut q: Query<Mut<Velocity>>) {
/// force.
fn collision_system(
wins: Res<Windows>,
mut q: Query<(Mut<Velocity>, Mut<Transform>), With<Contributor>>,
mut q: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
) {
let mut rnd = rand::thread_rng();

Expand Down Expand Up @@ -288,7 +283,7 @@ fn collision_system(
}

/// Apply velocity to positions and rotations.
fn move_system(time: Res<Time>, mut q: Query<(&Velocity, Mut<Transform>)>) {
fn move_system(time: Res<Time>, mut q: Query<(&Velocity, &mut Transform)>) {
let delta = time.delta_seconds();

for (v, mut t) in q.iter_mut() {
Expand Down
13 changes: 6 additions & 7 deletions examples/2d/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ fn main() {
}

fn setup(
commands: &mut Commands,
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let texture_handle = asset_server.load("branding/icon.png");
commands
.spawn(OrthographicCameraBundle::new_2d())
.spawn(SpriteBundle {
material: materials.add(texture_handle.into()),
..Default::default()
});
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(SpriteBundle {
material: materials.add(texture_handle.into()),
..Default::default()
});
}
10 changes: 5 additions & 5 deletions examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn animate_sprite_system(
mut query: Query<(&mut Timer, &mut TextureAtlasSprite, &Handle<TextureAtlas>)>,
) {
for (mut timer, mut sprite, texture_atlas_handle) in query.iter_mut() {
timer.tick(time.delta_seconds());
timer.tick(time.delta());
if timer.finished() {
let texture_atlas = texture_atlases.get(texture_atlas_handle).unwrap();
sprite.index = ((sprite.index as usize + 1) % texture_atlas.textures.len()) as u32;
Expand All @@ -23,19 +23,19 @@ fn animate_sprite_system(
}

fn setup(
commands: &mut Commands,
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
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_bundle(OrthographicCameraBundle::new_2d());
commands
.spawn(OrthographicCameraBundle::new_2d())
.spawn(SpriteSheetBundle {
.spawn_bundle(SpriteSheetBundle {
texture_atlas: texture_atlas_handle,
transform: Transform::from_scale(Vec3::splat(6.0)),
..Default::default()
})
.with(Timer::from_seconds(0.1, true));
.insert(Timer::from_seconds(0.1, true));
}
39 changes: 19 additions & 20 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,33 @@ fn main() {

/// set up a simple 3D scene
fn setup(
commands: &mut Commands,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// add entities to the world
commands
// plane
.spawn(PbrBundle {
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
})
// cube
.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_xyz(0.0, 0.5, 0.0),
..Default::default()
})
// light
.spawn(LightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
})
// camera
.spawn(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
// cube
commands.spawn_bundle(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_xyz(0.0, 0.5, 0.0),
..Default::default()
});
// light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::default(), Vec3::Y),
..Default::default()
});
}
36 changes: 26 additions & 10 deletions examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
use bevy::prelude::*;
use bevy::{pbr::AmbientLight, prelude::*};

fn main() {
App::build()
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.0 / 5.0f32,
})
.insert_resource(Msaa { samples: 4 })
.add_plugins(bevy_webgl2::DefaultPlugins)
.add_startup_system(setup.system())
.add_system(rotator_system.system())
.run();
}

fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"));
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
..Default::default()
});
commands
.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"))
.spawn(LightBundle {
transform: Transform::from_xyz(4.0, 5.0, 4.0),
.spawn_bundle(LightBundle {
transform: Transform::from_xyz(3.0, 5.0, 3.0),
..Default::default()
})
.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()
});
.insert(Rotates);
}

/// this component indicates what entities should rotate
struct Rotates;

fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotates>>) {
for mut transform in query.iter_mut() {
*transform = Transform::from_rotation(Quat::from_rotation_y(
(4.0 * std::f32::consts::PI / 20.0) * time.delta_seconds(),
)) * *transform;
}
}
32 changes: 15 additions & 17 deletions examples/3d/parenting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,41 @@ fn rotator_system(time: Res<Time>, mut query: Query<&mut Transform, With<Rotator

/// set up a simple scene with a "parent" cube and a "child" cube
fn setup(
commands: &mut Commands,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 2.0 }));
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.8, 0.7, 0.6),
base_color: Color::rgb(0.8, 0.7, 0.6),
..Default::default()
});

commands
// parent cube
.spawn(PbrBundle {
.spawn_bundle(PbrBundle {
mesh: cube_handle.clone(),
material: cube_material_handle.clone(),
transform: Transform::from_xyz(0.0, 0.0, 1.0),
..Default::default()
})
.with(Rotator)
.insert(Rotator)
.with_children(|parent| {
// child cube
parent.spawn(PbrBundle {
parent.spawn_bundle(PbrBundle {
mesh: cube_handle,
material: cube_material_handle,
transform: Transform::from_xyz(0.0, 0.0, 3.0),
..Default::default()
});
})
// light
.spawn(LightBundle {
transform: Transform::from_xyz(4.0, 5.0, -4.0),
..Default::default()
})
// camera
.spawn(PerspectiveCameraBundle {
transform: Transform::from_xyz(5.0, 10.0, 10.0)
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
// light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(4.0, 5.0, -4.0),
..Default::default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(5.0, 10.0, 10.0).looking_at(Vec3::default(), Vec3::Y),
..Default::default()
});
}
Loading