2021-12-08 20:09:53 +00:00
|
|
|
use bevy::{prelude::*, utils::HashSet};
|
2020-11-06 22:35:18 +00:00
|
|
|
use rand::{prelude::SliceRandom, Rng};
|
|
|
|
use std::{
|
2021-12-08 20:09:53 +00:00
|
|
|
env::VarError,
|
|
|
|
io::{self, BufRead, BufReader},
|
2020-11-06 22:35:18 +00:00
|
|
|
process::Stdio,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2020-11-06 22:35:18 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2021-12-08 20:09:53 +00:00
|
|
|
.add_startup_system(setup_contributor_selection)
|
2021-06-27 00:40:09 +00:00
|
|
|
.add_startup_system(setup)
|
|
|
|
.add_system(velocity_system)
|
|
|
|
.add_system(move_system)
|
|
|
|
.add_system(collision_system)
|
|
|
|
.add_system(select_system)
|
2022-02-03 23:56:57 +00:00
|
|
|
.insert_resource(SelectTimer(Timer::from_seconds(SHOWCASE_TIMER_SECS, true)))
|
2020-11-06 22:35:18 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
// Store contributors in a collection that preserves the uniqueness
|
|
|
|
type Contributors = HashSet<String>;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
struct ContributorSelection {
|
|
|
|
order: Vec<(String, Entity)>,
|
|
|
|
idx: usize,
|
|
|
|
}
|
|
|
|
|
2022-02-03 23:56:57 +00:00
|
|
|
struct SelectTimer(Timer);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-10-03 19:23:44 +00:00
|
|
|
#[derive(Component)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct ContributorDisplay;
|
|
|
|
|
2021-10-03 19:23:44 +00:00
|
|
|
#[derive(Component)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct Contributor {
|
2021-03-17 23:59:51 +00:00
|
|
|
hue: f32,
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 19:23:44 +00:00
|
|
|
#[derive(Component)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct Velocity {
|
|
|
|
translation: Vec3,
|
|
|
|
rotation: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
const GRAVITY: f32 = -9.821 * 100.0;
|
|
|
|
const SPRITE_SIZE: f32 = 75.0;
|
|
|
|
|
2021-03-17 23:59:51 +00:00
|
|
|
const SATURATION_DESELECTED: f32 = 0.3;
|
|
|
|
const LIGHTNESS_DESELECTED: f32 = 0.2;
|
|
|
|
const SATURATION_SELECTED: f32 = 0.9;
|
|
|
|
const LIGHTNESS_SELECTED: f32 = 0.7;
|
|
|
|
const ALPHA: f32 = 0.92;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
const SHOWCASE_TIMER_SECS: f32 = 3.0;
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
const CONTRIBUTORS_LIST: &[&str] = &["Carter Anderson", "And Many More"];
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
fn setup_contributor_selection(mut commands: Commands, asset_server: Res<AssetServer>) {
|
2021-12-08 20:09:53 +00:00
|
|
|
// Load contributors from the git history log or use default values from
|
|
|
|
// the constant array. Contributors must be unique, so they are stored in a HashSet
|
|
|
|
let contribs = contributors().unwrap_or_else(|_| {
|
|
|
|
CONTRIBUTORS_LIST
|
|
|
|
.iter()
|
|
|
|
.map(|name| name.to_string())
|
|
|
|
.collect()
|
|
|
|
});
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
let texture_handle = asset_server.load("branding/icon.png");
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let mut contributor_selection = ContributorSelection {
|
2020-11-06 22:35:18 +00:00
|
|
|
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);
|
2021-03-17 23:59:51 +00:00
|
|
|
let hue = rnd.gen_range(0.0..=360.0);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
// some sprites should be flipped
|
|
|
|
let flipped = rnd.gen_bool(0.5);
|
|
|
|
|
2021-03-07 19:50:19 +00:00
|
|
|
let transform = Transform::from_xyz(pos.0, pos.1, 0.0);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let entity = commands
|
2021-03-23 00:23:40 +00:00
|
|
|
.spawn()
|
|
|
|
.insert_bundle((
|
|
|
|
Contributor { hue },
|
|
|
|
Velocity {
|
|
|
|
translation: velocity,
|
|
|
|
rotation: -dir * 5.0,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.insert_bundle(SpriteBundle {
|
2020-11-06 22:35:18 +00:00
|
|
|
sprite: Sprite {
|
2021-12-14 03:58:23 +00:00
|
|
|
custom_size: Some(Vec2::new(1.0, 1.0) * SPRITE_SIZE),
|
|
|
|
color: Color::hsla(hue, SATURATION_DESELECTED, LIGHTNESS_DESELECTED, ALPHA),
|
2021-03-07 19:50:19 +00:00
|
|
|
flip_x: flipped,
|
Add Sprite Flipping (#1407)
OK, here's my attempt at sprite flipping. There are a couple of points that I need review/help on, but I think the UX is about ideal:
```rust
.spawn(SpriteBundle {
material: materials.add(texture_handle.into()),
sprite: Sprite {
// Flip the sprite along the x axis
flip: SpriteFlip { x: true, y: false },
..Default::default()
},
..Default::default()
});
```
Now for the issues. The big issue is that for some reason, when flipping the UVs on the sprite, there is a light "bleeding" or whatever you call it where the UV tries to sample past the texture boundry and ends up clipping. This is only noticed when resizing the window, though. You can see a screenshot below.
![image](https://user-images.githubusercontent.com/25393315/107098172-397aaa00-67d4-11eb-8e02-c90c820cd70e.png)
I am quite baffled why the texture sampling is overrunning like it is and could use some guidance if anybody knows what might be wrong.
The other issue, which I just worked around, is that I had to remove the `#[render_resources(from_self)]` annotation from the Spritesheet because the `SpriteFlip` render resource wasn't being picked up properly in the shader when using it. I'm not sure what the cause of that was, but by removing the annotation and re-organizing the shader inputs accordingly the problem was fixed.
I'm not sure if this is the most efficient way to do this or if there is a better way, but I wanted to try it out if only for the learning experience. Let me know what you think!
2021-03-03 19:26:45 +00:00
|
|
|
..Default::default()
|
2020-11-06 22:35:18 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
texture: texture_handle.clone(),
|
2021-03-17 23:59:51 +00:00
|
|
|
transform,
|
2020-11-06 22:35:18 +00:00
|
|
|
..Default::default()
|
2021-03-23 00:23:40 +00:00
|
|
|
})
|
|
|
|
.id();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
contributor_selection.order.push((name, entity));
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
contributor_selection.order.shuffle(&mut rnd);
|
|
|
|
|
|
|
|
commands.insert_resource(contributor_selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
|
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
|
|
|
|
commands.spawn_bundle(UiCameraBundle::default());
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2020-11-08 20:34:05 +00:00
|
|
|
commands
|
2021-03-23 00:23:40 +00:00
|
|
|
.spawn()
|
|
|
|
.insert(ContributorDisplay)
|
|
|
|
.insert_bundle(TextBundle {
|
2020-11-06 22:35:18 +00:00
|
|
|
style: Style {
|
|
|
|
align_self: AlignSelf::FlexEnd,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
text: Text {
|
2021-01-25 01:07:43 +00:00
|
|
|
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()
|
2020-11-06 22:35:18 +00:00
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the next contributor to display and selects the entity
|
|
|
|
fn select_system(
|
2022-02-03 23:56:57 +00:00
|
|
|
mut timer: ResMut<SelectTimer>,
|
2021-12-08 20:09:53 +00:00
|
|
|
mut contributor_selection: ResMut<ContributorSelection>,
|
|
|
|
mut text_query: Query<&mut Text, With<ContributorDisplay>>,
|
2021-12-14 03:58:23 +00:00
|
|
|
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
|
2020-12-13 19:31:50 +00:00
|
|
|
time: Res<Time>,
|
2020-11-06 22:35:18 +00:00
|
|
|
) {
|
2022-02-03 23:56:57 +00:00
|
|
|
if !timer.0.tick(time.delta()).just_finished() {
|
2020-11-06 22:35:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let prev = contributor_selection.idx;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
if (contributor_selection.idx + 1) < contributor_selection.order.len() {
|
|
|
|
contributor_selection.idx += 1;
|
2020-11-06 22:35:18 +00:00
|
|
|
} else {
|
2021-12-08 20:09:53 +00:00
|
|
|
contributor_selection.idx = 0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-12-08 20:09:53 +00:00
|
|
|
let (_, entity) = &contributor_selection.order[prev];
|
2021-12-14 03:58:23 +00:00
|
|
|
if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(*entity) {
|
|
|
|
deselect(&mut sprite, contributor, &mut *transform);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let (name, entity) = &contributor_selection.order[contributor_selection.idx];
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(*entity) {
|
2021-12-08 20:09:53 +00:00
|
|
|
if let Some(mut text) = text_query.iter_mut().next() {
|
2021-12-14 03:58:23 +00:00
|
|
|
select(&mut sprite, contributor, &mut *transform, &mut *text, name);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Change the modulate color to the "selected" colour,
|
|
|
|
/// bring the object to the front and display the name.
|
|
|
|
fn select(
|
2021-12-14 03:58:23 +00:00
|
|
|
sprite: &mut Sprite,
|
2021-12-08 20:09:53 +00:00
|
|
|
contributor: &Contributor,
|
|
|
|
transform: &mut Transform,
|
2020-11-06 22:35:18 +00:00
|
|
|
text: &mut Text,
|
|
|
|
name: &str,
|
2021-12-14 03:58:23 +00:00
|
|
|
) {
|
|
|
|
sprite.color = Color::hsla(
|
2021-12-08 20:09:53 +00:00
|
|
|
contributor.hue,
|
|
|
|
SATURATION_SELECTED,
|
|
|
|
LIGHTNESS_SELECTED,
|
|
|
|
ALPHA,
|
|
|
|
);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation.z = 100.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-01-25 01:07:43 +00:00
|
|
|
text.sections[0].value = "Contributor: ".to_string();
|
|
|
|
text.sections[1].value = name.to_string();
|
2021-12-14 03:58:23 +00:00
|
|
|
text.sections[1].style.color = sprite.color;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Change the modulate color to the "deselected" colour and push
|
|
|
|
/// the object to the back.
|
2021-12-14 03:58:23 +00:00
|
|
|
fn deselect(sprite: &mut Sprite, contributor: &Contributor, transform: &mut Transform) {
|
|
|
|
sprite.color = Color::hsla(
|
2021-12-08 20:09:53 +00:00
|
|
|
contributor.hue,
|
|
|
|
SATURATION_DESELECTED,
|
|
|
|
LIGHTNESS_DESELECTED,
|
|
|
|
ALPHA,
|
|
|
|
);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation.z = 0.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies gravity to all entities with velocity
|
2021-12-08 20:09:53 +00:00
|
|
|
fn velocity_system(time: Res<Time>, mut velocity_query: Query<&mut Velocity>) {
|
2020-11-28 21:08:31 +00:00
|
|
|
let delta = time.delta_seconds();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
for mut velocity in velocity_query.iter_mut() {
|
|
|
|
velocity.translation += Vec3::new(0.0, GRAVITY * delta, 0.0);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(
|
2021-12-08 20:09:53 +00:00
|
|
|
windows: Res<Windows>,
|
|
|
|
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
|
2020-11-06 22:35:18 +00:00
|
|
|
) {
|
|
|
|
let mut rnd = rand::thread_rng();
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let window = windows.get_primary().unwrap();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let ceiling = window.height() / 2.;
|
|
|
|
let ground = -(window.height() / 2.);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let wall_left = -(window.width() / 2.);
|
|
|
|
let wall_right = window.width() / 2.;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
for (mut velocity, mut transform) in query.iter_mut() {
|
|
|
|
let left = transform.translation.x - SPRITE_SIZE / 2.0;
|
|
|
|
let right = transform.translation.x + SPRITE_SIZE / 2.0;
|
|
|
|
let top = transform.translation.y + SPRITE_SIZE / 2.0;
|
|
|
|
let bottom = transform.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 {
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation.y = ground + SPRITE_SIZE / 2.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
// apply an impulse upwards
|
2021-12-08 20:09:53 +00:00
|
|
|
velocity.translation.y = rnd.gen_range(700.0..1000.0);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
if top > ceiling {
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.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 {
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation.x = wall_left + SPRITE_SIZE / 2.0;
|
|
|
|
velocity.translation.x *= -1.0;
|
|
|
|
velocity.rotation *= -1.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
if right > wall_right {
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation.x = wall_right - SPRITE_SIZE / 2.0;
|
|
|
|
velocity.translation.x *= -1.0;
|
|
|
|
velocity.rotation *= -1.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply velocity to positions and rotations.
|
2021-12-08 20:09:53 +00:00
|
|
|
fn move_system(time: Res<Time>, mut query: 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
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
for (velocity, mut transform) in query.iter_mut() {
|
|
|
|
transform.translation += delta * velocity.translation;
|
|
|
|
transform.rotate(Quat::from_rotation_z(velocity.rotation * delta));
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
enum LoadContributorsError {
|
|
|
|
IO(io::Error),
|
|
|
|
Var(VarError),
|
|
|
|
Stdout,
|
|
|
|
}
|
|
|
|
|
2020-11-06 22:35:18 +00:00
|
|
|
/// 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`.
|
2021-12-08 20:09:53 +00:00
|
|
|
fn contributors() -> Result<Contributors, LoadContributorsError> {
|
|
|
|
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").map_err(LoadContributorsError::Var)?;
|
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()
|
2021-12-08 20:09:53 +00:00
|
|
|
.map_err(LoadContributorsError::IO)?;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let stdout = cmd.stdout.take().ok_or(LoadContributorsError::Stdout)?;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let contributors = BufReader::new(stdout)
|
2020-11-06 22:35:18 +00:00
|
|
|
.lines()
|
|
|
|
.filter_map(|x| x.ok())
|
2021-12-08 20:09:53 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(contributors)
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|