-
-
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 #45 from thombruce/feat/ship-orbit
add simple gravitational impulse attracting ship to orbitables
- Loading branch information
Showing
5 changed files
with
40 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use bevy::{math::Vec3Swizzles, prelude::*}; | ||
use bevy_rapier2d::prelude::ExternalImpulse; | ||
use bevy_spatial::{kdtree::KDTree2, SpatialAccess}; | ||
|
||
use crate::{core::resources::state::GameState, world::spatial::KDNode}; | ||
|
||
use super::ship::Ship; | ||
|
||
pub struct DynamicOrbitPlugin; | ||
impl Plugin for DynamicOrbitPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems( | ||
Update, | ||
dynamic_orbital_positioning_system.run_if(in_state(GameState::Active)), | ||
); | ||
} | ||
} | ||
|
||
pub fn dynamic_orbital_positioning_system( | ||
tree: Res<KDTree2<KDNode>>, | ||
mut query: Query<(&Transform, &mut ExternalImpulse), With<Ship>>, | ||
) { | ||
let (transform, mut impulse) = query.single_mut(); | ||
|
||
// From Nav | ||
let player_translation = transform.translation.xy(); | ||
|
||
if let Some((pos, _entity)) = tree.nearest_neighbour(player_translation) { | ||
if pos.distance(player_translation) < 1500. { | ||
impulse.impulse += (pos - player_translation).normalize() * 2000.; | ||
} | ||
} | ||
} |
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
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