-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from thombruce/feat/stars
feat/stars
- Loading branch information
Showing
14 changed files
with
268 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use bevy::{prelude::*, render::view::NoFrustumCulling}; | ||
use bevy_tiling_background::{ | ||
BackgroundImageBundle, BackgroundMaterial, SetImageRepeatingExt, TilingBackgroundPlugin, | ||
}; | ||
|
||
use crate::assets::SpriteAssets; | ||
|
||
pub struct BackgroundPlugin; | ||
impl Plugin for BackgroundPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_plugins(TilingBackgroundPlugin::<BackgroundMaterial>::default()); | ||
|
||
app.add_systems(Startup, setup); | ||
} | ||
} | ||
|
||
/// The setup function | ||
fn setup( | ||
mut commands: Commands, | ||
sprites: Res<SpriteAssets>, | ||
mut materials: ResMut<Assets<BackgroundMaterial>>, | ||
) { | ||
let image = sprites.background.clone(); | ||
// Queue a command to set the image to be repeating once the image is loaded. | ||
commands.set_image_repeating(image.clone()); | ||
|
||
commands.spawn(( | ||
BackgroundImageBundle::from_image(image, materials.as_mut()) | ||
.at_z_layer(-0.1) | ||
.with_movement_scale(0.1), | ||
NoFrustumCulling, | ||
Name::new("Background"), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use bevy::{prelude::*, time::Stopwatch}; | ||
|
||
use crate::state::AppState; | ||
|
||
#[derive(Resource, Deref)] | ||
pub struct GameTime(Stopwatch); | ||
|
||
pub struct GameTimePlugin; | ||
impl Plugin for GameTimePlugin { | ||
fn build(&self, app: &mut App) { | ||
app.insert_resource(GameTime(Stopwatch::new())) | ||
.add_systems(Update, tick_game_time.run_if(in_state(AppState::Active))); | ||
} | ||
} | ||
|
||
fn tick_game_time(time: Res<Time>, mut game_time: ResMut<GameTime>) { | ||
game_time.0.tick(time.delta()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::game_time::GameTime; | ||
|
||
const ORBITAL_PERIOD_SCALING_FACTOR: f32 = 1.0; | ||
|
||
#[derive(Component, Clone, Debug)] | ||
pub struct Orbit { | ||
pub semi_major_axis: f32, | ||
// pub eccentricity: f32, | ||
// pub argument_of_periapsis: f32, | ||
// pub initial_mean_anomaly: f32, | ||
} | ||
|
||
/// Really basic circular motion around (0., 0.) or parent entity | ||
pub fn orbital_positioning_system( | ||
game_time: Res<GameTime>, | ||
mut orbits: Query<(&Orbit, &mut Transform)>, | ||
) { | ||
for (orbit, mut transform) in orbits.iter_mut() { | ||
transform.translation.x = (game_time.elapsed_secs() / orbit.semi_major_axis.sqrt() | ||
* ORBITAL_PERIOD_SCALING_FACTOR) | ||
.cos() | ||
* orbit.semi_major_axis; | ||
transform.translation.y = (game_time.elapsed_secs() / orbit.semi_major_axis.sqrt() | ||
* ORBITAL_PERIOD_SCALING_FACTOR) | ||
.sin() | ||
* orbit.semi_major_axis; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
use bevy::prelude::*; | ||
|
||
#[derive(Component, Clone, Debug)] | ||
pub struct Planet {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::{ | ||
assets::SpriteAssets, | ||
orbit::{orbital_positioning_system, Orbit}, | ||
planet::Planet, | ||
star::Star, | ||
state::AppState, | ||
}; | ||
|
||
pub struct PlanetarySystemPlugin; | ||
impl Plugin for PlanetarySystemPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(OnEnter(AppState::GameCreate), setup); | ||
app.add_systems( | ||
Update, | ||
(animate_sprite, orbital_positioning_system).run_if(in_state(AppState::Active)), | ||
); | ||
} | ||
} | ||
|
||
#[derive(Component)] | ||
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 setup(mut commands: Commands, sprites: Res<SpriteAssets>) { | ||
// Star | ||
let star_animation_indices = AnimationIndices { | ||
first: 0, | ||
last: 124, | ||
}; | ||
|
||
// Planet | ||
let planet_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 {}, | ||
Name::new("Star"), | ||
)) | ||
.with_children(|parent| { | ||
parent.spawn(( | ||
SpriteSheetBundle { | ||
texture_atlas: sprites.planet.clone(), | ||
sprite: TextureAtlasSprite::new(planet_animation_indices.first), | ||
transform: Transform::from_scale(Vec3::splat(2.0 / 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 {}, | ||
Orbit { | ||
semi_major_axis: 500.0 / 2.0, // Divide by parent scale? | ||
}, | ||
Name::new("Planet"), | ||
)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.