From cf67842d24609612c49fd5f2f847e374a8b2be99 Mon Sep 17 00:00:00 2001 From: Thom Bruce Date: Sat, 28 Oct 2023 23:08:30 +0100 Subject: [PATCH] generalise dynamic_orbit for any kind of entity with new Gravitable component --- CHANGELOG.md | 4 ++++ src/ships/bullet.rs | 18 +++++++++++++++++- src/ships/dynamic_orbit.rs | 5 +++-- src/ships/enemy.rs | 2 ++ src/ships/player.rs | 2 ++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d25787..e077e53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/ships/bullet.rs b/src/ships/bullet.rs index 6d1c0d5..e0c2e65 100644 --- a/src/ships/bullet.rs +++ b/src/ships/bullet.rs @@ -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; @@ -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(), diff --git a/src/ships/dynamic_orbit.rs b/src/ships/dynamic_orbit.rs index 589778f..aeb90a2 100644 --- a/src/ships/dynamic_orbit.rs +++ b/src/ships/dynamic_orbit.rs @@ -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 { @@ -21,7 +22,7 @@ impl Plugin for DynamicOrbitPlugin { pub fn dynamic_orbital_positioning_system( tree: Res>, - mut query: Query<(&Transform, &mut ExternalImpulse), With>, + mut query: Query<(&Transform, &mut ExternalImpulse), With>, masses: Query<&Mass, With>, ) { for (transform, mut impulse) in query.iter_mut() { diff --git a/src/ships/enemy.rs b/src/ships/enemy.rs index 7bf708c..077e685 100644 --- a/src/ships/enemy.rs +++ b/src/ships/enemy.rs @@ -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, @@ -57,6 +58,7 @@ fn setup(mut commands: Commands, sprites: Res) { 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 { diff --git a/src/ships/player.rs b/src/ships/player.rs index 9c99a86..caed860 100644 --- a/src/ships/player.rs +++ b/src/ships/player.rs @@ -9,6 +9,7 @@ use crate::{ use super::{ bullet::BulletSpawnEvent, + dynamic_orbit::Gravitable, ship::{dampening, ship_rotation, ship_thrust, AttackSet, Health, MovementSet, Ship}, }; @@ -43,6 +44,7 @@ fn setup(mut commands: Commands, sprites: Res) { 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(),