2020-11-06 22:35:18 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
use rand::{prelude::SliceRandom, Rng};
|
|
|
|
use std::{
|
|
|
|
collections::BTreeSet,
|
|
|
|
io::{BufRead, BufReader},
|
|
|
|
process::Stdio,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
|
|
|
.add_plugins(DefaultPlugins)
|
2020-12-16 05:57:16 +00:00
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.add_system(velocity_system.system())
|
|
|
|
.add_system(move_system.system())
|
|
|
|
.add_system(collision_system.system())
|
|
|
|
.add_system(select_system.system())
|
2020-11-06 22:35:18 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
type Contributors = BTreeSet<String>;
|
|
|
|
|
|
|
|
struct ContributorSelection {
|
|
|
|
order: Vec<(String, Entity)>,
|
|
|
|
idx: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SelectTimer;
|
|
|
|
|
|
|
|
struct ContributorDisplay;
|
|
|
|
|
|
|
|
struct Contributor {
|
|
|
|
color: [f32; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Velocity {
|
|
|
|
translation: Vec3,
|
|
|
|
rotation: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 SHOWCASE_TIMER_SECS: f32 = 3.0;
|
|
|
|
|
|
|
|
fn setup(
|
2020-11-08 20:34:05 +00:00
|
|
|
commands: &mut Commands,
|
2020-11-06 22:35:18 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
) {
|
|
|
|
let contribs = contributors();
|
|
|
|
|
|
|
|
let texture_handle = asset_server.load("branding/icon.png");
|
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
commands
|
2020-11-16 04:32:23 +00:00
|
|
|
.spawn(Camera2dBundle::default())
|
2020-12-03 21:46:15 +00:00
|
|
|
.spawn(CameraUiBundle::default());
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
let mut sel = ContributorSelection {
|
|
|
|
order: vec![],
|
|
|
|
idx: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut rnd = rand::thread_rng();
|
|
|
|
|
|
|
|
for name in contribs {
|
2021-01-17 21:43:03 +00:00
|
|
|
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);
|
2020-11-06 22:35:18 +00:00
|
|
|
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);
|
|
|
|
|
2021-01-07 01:17:06 +00:00
|
|
|
let mut transform = Transform::from_xyz(pos.0, pos.1, 0.0);
|
2020-11-17 21:40:18 +00:00
|
|
|
transform.scale.x *= if flipped { -1.0 } else { 1.0 };
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
commands
|
|
|
|
.spawn((Contributor { color: col },))
|
2020-11-06 22:35:18 +00:00
|
|
|
.with(Velocity {
|
|
|
|
translation: velocity,
|
|
|
|
rotation: -dir * 5.0,
|
|
|
|
})
|
2020-11-16 04:32:23 +00:00
|
|
|
.with_bundle(SpriteBundle {
|
2020-11-06 22:35:18 +00:00
|
|
|
sprite: Sprite {
|
|
|
|
size: Vec2::new(1.0, 1.0) * SPRITE_SIZE,
|
|
|
|
resize_mode: SpriteResizeMode::Manual,
|
|
|
|
},
|
|
|
|
material: materials.add(ColorMaterial {
|
|
|
|
color: COL_DESELECTED * col,
|
|
|
|
texture: Some(texture_handle.clone()),
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with(transform);
|
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
let e = commands.current_entity().unwrap();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
sel.order.push((name, e));
|
|
|
|
}
|
|
|
|
|
|
|
|
sel.order.shuffle(&mut rnd);
|
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
commands.spawn((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true)));
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
commands
|
|
|
|
.spawn((ContributorDisplay,))
|
2020-11-16 04:32:23 +00:00
|
|
|
.with_bundle(TextBundle {
|
2020-11-06 22:35:18 +00:00
|
|
|
style: Style {
|
|
|
|
align_self: AlignSelf::FlexEnd,
|
|
|
|
..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,
|
2020-11-13 00:21:48 +00:00
|
|
|
..Default::default()
|
2020-11-06 22:35:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
commands.insert_resource(sel);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the next contributor to display and selects the entity
|
|
|
|
fn select_system(
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
mut sel: ResMut<ContributorSelection>,
|
2020-11-13 02:22:46 +00:00
|
|
|
mut dq: Query<Mut<Text>, With<ContributorDisplay>>,
|
|
|
|
mut tq: Query<Mut<Timer>, With<SelectTimer>>,
|
2020-11-06 22:35:18 +00:00
|
|
|
mut q: Query<(&Contributor, &Handle<ColorMaterial>, &mut Transform)>,
|
2020-12-13 19:31:50 +00:00
|
|
|
time: Res<Time>,
|
2020-11-06 22:35:18 +00:00
|
|
|
) {
|
|
|
|
let mut timer_fired = false;
|
2020-11-13 02:22:46 +00:00
|
|
|
for mut t in tq.iter_mut() {
|
2020-12-13 19:31:50 +00:00
|
|
|
if !t.tick(time.delta_seconds()).just_finished() {
|
2020-11-06 22:35:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
t.reset();
|
|
|
|
timer_fired = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !timer_fired {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let prev = sel.idx;
|
|
|
|
|
|
|
|
if (sel.idx + 1) < sel.order.len() {
|
|
|
|
sel.idx += 1;
|
|
|
|
} else {
|
|
|
|
sel.idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let (_, e) = &sel.order[prev];
|
|
|
|
if let Ok((c, handle, mut tr)) = q.get_mut(*e) {
|
|
|
|
deselect(&mut *materials, handle.clone(), c, &mut *tr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (name, e) = &sel.order[sel.idx];
|
|
|
|
|
|
|
|
if let Ok((c, handle, mut tr)) = q.get_mut(*e) {
|
2020-11-13 02:22:46 +00:00
|
|
|
for mut text in dq.iter_mut() {
|
2020-11-06 22:35:18 +00:00
|
|
|
select(
|
|
|
|
&mut *materials,
|
|
|
|
handle.clone(),
|
|
|
|
c,
|
|
|
|
&mut *tr,
|
|
|
|
&mut *text,
|
|
|
|
name,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change the modulate color to the "selected" colour,
|
|
|
|
/// bring the object to the front and display the name.
|
|
|
|
fn select(
|
|
|
|
materials: &mut Assets<ColorMaterial>,
|
|
|
|
mat_handle: Handle<ColorMaterial>,
|
|
|
|
cont: &Contributor,
|
|
|
|
trans: &mut Transform,
|
|
|
|
text: &mut Text,
|
|
|
|
name: &str,
|
|
|
|
) -> Option<()> {
|
|
|
|
let mat = materials.get_mut(mat_handle)?;
|
|
|
|
mat.color = COL_SELECTED * cont.color;
|
|
|
|
|
2020-11-17 21:40:18 +00:00
|
|
|
trans.translation.z = 100.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
text.value = format!("Contributor: {}", name);
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change the modulate color to the "deselected" colour and push
|
|
|
|
/// the object to the back.
|
|
|
|
fn deselect(
|
|
|
|
materials: &mut Assets<ColorMaterial>,
|
|
|
|
mat_handle: Handle<ColorMaterial>,
|
|
|
|
cont: &Contributor,
|
|
|
|
trans: &mut Transform,
|
|
|
|
) -> Option<()> {
|
|
|
|
let mat = materials.get_mut(mat_handle)?;
|
|
|
|
mat.color = COL_DESELECTED * cont.color;
|
|
|
|
|
2020-11-17 21:40:18 +00:00
|
|
|
trans.translation.z = 0.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies gravity to all entities with velocity
|
|
|
|
fn velocity_system(time: Res<Time>, mut q: Query<Mut<Velocity>>) {
|
2020-11-28 21:08:31 +00:00
|
|
|
let delta = time.delta_seconds();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
for mut v in q.iter_mut() {
|
|
|
|
v.translation += Vec3::new(0.0, GRAVITY * delta, 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks for collisions of contributor-birds.
|
|
|
|
///
|
|
|
|
/// On collision with left-or-right wall it resets the horizontal
|
|
|
|
/// velocity. On collision with the ground it applies an upwards
|
|
|
|
/// force.
|
|
|
|
fn collision_system(
|
|
|
|
wins: Res<Windows>,
|
2020-11-13 02:22:46 +00:00
|
|
|
mut q: Query<(Mut<Velocity>, Mut<Transform>), With<Contributor>>,
|
2020-11-06 22:35:18 +00:00
|
|
|
) {
|
|
|
|
let mut rnd = rand::thread_rng();
|
|
|
|
|
|
|
|
let win = wins.get_primary().unwrap();
|
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
let ceiling = win.height() / 2.;
|
|
|
|
let ground = -(win.height() / 2.);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2020-12-13 23:05:56 +00:00
|
|
|
let wall_left = -(win.width() / 2.);
|
|
|
|
let wall_right = win.width() / 2.;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2020-11-13 02:22:46 +00:00
|
|
|
for (mut v, mut t) in q.iter_mut() {
|
2020-11-17 21:40:18 +00:00
|
|
|
let left = t.translation.x - SPRITE_SIZE / 2.0;
|
|
|
|
let right = t.translation.x + SPRITE_SIZE / 2.0;
|
|
|
|
let top = t.translation.y + SPRITE_SIZE / 2.0;
|
|
|
|
let bottom = t.translation.y - SPRITE_SIZE / 2.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
// clamp the translation to not go out of the bounds
|
|
|
|
if bottom < ground {
|
2020-11-17 21:40:18 +00:00
|
|
|
t.translation.y = ground + SPRITE_SIZE / 2.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
// apply an impulse upwards
|
2021-01-17 21:43:03 +00:00
|
|
|
v.translation.y = rnd.gen_range(700.0..1000.0);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
if top > ceiling {
|
2020-11-17 21:40:18 +00:00
|
|
|
t.translation.y = ceiling - SPRITE_SIZE / 2.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
// on side walls flip the horizontal velocity
|
|
|
|
if left < wall_left {
|
2020-11-17 21:40:18 +00:00
|
|
|
t.translation.x = wall_left + SPRITE_SIZE / 2.0;
|
|
|
|
v.translation.x *= -1.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
v.rotation *= -1.0;
|
|
|
|
}
|
|
|
|
if right > wall_right {
|
2020-11-17 21:40:18 +00:00
|
|
|
t.translation.x = wall_right - SPRITE_SIZE / 2.0;
|
|
|
|
v.translation.x *= -1.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
v.rotation *= -1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply velocity to positions and rotations.
|
|
|
|
fn move_system(time: Res<Time>, mut q: Query<(&Velocity, Mut<Transform>)>) {
|
2020-11-28 21:08:31 +00:00
|
|
|
let delta = time.delta_seconds();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
for (v, mut t) in q.iter_mut() {
|
|
|
|
t.translation += delta * v.translation;
|
|
|
|
t.rotate(Quat::from_rotation_z(v.rotation * delta));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the names of all contributors from the git log.
|
|
|
|
///
|
|
|
|
/// The names are deduplicated.
|
|
|
|
/// This function only works if `git` is installed and
|
|
|
|
/// the program is run through `cargo`.
|
|
|
|
fn contributors() -> Contributors {
|
|
|
|
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
|
2020-12-02 19:31:16 +00:00
|
|
|
.expect("This example needs to run through `cargo run --example`.");
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
let mut cmd = std::process::Command::new("git")
|
|
|
|
.args(&["--no-pager", "log", "--pretty=format:%an"])
|
|
|
|
.current_dir(manifest_dir)
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()
|
2020-12-02 19:31:16 +00:00
|
|
|
.expect("`git` needs to be installed.");
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2020-12-02 19:31:16 +00:00
|
|
|
let stdout = cmd.stdout.take().expect("`Child` should have a stdout.");
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
BufReader::new(stdout)
|
|
|
|
.lines()
|
|
|
|
.filter_map(|x| x.ok())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a color modulation
|
|
|
|
///
|
|
|
|
/// Because there is no `Mul<Color> for Color` instead `[f32; 3]` is
|
|
|
|
/// used.
|
|
|
|
fn gen_color(rng: &mut impl Rng) -> [f32; 3] {
|
2021-01-17 21:43:03 +00:00
|
|
|
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);
|
2020-11-06 22:35:18 +00:00
|
|
|
let v = Vec3::new(r, g, b);
|
|
|
|
v.normalize().into()
|
|
|
|
}
|