summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: e1a364552279bd84ede61fbaa0044c42a65db3ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
mod controllable;
mod lunar;
mod setup;

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_init,
                controllable::setup_player_with_controls,
                lunar::setup_moon,
            ),
        )
        .add_systems(
            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();
}

/*enum GameLevel {
    NearGround,
    Space,
    Moon,
    LunarSurface,
}*/
#[derive(Component)]
struct StartPlanet {
    display_name: String,
    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,
    pos_actor: Vec2,
    pos_incident: Vec2,
) -> Vec2 {
    let grav_const = 15.0;
    (grav_const * mass_actor * mass_incident / (pos_actor.distance_squared(pos_incident)))
        * Vec2 {
            x: pos_actor.x - pos_incident.x,
            y: pos_actor.y - pos_incident.y,
        }
        .normalize()
    /*Vec2 {
        x: pos_actor.x - pos_incident.x,
        y: pos_actor.y - pos_incident.y,
    }.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();
    let f0 = calculate_force_vector(
        ps.planet_mass_kg,
        player.mass_kg,
        ts.translation.xy(),
        td.translation.xy(),
    );
    let f1 = calculate_force_vector(
        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);
}