summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/PLAN.md17
-rw-r--r--src/controllable.rs5
-rw-r--r--src/controllable/player.rs10
-rw-r--r--src/lunar.rs13
-rw-r--r--src/lunar/structs.rs14
-rw-r--r--src/lunar/systems.rs46
-rw-r--r--src/main.rs149
-rw-r--r--src/roam_physics.rs0
-rw-r--r--src/roam_physics/gravitation.rs0
-rw-r--r--src/setup.rs26
10 files changed, 169 insertions, 111 deletions
diff --git a/src/PLAN.md b/src/PLAN.md
new file mode 100644
index 0000000..a6b375a
--- /dev/null
+++ b/src/PLAN.md
@@ -0,0 +1,17 @@
+# Master plan
+## UI
+UI will be designed later on.
+## Stage 1
+The premise of the rescue mission:
+- You are the lead of a multinational effort to land a man on the moon.
+- In Year 20xx, you succeeded, but because you used inches instead of metres, a catastrophic error caused astronaut X to become stranded.
+- Desperate to bring back X, you set out to recover him with a spacecraft in 2030.
+
+- A rocket is constructed, powerful enough to carry you to the moon.
+- Due to planetary physics, the rocket cannot reach the moon by its own propulsion, you will need to accelerate using the planet's gravitational pull.
+
+- After increasing your orbital radius enough to reach Moon, you land to begin a search.
+
+## The search - stage 2
+
+## Leaving, and landing \ No newline at end of file
diff --git a/src/controllable.rs b/src/controllable.rs
new file mode 100644
index 0000000..feaeced
--- /dev/null
+++ b/src/controllable.rs
@@ -0,0 +1,5 @@
+mod player;
+pub use player::{
+ ControlledObject,
+ Player,
+}; \ No newline at end of file
diff --git a/src/controllable/player.rs b/src/controllable/player.rs
new file mode 100644
index 0000000..a87ef9d
--- /dev/null
+++ b/src/controllable/player.rs
@@ -0,0 +1,10 @@
+use bevy::{ecs::component::Component, text::Text2d};
+#[derive(Component)]
+
+pub struct Player {
+ // pub display_name: Text2d,
+ pub mass_kg: f32,
+}
+pub struct ControlledObject {
+
+} \ No newline at end of file
diff --git a/src/lunar.rs b/src/lunar.rs
new file mode 100644
index 0000000..6b853d0
--- /dev/null
+++ b/src/lunar.rs
@@ -0,0 +1,13 @@
+mod structs;
+mod systems;
+
+pub use {
+ structs::{
+ DestPlanet,
+ LunarOrbitalData
+ },
+ systems::{
+ setup_moon,
+ update_lunar_transform,
+ },
+}; \ No newline at end of file
diff --git a/src/lunar/structs.rs b/src/lunar/structs.rs
new file mode 100644
index 0000000..61b3a66
--- /dev/null
+++ b/src/lunar/structs.rs
@@ -0,0 +1,14 @@
+use bevy::ecs::component::Component;
+
+#[derive(Component)]
+pub struct DestPlanet {
+ // pub display_name: String,
+ pub planet_mass_kg: f32,
+ pub radius: f32,
+}
+#[derive(Component)]
+pub struct LunarOrbitalData {
+ pub period: f32,
+ pub orbital_velocity: f32,
+ pub orbital_radius: f32,
+} \ No newline at end of file
diff --git a/src/lunar/systems.rs b/src/lunar/systems.rs
new file mode 100644
index 0000000..4cdebdc
--- /dev/null
+++ b/src/lunar/systems.rs
@@ -0,0 +1,46 @@
+use bevy::prelude::*;
+use avian2d::prelude::*;
+use super::DestPlanet;
+use super::LunarOrbitalData;
+pub fn lunar_period_to_vec(angular_pos: f32, radius: f32) -> Vec2 {
+ let x = radius * f32::cos(angular_pos);
+ let y = radius * f32::sin(angular_pos);
+ Vec2 { x, y }
+}
+pub fn update_lunar_transform(
+ time: Res<Time>,
+ mut lunar_query: Query<(
+ &DestPlanet,
+ &mut LunarOrbitalData,
+ &mut Transform,
+ &RigidBody
+ ), With<DestPlanet>>,
+) {
+ let (_moon_planet, mut lod, mut moon_transform, _moon_body) = lunar_query.single_mut();
+ let dt = time.delta_secs();
+ let new_transform = lunar_period_to_vec(
+ lod.period,
+ lod.orbital_radius).normalize_or_zero() * lod.orbital_radius;
+ moon_transform.translation = Vec3 {x: new_transform.x, y: new_transform.y, z: 0.0};
+ lod.period = dt * lod.orbital_velocity;
+}
+pub fn setup_moon(mut cmd: Commands) {
+ let objective_planet = DestPlanet {
+ // display_name: String::from("Objective Planet"),
+ planet_mass_kg: 50.0,
+ radius: 5.0,
+ };
+ let lunar_radius = 900.0;
+ let r = (&objective_planet).radius;
+ let m = (&objective_planet).planet_mass_kg;
+ let initial_transform = lunar_period_to_vec(0., 100.);
+ cmd.spawn((
+ objective_planet,
+ LunarOrbitalData { period: 0., orbital_velocity: 1., orbital_radius: lunar_radius},
+ RigidBody::Dynamic,
+ Transform::from_xyz(initial_transform.x, initial_transform.y, 0.0),
+ Sprite::from_color(Color::BLACK, Vec2 { x: 30., y: 30. }),
+ Collider::circle(r),
+ Mass(m),
+ ));
+} \ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index e0aa007..319eabc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,10 @@
+mod lunar;
+mod setup;
+mod controllable;
+
use avian2d::prelude::*;
-use bevy::{ecs::bundle, prelude::*, window::WindowResolution, winit::WinitSettings};
+use bevy::{prelude::*, winit::WinitSettings};
+use controllable::Player;
fn main() {
println!("Hello, world!");
App::new()
@@ -7,98 +12,45 @@ fn main() {
.insert_resource(ClearColor(Color::srgb(0.5, 0.5, 0.9)))
.insert_resource(WinitSettings::game())
.add_plugins(PhysicsPlugins::default())
- .add_systems(Startup, setup_init)
- .add_systems(Startup, setup_stellar_body)
- .add_systems(Startup, setup_starting_planet)
- .add_systems(Update, hello_system)
- .add_systems(Update, update_gravitational_forces)
+ .add_systems(
+ Startup,
+ (
+ setup::setup_starting_planet,
+ setup::setup_init,
+ lunar::setup_moon
+ )
+ )
+ .add_systems(
+ Update,
+ (
+ update_gravitational_forces,
+ lunar::update_lunar_transform
+ )
+ )
.run();
}
-#[derive(Component)]
-struct LunarOrbitalData {
- period: f32,
-}
-fn hello_system() {
- println!("hello");
-}
-fn lunar_period_to_vec(angular_pos: f32, radius: f32) -> Vec2 {
- let x = radius * f32::cos(angular_pos);
- let y = radius * f32::sin(angular_pos);
- Vec2 { x, y }
-}
-fn handle_lunar_orbit(
- time: Res<Time>,
- mut lunar_query: Query<(&DestPlanet, &mut LunarOrbitalData, &mut Transform, &RigidBody), With<DestPlanet>>,
-) {
- let (moon_planet, mut lod, mut moon_transform, moon_body) = lunar_query.single_mut();
- let dt = time.delta_secs();
-}
-#[derive(Component)]
-struct Starship {
- name: String,
- mass_kg: f32,
-}
-#[derive(Component)]
-struct StellarBody {
- name: String,
- mass_kg: f32,
-}
-enum GameLevel {
- Ground,
- Space,
- Nars,
-}
-#[derive(PartialEq, Eq)]
-enum PlanetType {
- Starting,
- Objective,
-}
-#[derive(Component)]
-struct Player {
- display_name: String,
- level: GameLevel,
-}
+/*enum GameLevel {
+ NearGround,
+ Space,
+ Moon,
+ LunarSurface,
+}*/
#[derive(Component)]
struct StartPlanet {
display_name: String,
planet_mass_kg: f32,
radius: f32,
}
-#[derive(Component)]
-struct DestPlanet {
- display_name: String,
- planet_mass_kg: f32,
- radius: f32,
-}
-fn setup_stellar_body(mut cmd: Commands) {
- let objective_planet = DestPlanet {
- display_name: String::from("Objective Planet"),
- planet_mass_kg: 50.0,
- radius: 5.0,
- };
- let lunar_radius = 900;
- let r = (&objective_planet).radius;
- let m = (&objective_planet).planet_mass_kg;
- let initial_transform = lunar_period_to_vec(0., 100.);
- cmd.spawn((
- objective_planet,
- LunarOrbitalData { period: 0. },
- RigidBody::Dynamic,
- Transform::from_xyz(initial_transform.x, initial_transform.y, 0.0),
- Sprite::from_color(Color::BLACK, Vec2 { x: 30., y: 30. }),
- Collider::circle(30.),
- Mass(m),
- ));
-}
+
fn calculate_force_vector(
mass_actor: f32,
mass_incident: f32,
pos_actor: Vec2,
pos_incident: Vec2,
) -> Vec2 {
- let GRAV_CONST = 100.0;
- (GRAV_CONST * mass_actor * mass_incident / (pos_actor.distance_squared(pos_incident)))
+ let grav_const = 100.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,
@@ -109,44 +61,19 @@ fn calculate_force_vector(
y: pos_actor.y - pos_incident.y,
}.normalize() * 50000.0*/
}
-fn update_gravitational_forces<'b>(
- mut query_start_planet: Query<(Entity, &StartPlanet, &Transform)>,
- mut query_dest_planet: Query<(Entity, &DestPlanet, &mut ExternalForce, &Transform)>,
+fn update_gravitational_forces(
+ mut query_planet: Query<(Entity, &StartPlanet, &Transform)>,
+ mut query_object: Query<(Entity, &Player, &mut ExternalForce, &Transform)>,
) {
- let (es, ps, ts) = query_start_planet.single_mut();
- let (ed, pd, mut exd, td) = query_dest_planet.single_mut();
+ let (_es, ps, ts) = query_planet.single_mut();
+ let (_ed, player, mut exd, td) = query_object.single_mut();
let f = calculate_force_vector(
ps.planet_mass_kg,
- pd.planet_mass_kg,
+ player.mass_kg,
ts.translation.xy(),
td.translation.xy(),
);
- println!("force vector: {}, transform_d: {}", f, td.translation);
+ info!("Force: {}, Transform: {}", f, td.translation);
exd.apply_force(f);
}
-fn setup_starting_planet(mut cmd: Commands) {
- let start_planet = StartPlanet {
- display_name: String::from("Earth 2"),
- planet_mass_kg: 15000.0,
- radius: 10.0,
- };
- let collider_radius = (&start_planet).radius;
- let planet_mass_kg = (&start_planet).planet_mass_kg;
- cmd.spawn((
- start_planet,
- RigidBody::Static,
- Transform::from_xyz(0.0, 0.0, 0.0),
- Sprite::from_color(Color::WHITE, Vec2 { x: 5.0, y: 5.0 }),
- Collider::circle(5.),
- Mass(planet_mass_kg),
- ));
-}
-type PlayerBundle = (Player, Sprite, Transform);
-fn setup_init(mut commands: Commands) {
- let player = Player {
- display_name: String::from("player"),
- level: GameLevel::Ground,
- };
- commands.spawn(Camera2d);
- println!("Setup initial things");
-}
+
diff --git a/src/roam_physics.rs b/src/roam_physics.rs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/roam_physics.rs
diff --git a/src/roam_physics/gravitation.rs b/src/roam_physics/gravitation.rs
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/roam_physics/gravitation.rs
diff --git a/src/setup.rs b/src/setup.rs
new file mode 100644
index 0000000..6a6a5d0
--- /dev/null
+++ b/src/setup.rs
@@ -0,0 +1,26 @@
+use avian2d::prelude::{Collider, Mass, RigidBody};
+use bevy::{color::Color, core_pipeline::core_2d::Camera2d, ecs::system::Commands, math::Vec2, sprite::Sprite, transform::components::Transform};
+
+use crate::StartPlanet;
+
+pub fn setup_starting_planet(mut cmd: Commands) {
+ let start_planet = StartPlanet {
+ display_name: String::from("Earth 2"),
+ planet_mass_kg: 15000.0,
+ radius: 10.0,
+ };
+ let collider_radius = (&start_planet).radius;
+ let planet_mass_kg = (&start_planet).planet_mass_kg;
+ cmd.spawn((
+ start_planet,
+ RigidBody::Static,
+ Transform::from_xyz(0.0, 0.0, 0.0),
+ Sprite::from_color(Color::WHITE, Vec2 { x: 5.0, y: 5.0 }),
+ Collider::circle(collider_radius),
+ Mass(planet_mass_kg),
+ ));
+}
+pub fn setup_init(mut commands: Commands) {
+ commands.spawn(Camera2d);
+ println!("Setup initial things");
+} \ No newline at end of file