Skip to content

Commit

Permalink
Merge pull request #115 from thombruce/feat/indicated-distance
Browse files Browse the repository at this point in the history
Indicator Distance
  • Loading branch information
thombruce authored Nov 19, 2023
2 parents 91c1106 + 14c355e commit e3bc537
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- PlayTime resource records unpaused play time
- Add rotation effect; will rotate entities with the Rotation component

### Changed

- Add distance value to Indicated entities, enabling some to be hidden from system when out of range

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions assets/space/celestials/teapot_large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/core/effects/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod animate;
pub mod blink;
pub mod rotate;
10 changes: 10 additions & 0 deletions src/core/effects/rotate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use bevy::prelude::*;

#[derive(Component)]
pub struct Rotation(pub f32);

pub(crate) fn rotate_system(time: Res<Time>, mut query: Query<(&mut Transform, &Rotation)>) {
for (mut transform, rotation) in query.iter_mut() {
transform.rotate_z(rotation.0 * time.delta_seconds());
}
}
3 changes: 3 additions & 0 deletions src/core/resources/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub struct SpriteAssets {

#[asset(path = "space/meteors/meteorGrey_med1.png")]
pub meteor: Handle<Image>,

#[asset(path = "space/celestials/teapot_large.png")]
pub teapot: Handle<Image>,
}

#[derive(AssetCollection, Resource)]
Expand Down
5 changes: 4 additions & 1 deletion src/ships/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ pub(crate) fn spawn_enemies(
},
Gravitable,
Health(1000.0),
Indicated { color: Color::RED },
Indicated {
color: Color::RED,
..default()
},
SpriteBundle {
texture: sprites.enemy_ship.clone(),
transform: Transform {
Expand Down
1 change: 1 addition & 0 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl Plugin for SystemsPlugin {
resources::game_time::tick_game_time,
resources::despawn_timer::despawn_system,
effects::animate::animate_sprite,
effects::rotate::rotate_system,
astronomy::orbit::orbitable_update_system,
astronomy::orbit::orbital_positioning_system,
enemy::spawn_enemies,
Expand Down
23 changes: 18 additions & 5 deletions src/ui/hud/indicator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use bevy::{math::Vec3Swizzles, prelude::*};
use bevy::prelude::*;

use crate::ships::player::Player;

#[derive(Component, Clone, Debug)]
pub struct Indicated {
pub color: Color,
pub distance: f32,
}
impl Default for Indicated {
fn default() -> Self {
Self {
color: Color::WHITE,
distance: 1_000_000.0,
}
}
}

#[derive(Component, Clone, Debug)]
Expand Down Expand Up @@ -74,23 +83,27 @@ pub(crate) fn indicators_system(
bounds_query: Query<&Node, (With<Bounds>, Without<Indicator>)>,
) {
if let Ok(player_transform) = player_query.get_single() {
let player_translation = player_transform.translation.xy();
let player_translation = player_transform.translation.truncate();

for (mut indicator_transform, mut indicator_style, indicator, mut indicator_color) in
&mut query
{
if let Ok((entity_transform, entity_visibility, indicated)) =
entity_query.get(indicator.entity)
{
if entity_visibility.get() {
let entity_translation = entity_transform.translation.truncate();

// If the entity is in view or its distance exceeds its Indicated::distance
// value, hide it and continue.
if entity_visibility.get()
|| (entity_translation.distance(player_translation) > indicated.distance)
{
indicator_style.display = Display::None;
continue;
}

indicator_style.display = Display::DEFAULT;

let entity_translation = entity_transform.translation.xy();

// get the vector from the entity to the player ship in 2D and normalize it.
let real_to_entity = entity_translation - player_translation;
let to_entity = real_to_entity.normalize();
Expand Down
1 change: 1 addition & 0 deletions src/world/astronomy/planet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Default for PlanetBundle {
marker: Planet,
indicated: Indicated {
color: Color::ORANGE_RED,
..default()
},
orbitable: Orbitable::default(),
orbit: Orbit::default(),
Expand Down
36 changes: 35 additions & 1 deletion src/world/astronomy/planetary_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use bevy::prelude::*;

use crate::{
core::{
effects::animate::{AnimationBundle, AnimationTimer},
effects::{
animate::{AnimationBundle, AnimationTimer},
rotate::Rotation,
},
resources::assets::SpriteAssets,
},
ui::hud::indicator::Indicated,
world::spatial::KDNode,
};

use super::{
Expand Down Expand Up @@ -40,6 +44,7 @@ pub(crate) fn spawn_planets(
name: Name::new("Mercury"),
indicated: Indicated {
color: Color::ORANGE,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand All @@ -61,6 +66,7 @@ pub(crate) fn spawn_planets(
name: Name::new("Venus"),
indicated: Indicated {
color: Color::ORANGE,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand All @@ -83,6 +89,7 @@ pub(crate) fn spawn_planets(
name: Name::new("Earth"),
indicated: Indicated {
color: Color::LIME_GREEN,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand All @@ -102,10 +109,33 @@ pub(crate) fn spawn_planets(
},
EarthLike,
));
commands.spawn((
Name::new("Russell's Teapot"),
Indicated {
color: Color::FUCHSIA,
distance: 5_000.0,
},
Orbit {
parent: Some(star_query.single()),
semi_major_axis: 188_768_694_434.0,
eccentricity: 0.1234,
argument_of_periapsis: f32::to_radians(89.0),
initial_mean_anomaly: f32::to_radians(0.), //TODO: Set me
},
SpriteBundle {
texture: sprites.teapot.clone(),
transform: Transform::from_scale(Vec3::splat(1.0)),
..default()
},
Mass(1.0),
KDNode,
Rotation(f32::to_radians(45.0)),
));
commands.spawn(PlanetBundle {
name: Name::new("Mars"),
indicated: Indicated {
color: Color::ORANGE,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand All @@ -127,6 +157,7 @@ pub(crate) fn spawn_planets(
name: Name::new("Jupiter"),
indicated: Indicated {
color: Color::SALMON,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand All @@ -152,6 +183,7 @@ pub(crate) fn spawn_planets(
name: Name::new("Saturn"),
indicated: Indicated {
color: Color::SALMON,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand All @@ -173,6 +205,7 @@ pub(crate) fn spawn_planets(
name: Name::new("Uranus"),
indicated: Indicated {
color: Color::SALMON,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand All @@ -194,6 +227,7 @@ pub(crate) fn spawn_planets(
name: Name::new("Neptune"),
indicated: Indicated {
color: Color::SALMON,
..default()
},
orbit: Orbit {
parent: Some(star_query.single()),
Expand Down
1 change: 1 addition & 0 deletions src/world/astronomy/star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Default for StarBundle {
marker: Star,
indicated: Indicated {
color: Color::ANTIQUE_WHITE,
..default()
},
orbitable: Orbitable::ZERO,
animation_bundle: AnimationBundle {
Expand Down

0 comments on commit e3bc537

Please sign in to comment.