2020-11-13 02:03:57 +00:00
|
|
|
use bevy::{
|
|
|
|
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
const BIRDS_PER_SECOND: u32 = 1000;
|
|
|
|
const GRAVITY: f32 = -9.8 * 100.0;
|
|
|
|
const MAX_VELOCITY: f32 = 750.;
|
|
|
|
const BIRD_SCALE: f32 = 0.15;
|
|
|
|
const HALF_BIRD_SIZE: f32 = 256. * BIRD_SCALE * 0.5;
|
|
|
|
struct BevyCounter {
|
|
|
|
pub count: u128,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bird {
|
|
|
|
velocity: Vec3,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BirdMaterial(Handle<ColorMaterial>);
|
|
|
|
|
|
|
|
impl FromResources for BirdMaterial {
|
|
|
|
fn from_resources(resources: &Resources) -> Self {
|
|
|
|
let mut color_materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
|
|
|
|
let asset_server = resources.get_mut::<AssetServer>().unwrap();
|
|
|
|
BirdMaterial(color_materials.add(asset_server.load("branding/icon.png").into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
|
|
|
.add_resource(WindowDescriptor {
|
|
|
|
title: "BevyMark".to_string(),
|
2020-12-13 23:05:56 +00:00
|
|
|
width: 800.,
|
|
|
|
height: 600.,
|
2020-11-13 02:03:57 +00:00
|
|
|
vsync: true,
|
|
|
|
resizable: false,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
|
|
|
.add_resource(BevyCounter { count: 0 })
|
|
|
|
.init_resource::<BirdMaterial>()
|
2020-11-17 02:18:00 +00:00
|
|
|
.add_startup_system(setup)
|
|
|
|
.add_system(mouse_handler)
|
|
|
|
.add_system(movement_system)
|
|
|
|
.add_system(collision_system)
|
|
|
|
.add_system(counter_system)
|
2020-11-13 02:03:57 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(commands: &mut Commands, asset_server: Res<AssetServer>) {
|
|
|
|
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-16 04:32:23 +00:00
|
|
|
.spawn(TextBundle {
|
2020-11-13 02:03:57 +00:00
|
|
|
text: Text {
|
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
value: "Bird Count:".to_string(),
|
|
|
|
style: TextStyle {
|
|
|
|
color: Color::rgb(0.0, 1.0, 0.0),
|
|
|
|
font_size: 40.0,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
style: Style {
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
position: Rect {
|
|
|
|
top: Val::Px(5.0),
|
|
|
|
left: Val::Px(5.0),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mouse_handler(
|
|
|
|
commands: &mut Commands,
|
|
|
|
time: Res<Time>,
|
|
|
|
mouse_button_input: Res<Input<MouseButton>>,
|
|
|
|
window: Res<WindowDescriptor>,
|
|
|
|
bird_material: Res<BirdMaterial>,
|
|
|
|
mut counter: ResMut<BevyCounter>,
|
|
|
|
) {
|
|
|
|
if mouse_button_input.pressed(MouseButton::Left) {
|
2020-11-28 21:08:31 +00:00
|
|
|
let spawn_count = (BIRDS_PER_SECOND as f32 * time.delta_seconds()) as u128;
|
2020-12-13 23:05:56 +00:00
|
|
|
let bird_x = (window.width / -2.) + HALF_BIRD_SIZE;
|
|
|
|
let bird_y = (window.height / 2.) - HALF_BIRD_SIZE;
|
2020-11-13 02:03:57 +00:00
|
|
|
|
|
|
|
for count in 0..spawn_count {
|
|
|
|
let bird_position = Vec3::new(bird_x, bird_y, (counter.count + count) as f32 * 0.00001);
|
|
|
|
let mut transform = Transform::from_translation(bird_position);
|
|
|
|
transform.scale = Vec3::new(BIRD_SCALE, BIRD_SCALE, BIRD_SCALE);
|
|
|
|
|
|
|
|
commands
|
2020-11-16 04:32:23 +00:00
|
|
|
.spawn(SpriteBundle {
|
2020-11-13 02:03:57 +00:00
|
|
|
material: bird_material.0.clone(),
|
|
|
|
transform,
|
2020-12-09 21:38:48 +00:00
|
|
|
visible: Visible {
|
2020-11-13 02:03:57 +00:00
|
|
|
is_transparent: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with(Bird {
|
|
|
|
velocity: Vec3::new(
|
|
|
|
rand::random::<f32>() * MAX_VELOCITY - (MAX_VELOCITY * 0.5),
|
|
|
|
0.,
|
|
|
|
0.,
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
counter.count += spawn_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn movement_system(time: Res<Time>, mut bird_query: Query<(&mut Bird, &mut Transform)>) {
|
|
|
|
for (mut bird, mut transform) in bird_query.iter_mut() {
|
2020-11-28 21:08:31 +00:00
|
|
|
transform.translation.x += bird.velocity.x * time.delta_seconds();
|
|
|
|
transform.translation.y += bird.velocity.y * time.delta_seconds();
|
|
|
|
bird.velocity.y += GRAVITY * time.delta_seconds();
|
2020-11-13 02:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collision_system(window: Res<WindowDescriptor>, mut bird_query: Query<(&mut Bird, &Transform)>) {
|
|
|
|
let half_width = window.width as f32 * 0.5;
|
|
|
|
let half_height = window.height as f32 * 0.5;
|
|
|
|
|
|
|
|
for (mut bird, transform) in bird_query.iter_mut() {
|
2020-11-17 21:40:18 +00:00
|
|
|
let x_vel = bird.velocity.x;
|
|
|
|
let y_vel = bird.velocity.y;
|
|
|
|
let x_pos = transform.translation.x;
|
|
|
|
let y_pos = transform.translation.y;
|
2020-11-13 02:03:57 +00:00
|
|
|
|
|
|
|
if (x_vel > 0. && x_pos + HALF_BIRD_SIZE > half_width)
|
|
|
|
|| (x_vel <= 0. && x_pos - HALF_BIRD_SIZE < -(half_width))
|
|
|
|
{
|
2020-11-17 21:40:18 +00:00
|
|
|
bird.velocity.x = -x_vel;
|
2020-11-13 02:03:57 +00:00
|
|
|
}
|
|
|
|
if y_vel < 0. && y_pos - HALF_BIRD_SIZE < -half_height {
|
2020-11-17 21:40:18 +00:00
|
|
|
bird.velocity.y = -y_vel;
|
2020-11-13 02:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn counter_system(
|
|
|
|
diagnostics: Res<Diagnostics>,
|
|
|
|
counter: Res<BevyCounter>,
|
|
|
|
mut query: Query<&mut Text>,
|
|
|
|
) {
|
|
|
|
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
|
|
|
|
if let Some(average) = fps.average() {
|
|
|
|
for mut text in query.iter_mut() {
|
|
|
|
text.value = format!("Bird Count: {}\nAverage FPS: {:.2}", counter.count, average);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|