Skip to content

Instantly share code, notes, and snippets.

@ThunderComplex
Created April 23, 2024 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThunderComplex/74ddd3549cb71855d0b343d11fe5512a to your computer and use it in GitHub Desktop.
Save ThunderComplex/74ddd3549cb71855d0b343d11fe5512a to your computer and use it in GitHub Desktop.
Orbits
use bevy::prelude::*;
#[derive(Component, Debug)]
struct SectorPosition(Vec3);
#[derive(Component, Debug)]
struct ShipVelocity(Vec3);
#[derive(Component)]
struct ShipFactionHuman;
#[derive(Component)]
struct ShipFactionAlien;
#[derive(Default, Reflect, GizmoConfigGroup)]
struct MyRoundGizmos {}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_gizmo_group::<MyRoundGizmos>()
.add_systems(Startup, setup_map)
.add_systems(Update, ship_combat)
.add_systems(Update, ship_move)
.run();
}
fn setup_map(mut commands: Commands) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(10., 20.0, 10.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..default()
});
/// Ships
commands.spawn((
ShipFactionHuman,
SectorPosition(Vec3::new(1.0, 0.0, 1.0)),
ShipVelocity(Vec3::ZERO)
));
commands.spawn((
ShipFactionAlien,
SectorPosition(Vec3::new(0.0, 0.0, 0.0)),
ShipVelocity(Vec3::ZERO)
));
}
fn ship_move(
mut query: Query<(&ShipVelocity, &mut SectorPosition)>,
mut gizmos: Gizmos,
mut my_gizmos: Gizmos<MyRoundGizmos>,
time: Res<Time>
) {
let mut col = Color::ORANGE;
for (velo, mut pos) in &mut query {
pos.0 += velo.0 * time.delta_seconds();
my_gizmos
.sphere(pos.0, Quat::IDENTITY, 1.0, col)
.circle_segments(64);
col = Color::NAVY;
}
}
fn ship_combat(
mut human_query: Query<(&SectorPosition, &mut ShipVelocity), (With<ShipFactionHuman>, Without<ShipFactionAlien>)>,
mut alien_query: Query<(&SectorPosition, &mut ShipVelocity), (With<ShipFactionAlien>, Without<ShipFactionHuman>)>,
) {
for (human_position, mut human_velocity) in &mut human_query {
for (alien_position, mut alien_velocity) in &mut alien_query {
let pos_diff = human_position.0 - alien_position.0;
let cross = pos_diff.normalize().cross(Vec3::Y);
human_velocity.0 = cross.normalize() * 20.0;
let pos_diff = alien_position.0 - human_position.0;
let cross = pos_diff.normalize().cross(Vec3::Y);
alien_velocity.0 = cross.normalize() * 20.0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment