Compare commits
14 Commits
809810b8ce
...
dea8a0dc1a
| Author | SHA1 | Date | |
|---|---|---|---|
| dea8a0dc1a | |||
| 6191fde25a | |||
| d4f11faf5a | |||
| e841facf73 | |||
| 939ffc70a1 | |||
| 877c7f93d7 | |||
| 2b1a0f386e | |||
| 515ecaac27 | |||
| 3922cac3d7 | |||
| e834d94b8a | |||
| ad5e86a06b | |||
| 3d0da6df2d | |||
| 73b97ad15c | |||
| 61c57783f1 |
@@ -1,12 +1,13 @@
|
||||
//! This is the module containing all the rock-related things.
|
||||
//! Not... not the whole game.
|
||||
|
||||
use bevy_rapier2d::prelude::*;
|
||||
use rand::{Rng, SeedableRng};
|
||||
use std::time::Duration;
|
||||
|
||||
/// This is the module containing all the rock-related things
|
||||
/// not... not the whole game.
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{GameAssets, Position, Rotation, Velocity, WorldSize};
|
||||
use crate::{GameAssets, WorldSize, physics::Velocity};
|
||||
|
||||
#[derive(Component, Deref, DerefMut)]
|
||||
pub struct Asteroid(AsteroidSize);
|
||||
@@ -119,9 +120,8 @@ pub fn spawn_asteroid(
|
||||
Asteroid(AsteroidSize::Small),
|
||||
Collider::ball(collider_radius),
|
||||
Sensor,
|
||||
Position(spawn.pos),
|
||||
Transform::from_translation(spawn.pos.extend(0.0)),
|
||||
Velocity(spawn.vel),
|
||||
Rotation(0.0),
|
||||
Mesh2d(mesh),
|
||||
MeshMaterial2d(material),
|
||||
));
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/*
|
||||
Global constants used all over the program. Rather than leaving them scattered
|
||||
where ever they happen to be needed, I'm concentrating them here.
|
||||
*/
|
||||
//! Global constants used all over the program. Rather than leaving them scattered
|
||||
//! where ever they happen to be needed, I'm concentrating them here.
|
||||
|
||||
use bevy::color::Color;
|
||||
|
||||
@@ -15,6 +13,6 @@ pub(crate) const ASTEROID_SMALL_COLOR: Color = Color::srgb(1.0, 0., 0.);
|
||||
// TODO: asteroid medium & large
|
||||
|
||||
pub(crate) const SHIP_THRUST: f32 = 1.0;
|
||||
pub(crate) const SHIP_ROTATION: f32 = 0.1; // +/- rotation speed in... radians per frame
|
||||
pub(crate) const SHIP_ROTATION: f32 = 4.0; // +/- rotation speed in... radians per frame
|
||||
|
||||
pub const RNG_SEED: [u8; 32] = *b"12345678909876543210123456789098";
|
||||
|
||||
101
src/lib.rs
101
src/lib.rs
@@ -1,24 +1,27 @@
|
||||
mod asteroids;
|
||||
pub mod config;
|
||||
mod event;
|
||||
mod physics;
|
||||
mod preparation_widget;
|
||||
mod ship;
|
||||
mod title_screen;
|
||||
|
||||
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE};
|
||||
use crate::asteroids::{Asteroid, AsteroidSpawner};
|
||||
use crate::config::{
|
||||
ASTEROID_SMALL_COLOR, BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST,
|
||||
SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE, WINDOW_SIZE,
|
||||
};
|
||||
use crate::physics::AngularVelocity;
|
||||
use crate::ship::Ship;
|
||||
|
||||
use asteroids::{Asteroid, AsteroidSpawner};
|
||||
use bevy::prelude::*;
|
||||
use bevy_inspector_egui::InspectorOptions;
|
||||
use bevy_inspector_egui::prelude::ReflectInspectorOptions;
|
||||
|
||||
use bevy_rapier2d::{
|
||||
plugin::{NoUserData, RapierPhysicsPlugin},
|
||||
prelude::*,
|
||||
render::RapierDebugRenderPlugin,
|
||||
};
|
||||
use config::{ASTEROID_SMALL_COLOR, SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE};
|
||||
use ship::Ship;
|
||||
|
||||
pub struct AsteroidPlugin;
|
||||
|
||||
@@ -47,7 +50,7 @@ impl Plugin for AsteroidPlugin {
|
||||
(
|
||||
input_ship_thruster,
|
||||
input_ship_rotation,
|
||||
wrap_entities,
|
||||
physics::wrap_entities,
|
||||
asteroids::tick_asteroid_manager,
|
||||
asteroids::spawn_asteroid.after(asteroids::tick_asteroid_manager),
|
||||
collision_listener,
|
||||
@@ -58,13 +61,16 @@ impl Plugin for AsteroidPlugin {
|
||||
)
|
||||
.add_systems(
|
||||
FixedPostUpdate,
|
||||
(integrate_velocity, update_positions, apply_rotation_to_mesh)
|
||||
(
|
||||
physics::integrate_velocity,
|
||||
physics::integrate_angular_velocity,
|
||||
)
|
||||
.run_if(in_state(GameState::Playing)),
|
||||
)
|
||||
.add_event::<asteroids::SpawnAsteroid>()
|
||||
.add_event::<event::AsteroidDestroy>()
|
||||
.add_event::<event::ShipDestroy>();
|
||||
app.insert_state(GameState::TitleScreen);
|
||||
app.insert_state(GameState::Playing);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,19 +136,6 @@ pub enum GameState {
|
||||
GameOver, // Game has ended. Present game over dialogue and await user restart
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Position(bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
struct Velocity(bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
struct Rotation(f32);
|
||||
|
||||
/// Marker for any entity that should wrap on screen edges
|
||||
#[derive(Component)]
|
||||
struct Wrapping;
|
||||
|
||||
#[derive(Resource, Debug, Deref, Clone, Copy)]
|
||||
struct Score(i32);
|
||||
|
||||
@@ -244,14 +237,14 @@ fn spawn_camera(mut commands: Commands) {
|
||||
*/
|
||||
fn input_ship_thruster(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&mut Velocity, &Rotation, &mut Children), With<Ship>>,
|
||||
mut query: Query<(&mut physics::Velocity, &Transform, &mut Children), With<Ship>>,
|
||||
mut commands: Commands,
|
||||
game_assets: Res<GameAssets>,
|
||||
) {
|
||||
// TODO: Maybe change for a Single<Ship>> so this only runs for the one ship
|
||||
// buuut... that would silently do nothing if there are 0 or >1 ships, and
|
||||
// I might want to crash on purpose in that case.
|
||||
let Ok((mut velocity, rotation, children)) = query.single_mut() else {
|
||||
let Ok((mut velocity, transform, children)) = query.single_mut() else {
|
||||
let count = query.iter().count();
|
||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||
};
|
||||
@@ -261,7 +254,7 @@ fn input_ship_thruster(
|
||||
.expect("Couldn't find first child, which should be the thruster");
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
velocity.0 += Vec2::from_angle(rotation.0) * SHIP_THRUST;
|
||||
velocity.0 += (transform.rotation * Vec3::X).xy() * SHIP_THRUST;
|
||||
commands
|
||||
.entity(*thrusters)
|
||||
.insert(MeshMaterial2d(game_assets.thruster_mat_active()));
|
||||
@@ -278,67 +271,19 @@ fn input_ship_thruster(
|
||||
*/
|
||||
fn input_ship_rotation(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<&mut Rotation, With<Ship>>,
|
||||
mut query: Query<&mut AngularVelocity, With<Ship>>,
|
||||
) {
|
||||
let Ok(mut rotation) = query.single_mut() else {
|
||||
let Ok(mut angular_vel) = query.single_mut() else {
|
||||
let count = query.iter().count();
|
||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||
};
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
rotation.0 += SHIP_ROTATION;
|
||||
angular_vel.0 = SHIP_ROTATION;
|
||||
} else if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
rotation.0 -= SHIP_ROTATION;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Combine movement integration steps into one function
|
||||
// They need to be ordered so the physics is deterministic. Bevy can enforce
|
||||
// order, but it makes more sense to cut out the extra machinery and have one
|
||||
// single function. Probably better for cache locality or whatever, too.
|
||||
/*
|
||||
Add velocity to position
|
||||
*/
|
||||
fn integrate_velocity(mut query: Query<(&mut Position, &Velocity)>, time: Res<Time>) {
|
||||
for (mut position, velocity) in &mut query {
|
||||
position.0 += velocity.0 * time.delta_secs();
|
||||
}
|
||||
}
|
||||
|
||||
fn update_positions(mut query: Query<(&mut Transform, &Position)>) {
|
||||
for (mut transform, position) in &mut query {
|
||||
transform.translation.x = position.0.x;
|
||||
transform.translation.y = position.0.y;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Assigns the rotation to the transform by copying it from the Rotation component.
|
||||
*/
|
||||
fn apply_rotation_to_mesh(mut query: Query<(&mut Transform, &Rotation)>) {
|
||||
for (mut transform, rotation) in &mut query {
|
||||
transform.rotation = Quat::from_rotation_z(rotation.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn wrap_entities(mut query: Query<&mut Position, With<Wrapping>>, world_size: Res<WorldSize>) {
|
||||
let right = world_size.width / 2.0;
|
||||
let left = -right;
|
||||
let top = world_size.height / 2.0;
|
||||
let bottom = -top;
|
||||
|
||||
for mut pos in query.iter_mut() {
|
||||
if pos.0.x > right {
|
||||
pos.0.x = left;
|
||||
} else if pos.0.x < left {
|
||||
pos.0.x = right;
|
||||
}
|
||||
|
||||
if pos.0.y > top {
|
||||
pos.0.y = bottom;
|
||||
} else if pos.0.y < bottom {
|
||||
pos.0.y = top;
|
||||
}
|
||||
angular_vel.0 = -SHIP_ROTATION;
|
||||
} else {
|
||||
angular_vel.0 = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
59
src/physics.rs
Normal file
59
src/physics.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
//! Custom physics items
|
||||
//! TODO: Refactor in terms of Rapier2D, *or* implement colliders and remove it.
|
||||
|
||||
use crate::WorldSize;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component)]
|
||||
pub(crate) struct Velocity(pub(crate) bevy::math::Vec2);
|
||||
|
||||
#[derive(Component)]
|
||||
pub(crate) struct AngularVelocity(pub(crate) f32);
|
||||
|
||||
/// Marker for any entity that should wrap on screen edges
|
||||
#[derive(Component)]
|
||||
pub(crate) struct Wrapping;
|
||||
|
||||
/// Integrate linear velocity and update the entity's transform.
|
||||
pub(crate) fn integrate_velocity(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Time>) {
|
||||
for (mut transform, velocity) in &mut query {
|
||||
let delta = velocity.0 * time.delta_secs();
|
||||
transform.translation += delta.extend(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Integrate angular velocity and update the entity's transform.
|
||||
pub(crate) fn integrate_angular_velocity(
|
||||
mut objects: Query<(&mut Transform, &AngularVelocity)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (mut transform, ang_vel) in &mut objects {
|
||||
let delta = ang_vel.0 * time.delta_secs();
|
||||
transform.rotate_z(delta);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn wrap_entities(
|
||||
mut query: Query<&mut Transform, With<Wrapping>>,
|
||||
world_size: Res<WorldSize>,
|
||||
) {
|
||||
let right = world_size.width / 2.0;
|
||||
let left = -right;
|
||||
let top = world_size.height / 2.0;
|
||||
let bottom = -top;
|
||||
|
||||
for mut pos in query.iter_mut() {
|
||||
if pos.translation.x > right {
|
||||
pos.translation.x = left;
|
||||
} else if pos.translation.x < left {
|
||||
pos.translation.x = right;
|
||||
}
|
||||
|
||||
if pos.translation.y > top {
|
||||
pos.translation.y = bottom;
|
||||
} else if pos.translation.y < bottom {
|
||||
pos.translation.y = top;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::GameState;
|
||||
|
||||
use bevy::{
|
||||
color::palettes::css::{BLACK, GREEN, LIGHT_BLUE, RED},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
use crate::GameState;
|
||||
|
||||
pub fn preparation_widget_plugin(app: &mut App) {
|
||||
app.add_systems(OnEnter(GameState::GetReady), spawn_get_ready)
|
||||
.add_systems(OnExit(GameState::GetReady), despawn_get_ready)
|
||||
|
||||
10
src/ship.rs
10
src/ship.rs
@@ -1,8 +1,11 @@
|
||||
use crate::{
|
||||
AngularVelocity, GameAssets,
|
||||
physics::{Velocity, Wrapping},
|
||||
};
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier2d::prelude::*;
|
||||
|
||||
use crate::{GameAssets, Position, Rotation, Velocity, Wrapping};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Ship;
|
||||
|
||||
@@ -15,9 +18,8 @@ pub fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
||||
ActiveCollisionTypes::STATIC_STATIC,
|
||||
Ship,
|
||||
Wrapping,
|
||||
Position(Vec2::default()),
|
||||
Velocity(Vec2::ZERO),
|
||||
Rotation(0.0),
|
||||
AngularVelocity(0.0),
|
||||
Mesh2d(game_assets.ship().0),
|
||||
MeshMaterial2d(game_assets.ship().1),
|
||||
Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)),
|
||||
|
||||
Reference in New Issue
Block a user