summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs65
1 files changed, 53 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs
index 5e821f9..e1a3645 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,36 +1,43 @@
+mod controllable;
mod lunar;
mod setup;
-mod controllable;
use avian2d::prelude::*;
use bevy::{prelude::*, winit::WinitSettings};
use controllable::Player;
use lunar::DestPlanet;
+#[derive(Resource)]
+struct TouchdownGoal {
+ reached: bool,
+}
fn main() {
println!("Hello, world!");
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::srgb(0.5, 0.5, 0.9)))
.insert_resource(WinitSettings::game())
+ .insert_resource(TouchdownGoal { reached: false })
.add_plugins(PhysicsPlugins::default())
.add_systems(
Startup,
(
- setup::setup_starting_planet,
+ setup::setup_starting_planet,
setup::setup_init,
controllable::setup_player_with_controls,
- lunar::setup_moon
- )
+ lunar::setup_moon,
+ ),
)
.add_systems(
- Update,
+ Update,
(
lunar::update_lunar_transform,
controllable::player_movements,
controllable::camera_follow_player,
controllable::check_transition_level,
update_gravitational_forces,
- )
+ handle_collision,
+ remove_all_if_goal,
+ ),
)
.run();
}
@@ -47,7 +54,37 @@ struct StartPlanet {
planet_mass_kg: f32,
radius: f32,
}
-
+#[derive(Component)]
+struct Stickable {}
+fn handle_collision(
+ mut commands: Commands,
+ mut collision_events: EventReader<CollisionStarted>,
+ mut goal: ResMut<TouchdownGoal>,
+ query: Query<(Entity, &Stickable, Option<&RigidBody>)>,
+) {
+ for event in collision_events.read() {
+ // Identify the entities involved in the collision
+ if let Some((entity, _, _)) = query.iter().find(|(e, _, _)| *e == event.0) {
+ if let Some((entity2, _, _)) = query.iter().find(|(e2, _, _)| *e2 == event.1) {
+ // Set the dynamic object to a kinematic state to make it stick
+ commands.entity(entity).insert(RigidBody::Kinematic);
+ commands.entity(entity2).insert(RigidBody::Kinematic);
+ goal.reached = true;
+ }
+ }
+ }
+}
+fn remove_all_if_goal(
+ mut commands: Commands,
+ query: Query<Entity>,
+ goal: Res<TouchdownGoal>
+) {
+ if goal.reached {
+ for e in &query {
+ commands.entity(e).despawn();
+ }
+ }
+}
fn calculate_force_vector(
mass_actor: f32,
mass_incident: f32,
@@ -67,10 +104,14 @@ fn calculate_force_vector(
}.normalize() * 50000.0*/
}
fn update_gravitational_forces(
+ has_touched_down: Res<TouchdownGoal>,
mut query_planet: Query<(Entity, &StartPlanet, &Transform)>,
mut query_planet2: Query<(Entity, &DestPlanet, &Transform)>,
mut query_object: Query<(Entity, &Player, &mut ExternalForce, &Transform)>,
) {
+ if has_touched_down.reached {
+ return;
+ }
let (_es, ps, ts) = query_planet.single_mut();
let (_es, planet2, trans2) = query_planet2.single_mut();
let (_ed, player, mut exd, td) = query_object.single_mut();
@@ -81,11 +122,11 @@ fn update_gravitational_forces(
td.translation.xy(),
);
let f1 = calculate_force_vector(
- planet2.planet_mass_kg,
- player.mass_kg,
- trans2.translation.xy(),
- td.translation.xy());
+ planet2.planet_mass_kg,
+ player.mass_kg,
+ trans2.translation.xy(),
+ td.translation.xy(),
+ );
//info!("Force: {}, Transform: {}", f, td.translation);
exd.apply_force(f0).apply_force(f1);
}
-