2022-05-16 13:53:20 +00:00
|
|
|
//! This example displays each contributor to the bevy source code as a bouncing bevy-ball.
|
|
|
|
|
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-04-14 19:30:36 +00:00
|
|
|
.init_resource::<SelectionState>()
|
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
|
|
|
|
Make `Resource` trait opt-in, requiring `#[derive(Resource)]` V2 (#5577)
*This PR description is an edited copy of #5007, written by @alice-i-cecile.*
# Objective
Follow-up to https://github.com/bevyengine/bevy/pull/2254. The `Resource` trait currently has a blanket implementation for all types that meet its bounds.
While ergonomic, this results in several drawbacks:
* it is possible to make confusing, silent mistakes such as inserting a function pointer (Foo) rather than a value (Foo::Bar) as a resource
* it is challenging to discover if a type is intended to be used as a resource
* we cannot later add customization options (see the [RFC](https://github.com/bevyengine/rfcs/blob/main/rfcs/27-derive-component.md) for the equivalent choice for Component).
* dependencies can use the same Rust type as a resource in invisibly conflicting ways
* raw Rust types used as resources cannot preserve privacy appropriately, as anyone able to access that type can read and write to internal values
* we cannot capture a definitive list of possible resources to display to users in an editor
## Notes to reviewers
* Review this commit-by-commit; there's effectively no back-tracking and there's a lot of churn in some of these commits.
*ira: My commits are not as well organized :')*
* I've relaxed the bound on Local to Send + Sync + 'static: I don't think these concerns apply there, so this can keep things simple. Storing e.g. a u32 in a Local is fine, because there's a variable name attached explaining what it does.
* I think this is a bad place for the Resource trait to live, but I've left it in place to make reviewing easier. IMO that's best tackled with https://github.com/bevyengine/bevy/issues/4981.
## Changelog
`Resource` is no longer automatically implemented for all matching types. Instead, use the new `#[derive(Resource)]` macro.
## Migration Guide
Add `#[derive(Resource)]` to all types you are using as a resource.
If you are using a third party type as a resource, wrap it in a tuple struct to bypass orphan rules. Consider deriving `Deref` and `DerefMut` to improve ergonomics.
`ClearColor` no longer implements `Component`. Using `ClearColor` as a component in 0.8 did nothing.
Use the `ClearColorConfig` in the `Camera3d` and `Camera2d` components instead.
Co-authored-by: Alice <alice.i.cecile@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: devil-ira <justthecooldude@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-08-08 21:36:35 +00:00
|
|
|
#[derive(Resource)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct ContributorSelection {
|
2022-04-14 19:30:36 +00:00
|
|
|
order: Vec<Entity>,
|
2020-11-06 22:35:18 +00:00
|
|
|
idx: usize,
|
|
|
|
}
|
|
|
|
|
Make `Resource` trait opt-in, requiring `#[derive(Resource)]` V2 (#5577)
*This PR description is an edited copy of #5007, written by @alice-i-cecile.*
# Objective
Follow-up to https://github.com/bevyengine/bevy/pull/2254. The `Resource` trait currently has a blanket implementation for all types that meet its bounds.
While ergonomic, this results in several drawbacks:
* it is possible to make confusing, silent mistakes such as inserting a function pointer (Foo) rather than a value (Foo::Bar) as a resource
* it is challenging to discover if a type is intended to be used as a resource
* we cannot later add customization options (see the [RFC](https://github.com/bevyengine/rfcs/blob/main/rfcs/27-derive-component.md) for the equivalent choice for Component).
* dependencies can use the same Rust type as a resource in invisibly conflicting ways
* raw Rust types used as resources cannot preserve privacy appropriately, as anyone able to access that type can read and write to internal values
* we cannot capture a definitive list of possible resources to display to users in an editor
## Notes to reviewers
* Review this commit-by-commit; there's effectively no back-tracking and there's a lot of churn in some of these commits.
*ira: My commits are not as well organized :')*
* I've relaxed the bound on Local to Send + Sync + 'static: I don't think these concerns apply there, so this can keep things simple. Storing e.g. a u32 in a Local is fine, because there's a variable name attached explaining what it does.
* I think this is a bad place for the Resource trait to live, but I've left it in place to make reviewing easier. IMO that's best tackled with https://github.com/bevyengine/bevy/issues/4981.
## Changelog
`Resource` is no longer automatically implemented for all matching types. Instead, use the new `#[derive(Resource)]` macro.
## Migration Guide
Add `#[derive(Resource)]` to all types you are using as a resource.
If you are using a third party type as a resource, wrap it in a tuple struct to bypass orphan rules. Consider deriving `Deref` and `DerefMut` to improve ergonomics.
`ClearColor` no longer implements `Component`. Using `ClearColor` as a component in 0.8 did nothing.
Use the `ClearColorConfig` in the `Camera3d` and `Camera2d` components instead.
Co-authored-by: Alice <alice.i.cecile@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: devil-ira <justthecooldude@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-08-08 21:36:35 +00:00
|
|
|
#[derive(Resource)]
|
2022-04-08 04:02:14 +00:00
|
|
|
struct SelectionState {
|
|
|
|
timer: Timer,
|
|
|
|
has_triggered: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for SelectionState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
Replace the `bool` argument of `Timer` with `TimerMode` (#6247)
As mentioned in #2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation.
This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from #2683, into one struct.
Signed-off-by: Lena Milizé <me@lvmn.org>
# Objective
Fixes #2926.
## Solution
Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`.
---
## Changelog
### Added
- `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating`
### Changed
- Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode`
- Change `repeating: bool` field of `Timer` with `mode: TimerMode`
## Migration Guide
- Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`.
- Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`.
- Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`.
- Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`.
- Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
2022-10-17 13:47:01 +00:00
|
|
|
timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, TimerMode::Repeating),
|
2022-04-08 04:02:14 +00:00
|
|
|
has_triggered: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-04-14 19:30:36 +00:00
|
|
|
name: String,
|
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,
|
|
|
|
}
|
|
|
|
|
2022-07-17 12:34:31 +00:00
|
|
|
const GRAVITY: f32 = 9.821 * 100.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
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 {
|
2022-04-08 04:02:14 +00:00
|
|
|
order: Vec::with_capacity(contribs.len()),
|
2020-11-06 22:35:18 +00:00
|
|
|
idx: 0,
|
|
|
|
};
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
for name in contribs {
|
2022-04-14 19:30:36 +00:00
|
|
|
let pos = (rng.gen_range(-400.0..400.0), rng.gen_range(0.0..400.0));
|
|
|
|
let dir = rng.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);
|
2022-04-14 19:30:36 +00:00
|
|
|
let hue = rng.gen_range(0.0..=360.0);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
// some sprites should be flipped
|
2022-04-14 19:30:36 +00:00
|
|
|
let flipped = rng.gen_bool(0.5);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
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
|
Spawn now takes a Bundle (#6054)
# Objective
Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands).
## Solution
All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input:
```rust
// before:
commands
.spawn()
.insert((A, B, C));
world
.spawn()
.insert((A, B, C);
// after
commands.spawn((A, B, C));
world.spawn((A, B, C));
```
All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api.
By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`).
This improves spawn performance by over 10%:
![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png)
To take this measurement, I added a new `world_spawn` benchmark.
Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main.
**Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).**
---
## Changelog
- All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input
- All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api
- World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior.
## Migration Guide
```rust
// Old (0.8):
commands
.spawn()
.insert_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
commands.spawn_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
let entity = commands.spawn().id();
// New (0.9)
let entity = commands.spawn_empty().id();
// Old (0.8)
let entity = world.spawn().id();
// New (0.9)
let entity = world.spawn_empty();
```
2022-09-23 19:55:54 +00:00
|
|
|
.spawn((
|
2022-04-14 19:30:36 +00:00
|
|
|
Contributor { name, hue },
|
2021-03-23 00:23:40 +00:00
|
|
|
Velocity {
|
|
|
|
translation: velocity,
|
|
|
|
rotation: -dir * 5.0,
|
|
|
|
},
|
2022-09-21 21:47:53 +00:00
|
|
|
SpriteBundle {
|
|
|
|
sprite: Sprite {
|
|
|
|
custom_size: Some(Vec2::new(1.0, 1.0) * SPRITE_SIZE),
|
|
|
|
color: Color::hsla(hue, SATURATION_DESELECTED, LIGHTNESS_DESELECTED, ALPHA),
|
|
|
|
flip_x: flipped,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
texture: texture_handle.clone(),
|
|
|
|
transform,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-11-06 22:35:18 +00:00
|
|
|
},
|
2022-09-21 21:47:53 +00:00
|
|
|
))
|
2021-03-23 00:23:40 +00:00
|
|
|
.id();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
contributor_selection.order.push(entity);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
contributor_selection.order.shuffle(&mut rng);
|
2021-12-08 20:09:53 +00:00
|
|
|
|
|
|
|
commands.insert_resource(contributor_selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
Spawn now takes a Bundle (#6054)
# Objective
Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands).
## Solution
All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input:
```rust
// before:
commands
.spawn()
.insert((A, B, C));
world
.spawn()
.insert((A, B, C);
// after
commands.spawn((A, B, C));
world.spawn((A, B, C));
```
All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api.
By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`).
This improves spawn performance by over 10%:
![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png)
To take this measurement, I added a new `world_spawn` benchmark.
Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main.
**Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).**
---
## Changelog
- All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input
- All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api
- World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior.
## Migration Guide
```rust
// Old (0.8):
commands
.spawn()
.insert_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
commands.spawn_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
let entity = commands.spawn().id();
// New (0.9)
let entity = commands.spawn_empty().id();
// Old (0.8)
let entity = world.spawn().id();
// New (0.9)
let entity = world.spawn_empty();
```
2022-09-23 19:55:54 +00:00
|
|
|
commands.spawn(Camera2dBundle::default());
|
2020-11-06 22:35:18 +00:00
|
|
|
|
Spawn now takes a Bundle (#6054)
# Objective
Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands).
## Solution
All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input:
```rust
// before:
commands
.spawn()
.insert((A, B, C));
world
.spawn()
.insert((A, B, C);
// after
commands.spawn((A, B, C));
world.spawn((A, B, C));
```
All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api.
By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`).
This improves spawn performance by over 10%:
![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png)
To take this measurement, I added a new `world_spawn` benchmark.
Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main.
**Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).**
---
## Changelog
- All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input
- All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api
- World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior.
## Migration Guide
```rust
// Old (0.8):
commands
.spawn()
.insert_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
commands.spawn_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
let entity = commands.spawn().id();
// New (0.9)
let entity = commands.spawn_empty().id();
// Old (0.8)
let entity = world.spawn().id();
// New (0.9)
let entity = world.spawn_empty();
```
2022-09-23 19:55:54 +00:00
|
|
|
commands.spawn((
|
2022-07-20 14:14:29 +00:00
|
|
|
TextBundle::from_sections([
|
|
|
|
TextSection::new(
|
|
|
|
"Contributor showcase",
|
|
|
|
TextStyle {
|
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
font_size: 60.0,
|
|
|
|
color: Color::WHITE,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextSection::from_style(TextStyle {
|
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
font_size: 60.0,
|
|
|
|
color: Color::WHITE,
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
.with_style(Style {
|
|
|
|
align_self: AlignSelf::FlexEnd,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2022-07-20 14:14:29 +00:00
|
|
|
}),
|
2022-09-21 21:47:53 +00:00
|
|
|
ContributorDisplay,
|
|
|
|
));
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the next contributor to display and selects the entity
|
|
|
|
fn select_system(
|
2022-04-08 04:02:14 +00:00
|
|
|
mut timer: ResMut<SelectionState>,
|
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-04-08 04:02:14 +00:00
|
|
|
if !timer.timer.tick(time.delta()).just_finished() {
|
2020-11-06 22:35:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-08 04:02:14 +00:00
|
|
|
if !timer.has_triggered {
|
|
|
|
let mut text = text_query.single_mut();
|
|
|
|
text.sections[0].value = "Contributor: ".to_string();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2022-04-08 04:02:14 +00:00
|
|
|
timer.has_triggered = true;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
let entity = contributor_selection.order[contributor_selection.idx];
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-08 04:02:14 +00:00
|
|
|
if (contributor_selection.idx + 1) < contributor_selection.order.len() {
|
|
|
|
contributor_selection.idx += 1;
|
|
|
|
} else {
|
|
|
|
contributor_selection.idx = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
let entity = contributor_selection.order[contributor_selection.idx];
|
2021-12-08 20:09:53 +00:00
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(entity) {
|
2022-04-08 04:02:14 +00:00
|
|
|
let mut text = text_query.single_mut();
|
2022-04-14 19:30:36 +00:00
|
|
|
select(&mut sprite, contributor, &mut transform, &mut text);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
/// Change the tint color to the "selected" color, bring the object to the front
|
|
|
|
/// and display the name.
|
2020-11-06 22:35:18 +00:00
|
|
|
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,
|
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
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
text.sections[1].value.clone_from(&contributor.name);
|
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
|
|
|
|
2022-07-11 15:28:50 +00:00
|
|
|
for mut velocity in &mut velocity_query {
|
2022-07-17 12:34:31 +00:00
|
|
|
velocity.translation.y -= GRAVITY * delta;
|
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
|
|
|
) {
|
2022-07-17 12:34:31 +00:00
|
|
|
let window = if let Some(window) = windows.get_primary() {
|
|
|
|
window
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
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
|
|
|
|
2022-07-17 12:34:31 +00:00
|
|
|
// The maximum height the birbs should try to reach is one birb below the top of the window.
|
|
|
|
let max_bounce_height = window.height() - SPRITE_SIZE * 2.0;
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
2022-07-11 15:28:50 +00:00
|
|
|
for (mut velocity, mut transform) in &mut query {
|
2021-12-08 20:09:53 +00:00
|
|
|
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;
|
2022-07-17 12:34:31 +00:00
|
|
|
|
|
|
|
// How high this birb will bounce.
|
|
|
|
let bounce_height = rng.gen_range((max_bounce_height * 0.4)..max_bounce_height);
|
|
|
|
|
|
|
|
// Apply the velocity that would bounce the birb up to bounce_height.
|
|
|
|
velocity.translation.y = (bounce_height * GRAVITY * 2.).sqrt();
|
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;
|
2022-07-17 12:34:31 +00:00
|
|
|
velocity.translation.y *= -1.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
|
|
|
|
2022-07-11 15:28:50 +00:00
|
|
|
for (velocity, mut transform) in &mut query {
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation += delta * velocity.translation;
|
2022-07-01 03:58:54 +00:00
|
|
|
transform.rotate_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
|
|
|
}
|