authored by
Asherah Connor
<ashe@kivikakk.ee>
6 months ago
committed by Asherah Connor <ashe@kivikakk.ee> 6 months ago
committed by Asherah Connor <ashe@kivikakk.ee> 6 months ago
src/main.rs
| 40 +++++++++++++++++++++-------------------
1 file changed, 21 insertions(+), 19 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 65b1401..8217a75 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
-use std::{f32::consts::PI, time::Duration};
+use std::f32::consts::PI;
use bevy::{
math::ops::{atan2, hypot},
@@ -19,6 +19,7 @@ const PLAYER_ACCELERATION: f32 = 800.;
const BULLET_SIZE: f32 = 5.;
const BULLET_SPEED: f32 = 700.;
+const BULLET_SPREAD: f32 = PI / 16.;
const SCOREBOARD_FONT_SIZE: f32 = 17.;
const SCOREBOARD_TEXT_PADDING: Val = Val::Px(5.0);
@@ -48,18 +49,15 @@ fn main() {
.insert_resource(ClearColor(BACKGROUND_COLOR))
.add_event::<CollisionEvent>()
.add_systems(Startup, setup)
+ .add_systems(FixedUpdate, (apply_velocity, apply_friction, edges).chain())
.add_systems(
- FixedUpdate,
+ Update,
(
- apply_velocity,
- apply_friction,
- edges,
handle_player_movement,
handle_player_fire,
- )
- .chain(),
+ update_scoreboard,
+ ),
)
- .add_systems(Update, update_scoreboard)
.run();
}
@@ -149,7 +147,7 @@ fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Time>
}
}
-fn apply_friction(mut query: Query<(&mut Velocity), With<Friction>>, time: Res<Time>) {
+fn apply_friction(mut query: Query<&mut Velocity, With<Friction>>, time: Res<Time>) {
for mut velocity in &mut query {
velocity.x -= velocity.x * UNIVERSAL_FRICTION * time.delta_secs();
velocity.y -= velocity.y * UNIVERSAL_FRICTION * time.delta_secs();
@@ -226,32 +224,36 @@ fn handle_player_fire(
for gamepad in gamepads.iter() {
let rt2 = gamepad.get(GamepadButton::RightTrigger2).unwrap();
- if rt2.abs() > 0.01 {
+ if rt2.abs() > 0. {
if fire_cooldown.0.finished() {
+ let base_trans = Transform::from_translation(transform.translation.with_z(0.))
+ .with_rotation(transform.rotation)
+ .with_scale(Vec2::splat(BULLET_SIZE).extend(1.));
+ let mut rl = base_trans.clone();
+ rl.rotate_z(BULLET_SPREAD);
+ let mut rr = base_trans.clone();
+ rr.rotate_z(-BULLET_SPREAD);
commands.spawn_batch([
(
Bullet,
Mesh2d(meshes.add(Circle::default())),
MeshMaterial2d(materials.add(BULLET_COLOR)),
- Transform::from_translation(transform.translation.with_z(0.))
- .with_scale(Vec2::splat(BULLET_SIZE).extend(1.)),
- Velocity(velocity.0 + (transform.rotation * Vec3::Y).xy() * BULLET_SPEED),
+ base_trans,
+ Velocity(velocity.0 + (base_trans.rotation * Vec3::Y).xy() * BULLET_SPEED),
),
(
Bullet,
Mesh2d(meshes.add(Circle::default())),
MeshMaterial2d(materials.add(BULLET_COLOR)),
- Transform::from_translation(transform.translation.with_z(0.))
- .with_scale(Vec2::splat(BULLET_SIZE).extend(1.)),
- Velocity(velocity.0 + (transform.rotation * Vec3::Y).xy() * BULLET_SPEED),
+ rl,
+ Velocity(velocity.0 + (rl.rotation * Vec3::Y).xy() * BULLET_SPEED),
),
(
Bullet,
Mesh2d(meshes.add(Circle::default())),
MeshMaterial2d(materials.add(BULLET_COLOR)),
- Transform::from_translation(transform.translation.with_z(0.))
- .with_scale(Vec2::splat(BULLET_SIZE).extend(1.)),
- Velocity(velocity.0 + (transform.rotation * Vec3::Y).xy() * BULLET_SPEED),
+ rr,
+ Velocity(velocity.0 + (rr.rotation * Vec3::Y).xy() * BULLET_SPEED),
),
]);
fire_cooldown.0 = Timer::from_seconds(1. / fire_rate.0, TimerMode::Repeating);