Skip to content

Commit

Permalink
generalise dynamic_orbit for any kind of entity with new Gravitable c…
Browse files Browse the repository at this point in the history
…omponent
  • Loading branch information
thombruce committed Oct 28, 2023
1 parent ee8d7af commit cf67842
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Gravitable component added allowing dynamic_orbit system to be applied to any entity

### Fixed

- follow_player camera system now constrained to .after() player movement
Expand Down
18 changes: 17 additions & 1 deletion src/ships/bullet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::core::resources::{
state::{ForState, GameState},
};

use super::ship::MovementSet;
#[allow(unused_imports)]
use super::{dynamic_orbit::Gravitable, ship::MovementSet};

#[derive(Component)]
pub struct Bullet;
Expand Down Expand Up @@ -62,6 +63,21 @@ fn spawn_bullet(
..default()
},
Bullet,
// TODO: Should bullets be affected by gravity?
// If so, set their initial velocity higher and consider
// despawning them either based on distance or a longer
// timer. Systems as they are, it's far too ridiculous and
// highly unpredictable.
// A bullet speed of around 5000.0 is a reasonable demo of the effect.
// But think again about despawning based on distance - this would mean
// the player could spawn millions of bullets inside of a gravitational
// well and never have them despawn. There has to be a limit to either
// the entity lifetime or the entity count.
// Also consider an alternative to adjusting bullet's speed:
// - introduce a gravitational scaling factor that may be unique per entity
// This might be unrealistic, but it might make for better gameplay.
// Gravitable,
// ExternalImpulse::default(),
DespawnTimer(Timer::from_seconds(2.0, TimerMode::Once)),
ForState {
states: GameState::IN_GAME_STATE.to_vec(),
Expand Down
5 changes: 3 additions & 2 deletions src/ships/dynamic_orbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
world::{astronomy::orbit::Mass, spatial::KDNode},
};

use super::ship::Ship;
#[derive(Component)]
pub struct Gravitable;

pub struct DynamicOrbitPlugin;
impl Plugin for DynamicOrbitPlugin {
Expand All @@ -21,7 +22,7 @@ impl Plugin for DynamicOrbitPlugin {

pub fn dynamic_orbital_positioning_system(
tree: Res<KDTree2<KDNode>>,
mut query: Query<(&Transform, &mut ExternalImpulse), With<Ship>>,
mut query: Query<(&Transform, &mut ExternalImpulse), With<Gravitable>>,
masses: Query<&Mass, With<KDNode>>,
) {
for (transform, mut impulse) in query.iter_mut() {
Expand Down
2 changes: 2 additions & 0 deletions src/ships/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_rapier2d::prelude::*;
use crate::core::resources::{assets::SpriteAssets, state::GameState};
use crate::ui::hud::indicator::Indicated;

use super::dynamic_orbit::Gravitable;
use super::{
bullet::BulletSpawnEvent,
player::Player,
Expand Down Expand Up @@ -57,6 +58,7 @@ fn setup(mut commands: Commands, sprites: Res<SpriteAssets>) {
rotation: f32::to_radians(360.0), // Ship manoeuvrability (rad)
bullet_timer: Timer::from_seconds(0.1, TimerMode::Once),
},
Gravitable,
Health(1000.0),
Indicated { color: Color::RED },
SpriteBundle {
Expand Down
2 changes: 2 additions & 0 deletions src/ships/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{

use super::{
bullet::BulletSpawnEvent,
dynamic_orbit::Gravitable,
ship::{dampening, ship_rotation, ship_thrust, AttackSet, Health, MovementSet, Ship},
};

Expand Down Expand Up @@ -43,6 +44,7 @@ fn setup(mut commands: Commands, sprites: Res<SpriteAssets>) {
rotation: f32::to_radians(360.0), // Ship manoeuvrability (rad)
bullet_timer: Timer::from_seconds(0.1, TimerMode::Once),
},
Gravitable,
Health(10000.0), // TODO: Game balancing
SpriteBundle {
texture: sprites.player_ship.clone(),
Expand Down

0 comments on commit cf67842

Please sign in to comment.