Skip to content

Commit

Permalink
move planet and star setup to own modules
Browse files Browse the repository at this point in the history
  • Loading branch information
thombruce committed Oct 9, 2023
1 parent 21db6c4 commit dc165d3
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 96 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Indicators now appear in circle close to player ship for better readability
- Add transparency to indicators
- Moved bulk of planet and star setup to own modules
- Renamed AppState to GameState for better clarity
- Improved readability and versatility of assets with bevy_asset_loader
- Moved menu music loading from main to menu module
- Moved celestial bodies and starfield background to new astronomy module
- Moved assets, camera, game_time and state to resources module
- Moved credits, pause and start_menu to menus module
- Moved blink effect to effects module
- Moved animation to effects module

## [0.0.12] - 2023-10-07

Expand Down
46 changes: 45 additions & 1 deletion src/astronomy/planet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
use bevy::prelude::*;

use crate::{
effects::animate::{AnimationBundle, AnimationIndices, AnimationTimer},
hud::indicator::Indicated,
};

use super::orbit::{Orbit, Orbitable};

const PLANET_ANIMATION_INDICES: AnimationIndices = AnimationIndices {
first: 0,
last: 124,
};

#[derive(Component, Clone, Debug)]
pub struct Planet {}
pub struct Planet;

#[derive(Bundle)]
pub struct PlanetBundle {
pub name: Name,
pub marker: Planet,
pub indicated: Indicated,
pub orbitable: Orbitable,
pub orbit: Orbit,
pub animation_bundle: AnimationBundle,
pub sprite_sheet_bundle: SpriteSheetBundle,
}
impl Default for PlanetBundle {
fn default() -> Self {
Self {
name: Name::new("Planet"),
marker: Planet,
indicated: Indicated {
color: Color::ORANGE_RED,
},
orbitable: Orbitable::default(),
orbit: Orbit {
parent: None,
semi_major_axis: 0.,
},
animation_bundle: AnimationBundle {
indices: PLANET_ANIMATION_INDICES,
timer: AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
},
sprite_sheet_bundle: SpriteSheetBundle::default(),
}
}
}
119 changes: 26 additions & 93 deletions src/astronomy/planetary_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::{
};

use super::{
orbit::{Orbit, OrbitPlugin, Orbitable},
planet::Planet,
star::Star,
orbit::{Orbit, OrbitPlugin},
planet::{Planet, PlanetBundle},
star::{Star, StarBundle},
};

pub struct PlanetarySystemPlugin;
Expand All @@ -27,118 +27,51 @@ impl Plugin for PlanetarySystemPlugin {
)
.chain(),
);
app.add_systems(Update, animate_sprite.run_if(in_state(GameState::Active)));
}
}

#[derive(Component, Clone, Copy)]
struct AnimationIndices {
first: usize,
last: usize,
}

#[derive(Component, Deref, DerefMut)]
struct AnimationTimer(Timer);

fn animate_sprite(
time: Res<Time>,
mut query: Query<(
&AnimationIndices,
&mut AnimationTimer,
&mut TextureAtlasSprite,
)>,
) {
for (indices, mut timer, mut sprite) in &mut query {
timer.tick(time.delta());
if timer.just_finished() {
sprite.index = if sprite.index == indices.last {
indices.first
} else {
sprite.index + 1
};
}
}
}

fn spawn_star(mut commands: Commands, sprites: Res<SpriteAssets>) {
// Star
let star_animation_indices = AnimationIndices {
first: 0,
last: 124,
};

commands.spawn((
SpriteSheetBundle {
texture_atlas: sprites.star.clone(),
sprite: TextureAtlasSprite::new(star_animation_indices.first),
transform: Transform::from_scale(Vec3::splat(2.0)),
..default()
},
star_animation_indices,
// TODO: .1 is too fast, .2 is too choppy; needs more animation frames.
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
Star {},
Indicated {
color: Color::ANTIQUE_WHITE,
},
Orbitable::ZERO,
Name::new("Star"),
));
commands.spawn(StarBundle::from_sprites(sprites));
}

fn spawn_planets(
mut commands: Commands,
sprites: Res<SpriteAssets>,
star_query: Query<Entity, With<Star>>,
) {
// Planet
let planet_animation_indices = AnimationIndices {
first: 0,
last: 124,
};

commands.spawn((
SpriteSheetBundle {
texture_atlas: sprites.planet.clone(),
sprite: TextureAtlasSprite::new(planet_animation_indices.first),
transform: Transform::from_scale(Vec3::splat(2.0)), // Divide by parent scale?
..default()
},
planet_animation_indices,
// TODO: .1 is too fast, .2 is too choppy; needs more animation frames.
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
Planet {},
Indicated {
commands.spawn(PlanetBundle {
indicated: Indicated {
color: Color::LIME_GREEN,
},
Orbitable::default(),
Orbit {
orbit: Orbit {
parent: Some(star_query.single()),
semi_major_axis: 5000.0,
},
Name::new("Planet"),
));
sprite_sheet_bundle: SpriteSheetBundle {
texture_atlas: sprites.planet.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_scale(Vec3::splat(2.0)), // Divide by parent scale?
..default()
},
..default()
});

for radius in [10000.0, 50000.0] {
commands.spawn((
SpriteSheetBundle {
commands.spawn(PlanetBundle {
name: Name::new("Secondary Planet"),
indicated: Indicated { color: Color::GRAY },
orbit: Orbit {
parent: Some(star_query.single()),
semi_major_axis: radius,
},
sprite_sheet_bundle: SpriteSheetBundle {
texture_atlas: sprites.noatmos.clone(),
sprite: TextureAtlasSprite::new(planet_animation_indices.first),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_scale(Vec3::splat(2.0)), // Divide by parent scale?
..default()
},
planet_animation_indices,
// TODO: .1 is too fast, .2 is too choppy; needs more animation frames.
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
Planet {},
Indicated { color: Color::GRAY },
Orbitable::default(),
Orbit {
parent: Some(star_query.single()),
semi_major_axis: radius,
},
Name::new("NoAtmos"),
));
..default()
});
}
}

Expand Down
55 changes: 54 additions & 1 deletion src/astronomy/star.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
use bevy::prelude::*;

use crate::{
effects::animate::{AnimationBundle, AnimationIndices, AnimationTimer},
hud::indicator::Indicated,
resources::assets::SpriteAssets,
};

use super::orbit::Orbitable;

const STAR_ANIMATION_INDICES: AnimationIndices = AnimationIndices {
first: 0,
last: 124,
};

#[derive(Component, Clone, Debug)]
pub struct Star {}
pub struct Star;

#[derive(Bundle)]
pub struct StarBundle {
pub name: Name,
pub marker: Star,
pub indicated: Indicated,
pub orbitable: Orbitable,
pub animation_bundle: AnimationBundle,
pub sprite_sheet_bundle: SpriteSheetBundle,
}
impl Default for StarBundle {
fn default() -> Self {
Self {
name: Name::new("Star"),
marker: Star,
indicated: Indicated {
color: Color::ANTIQUE_WHITE,
},
orbitable: Orbitable::ZERO,
animation_bundle: AnimationBundle {
indices: STAR_ANIMATION_INDICES,
timer: AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
},
sprite_sheet_bundle: SpriteSheetBundle::default(),
}
}
}
impl StarBundle {
pub fn from_sprites(sprites: Res<SpriteAssets>) -> Self {
Self {
sprite_sheet_bundle: SpriteSheetBundle {
texture_atlas: sprites.star.clone(),
sprite: TextureAtlasSprite::new(0),
transform: Transform::from_scale(Vec3::splat(2.0)),
..default()
},
..default()
}
}
}
45 changes: 45 additions & 0 deletions src/effects/animate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use bevy::prelude::*;

use crate::resources::state::GameState;

pub struct AnimatePlugin;
impl Plugin for AnimatePlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, animate_sprite.run_if(in_state(GameState::Active)));
}
}

#[derive(Component, Clone, Copy)]
pub struct AnimationIndices {
pub first: usize,
pub last: usize,
}

#[derive(Component, Deref, DerefMut)]
pub struct AnimationTimer(pub Timer);

#[derive(Bundle)]
pub struct AnimationBundle {
pub indices: AnimationIndices,
pub timer: AnimationTimer,
}

fn animate_sprite(
time: Res<Time>,
mut query: Query<(
&AnimationIndices,
&mut AnimationTimer,
&mut TextureAtlasSprite,
)>,
) {
for (indices, mut timer, mut sprite) in &mut query {
timer.tick(time.delta());
if timer.just_finished() {
sprite.index = if sprite.index == indices.last {
indices.first
} else {
sprite.index + 1
};
}
}
}
1 change: 1 addition & 0 deletions src/effects/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod animate;
pub mod blink;
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod ship;

use crate::{
astronomy::{planetary_system::PlanetarySystemPlugin, starfield::BackgroundPlugin},
effects::blink::EffectsPlugin,
effects::{animate::AnimatePlugin, blink::EffectsPlugin},
hud::HudPlugin,
menus::{
credits::CreditsPlugin,
Expand Down Expand Up @@ -56,6 +56,7 @@ fn main() {
MenuPlugin,
CreditsPlugin,
EffectsPlugin,
AnimatePlugin,
PausePlugin,
PlanetarySystemPlugin,
));
Expand Down

0 comments on commit dc165d3

Please sign in to comment.