Compare commits
2 Commits
5dfe11d31f
...
911b6c5fe7
| Author | SHA1 | Date | |
|---|---|---|---|
| 911b6c5fe7 | |||
| 71ec77f5b1 |
61
src/asteroids.rs
Normal file
61
src/asteroids.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
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};
|
||||||
|
|
||||||
|
#[derive(Component, Deref, DerefMut)]
|
||||||
|
pub struct Asteroid(AsteroidSize);
|
||||||
|
|
||||||
|
pub enum AsteroidSize {
|
||||||
|
SMALL,
|
||||||
|
MEDIUM,
|
||||||
|
LARGE,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct AsteroidSpawner {
|
||||||
|
timer: Timer,
|
||||||
|
// TODO: Configurables?
|
||||||
|
// - interval
|
||||||
|
// - density
|
||||||
|
// - size distribution
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsteroidSpawner {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
timer: Timer::new(Duration::from_secs(3), TimerMode::Repeating),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update the asteroid spawn timer and spawn any asteroids
|
||||||
|
/// that are due this frame.
|
||||||
|
pub fn tick_asteroid_manager(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut spawner: ResMut<AsteroidSpawner>,
|
||||||
|
game_assets: Res<GameAssets>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
spawner.timer.tick(time.delta());
|
||||||
|
if spawner.timer.just_finished() {
|
||||||
|
commands.spawn((
|
||||||
|
Asteroid(AsteroidSize::SMALL),
|
||||||
|
Position(Vec2::new(40.0, 40.0)),
|
||||||
|
Velocity(Vec2::new(10.0, 0.0)),
|
||||||
|
Rotation(0.0),
|
||||||
|
Mesh2d(game_assets.asteroid_small().0),
|
||||||
|
MeshMaterial2d(game_assets.asteroid_small().1),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Utility function to spawn a single asteroid of a given type
|
||||||
|
/// TODO: convert to an event listener monitoring for "spawn asteroid" events
|
||||||
|
/// from the `fn tick_asteroid_manager(...)` system.
|
||||||
|
fn spawn_asteroid(mut commands: Commands) {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
56
src/lib.rs
56
src/lib.rs
@@ -1,11 +1,11 @@
|
|||||||
|
mod asteroids;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
mod preparation_widget;
|
mod preparation_widget;
|
||||||
mod title_screen;
|
mod title_screen;
|
||||||
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE};
|
use crate::config::{BACKGROUND_COLOR, PLAYER_SHIP_COLOR, SHIP_ROTATION, SHIP_THRUST, WINDOW_SIZE};
|
||||||
|
|
||||||
|
use asteroids::AsteroidSpawner;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_inspector_egui::prelude::ReflectInspectorOptions;
|
use bevy_inspector_egui::prelude::ReflectInspectorOptions;
|
||||||
use bevy_inspector_egui::InspectorOptions;
|
use bevy_inspector_egui::InspectorOptions;
|
||||||
@@ -28,9 +28,7 @@ impl Plugin for AsteroidPlugin {
|
|||||||
.insert_resource(Lives(3))
|
.insert_resource(Lives(3))
|
||||||
.register_type::<Lives>()
|
.register_type::<Lives>()
|
||||||
.insert_resource(Score(0))
|
.insert_resource(Score(0))
|
||||||
.insert_resource(AsteroidSpawner {
|
.insert_resource(AsteroidSpawner::new())
|
||||||
timer: Timer::new(Duration::from_secs(3), TimerMode::Repeating),
|
|
||||||
})
|
|
||||||
.init_resource::<GameAssets>()
|
.init_resource::<GameAssets>()
|
||||||
.add_systems(Startup, spawn_camera)
|
.add_systems(Startup, spawn_camera)
|
||||||
.add_systems(OnEnter(GameState::Playing), (spawn_player, spawn_ui))
|
.add_systems(OnEnter(GameState::Playing), (spawn_player, spawn_ui))
|
||||||
@@ -40,7 +38,7 @@ impl Plugin for AsteroidPlugin {
|
|||||||
input_ship_thruster,
|
input_ship_thruster,
|
||||||
input_ship_rotation,
|
input_ship_rotation,
|
||||||
wrap_entities,
|
wrap_entities,
|
||||||
tick_asteroid_manager,
|
asteroids::tick_asteroid_manager,
|
||||||
)
|
)
|
||||||
.run_if(in_state(GameState::Playing)),
|
.run_if(in_state(GameState::Playing)),
|
||||||
)
|
)
|
||||||
@@ -73,24 +71,6 @@ struct Rotation(f32);
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Ship;
|
struct Ship;
|
||||||
|
|
||||||
#[derive(Component, Deref, DerefMut)]
|
|
||||||
struct Asteroid(AsteroidSize);
|
|
||||||
|
|
||||||
enum AsteroidSize {
|
|
||||||
SMALL,
|
|
||||||
MEDIUM,
|
|
||||||
LARGE,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
struct AsteroidSpawner {
|
|
||||||
timer: Timer,
|
|
||||||
// TODO: Configurables?
|
|
||||||
// - interval
|
|
||||||
// - density
|
|
||||||
// - size distribution
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Marker for any entity that should wrap on screen edges
|
/// Marker for any entity that should wrap on screen edges
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Wrapping;
|
struct Wrapping;
|
||||||
@@ -212,34 +192,6 @@ fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the asteroid spawn timer and spawn any asteroids
|
|
||||||
/// that are due this frame.
|
|
||||||
fn tick_asteroid_manager(
|
|
||||||
mut commands: Commands,
|
|
||||||
mut spawner: ResMut<AsteroidSpawner>,
|
|
||||||
game_assets: Res<GameAssets>,
|
|
||||||
time: Res<Time>,
|
|
||||||
) {
|
|
||||||
spawner.timer.tick(time.delta());
|
|
||||||
if spawner.timer.just_finished() {
|
|
||||||
commands.spawn((
|
|
||||||
Asteroid(AsteroidSize::SMALL),
|
|
||||||
Position(Vec2::new(40.0, 40.0)),
|
|
||||||
Velocity(Vec2::new(10.0, 0.0)),
|
|
||||||
Rotation(0.0),
|
|
||||||
Mesh2d(game_assets.asteroid_small().0),
|
|
||||||
MeshMaterial2d(game_assets.asteroid_small().1),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Utility function to spawn a single asteroid of a given type
|
|
||||||
/// TODO: convert to an event listener monitoring for "spawn asteroid" events
|
|
||||||
/// from the `fn tick_asteroid_manager(...)` system.
|
|
||||||
fn spawn_asteroid(mut commands: Commands) {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Checks if "W" is pressed and increases velocity accordingly.
|
Checks if "W" is pressed and increases velocity accordingly.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user