Compare commits
5 Commits
c0cf6d7f30
...
0967795d51
| Author | SHA1 | Date | |
|---|---|---|---|
| 0967795d51 | |||
| 20c71658c3 | |||
| 960861af79 | |||
| 4b70be7048 | |||
| 8bf21e74f0 |
@@ -25,5 +25,6 @@ pub(crate) const BULLET_SPEED: f32 = 150.0;
|
|||||||
pub(crate) const BULLET_LIFETIME: f32 = 2.0;
|
pub(crate) const BULLET_LIFETIME: f32 = 2.0;
|
||||||
|
|
||||||
pub(crate) const ASTEROID_LIFETIME: f32 = 40.0;
|
pub(crate) const ASTEROID_LIFETIME: f32 = 40.0;
|
||||||
|
pub(crate) const DEBRIS_LIFETIME: f32 = 3.0; // lifetime, in seconds
|
||||||
|
|
||||||
pub const RNG_SEED: [u8; 32] = *b"12345678909876543210123456789098";
|
pub const RNG_SEED: [u8; 32] = *b"12345678909876543210123456789098";
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ use bevy::prelude::*;
|
|||||||
use bevy_rapier2d::{
|
use bevy_rapier2d::{
|
||||||
plugin::{NoUserData, RapierPhysicsPlugin},
|
plugin::{NoUserData, RapierPhysicsPlugin},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
render::RapierDebugRenderPlugin,
|
|
||||||
};
|
};
|
||||||
use machinery::Lifetime;
|
use machinery::Lifetime;
|
||||||
use resources::{GameAssets, Lives, Score, WorldSize};
|
use resources::{GameAssets, Lives, Score, WorldSize};
|
||||||
@@ -39,7 +38,6 @@ impl Plugin for AsteroidPlugin {
|
|||||||
widgets::PluginGetReady,
|
widgets::PluginGetReady,
|
||||||
widgets::PluginGameHud,
|
widgets::PluginGameHud,
|
||||||
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
|
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0),
|
||||||
RapierDebugRenderPlugin::default(),
|
|
||||||
))
|
))
|
||||||
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
.insert_resource(ClearColor(BACKGROUND_COLOR))
|
||||||
.insert_resource(WorldSize::default())
|
.insert_resource(WorldSize::default())
|
||||||
@@ -60,6 +58,7 @@ impl Plugin for AsteroidPlugin {
|
|||||||
input_ship_shoot,
|
input_ship_shoot,
|
||||||
physics::wrap_entities,
|
physics::wrap_entities,
|
||||||
machinery::tick_asteroid_manager,
|
machinery::tick_asteroid_manager,
|
||||||
|
machinery::operate_sparklers,
|
||||||
objects::spawn_asteroid.after(machinery::tick_asteroid_manager),
|
objects::spawn_asteroid.after(machinery::tick_asteroid_manager),
|
||||||
objects::split_asteroids,
|
objects::split_asteroids,
|
||||||
objects::bullet_impact_listener,
|
objects::bullet_impact_listener,
|
||||||
|
|||||||
@@ -103,3 +103,35 @@ pub fn tick_lifetimes(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Entities marked with this will flash. Used to make the debris field sparkle.
|
||||||
|
#[derive(Component, Deref, DerefMut)]
|
||||||
|
pub struct Sparkler(Timer);
|
||||||
|
|
||||||
|
impl Sparkler {
|
||||||
|
pub fn at_interval(period: f32) -> Self {
|
||||||
|
Self(Timer::from_seconds(period, TimerMode::Repeating))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Advances the timer in a sparkler, swapping between visible and invisible
|
||||||
|
/// each time the timer expires.
|
||||||
|
pub fn operate_sparklers(sparklers: Query<(&mut Visibility, &mut Sparkler)>, time: Res<Time>) {
|
||||||
|
for (mut vis, mut timer) in sparklers {
|
||||||
|
if timer.tick(time.delta()).just_finished() {
|
||||||
|
// Cycle between visible and in-visible modes (and print warning for "Inherited")
|
||||||
|
*vis = match *vis {
|
||||||
|
Visibility::Inherited => {
|
||||||
|
// I don't know when entities have this mode, so I'm going
|
||||||
|
// print a warning for a while.
|
||||||
|
eprintln!(
|
||||||
|
"->> WARN: `machinery::operate_sparklers` found an entity with Visibility::Inherited"
|
||||||
|
);
|
||||||
|
Visibility::Inherited
|
||||||
|
}
|
||||||
|
Visibility::Hidden => Visibility::Visible,
|
||||||
|
Visibility::Visible => Visibility::Hidden,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
ecs::{
|
ecs::{
|
||||||
|
bundle::Bundle,
|
||||||
component::Component,
|
component::Component,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
event::{EventReader, EventWriter},
|
event::{EventReader, EventWriter},
|
||||||
@@ -12,7 +13,7 @@ use bevy::{
|
|||||||
},
|
},
|
||||||
math::{Vec2, Vec3, Vec3Swizzles},
|
math::{Vec2, Vec3, Vec3Swizzles},
|
||||||
prelude::{Deref, DerefMut},
|
prelude::{Deref, DerefMut},
|
||||||
render::mesh::Mesh2d,
|
render::{mesh::Mesh2d, view::Visibility},
|
||||||
sprite::MeshMaterial2d,
|
sprite::MeshMaterial2d,
|
||||||
state::state::NextState,
|
state::state::NextState,
|
||||||
time::{Timer, TimerMode},
|
time::{Timer, TimerMode},
|
||||||
@@ -22,9 +23,9 @@ use bevy_rapier2d::prelude::{ActiveCollisionTypes, ActiveEvents, Collider, Senso
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
AngularVelocity, GameAssets, GameState, Lives,
|
AngularVelocity, GameAssets, GameState, Lives,
|
||||||
config::{ASTEROID_LIFETIME, SHIP_FIRE_RATE},
|
config::{ASTEROID_LIFETIME, DEBRIS_LIFETIME, SHIP_FIRE_RATE},
|
||||||
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
events::{AsteroidDestroy, BulletDestroy, ShipDestroy, SpawnAsteroid},
|
||||||
machinery::Lifetime,
|
machinery::{Lifetime, Sparkler},
|
||||||
physics::{Velocity, Wrapping},
|
physics::{Velocity, Wrapping},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -63,6 +64,10 @@ pub struct Weapon(Timer);
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Bullet;
|
pub struct Bullet;
|
||||||
|
|
||||||
|
/// Debris left behind after the ship is destroyed.
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Debris;
|
||||||
|
|
||||||
/// Responds to [`SpawnAsteroid`] events, spawning as specified
|
/// Responds to [`SpawnAsteroid`] events, spawning as specified
|
||||||
pub fn spawn_asteroid(
|
pub fn spawn_asteroid(
|
||||||
mut events: EventReader<SpawnAsteroid>,
|
mut events: EventReader<SpawnAsteroid>,
|
||||||
@@ -189,6 +194,7 @@ pub fn ship_impact_listener(
|
|||||||
rocks: Query<Entity, With<Asteroid>>,
|
rocks: Query<Entity, With<Asteroid>>,
|
||||||
mut player: Single<(&mut Transform, &mut Velocity), With<Ship>>,
|
mut player: Single<(&mut Transform, &mut Velocity), With<Ship>>,
|
||||||
mut next_state: ResMut<NextState<GameState>>,
|
mut next_state: ResMut<NextState<GameState>>,
|
||||||
|
game_assets: Res<GameAssets>,
|
||||||
) {
|
) {
|
||||||
for _ in events.read() {
|
for _ in events.read() {
|
||||||
// STEP 1: Decrement lives (and maybe go to game over)
|
// STEP 1: Decrement lives (and maybe go to game over)
|
||||||
@@ -205,6 +211,23 @@ pub fn ship_impact_listener(
|
|||||||
commands.entity(rock).despawn();
|
commands.entity(rock).despawn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// STEP 3: spawn the debris field where the player used to be.
|
||||||
|
for i in 0..10 {
|
||||||
|
let angle_rads = (i as f32) / 10.0 * std::f32::consts::TAU;
|
||||||
|
let dir = Vec2::from_angle(angle_rads);
|
||||||
|
let vel = player.1.0 + dir * 100.0;
|
||||||
|
commands.spawn((
|
||||||
|
Debris,
|
||||||
|
Visibility::Visible, // make sure it's "visible" not "Inherited" so the cycle works right
|
||||||
|
Lifetime(Timer::from_seconds(DEBRIS_LIFETIME, TimerMode::Once)),
|
||||||
|
Sparkler::at_interval(0.15),
|
||||||
|
Mesh2d(game_assets.thruster_mesh()), // borrow the thruster mesh for now
|
||||||
|
MeshMaterial2d(game_assets.thruster_mat_active()), // ... and the active thruster material
|
||||||
|
player.0.clone(), // clone the player transform
|
||||||
|
Velocity(vel),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// STEP 3: Respawn player (teleport them to the origin)
|
// STEP 3: Respawn player (teleport them to the origin)
|
||||||
player.0.translation = Vec3::ZERO;
|
player.0.translation = Vec3::ZERO;
|
||||||
player.1.0 = Vec2::ZERO;
|
player.1.0 = Vec2::ZERO;
|
||||||
|
|||||||
@@ -83,10 +83,14 @@ struct MarkerGameOver;
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct MarkerHUD;
|
struct MarkerHUD;
|
||||||
|
|
||||||
/// Action specifier for the game-over menu's buttons.
|
/// Action specifier for the buttons on the menus.
|
||||||
///
|
///
|
||||||
/// Attach this component to a button and [`PluginGameOver`] will use it to
|
/// Instead of holding function pointers for use as callbacks, I'm doing enum-
|
||||||
/// decide what to do when that button is pressed.
|
/// dispatch. Add a variant according to what action the button press should
|
||||||
|
/// trigger.
|
||||||
|
///
|
||||||
|
/// Only [`PluginGameMenu`] and [`PluginGameOver`] will use it to this
|
||||||
|
/// component. There is no always-on, global system.
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
enum ButtonMenuAction {
|
enum ButtonMenuAction {
|
||||||
ToMainMenu,
|
ToMainMenu,
|
||||||
|
|||||||
Reference in New Issue
Block a user