Skip to content

Commit

Permalink
Upgraded to Bevy 0.9 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
neshume authored Jan 12, 2023
1 parent c6c86e4 commit ba1973a
Show file tree
Hide file tree
Showing 8 changed files with 708 additions and 583 deletions.
1,140 changes: 627 additions & 513 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ opt-level = 3
lto = "thin"

[dependencies]
bevy = "0.7.0"
bevy_prototype_lyon = "0.5.0"
bevy = "0.9.0"
bevy_prototype_lyon = "0.7.2"
derive_more = "0.99.17"
rand = { version = "0.8.5", features = ["small_rng"] }
58 changes: 31 additions & 27 deletions src/boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,49 @@ pub struct BoundaryWrap;
pub struct BoundaryRemoval;

fn boundary_wrap_system(
window: Res<WindowDescriptor>,
windows: ResMut<Windows>,
mut query: Query<(&mut Transform, &Bounding), With<BoundaryWrap>>,
) {
for (mut transform, radius) in query.iter_mut() {
let x = transform.translation.x;
let y = transform.translation.y;
if let Some(window) = windows.get_primary() {
for (mut transform, radius) in query.iter_mut() {
let x = transform.translation.x;
let y = transform.translation.y;

let half_width = window.width / 2.0;
if x + radius.0 * 2.0 < -half_width {
transform.translation.x = half_width + radius.0 * 2.0;
} else if x - radius.0 * 2.0 > half_width {
transform.translation.x = -half_width - radius.0 * 2.0;
}
let half_width = window.width() / 2.0;
if x + radius.0 * 2.0 < -half_width {
transform.translation.x = half_width + radius.0 * 2.0;
} else if x - radius.0 * 2.0 > half_width {
transform.translation.x = -half_width - radius.0 * 2.0;
}

let half_height = window.height / 2.0;
if y + radius.0 * 2.0 < -half_height {
transform.translation.y = half_height + radius.0 * 2.0;
} else if y - radius.0 * 2.0 > half_height {
transform.translation.y = -half_height - radius.0 * 2.0;
let half_height = window.height() / 2.0;
if y + radius.0 * 2.0 < -half_height {
transform.translation.y = half_height + radius.0 * 2.0;
} else if y - radius.0 * 2.0 > half_height {
transform.translation.y = -half_height - radius.0 * 2.0;
}
}
}
}

fn boundary_remove_system(
window: Res<WindowDescriptor>,
mut commands: Commands,
windows: ResMut<Windows>,
query: Query<(Entity, &Transform, &Bounding), With<BoundaryRemoval>>,
) {
for (entity, transform, radius) in query.iter() {
let half_width = window.width / 2.0;
let half_height = window.height / 2.0;
let x = transform.translation.x;
let y = transform.translation.y;
if x + radius.0 * 2.0 < -half_width
|| x - radius.0 * 2.0 > half_width
|| y + radius.0 * 2.0 < -half_height
|| y - radius.0 * 2.0 > half_height
{
commands.entity(entity).despawn();
if let Some(window) = windows.get_primary() {
for (entity, transform, radius) in query.iter() {
let half_width = window.width() / 2.0;
let half_height = window.height() / 2.0;
let x = transform.translation.x;
let y = transform.translation.y;
if x + radius.0 * 2.0 < -half_width
|| x - radius.0 * 2.0 > half_width
|| y + radius.0 * 2.0 < -half_height
|| y - radius.0 * 2.0 > half_height
{
commands.entity(entity).despawn();
}
}
}
}
2 changes: 1 addition & 1 deletion src/expiration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Expiration(Timer);

impl Expiration {
pub fn new(duration: Duration) -> Self {
Self(Timer::new(duration, false))
Self(Timer::new(duration, TimerMode::Once))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/flickering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Flick(Timer);

impl Flick {
pub fn new(frequency: Duration) -> Self {
Self(Timer::new(frequency, true))
Self(Timer::new(frequency, TimerMode::Repeating))
}
}

Expand Down
76 changes: 41 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{f32::consts::PI, ops::Range, time::Duration};

use bevy::{core::FixedTimestep, prelude::*, utils::HashSet, window::PresentMode};
use bevy::{prelude::*, time::FixedTimestep, utils::HashSet};
use bevy_prototype_lyon::{
entity::ShapeBundle,
prelude::{
Expand All @@ -28,14 +28,17 @@ mod random;

fn main() {
App::new()
.insert_resource(WindowDescriptor {
title: "Bevyroids".to_string(),
present_mode: PresentMode::Fifo,
..default()
})
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
title: "Bevyroids".to_string(),
width: 800.0,
height: 600.0,
..Default::default()
},
..Default::default()
}))
.insert_resource(ClearColor(Color::BLACK))
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(ShapePlugin)
.insert_resource(AsteroidSizes {
big: 50.0..60.0,
Expand Down Expand Up @@ -77,7 +80,7 @@ fn main() {
.run();
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Resource)]
struct AsteroidSizes {
big: Range<f32>,
medium: Range<f32>,
Expand Down Expand Up @@ -124,7 +127,7 @@ impl Default for Weapon {
impl Weapon {
fn new(rate_of_fire: Duration) -> Self {
Self {
cooldown: Timer::new(rate_of_fire, true),
cooldown: Timer::new(rate_of_fire, TimerMode::Repeating),
..Default::default()
}
}
Expand All @@ -147,13 +150,13 @@ impl Ship {

fn dead(duration: Duration) -> Self {
Ship {
state: ShipState::Dead(Timer::new(duration, false)),
state: ShipState::Dead(Timer::new(duration, TimerMode::Once)),
}
}

fn spawn(duration: Duration) -> Self {
Ship {
state: ShipState::Spawning(Timer::new(duration, false)),
state: ShipState::Spawning(Timer::new(duration, TimerMode::Once)),
}
}
}
Expand Down Expand Up @@ -187,20 +190,20 @@ enum UfoState {

impl Default for UfoState {
fn default() -> Self {
UfoState::Alive(Timer::new(Duration::from_secs(5), false))
UfoState::Alive(Timer::new(Duration::from_secs(5), TimerMode::Once))
}
}

impl Ufo {
fn alive(duration: Duration) -> Self {
Ufo {
state: UfoState::Alive(Timer::new(duration, false)),
state: UfoState::Alive(Timer::new(duration, TimerMode::Once)),
}
}

fn changing_direction(duration: Duration) -> Self {
Ufo {
state: UfoState::ChangingDirection(Timer::new(duration, false)),
state: UfoState::ChangingDirection(Timer::new(duration, TimerMode::Once)),
}
}
}
Expand Down Expand Up @@ -244,8 +247,8 @@ impl Default for ExplosionBundle {
}

fn setup_system(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn().insert(Ship::spawn(Duration::from_secs(0)));
commands.spawn(Camera2dBundle::default());
commands.spawn(Ship::spawn(Duration::from_secs(0)));
}

fn thrust_system(mut query: Query<(&mut Velocity, &ThrustEngine, &Transform)>) {
Expand Down Expand Up @@ -280,7 +283,7 @@ fn weapon_system(
let bullet_pos = transform.translation + (bullet_dir * bounds);

commands
.spawn_bundle(GeometryBuilder::build_as(
.spawn(GeometryBuilder::build_as(
&shapes::Circle {
radius: 2.0,
center: Vec2::ZERO,
Expand Down Expand Up @@ -314,7 +317,7 @@ fn ship_state_system(
if timer.elapsed().is_zero() {
commands
.entity(entity)
.remove_bundle::<ShapeBundle>()
.remove::<ShapeBundle>()
.remove::<SteeringControl>()
.remove::<Weapon>()
.remove::<ThrustEngine>()
Expand All @@ -332,7 +335,7 @@ fn ship_state_system(
if timer.elapsed().is_zero() {
commands
.entity(entity)
.insert_bundle(GeometryBuilder::build_as(
.insert(GeometryBuilder::build_as(
&{
let mut path_builder = PathBuilder::new();
path_builder.move_to(Vec2::ZERO);
Expand Down Expand Up @@ -429,32 +432,33 @@ fn ufo_state_system(
}

fn ufo_spawn_system(
window: Res<WindowDescriptor>,
windows: Res<Windows>,
mut rng: Local<Random>,
mut commands: Commands,
ships: Query<Entity, With<Ship>>,
) {
if rng.gen_bool(1.0 / 10.0) {
let h = (window.height * 0.8) / 2.0;
let w = window.width / 2.0;
let window = windows.primary();
let h = (window.height() * 0.8) / 2.0;
let w = window.width() / 2.0;

let y = rng.gen_range(-h..h);
let x = [-w, w].choose(&mut **rng).copied().unwrap();

let c = 30.0;
let position = Vec3::new(if x > 0.0 { w + c } else { -w - c }, y, 0.0);

let mut ufo = commands.spawn();
let mut ufo = commands.spawn_empty();

ufo.insert_bundle(GeometryBuilder::build_as(
ufo.insert(GeometryBuilder::build_as(
&shapes::Rectangle {
extents: Vec2::new(c, c / 2.0),
..Default::default()
},
DrawMode::Stroke(StrokeMode::new(Color::WHITE, 1.0)),
Transform::default().with_translation(position),
))
.insert_bundle(GeometryBuilder::build_as(
.insert(GeometryBuilder::build_as(
&{
let h = c / 2.5;
let w = c;
Expand Down Expand Up @@ -505,14 +509,15 @@ fn ufo_spawn_system(
}

fn asteroid_spawn_system(
window: Res<WindowDescriptor>,
windows: ResMut<Windows>,
asteroid_sizes: Res<AsteroidSizes>,
mut rng: Local<Random>,
mut asteroids: EventWriter<AsteroidSpawnEvent>,
) {
if rng.gen_bool(1.0 / 3.0) {
let w = window.width / 2.0;
let h = window.height / 2.0;
let window = windows.primary();
let w = window.width() / 2.0;
let h = window.height() / 2.0;

let x = rng.gen_range(-w..w);
let y = rng.gen_range(-h..h);
Expand All @@ -534,14 +539,15 @@ fn asteroid_spawn_system(
}

fn asteroid_generation_system(
window: Res<WindowDescriptor>,
windows: Res<Windows>,
asteroid_sizes: Res<AsteroidSizes>,
mut rng: Local<Random>,
mut asteroids: EventReader<AsteroidSpawnEvent>,
mut commands: Commands,
) {
let w = window.width / 2.0;
let h = window.height / 2.0;
let window = windows.primary();
let w = window.width() / 2.0;
let h = window.height() / 2.0;

for AsteroidSpawnEvent(position, bounds) in asteroids.iter() {
let velocity = Vec2::new(rng.gen_range(-w..w), rng.gen_range(-h..h));
Expand Down Expand Up @@ -575,7 +581,7 @@ fn asteroid_generation_system(
};

commands
.spawn_bundle(GeometryBuilder::build_as(
.spawn(GeometryBuilder::build_as(
&shape,
DrawMode::Stroke(StrokeMode::new(Color::WHITE, 1.0)),
Transform::default().with_translation(Vec3::new(position.x, position.y, 0.0)),
Expand Down Expand Up @@ -655,7 +661,7 @@ fn ship_hit_system(
.insert(Ship::dead(Duration::from_secs(2)));

commands
.spawn_bundle(ExplosionBundle::default())
.spawn(ExplosionBundle::default())
.insert(Transform::default().with_translation(position))
.insert(Velocity::from(
Vec2::new(angle.cos(), angle.sin()) * rng.gen_range(150.0..250.0),
Expand Down Expand Up @@ -711,7 +717,7 @@ fn asteroid_hit_system(
let position = direction * rng.gen_range(1.0..20.0) + transform.translation;

commands
.spawn_bundle(ExplosionBundle::default())
.spawn(ExplosionBundle::default())
.insert(Transform::default().with_translation(position))
.insert(Velocity::from(
Vec2::new(angle.cos(), angle.sin()) * rng.gen_range(50.0..100.0),
Expand Down Expand Up @@ -757,7 +763,7 @@ fn ufo_hit_system(
let position = direction * rng.gen_range(1.0..20.0) + transform.translation;

commands
.spawn_bundle(ExplosionBundle::default())
.spawn(ExplosionBundle::default())
.insert(Transform::default().with_translation(position))
.insert(Velocity::from(
Vec2::new(angle.cos(), angle.sin()) * rng.gen_range(50.0..100.0),
Expand Down
7 changes: 4 additions & 3 deletions src/physics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{core::FixedTimestep, prelude::*};
use bevy::{prelude::*, time::FixedTimestep};
use derive_more::From;

pub struct PhysicsPlugin {
Expand All @@ -20,6 +20,7 @@ impl Default for PhysicsPlugin {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemLabel)]
pub struct PhysicsSystemLabel;

#[derive(Resource)]
pub struct TimeStep(pub f32);

impl Plugin for PhysicsPlugin {
Expand All @@ -36,10 +37,10 @@ impl Plugin for PhysicsPlugin {
}
}

#[derive(Debug, Component, Default, Deref, DerefMut, From)]
#[derive(Debug, Component, Default, Deref, DerefMut, From, Resource)]
pub struct Velocity(Vec2);

#[derive(Debug, Component, Default, Deref, DerefMut, From)]
#[derive(Debug, Component, Default, Deref, DerefMut, From, Resource)]
pub struct AngularVelocity(f32);

#[derive(Debug, Component, Default, Deref, DerefMut, From)]
Expand Down
2 changes: 1 addition & 1 deletion src/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl Plugin for RandomPlugin {
}
}

#[derive(Debug, Deref, DerefMut)]
#[derive(Debug, Deref, DerefMut, Resource)]
pub struct Random(SmallRng);

impl FromWorld for Random {
Expand Down

0 comments on commit ba1973a

Please sign in to comment.