Compare commits
119 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d9b25d1df | |||
| 89ee328807 | |||
| 90d8142855 | |||
| 10366b642c | |||
| 94e0cd0999 | |||
| 18d026637b | |||
| 51e6989ef4 | |||
| 2ffc0e8861 | |||
| d15b96ef48 | |||
| 0aefc96f7a | |||
| 4102446e90 | |||
| f4df2ae33a | |||
| fc43be0777 | |||
| a74b99deb4 | |||
| 2f9401e93f | |||
| 54ef257ab4 | |||
| 69bef24913 | |||
| df4479bf49 | |||
| 33828d6a85 | |||
| 5e2018d3e4 | |||
| 7c877264a2 | |||
| 16842c13f7 | |||
| e199db16eb | |||
| f17910daef | |||
| 70f2313766 | |||
| 58bbc1e614 | |||
| 79679759c5 | |||
| 0e517de419 | |||
| cb2b57449a | |||
| 9a262fcffc | |||
| 34ee2fcc7d | |||
| 93da225636 | |||
| cd194e2dbf | |||
| 1369a3092f | |||
| 619037dab0 | |||
| c1b69c412a | |||
| d8a83b77c2 | |||
| f5ff7c8779 | |||
| f4484f759f | |||
| 571b910945 | |||
| 804186ea2f | |||
| 45b1fe751f | |||
| 3d28d489b0 | |||
| cdd665cc93 | |||
| 364fbd7530 | |||
| 2f463303a0 | |||
| 8d689d7842 | |||
| 65f28e832f | |||
| dea8a0dc1a | |||
| 6191fde25a | |||
| d4f11faf5a | |||
| e841facf73 | |||
| 939ffc70a1 | |||
| 877c7f93d7 | |||
| 2b1a0f386e | |||
| 515ecaac27 | |||
| 3922cac3d7 | |||
| e834d94b8a | |||
| ad5e86a06b | |||
| 3d0da6df2d | |||
| 73b97ad15c | |||
| 61c57783f1 | |||
| 809810b8ce | |||
| 29735e7426 | |||
| ab5f489450 | |||
| d4ceaa6312 | |||
| f553574e3e | |||
| 96e9376330 | |||
| 07105760f5 | |||
| 1555c93bed | |||
| 2b93654491 | |||
| c80ada4aa6 | |||
| eee039339e | |||
| 04f192b62a | |||
| 2dd3b2ff61 | |||
| 2c43bc699e | |||
| 911b6c5fe7 | |||
| 71ec77f5b1 | |||
| 5dfe11d31f | |||
| 40102bf46b | |||
| 0eac337c00 | |||
| 3d4e0afc58 | |||
| f62ab2c95d | |||
| 6681b25728 | |||
| 68a8de1809 | |||
| a6622f24b5 | |||
| c37887b0e7 | |||
| 31451732c4 | |||
| 4ecbfaa370 | |||
| eb50655671 | |||
| a7d54c9192 | |||
| fccd2e6a8b | |||
| efabcbf636 | |||
| f848de6b2e | |||
| e605bbf80d | |||
| 39bddf1c9e | |||
| 477460ad2f | |||
| c11322969c | |||
| 7123192271 | |||
| 9a2381249f | |||
| f68d841e52 | |||
| 88db8a868a | |||
| 584a30f7f8 | |||
| 430b77be2e | |||
| 6eb69f476f | |||
| a4409cb946 | |||
| 38fbc85505 | |||
| 08c9625e71 | |||
| 96aff4ae46 | |||
| a52311eac6 | |||
| cf678f9f16 | |||
| 2f9afaeac1 | |||
| 7f5a166f10 | |||
| 5e6440340f | |||
| 290aab45f5 | |||
| 406e611e31 | |||
| c86cd0d642 | |||
| f114203665 | |||
| 37d7c1db42 |
4
.cargo/config.toml
Normal file
4
.cargo/config.toml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
[target.wasm32-unknown-unknown]
|
||||||
|
runner = "wasm-server-runner"
|
||||||
|
rustflags = ["--cfg", "getrandom_backend=\"wasm_js\""]
|
||||||
32
Cargo.toml
32
Cargo.toml
@@ -1,7 +1,33 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "asteroids"
|
name = "asteroids"
|
||||||
version = "0.1.0"
|
version = "0.3.0"
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.14.2"
|
bevy = "0.16"
|
||||||
|
bevy-inspector-egui = "0.32.0"
|
||||||
|
bevy_rapier2d = "0.31.0"
|
||||||
|
rand = "0.9.2"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
dynamic_linking = ["bevy/dynamic_linking"]
|
||||||
|
debug_ui = ["bevy_rapier2d/debug-render-2d"]
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
getrandom = { version = "0.3.3", features = ["wasm_js"] }
|
||||||
|
|
||||||
|
[profile.speedy]
|
||||||
|
inherits = "release"
|
||||||
|
codegen-units = 1
|
||||||
|
lto = "fat"
|
||||||
|
opt-level = 3
|
||||||
|
strip = "symbols"
|
||||||
|
panic = "abort"
|
||||||
|
|
||||||
|
[profile.tiny]
|
||||||
|
inherits = "release"
|
||||||
|
codegen-units = 1
|
||||||
|
lto = "fat"
|
||||||
|
opt-level = "z"
|
||||||
|
strip = "symbols"
|
||||||
|
panic = "abort"
|
||||||
|
|||||||
@@ -1,16 +1,28 @@
|
|||||||
/*
|
//! Global constants used all over the program. Rather than leaving them scattered
|
||||||
Global constants used all over the program. Rather than leaving them scattered
|
//! where ever they happen to be needed, I'm concentrating them here.
|
||||||
where ever they happen to be needed, I'm concentrating them here.
|
|
||||||
*/
|
|
||||||
|
|
||||||
use bevy::color::Color;
|
use bevy::color::Color;
|
||||||
|
|
||||||
pub const WINDOW_SIZE: bevy::prelude::Vec2 = bevy::prelude::Vec2::new(800.0, 600.0);
|
pub const WINDOW_SIZE: bevy::prelude::Vec2 = bevy::prelude::Vec2::new(800.0, 600.0);
|
||||||
|
|
||||||
|
pub const UI_BUTTON_NORMAL: Color = Color::srgb(0.15, 0.15, 0.15); // Button color when it's just hanging out
|
||||||
|
pub const UI_BUTTON_HOVERED: Color = Color::srgb(0.25, 0.25, 0.25); // ... when it's hovered
|
||||||
|
pub const UI_BUTTON_PRESSED: Color = Color::srgb(0.55, 0.55, 0.55); // ... when it's pressed
|
||||||
|
|
||||||
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
pub(crate) const BACKGROUND_COLOR: Color = Color::srgb(0.3, 0.3, 0.3);
|
||||||
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
pub(crate) const PLAYER_SHIP_COLOR: Color = Color::srgb(1.0, 1.0, 1.0);
|
||||||
pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2);
|
pub(crate) const SHIP_THRUSTER_COLOR_ACTIVE: Color = Color::srgb(1.0, 0.2, 0.2);
|
||||||
pub(crate) const SHIP_THRUSTER_COLOR_INACTIVE: Color = Color::srgb(0.5, 0.5, 0.5);
|
pub(crate) const SHIP_THRUSTER_COLOR_INACTIVE: Color = Color::srgb(0.5, 0.5, 0.5);
|
||||||
|
pub(crate) const ASTEROID_SMALL_COLOR: Color = Color::srgb(1.0, 0., 0.);
|
||||||
|
pub(crate) const BULLET_COLOR: Color = Color::srgb(0.0, 0.1, 0.9);
|
||||||
|
// TODO: asteroid medium & large
|
||||||
|
|
||||||
pub(crate) const SHIP_THRUST: f32 = 1.0;
|
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(crate) const BULLET_SPEED: f32 = 150.0;
|
||||||
|
pub(crate) const BULLET_LIFETIME: f32 = 2.0;
|
||||||
|
|
||||||
|
pub(crate) const ASTEROID_LIFETIME: f32 = 40.0;
|
||||||
|
|
||||||
|
pub const RNG_SEED: [u8; 32] = *b"12345678909876543210123456789098";
|
||||||
|
|||||||
36
src/events.rs
Normal file
36
src/events.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::objects::AsteroidSize;
|
||||||
|
|
||||||
|
/// Signals that the player's ship has been destroyed.
|
||||||
|
///
|
||||||
|
/// Produced by the [`fn collision_listener(...)`](`crate::physics::collision_listener`)
|
||||||
|
/// system when the player collides with an asteroid. They are consumed by [`fn ship_impact_listener(...)`](`crate::objects::ship_impact_listener`).
|
||||||
|
#[derive(Event)]
|
||||||
|
pub(crate) struct ShipDestroy;
|
||||||
|
|
||||||
|
/// Signals that a particular asteroid has been destroyed.
|
||||||
|
///
|
||||||
|
/// Produced by the [`fn collision_listener(...)`](`crate::physics::collision_listener`)
|
||||||
|
/// system when bullets contact asteroids. It is consumed by [`fn split_asteroid(...)`](`crate::objects::spawn_asteroid`)
|
||||||
|
/// system, which re-emits [spawn events](`SpawnAsteroid`).
|
||||||
|
#[derive(Event)]
|
||||||
|
pub(crate) struct AsteroidDestroy(pub Entity);
|
||||||
|
|
||||||
|
/// Signals that an asteroid needs to be spawned and provides the parameters
|
||||||
|
/// for that action.
|
||||||
|
///
|
||||||
|
/// Produced by the [`tick_asteroid_manager(...)`](`crate::machinery::tick_asteroid_manager`)
|
||||||
|
/// system and consumed by the [`spawn_asteroid(...)`](`crate::objects::spawn_asteroid`) system.
|
||||||
|
#[derive(Event)]
|
||||||
|
pub struct SpawnAsteroid {
|
||||||
|
pub pos: Vec2,
|
||||||
|
pub vel: Vec2,
|
||||||
|
pub size: AsteroidSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Signals that a particular bullet has been destroyed (after it strikes an Asteroid).
|
||||||
|
///
|
||||||
|
/// TODO: Maybe use it for lifetime expiration (which is also a TODO item).
|
||||||
|
#[derive(Event)]
|
||||||
|
pub(crate) struct BulletDestroy(pub Entity);
|
||||||
283
src/lib.rs
283
src/lib.rs
@@ -1,111 +1,116 @@
|
|||||||
|
//! Asteroids, implemented as a Bevy plugin.
|
||||||
|
//!
|
||||||
|
//! Compile-time configurables can be found in the [`config`] module.
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
mod events;
|
||||||
|
mod machinery;
|
||||||
|
mod objects;
|
||||||
|
mod physics;
|
||||||
|
mod resources;
|
||||||
|
mod widgets;
|
||||||
|
|
||||||
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE};
|
use crate::config::{
|
||||||
|
ASTEROID_SMALL_COLOR, BACKGROUND_COLOR, BULLET_COLOR, BULLET_LIFETIME, BULLET_SPEED,
|
||||||
|
PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, SHIP_THRUSTER_COLOR_ACTIVE,
|
||||||
|
SHIP_THRUSTER_COLOR_INACTIVE,
|
||||||
|
};
|
||||||
|
use crate::machinery::AsteroidSpawner;
|
||||||
|
use crate::objects::{Bullet, Ship};
|
||||||
|
use crate::physics::AngularVelocity;
|
||||||
|
|
||||||
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
|
use bevy::prelude::*;
|
||||||
use config::{SHIP_THRUSTER_COLOR_ACTIVE, SHIP_THRUSTER_COLOR_INACTIVE};
|
use bevy_rapier2d::{
|
||||||
|
plugin::{NoUserData, RapierPhysicsPlugin},
|
||||||
|
prelude::*,
|
||||||
|
render::RapierDebugRenderPlugin,
|
||||||
|
};
|
||||||
|
use machinery::Lifetime;
|
||||||
|
use resources::{GameAssets, Lives, Score, WorldSize};
|
||||||
|
|
||||||
|
/// The main game plugin.
|
||||||
pub struct AsteroidPlugin;
|
pub struct AsteroidPlugin;
|
||||||
|
|
||||||
impl Plugin for AsteroidPlugin {
|
impl Plugin for AsteroidPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Startup, (spawn_camera, spawn_player))
|
app.add_plugins((
|
||||||
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
widgets::PluginGameMenu,
|
||||||
.insert_resource(WorldSize {
|
widgets::PluginGameOver,
|
||||||
width: WINDOW_SIZE.x,
|
widgets::PluginGetReady,
|
||||||
height: WINDOW_SIZE.y,
|
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
|
||||||
})
|
RapierDebugRenderPlugin::default(),
|
||||||
.add_systems(
|
))
|
||||||
FixedUpdate,
|
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
||||||
(input_ship_thruster, input_ship_rotation, wrap_entities),
|
.insert_resource(WorldSize::default())
|
||||||
|
.insert_resource(Lives(3))
|
||||||
|
.register_type::<Lives>()
|
||||||
|
.insert_resource(Score(0))
|
||||||
|
.insert_resource(AsteroidSpawner::new())
|
||||||
|
.init_resource::<GameAssets>()
|
||||||
|
.add_systems(Startup, spawn_camera)
|
||||||
|
.add_systems(
|
||||||
|
OnEnter(GameState::Playing),
|
||||||
|
(objects::spawn_player, widgets::spawn_ui),
|
||||||
|
)
|
||||||
|
.add_systems(
|
||||||
|
FixedUpdate,
|
||||||
|
(
|
||||||
|
input_ship_thruster,
|
||||||
|
input_ship_rotation,
|
||||||
|
input_ship_shoot,
|
||||||
|
physics::wrap_entities,
|
||||||
|
machinery::tick_asteroid_manager,
|
||||||
|
objects::spawn_asteroid.after(machinery::tick_asteroid_manager),
|
||||||
|
objects::split_asteroids,
|
||||||
|
objects::bullet_impact_listener,
|
||||||
|
objects::ship_impact_listener,
|
||||||
|
physics::collision_listener,
|
||||||
|
machinery::tick_lifetimes,
|
||||||
)
|
)
|
||||||
.add_systems(
|
.run_if(in_state(GameState::Playing)),
|
||||||
FixedPostUpdate,
|
)
|
||||||
(integrate_velocity, update_positions, apply_rotation_to_mesh),
|
.add_systems(
|
||||||
);
|
FixedPostUpdate,
|
||||||
|
(
|
||||||
|
physics::integrate_velocity,
|
||||||
|
physics::integrate_angular_velocity,
|
||||||
|
)
|
||||||
|
.run_if(in_state(GameState::Playing)),
|
||||||
|
)
|
||||||
|
.add_event::<events::SpawnAsteroid>()
|
||||||
|
.add_event::<events::AsteroidDestroy>()
|
||||||
|
.add_event::<events::ShipDestroy>()
|
||||||
|
.add_event::<events::BulletDestroy>();
|
||||||
|
app.insert_state(GameState::TitleScreen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
/// The game's main state tracking mechanism, registered with Bevy as a [`State`](`bevy::prelude::State`).
|
||||||
struct Position(bevy::math::Vec2);
|
#[derive(Clone, Debug, Eq, Hash, PartialEq, States)]
|
||||||
|
pub enum GameState {
|
||||||
#[derive(Component)]
|
TitleScreen, // Program is started. Present title screen and await user start
|
||||||
struct Velocity(bevy::math::Vec2);
|
GetReady, // Short timer to let the player get ready after pressing start
|
||||||
|
Playing, // Player has started the game. Run the main loop
|
||||||
#[derive(Component)]
|
GameOver, // Game has ended. Present game over dialogue and await user restart
|
||||||
struct Rotation(f32);
|
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
struct Ship;
|
|
||||||
|
|
||||||
// Data component to store color properties attached to an entity
|
|
||||||
// This was easier (and imo better) than holding global consts with
|
|
||||||
// UUID assets.
|
|
||||||
#[derive(Component)]
|
|
||||||
struct ThrusterColors(Handle<ColorMaterial>, Handle<ColorMaterial>);
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
struct WorldSize {
|
|
||||||
width: f32,
|
|
||||||
height: f32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_camera(mut commands: Commands) {
|
fn spawn_camera(mut commands: Commands) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2d);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_player(
|
/// Player's thruster control system.
|
||||||
mut commands: Commands,
|
///
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
/// Checks if "W" is pressed and increases velocity accordingly.
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
||||||
) {
|
|
||||||
let triangle = Triangle2d::new(
|
|
||||||
Vec2::new(0.5, 0.0),
|
|
||||||
Vec2::new(-0.5, 0.45),
|
|
||||||
Vec2::new(-0.5, -0.45),
|
|
||||||
);
|
|
||||||
let thruster_firing_id = materials.add(SHIP_THRUSTER_COLOR_ACTIVE);
|
|
||||||
let thruster_stopped_id = materials.add(SHIP_THRUSTER_COLOR_INACTIVE);
|
|
||||||
|
|
||||||
let ship_mesh = MaterialMesh2dBundle {
|
|
||||||
mesh: meshes.add(triangle).into(),
|
|
||||||
material: materials.add(PLAYER_SHIP_COLOR),
|
|
||||||
transform: Transform::default().with_scale(Vec3::new(20.0, 20.0, 20.0)),
|
|
||||||
..default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let thruster_mesh = MaterialMesh2dBundle {
|
|
||||||
mesh: meshes.add(triangle).into(),
|
|
||||||
material: materials.add(PLAYER_SHIP_COLOR),
|
|
||||||
transform: Transform::default()
|
|
||||||
.with_scale(Vec3::splat(0.5))
|
|
||||||
.with_translation(Vec3::new(-0.5, 0.0, -0.1)),
|
|
||||||
..default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let thruster = commands.spawn(thruster_mesh).id();
|
|
||||||
|
|
||||||
let mut ship_id = commands.spawn((
|
|
||||||
Ship,
|
|
||||||
Position(Vec2::default()),
|
|
||||||
Velocity(Vec2::ZERO),
|
|
||||||
Rotation(0.0),
|
|
||||||
ship_mesh,
|
|
||||||
ThrusterColors(thruster_firing_id, thruster_stopped_id),
|
|
||||||
));
|
|
||||||
|
|
||||||
ship_id.add_child(thruster);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Checks if "W" is pressed and increases velocity accordingly.
|
|
||||||
*/
|
|
||||||
fn input_ship_thruster(
|
fn input_ship_thruster(
|
||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Query<(&mut Velocity, &Rotation, &mut Children, &ThrusterColors), With<Ship>>,
|
mut query: Query<(&mut physics::Velocity, &Transform, &mut Children), With<Ship>>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
game_assets: Res<GameAssets>,
|
||||||
) {
|
) {
|
||||||
let Ok((mut velocity, rotation, children, colors)) = query.get_single_mut() else {
|
// 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, transform, children)) = query.single_mut() else {
|
||||||
let count = query.iter().count();
|
let count = query.iter().count();
|
||||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
};
|
};
|
||||||
@@ -115,75 +120,71 @@ fn input_ship_thruster(
|
|||||||
.expect("Couldn't find first child, which should be the thruster");
|
.expect("Couldn't find first child, which should be the thruster");
|
||||||
|
|
||||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
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(colors.0.clone());
|
commands
|
||||||
|
.entity(*thrusters)
|
||||||
|
.insert(MeshMaterial2d(game_assets.thruster_mat_active()));
|
||||||
} else {
|
} else {
|
||||||
commands.entity(*thrusters).insert(colors.1.clone());
|
commands
|
||||||
|
.entity(*thrusters)
|
||||||
|
.insert(MeshMaterial2d(game_assets.thruster_mat_inactive()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Player's rotation control system.
|
||||||
Checks if "A" or "D" is pressed and updates the player's Rotation component accordingly
|
///
|
||||||
Does *not* rotate the graphical widget! (that's done by the `apply_rotation_to_mesh` system)
|
/// Checks if "A" or "D" is pressed and updates the player's [`AngularVelocity`]
|
||||||
*/
|
/// component accordingly.
|
||||||
fn input_ship_rotation(
|
fn input_ship_rotation(
|
||||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Query<&mut Rotation, With<Ship>>,
|
mut query: Query<&mut AngularVelocity, With<Ship>>,
|
||||||
) {
|
) {
|
||||||
let Ok(mut rotation) = query.get_single_mut() else {
|
let Ok(mut angular_vel) = query.single_mut() else {
|
||||||
let count = query.iter().count();
|
let count = query.iter().count();
|
||||||
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
panic!("There should be exactly one player ship! Instead, there seems to be {count}.");
|
||||||
};
|
};
|
||||||
|
|
||||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||||
rotation.0 += SHIP_ROTATION;
|
angular_vel.0 = SHIP_ROTATION;
|
||||||
} else if keyboard_input.pressed(KeyCode::KeyD) {
|
} else if keyboard_input.pressed(KeyCode::KeyD) {
|
||||||
rotation.0 -= SHIP_ROTATION;
|
angular_vel.0 = -SHIP_ROTATION;
|
||||||
|
} else {
|
||||||
|
angular_vel.0 = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Player's gun trigger.
|
||||||
Add velocity to position
|
///
|
||||||
*/
|
/// Checks if the spacebar has just been pressed, spawning a bullet if so.
|
||||||
fn integrate_velocity(mut query: Query<(&mut Position, &Velocity)>, time: Res<Time>) {
|
///
|
||||||
for (mut position, velocity) in &mut query {
|
/// TODO: Hook up a timer to control weapon fire-rate. Something will have to
|
||||||
position.0 += velocity.0 * time.delta_seconds();
|
/// tick those timers. Maybe this system?
|
||||||
}
|
fn input_ship_shoot(
|
||||||
}
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||||
|
ship: Single<(&Transform, &physics::Velocity), With<Ship>>,
|
||||||
fn update_positions(mut query: Query<(&mut Transform, &Position)>) {
|
game_assets: Res<GameAssets>,
|
||||||
for (mut transform, position) in &mut query {
|
mut commands: Commands,
|
||||||
transform.translation.x = position.0.x;
|
) {
|
||||||
transform.translation.y = position.0.y;
|
let (ship_pos, ship_vel) = *ship;
|
||||||
}
|
|
||||||
}
|
// Derive bullet velocity, add to the ship's velocity
|
||||||
|
let bullet_vel = (ship_pos.rotation * Vec3::X).xy() * BULLET_SPEED;
|
||||||
/*
|
let bullet_vel = bullet_vel + ship_vel.0;
|
||||||
Assigns the rotation to the transform by copying it from the Rotation component.
|
|
||||||
*/
|
// TODO: create a timer for the gun fire rate.
|
||||||
fn apply_rotation_to_mesh(mut query: Query<(&mut Transform, &Rotation)>) {
|
// For now, spawn one for each press of the spacebar.
|
||||||
for (mut transform, rotation) in &mut query {
|
if keyboard_input.just_pressed(KeyCode::Space) {
|
||||||
transform.rotation = Quat::from_rotation_z(rotation.0);
|
commands.spawn((
|
||||||
}
|
Bullet,
|
||||||
}
|
Collider::ball(0.2),
|
||||||
|
Sensor,
|
||||||
fn wrap_entities(mut query: Query<&mut Position>, world_size: Res<WorldSize>) {
|
ActiveEvents::COLLISION_EVENTS,
|
||||||
let right = world_size.width / 2.0;
|
ActiveCollisionTypes::STATIC_STATIC,
|
||||||
let left = -right;
|
physics::Velocity(bullet_vel),
|
||||||
let top = world_size.height / 2.0;
|
Mesh2d(game_assets.bullet().0),
|
||||||
let bottom = -top;
|
MeshMaterial2d(game_assets.bullet().1),
|
||||||
|
ship_pos.clone(), // clone ship transform
|
||||||
for mut pos in query.iter_mut() {
|
Lifetime(Timer::from_seconds(BULLET_LIFETIME, TimerMode::Once)),
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
105
src/machinery.rs
Normal file
105
src/machinery.rs
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
//! Systems, Components, and any other items for powering the game logic.
|
||||||
|
//!
|
||||||
|
//! Where the objects (ship, asteroid, etc) carry their own behavioral systems,
|
||||||
|
//! the *game* keeps its main logic here. Its for ambient behaviors, like
|
||||||
|
//! asteroid spawning, or eventually the flying saucer spawns.
|
||||||
|
use rand::{Rng, SeedableRng};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::{WorldSize, events::SpawnAsteroid, objects::AsteroidSize};
|
||||||
|
|
||||||
|
/// Asteroid spawning parameters and state.
|
||||||
|
///
|
||||||
|
/// This struct keeps track of the rng and timer for spawning asteroids. In the
|
||||||
|
/// future it may contain additional fields to allow for more control.
|
||||||
|
///
|
||||||
|
/// It's values are operated by the [`tick_asteroid_manager`] system.
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct AsteroidSpawner {
|
||||||
|
rng: std::sync::Mutex<rand::rngs::StdRng>,
|
||||||
|
timer: Timer,
|
||||||
|
// TODO: Configurables?
|
||||||
|
// - interval
|
||||||
|
// - density
|
||||||
|
// - size distribution
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsteroidSpawner {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
rng: std::sync::Mutex::new(rand::rngs::StdRng::from_seed(crate::config::RNG_SEED)),
|
||||||
|
timer: Timer::new(Duration::from_secs(3), TimerMode::Repeating),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update the asteroid spawn timer in the [`AsteroidSpawner`] resource, and
|
||||||
|
/// spawns any asteroids that are due this frame.
|
||||||
|
pub fn tick_asteroid_manager(
|
||||||
|
mut events: EventWriter<SpawnAsteroid>,
|
||||||
|
mut spawner: ResMut<AsteroidSpawner>,
|
||||||
|
time: Res<Time>,
|
||||||
|
play_area: Res<WorldSize>,
|
||||||
|
) {
|
||||||
|
spawner.timer.tick(time.delta());
|
||||||
|
if spawner.timer.just_finished() {
|
||||||
|
let mut rng = spawner
|
||||||
|
.rng
|
||||||
|
.lock()
|
||||||
|
.expect("Expected to acquire lock on the AsteroidSpawner's RNG field.");
|
||||||
|
|
||||||
|
// Use polar coordinate to decide where the asteroid will spawn
|
||||||
|
// Theta will be random between 0 to 2pi
|
||||||
|
let spawn_angle = rng.random_range(0.0..(std::f32::consts::PI * 2.0));
|
||||||
|
// Rho will be the radius of a circle bordering the viewport, multiplied by 1.2
|
||||||
|
// TODO: Use view diagonal to get a minimally sized circle around the play area
|
||||||
|
let spawn_distance = play_area.x.max(play_area.y) / 2.0;
|
||||||
|
|
||||||
|
// Convert polar to Cartesian, use as position
|
||||||
|
let pos = Vec2::new(
|
||||||
|
spawn_distance * spawn_angle.cos(),
|
||||||
|
spawn_distance * spawn_angle.sin(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Right now, I'm thinking I can use the opposite signs attached to the position Vec components.
|
||||||
|
// pos.x == -100, then vel.x = + <random>
|
||||||
|
// pos.x == 100, then vel.x = - <random>
|
||||||
|
// etc,
|
||||||
|
let mut vel = Vec2::new(rng.random_range(0.0..100.0), rng.random_range(0.0..100.0));
|
||||||
|
if pos.x > 0.0 {
|
||||||
|
vel.x *= -1.0;
|
||||||
|
}
|
||||||
|
if pos.y > 0.0 {
|
||||||
|
vel.y *= -1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let size = match rng.random_range(0..=2) {
|
||||||
|
0 => AsteroidSize::Small,
|
||||||
|
1 => AsteroidSize::Medium,
|
||||||
|
2 => AsteroidSize::Large,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
events.write(SpawnAsteroid { pos, vel, size });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets a lifetime on an entity to automatically delete it after expiration.
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Lifetime(pub Timer);
|
||||||
|
|
||||||
|
/// System to tick the [`Lifetime`] timers and despawn expired entities.
|
||||||
|
pub fn tick_lifetimes(
|
||||||
|
mut commands: Commands,
|
||||||
|
time: Res<Time>,
|
||||||
|
query: Query<(Entity, &mut Lifetime)>,
|
||||||
|
) {
|
||||||
|
for (e, mut life) in query {
|
||||||
|
life.0.tick(time.delta());
|
||||||
|
if life.0.just_finished() {
|
||||||
|
commands.entity(e).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
use bevy::{prelude::*, window::WindowResolution};
|
use bevy::{prelude::*, window::WindowResolution};
|
||||||
|
|
||||||
use asteroids::{config::WINDOW_SIZE, AsteroidPlugin};
|
use asteroids::{AsteroidPlugin, config::WINDOW_SIZE};
|
||||||
|
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
@@ -12,5 +13,7 @@ fn main() {
|
|||||||
..default()
|
..default()
|
||||||
}))
|
}))
|
||||||
.add_plugins(AsteroidPlugin)
|
.add_plugins(AsteroidPlugin)
|
||||||
|
.add_plugins(EguiPlugin::default())
|
||||||
|
.add_plugins(WorldInspectorPlugin::new())
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|||||||
207
src/objects.rs
Normal file
207
src/objects.rs
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
//! This module contains all the "things" in the game.
|
||||||
|
//!
|
||||||
|
//! Asteroids, the player's ship, and such.
|
||||||
|
|
||||||
|
use bevy::{
|
||||||
|
ecs::{
|
||||||
|
component::Component,
|
||||||
|
entity::Entity,
|
||||||
|
event::{EventReader, EventWriter},
|
||||||
|
query::With,
|
||||||
|
system::{Commands, Query, Res, ResMut, Single},
|
||||||
|
},
|
||||||
|
math::{Vec2, Vec3, Vec3Swizzles},
|
||||||
|
prelude::{Deref, DerefMut},
|
||||||
|
render::mesh::Mesh2d,
|
||||||
|
sprite::MeshMaterial2d,
|
||||||
|
state::state::NextState,
|
||||||
|
time::{Timer, TimerMode},
|
||||||
|
transform::components::Transform,
|
||||||
|
};
|
||||||
|
use bevy_rapier2d::prelude::{ActiveCollisionTypes, ActiveEvents, Collider, Sensor};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
AngularVelocity, GameAssets, GameState, Lives,
|
||||||
|
config::ASTEROID_LIFETIME,
|
||||||
|
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
||||||
|
machinery::Lifetime,
|
||||||
|
physics::{Velocity, Wrapping},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The asteroid, defined entirely by [it's size](`AsteroidSize`).
|
||||||
|
#[derive(Component, Deref, DerefMut)]
|
||||||
|
pub struct Asteroid(pub AsteroidSize);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum AsteroidSize {
|
||||||
|
Small,
|
||||||
|
Medium,
|
||||||
|
Large,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsteroidSize {
|
||||||
|
/// Convenience util to get the "next smallest" size. Useful for splitting
|
||||||
|
/// after collision.
|
||||||
|
pub fn next(&self) -> Option<Self> {
|
||||||
|
match self {
|
||||||
|
AsteroidSize::Small => None,
|
||||||
|
AsteroidSize::Medium => Some(AsteroidSize::Small),
|
||||||
|
AsteroidSize::Large => Some(AsteroidSize::Medium),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Marker component for the player's ship.
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Ship;
|
||||||
|
|
||||||
|
/// Marker component for bullets.
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Bullet;
|
||||||
|
|
||||||
|
/// Responds to [`SpawnAsteroid`] events, spawning as specified
|
||||||
|
pub fn spawn_asteroid(
|
||||||
|
mut events: EventReader<SpawnAsteroid>,
|
||||||
|
mut commands: Commands,
|
||||||
|
game_assets: Res<GameAssets>,
|
||||||
|
) {
|
||||||
|
for spawn in events.read() {
|
||||||
|
let (mesh, material) = match spawn.size {
|
||||||
|
AsteroidSize::Small => game_assets.asteroid_small(),
|
||||||
|
AsteroidSize::Medium => game_assets.asteroid_medium(),
|
||||||
|
AsteroidSize::Large => game_assets.asteroid_large(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let collider_radius = match spawn.size {
|
||||||
|
AsteroidSize::Small => 10.0,
|
||||||
|
AsteroidSize::Medium => 20.0,
|
||||||
|
AsteroidSize::Large => 40.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
Asteroid(spawn.size),
|
||||||
|
Collider::ball(collider_radius),
|
||||||
|
Sensor,
|
||||||
|
Transform::from_translation(spawn.pos.extend(0.0)),
|
||||||
|
Velocity(spawn.vel),
|
||||||
|
Mesh2d(mesh),
|
||||||
|
MeshMaterial2d(material),
|
||||||
|
Lifetime(Timer::from_seconds(ASTEROID_LIFETIME, TimerMode::Once)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Event listener for asteroid destruction events. Shrinks and multiplies
|
||||||
|
/// asteroids until they vanish.
|
||||||
|
///
|
||||||
|
/// - Large -> 2x Medium
|
||||||
|
/// - Medium -> 2x Small
|
||||||
|
/// - Small -> (despawned)
|
||||||
|
///
|
||||||
|
/// The velocity of the child asteroids is scattered somewhat, as if they were
|
||||||
|
/// explosively pushed apart.
|
||||||
|
pub fn split_asteroids(
|
||||||
|
mut destroy_events: EventReader<AsteroidDestroy>,
|
||||||
|
mut respawn_events: EventWriter<SpawnAsteroid>,
|
||||||
|
mut commands: Commands,
|
||||||
|
query: Query<(&Transform, &Asteroid, &Velocity)>,
|
||||||
|
) {
|
||||||
|
for event in destroy_events.read() {
|
||||||
|
if let Ok((transform, rock, velocity)) = query.get(event.0) {
|
||||||
|
let next_size = rock.0.next();
|
||||||
|
if let Some(size) = next_size {
|
||||||
|
let pos = transform.translation.xy();
|
||||||
|
let left_offset = Vec2::from_angle(0.4);
|
||||||
|
let right_offset = Vec2::from_angle(-0.4);
|
||||||
|
respawn_events.write(SpawnAsteroid {
|
||||||
|
pos,
|
||||||
|
vel: left_offset.rotate(velocity.0),
|
||||||
|
size,
|
||||||
|
});
|
||||||
|
respawn_events.write(SpawnAsteroid {
|
||||||
|
pos,
|
||||||
|
vel: right_offset.rotate(velocity.0),
|
||||||
|
size,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Always despawn the asteroid. New ones (may) be spawned in it's
|
||||||
|
// place, but this one is gone.
|
||||||
|
commands.entity(event.0).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawns the player at the world origin. Used during the state change to
|
||||||
|
/// [`GameState::Playing`] to spawn the player.
|
||||||
|
///
|
||||||
|
/// This only spawns the player. For player **re**-spawn activity, see the
|
||||||
|
/// [`ship_impact_listener()`] system.
|
||||||
|
pub fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
||||||
|
commands
|
||||||
|
.spawn((
|
||||||
|
Collider::ball(0.7),
|
||||||
|
Sensor,
|
||||||
|
ActiveEvents::COLLISION_EVENTS,
|
||||||
|
ActiveCollisionTypes::STATIC_STATIC,
|
||||||
|
Ship,
|
||||||
|
Wrapping,
|
||||||
|
Velocity(Vec2::ZERO),
|
||||||
|
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)),
|
||||||
|
))
|
||||||
|
.with_child((
|
||||||
|
Mesh2d(game_assets.thruster_mesh()),
|
||||||
|
MeshMaterial2d(game_assets.thruster_mat_inactive()),
|
||||||
|
Transform::default()
|
||||||
|
.with_scale(Vec3::splat(0.5))
|
||||||
|
.with_translation(Vec3::new(-0.5, 0.0, -0.1)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Watch for [`BulletDestroy`] events and despawn
|
||||||
|
/// the associated bullet.
|
||||||
|
pub fn bullet_impact_listener(mut commands: Commands, mut events: EventReader<BulletDestroy>) {
|
||||||
|
for event in events.read() {
|
||||||
|
commands.entity(event.0).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Watch for [`ShipDestroy`] events and update game state accordingly.
|
||||||
|
///
|
||||||
|
/// One life is taken from the counter, asteroids are cleared, and the player
|
||||||
|
/// is placed back at the origin. If lives reach 0, this system will change
|
||||||
|
/// states to [`GameState::GameOver`].
|
||||||
|
/// - Subtract a life
|
||||||
|
/// - Check life count. If 0, go to game-over state
|
||||||
|
/// - Clear all asteroids
|
||||||
|
/// - Respawn player
|
||||||
|
pub fn ship_impact_listener(
|
||||||
|
mut events: EventReader<ShipDestroy>,
|
||||||
|
mut commands: Commands,
|
||||||
|
mut lives: ResMut<Lives>,
|
||||||
|
rocks: Query<Entity, With<Asteroid>>,
|
||||||
|
mut player: Single<(&mut Transform, &mut Velocity), With<Ship>>,
|
||||||
|
mut next_state: ResMut<NextState<GameState>>,
|
||||||
|
) {
|
||||||
|
for _ in events.read() {
|
||||||
|
// STEP 1: Decrement lives (and maybe go to game over)
|
||||||
|
if lives.0 == 0 {
|
||||||
|
// If already at 0, game is over.
|
||||||
|
next_state.set(GameState::GameOver);
|
||||||
|
} else {
|
||||||
|
// Decrease life count.
|
||||||
|
lives.0 -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// STEP 2: Clear asteroids
|
||||||
|
for rock in rocks {
|
||||||
|
commands.entity(rock).despawn();
|
||||||
|
}
|
||||||
|
|
||||||
|
// STEP 3: Respawn player (teleport them to the origin)
|
||||||
|
player.0.translation = Vec3::ZERO;
|
||||||
|
player.1.0 = Vec2::ZERO;
|
||||||
|
}
|
||||||
|
}
|
||||||
140
src/physics.rs
Normal file
140
src/physics.rs
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
//! Custom physics items
|
||||||
|
//!
|
||||||
|
//! TODO: Refactor in terms of Rapier2D, *or* implement colliders and remove it.
|
||||||
|
//!
|
||||||
|
//! Position, both translation and rotation, are tracked by the built-in
|
||||||
|
//! Bevy [`Transform`] component. This module adds a (Linear) [`Velocity`] and
|
||||||
|
//! [`AngularVelocity`] component to the mix.
|
||||||
|
//! These two components are operated by simple integrator systems to
|
||||||
|
//! accumulate translation and rotation from the velocities.
|
||||||
|
//!
|
||||||
|
//! The [`Wrapping`] component marks things that should wrap around the world
|
||||||
|
//! boundaries. It's a kind of physical property of this world, so I'm putting
|
||||||
|
//! it here.
|
||||||
|
//!
|
||||||
|
//! Collisions are also considered physics. After all, I got *a physics engine*
|
||||||
|
//! to detect them for me (so that I don't have to write clipping code).
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
WorldSize, events,
|
||||||
|
objects::{Asteroid, Bullet, Ship},
|
||||||
|
};
|
||||||
|
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use bevy_rapier2d::pipeline::CollisionEvent;
|
||||||
|
|
||||||
|
#[derive(Clone, 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.x / 2.0;
|
||||||
|
let left = -right;
|
||||||
|
let top = world_size.y / 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The collision event routing system.
|
||||||
|
///
|
||||||
|
/// When a `CollisionEvent` occurrs, this system checks which things collided
|
||||||
|
/// and emits secondary events accordignly.
|
||||||
|
///
|
||||||
|
/// | Objects | Response |
|
||||||
|
/// |-|-|
|
||||||
|
/// | Ship & Asteroid | emits event [`ShipDestroy`](`crate::events::ShipDestroy`) |
|
||||||
|
/// | Asteroid & Bullet | emits event [`AsteroidDestroy`](`crate::events::AsteroidDestroy`) |
|
||||||
|
/// | Asteroid & Asteroid | Nothing. Asteroids won't collide with each other |
|
||||||
|
/// | Bullet & Bullet | Nothing. Bullets won't collide with each other (and probably can't under normal gameplay conditions) |
|
||||||
|
/// | Bullet & Ship | Nothing. The player shouldn't be able to shoot themselves (and the Flying Saucer hasn't been impl.'d, so it's bullets don't count) |
|
||||||
|
pub fn collision_listener(
|
||||||
|
mut collisions: EventReader<CollisionEvent>,
|
||||||
|
mut ship_writer: EventWriter<events::ShipDestroy>,
|
||||||
|
mut asteroid_writer: EventWriter<events::AsteroidDestroy>,
|
||||||
|
mut bullet_writer: EventWriter<events::BulletDestroy>,
|
||||||
|
player: Single<Entity, With<Ship>>,
|
||||||
|
bullets: Query<&Bullet>,
|
||||||
|
rocks: Query<&Asteroid>,
|
||||||
|
) {
|
||||||
|
for event in collisions.read() {
|
||||||
|
if let CollisionEvent::Started(one, two, _flags) = event {
|
||||||
|
// Valid collisions are:
|
||||||
|
//
|
||||||
|
// - Ship & Asteroid
|
||||||
|
// - Bullet & Asteroid
|
||||||
|
//
|
||||||
|
// Asteroids don't collide with each other, bullets don't collide
|
||||||
|
// with each other, and bullets don't collide with the player ship.
|
||||||
|
|
||||||
|
// Option 1: Ship & Asteroid
|
||||||
|
if *one == *player {
|
||||||
|
if rocks.contains(*two) {
|
||||||
|
// player-asteroid collision
|
||||||
|
dbg!("Writing ShipDestroy event");
|
||||||
|
ship_writer.write(events::ShipDestroy);
|
||||||
|
} // else, we don't care
|
||||||
|
} else if *two == *player {
|
||||||
|
if rocks.contains(*one) {
|
||||||
|
dbg!("Writing ShipDestroy event");
|
||||||
|
ship_writer.write(events::ShipDestroy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Option 2: Bullet & Asteroid
|
||||||
|
if bullets.contains(*one) {
|
||||||
|
if rocks.contains(*two) {
|
||||||
|
dbg!("Writing AsteroidDestroy & BulletDestroy events");
|
||||||
|
asteroid_writer.write(events::AsteroidDestroy(*two));
|
||||||
|
bullet_writer.write(events::BulletDestroy(*one));
|
||||||
|
}
|
||||||
|
} else if rocks.contains(*one) {
|
||||||
|
if bullets.contains(*two) {
|
||||||
|
dbg!("Writing AsteroidDestroy & BulletDestroy events");
|
||||||
|
asteroid_writer.write(events::AsteroidDestroy(*one));
|
||||||
|
bullet_writer.write(events::BulletDestroy(*two));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
124
src/resources.rs
Normal file
124
src/resources.rs
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
//! All the resources for the game
|
||||||
|
|
||||||
|
use bevy::{
|
||||||
|
asset::{Assets, Handle},
|
||||||
|
ecs::{
|
||||||
|
resource::Resource,
|
||||||
|
world::{FromWorld, World},
|
||||||
|
},
|
||||||
|
math::{
|
||||||
|
Vec2,
|
||||||
|
primitives::{Circle, Triangle2d},
|
||||||
|
},
|
||||||
|
prelude::{Deref, DerefMut, Reflect, ReflectResource},
|
||||||
|
render::mesh::Mesh,
|
||||||
|
sprite::ColorMaterial,
|
||||||
|
};
|
||||||
|
use bevy_inspector_egui::InspectorOptions;
|
||||||
|
use bevy_inspector_egui::inspector_options::ReflectInspectorOptions;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
ASTEROID_SMALL_COLOR, BULLET_COLOR, PLAYER_SHIP_COLOR, SHIP_THRUSTER_COLOR_ACTIVE,
|
||||||
|
SHIP_THRUSTER_COLOR_INACTIVE, config::WINDOW_SIZE,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Resource, Debug, Deref, Clone, Copy)]
|
||||||
|
pub struct Score(pub i32);
|
||||||
|
|
||||||
|
impl From<Score> for String {
|
||||||
|
fn from(value: Score) -> Self {
|
||||||
|
value.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(InspectorOptions, Reflect, Resource, Debug, Deref, Clone, Copy)]
|
||||||
|
#[reflect(Resource, InspectorOptions)]
|
||||||
|
pub struct Lives(pub i32);
|
||||||
|
|
||||||
|
impl From<Lives> for String {
|
||||||
|
fn from(value: Lives) -> Self {
|
||||||
|
value.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deref, DerefMut, Resource)]
|
||||||
|
pub struct WorldSize(Vec2);
|
||||||
|
|
||||||
|
impl Default for WorldSize {
|
||||||
|
fn default() -> Self {
|
||||||
|
WorldSize(Vec2::new(WINDOW_SIZE.x, WINDOW_SIZE.y))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct GameAssets {
|
||||||
|
meshes: [Handle<Mesh>; 5],
|
||||||
|
materials: [Handle<ColorMaterial>; 7],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GameAssets {
|
||||||
|
pub fn ship(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
|
(self.meshes[0].clone(), self.materials[0].clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
// The thruster mesh is actually just the ship mesh
|
||||||
|
pub fn thruster_mesh(&self) -> Handle<Mesh> {
|
||||||
|
self.meshes[0].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Look into parameterizing the material
|
||||||
|
// A shader uniform should be able to do this, but I don't know how to
|
||||||
|
// load those in Bevy.
|
||||||
|
pub fn thruster_mat_inactive(&self) -> Handle<ColorMaterial> {
|
||||||
|
self.materials[1].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn thruster_mat_active(&self) -> Handle<ColorMaterial> {
|
||||||
|
self.materials[2].clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn asteroid_small(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
|
(self.meshes[1].clone(), self.materials[1].clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn asteroid_medium(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
|
(self.meshes[2].clone(), self.materials[2].clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn asteroid_large(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
|
(self.meshes[3].clone(), self.materials[3].clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bullet(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
|
||||||
|
(self.meshes[4].clone(), self.materials[6].clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromWorld for GameAssets {
|
||||||
|
fn from_world(world: &mut World) -> Self {
|
||||||
|
let mut world_meshes = world.resource_mut::<Assets<Mesh>>();
|
||||||
|
let meshes = [
|
||||||
|
world_meshes.add(Triangle2d::new(
|
||||||
|
Vec2::new(0.5, 0.0),
|
||||||
|
Vec2::new(-0.5, 0.45),
|
||||||
|
Vec2::new(-0.5, -0.45),
|
||||||
|
)),
|
||||||
|
world_meshes.add(Circle::new(10.0)),
|
||||||
|
world_meshes.add(Circle::new(20.0)),
|
||||||
|
world_meshes.add(Circle::new(40.0)),
|
||||||
|
world_meshes.add(Circle::new(0.2)),
|
||||||
|
];
|
||||||
|
let mut world_materials = world.resource_mut::<Assets<ColorMaterial>>();
|
||||||
|
let materials = [
|
||||||
|
world_materials.add(PLAYER_SHIP_COLOR),
|
||||||
|
world_materials.add(SHIP_THRUSTER_COLOR_INACTIVE),
|
||||||
|
world_materials.add(SHIP_THRUSTER_COLOR_ACTIVE),
|
||||||
|
world_materials.add(ASTEROID_SMALL_COLOR),
|
||||||
|
// TODO: asteroid medium and large colors
|
||||||
|
world_materials.add(ASTEROID_SMALL_COLOR),
|
||||||
|
world_materials.add(ASTEROID_SMALL_COLOR),
|
||||||
|
world_materials.add(BULLET_COLOR),
|
||||||
|
];
|
||||||
|
GameAssets { meshes, materials }
|
||||||
|
}
|
||||||
|
}
|
||||||
301
src/widgets.rs
Normal file
301
src/widgets.rs
Normal file
@@ -0,0 +1,301 @@
|
|||||||
|
use crate::{
|
||||||
|
GameState,
|
||||||
|
config::{UI_BUTTON_HOVERED, UI_BUTTON_NORMAL, UI_BUTTON_PRESSED},
|
||||||
|
resources::{Lives, Score},
|
||||||
|
};
|
||||||
|
|
||||||
|
use bevy::{
|
||||||
|
color::palettes::css::{BLACK, DARK_GRAY, GREEN, LIGHT_BLUE, RED, WHITE},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Plugin for the main menu
|
||||||
|
pub struct PluginGameMenu;
|
||||||
|
|
||||||
|
impl Plugin for PluginGameMenu {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(OnEnter(GameState::TitleScreen), spawn_menu)
|
||||||
|
.add_systems(OnExit(GameState::TitleScreen), despawn::<MarkerMainMenu>)
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
(handle_spacebar, operate_buttons).run_if(in_state(GameState::TitleScreen)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Plugin for the "get ready" period as gameplay starts.
|
||||||
|
pub struct PluginGetReady;
|
||||||
|
|
||||||
|
impl Plugin for PluginGetReady {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(OnEnter(GameState::GetReady), spawn_get_ready)
|
||||||
|
.add_systems(OnExit(GameState::GetReady), despawn::<OnReadySetGo>)
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
(animate_get_ready_widget).run_if(in_state(GameState::GetReady)),
|
||||||
|
)
|
||||||
|
.insert_resource(ReadySetGoTimer(Timer::from_seconds(3.0, TimerMode::Once)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Plugin for the game-over screen
|
||||||
|
pub struct PluginGameOver;
|
||||||
|
|
||||||
|
impl Plugin for PluginGameOver {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.add_systems(OnEnter(GameState::GameOver), spawn_gameover_ui)
|
||||||
|
.add_systems(OnExit(GameState::GameOver), despawn::<MarkerGameOver>)
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
operate_buttons.run_if(in_state(GameState::GameOver)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marker component for the title screen UI entity.
|
||||||
|
// This way, a query for the TitleUI can be used to despawn the title screen
|
||||||
|
#[derive(Component)]
|
||||||
|
struct MarkerMainMenu;
|
||||||
|
|
||||||
|
/// Marker component for things on the get-ready indicator
|
||||||
|
#[derive(Component)]
|
||||||
|
struct OnReadySetGo;
|
||||||
|
|
||||||
|
/// Marker for things on the game-over screen
|
||||||
|
#[derive(Component)]
|
||||||
|
struct MarkerGameOver;
|
||||||
|
|
||||||
|
/// Action specifier for the game-over menu's buttons.
|
||||||
|
///
|
||||||
|
/// Attach this component to a button and [`PluginGameOver`] will use it to
|
||||||
|
/// decide what to do when that button is pressed.
|
||||||
|
#[derive(Component)]
|
||||||
|
enum ButtonMenuAction {
|
||||||
|
ToMainMenu,
|
||||||
|
StartGame,
|
||||||
|
Quit,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Newtype wrapper for `Timer`. Used to count down during the "get ready" phase.
|
||||||
|
#[derive(Deref, DerefMut, Resource)]
|
||||||
|
struct ReadySetGoTimer(Timer);
|
||||||
|
|
||||||
|
/// Marker for the counter text segment
|
||||||
|
#[derive(Component)]
|
||||||
|
struct CountdownText;
|
||||||
|
|
||||||
|
/// Marker for the counter bar segment
|
||||||
|
#[derive(Component)]
|
||||||
|
struct CountdownBar;
|
||||||
|
|
||||||
|
/// Despawns entities matching the generic argument. Intended to remove UI
|
||||||
|
/// elements.
|
||||||
|
fn despawn<T: Component>(mut commands: Commands, to_despawn: Query<Entity, With<T>>) {
|
||||||
|
for entity in to_despawn {
|
||||||
|
commands.entity(entity).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Utility function for creating a standard button.
|
||||||
|
fn button_bundle(text: &str) -> impl Bundle {
|
||||||
|
(
|
||||||
|
Button,
|
||||||
|
// TODO: Generic action
|
||||||
|
Node {
|
||||||
|
width: Val::Px(150.0),
|
||||||
|
height: Val::Px(65.0),
|
||||||
|
border: UiRect::all(Val::Px(2.0)),
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
margin: UiRect::all(Val::Px(5.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
BorderColor(Color::BLACK),
|
||||||
|
BorderRadius::MAX,
|
||||||
|
BackgroundColor(UI_BUTTON_NORMAL),
|
||||||
|
children![(
|
||||||
|
Text::new(text),
|
||||||
|
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
||||||
|
TextShadow::default(),
|
||||||
|
)],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_menu(mut commands: Commands) {
|
||||||
|
commands
|
||||||
|
.spawn((
|
||||||
|
MarkerMainMenu,
|
||||||
|
Node {
|
||||||
|
width: Val::Percent(100.0),
|
||||||
|
height: Val::Percent(100.0),
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.with_children(|cmds| {
|
||||||
|
cmds.spawn((
|
||||||
|
Text::new("Robert's Bad Asteroids Game"),
|
||||||
|
TextFont::from_font_size(50.0),
|
||||||
|
TextLayout::new_with_justify(JustifyText::Center),
|
||||||
|
TextShadow::default(),
|
||||||
|
));
|
||||||
|
cmds.spawn((
|
||||||
|
Text::new("Press space to begin"),
|
||||||
|
TextFont::from_font_size(20.0),
|
||||||
|
TextColor(Color::srgb(0.7, 0.7, 0.7)),
|
||||||
|
TextShadow::default(),
|
||||||
|
));
|
||||||
|
cmds.spawn((button_bundle("Start Game"), ButtonMenuAction::StartGame));
|
||||||
|
cmds.spawn((button_bundle("Quit"), ButtonMenuAction::Quit));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_get_ready(mut commands: Commands) {
|
||||||
|
commands.spawn((
|
||||||
|
OnReadySetGo, // marker, so this can be de-spawned properly
|
||||||
|
Node {
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
justify_self: JustifySelf::Center,
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
width: Val::Percent(30.),
|
||||||
|
height: Val::Percent(30.),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
BackgroundColor(LIGHT_BLUE.into()),
|
||||||
|
children![
|
||||||
|
(Text::new("Get Ready!"), TextColor(BLACK.into())),
|
||||||
|
(
|
||||||
|
CountdownBar,
|
||||||
|
Node {
|
||||||
|
width: Val::Percent(90.0),
|
||||||
|
height: Val::Percent(10.),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
BackgroundColor(GREEN.into()),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
CountdownText,
|
||||||
|
Text::new("<uninit timer>"),
|
||||||
|
TextColor(RED.into()),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawns the game over screen.
|
||||||
|
///
|
||||||
|
/// Used by [`PluginGameOver`] when entering the [`GameState::GameOver`] state.
|
||||||
|
fn spawn_gameover_ui(mut commands: Commands) {
|
||||||
|
commands.spawn((
|
||||||
|
MarkerGameOver, // Marker, so `despawn<T>` can remove this on state exit.
|
||||||
|
Node {
|
||||||
|
width: Val::Percent(100.0),
|
||||||
|
height: Val::Percent(100.0),
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
children![
|
||||||
|
(button_bundle("Main Menu"), ButtonMenuAction::ToMainMenu,),
|
||||||
|
(button_bundle("Quit"), ButtonMenuAction::Quit),
|
||||||
|
],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn animate_get_ready_widget(
|
||||||
|
mut text_segment: Single<&mut Text, With<CountdownText>>,
|
||||||
|
mut bar_segment: Single<&mut Node, With<CountdownBar>>,
|
||||||
|
time: Res<Time>,
|
||||||
|
mut timer: ResMut<ReadySetGoTimer>,
|
||||||
|
mut game_state: ResMut<NextState<GameState>>,
|
||||||
|
) {
|
||||||
|
// Advance the timer, read the remaining time and write it onto the label.
|
||||||
|
timer.tick(time.delta());
|
||||||
|
|
||||||
|
// Add one to the visual value so the countdown starts at 3 and stops at 1.
|
||||||
|
// Otherwise it starts at 2 and disappears after showing 0.
|
||||||
|
// That feels wrong even though it's functionally identical.
|
||||||
|
let tval = timer.0.remaining().as_secs() + 1;
|
||||||
|
**text_segment = format!("{tval}").into();
|
||||||
|
|
||||||
|
// Shrink the progress bar Node
|
||||||
|
bar_segment.width = Val::Percent(100.0 * (1.0 - timer.fraction()));
|
||||||
|
|
||||||
|
// If the timer has expired, change state to playing.
|
||||||
|
if timer.finished() {
|
||||||
|
game_state.set(GameState::Playing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles interactions with the menu buttons.
|
||||||
|
///
|
||||||
|
/// The buttons are used by the main menu and the game-over menu to change
|
||||||
|
/// between game states.
|
||||||
|
///
|
||||||
|
/// Button animation and action handling is done entirely within this system.
|
||||||
|
///
|
||||||
|
/// There are no checks for current state. If a "quit" button was put somewhere
|
||||||
|
/// on the HUD, this system would quit the game. The same will happen for
|
||||||
|
/// returning to the title screen. This should be useful for making a pause
|
||||||
|
/// menu, too.
|
||||||
|
fn operate_buttons(
|
||||||
|
mut interactions: Query<
|
||||||
|
(
|
||||||
|
&Interaction,
|
||||||
|
&mut BackgroundColor,
|
||||||
|
&mut BorderColor,
|
||||||
|
&ButtonMenuAction,
|
||||||
|
),
|
||||||
|
(Changed<Interaction>, With<Button>),
|
||||||
|
>,
|
||||||
|
mut game_state: ResMut<NextState<GameState>>,
|
||||||
|
mut app_exit_events: EventWriter<AppExit>,
|
||||||
|
) {
|
||||||
|
// TODO: Better colors. These are taken from the example and they're ugly.
|
||||||
|
for (interaction, mut color, mut border_color, menu_action) in &mut interactions {
|
||||||
|
match *interaction {
|
||||||
|
Interaction::Pressed => {
|
||||||
|
*color = UI_BUTTON_PRESSED.into();
|
||||||
|
border_color.0 = DARK_GRAY.into();
|
||||||
|
match menu_action {
|
||||||
|
ButtonMenuAction::ToMainMenu => {
|
||||||
|
game_state.set(GameState::TitleScreen);
|
||||||
|
}
|
||||||
|
ButtonMenuAction::StartGame => {
|
||||||
|
game_state.set(GameState::Playing);
|
||||||
|
}
|
||||||
|
ButtonMenuAction::Quit => {
|
||||||
|
app_exit_events.write(AppExit::Success);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Interaction::Hovered => {
|
||||||
|
*color = UI_BUTTON_HOVERED.into();
|
||||||
|
border_color.0 = WHITE.into();
|
||||||
|
}
|
||||||
|
Interaction::None => {
|
||||||
|
*color = UI_BUTTON_NORMAL.into();
|
||||||
|
border_color.0 = BLACK.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Main menu input listener. Starts game when the spacebar is pressed.
|
||||||
|
fn handle_spacebar(input: Res<ButtonInput<KeyCode>>, mut game_state: ResMut<NextState<GameState>>) {
|
||||||
|
if input.just_pressed(KeyCode::Space) {
|
||||||
|
game_state.set(GameState::GetReady);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spawn_ui(mut commands: Commands, score: Res<Score>, lives: Res<Lives>) {
|
||||||
|
commands.spawn((
|
||||||
|
Text::new(format!("Score: {score:?} | Lives: {lives:?}")),
|
||||||
|
TextFont::from_font_size(25.0),
|
||||||
|
));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user