|
|
@@ -1,119 +1,15 @@
|
|
|
-use bevy::prelude::*;
|
|
|
+mod body;
|
|
|
+mod camera;
|
|
|
+mod ship;
|
|
|
|
|
|
-const RCS_THRUST: f32 = 0.5;
|
|
|
-const MAIN_THRUST: f32 = 1.0;
|
|
|
+use bevy::prelude::*;
|
|
|
+use camera::CameraPlugin;
|
|
|
+use ship::ShipsPlugin;
|
|
|
|
|
|
fn main() {
|
|
|
App::new()
|
|
|
.insert_resource(ClearColor(Color::rgb(0.3, 0.3, 0.4)))
|
|
|
+ .add_plugins(CameraPlugin)
|
|
|
.add_plugins((DefaultPlugins, ShipsPlugin))
|
|
|
.run();
|
|
|
}
|
|
|
-
|
|
|
-#[derive(Component)]
|
|
|
-struct Velocity {
|
|
|
- v: Vec3,
|
|
|
-}
|
|
|
-
|
|
|
-#[derive(Component)]
|
|
|
-struct Rotation {
|
|
|
- r: f32, // radiants per second
|
|
|
-}
|
|
|
-
|
|
|
-#[derive(Component)]
|
|
|
-struct Ship;
|
|
|
-
|
|
|
-#[derive(Component)]
|
|
|
-struct Name(String);
|
|
|
-
|
|
|
-#[derive(Resource)]
|
|
|
-struct TelemetryTimer(Timer);
|
|
|
-
|
|
|
-pub struct ShipsPlugin;
|
|
|
-
|
|
|
-impl Plugin for ShipsPlugin {
|
|
|
- fn build(&self, app: &mut App) {
|
|
|
- app.insert_resource(TelemetryTimer(Timer::from_seconds(
|
|
|
- 1.0,
|
|
|
- TimerMode::Repeating,
|
|
|
- )))
|
|
|
- //.insert_resource(FixedTime::new_from_secs(TIME_STEP))
|
|
|
- .add_systems(Startup, spawn_ships)
|
|
|
- .add_systems(Update, print_telemetry)
|
|
|
- .add_systems(Update, apply_thrust)
|
|
|
- .add_systems(Update, rotate_ship);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-fn spawn_ships(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
- let ship_texture = asset_server.load("ship.png");
|
|
|
- commands.spawn(Camera2dBundle::default());
|
|
|
- commands.spawn((
|
|
|
- SpriteBundle {
|
|
|
- texture: ship_texture,
|
|
|
- transform: Transform::from_xyz(100., 100., 0.).with_scale(Vec3::new(0.2, 0.2, 0.2)),
|
|
|
- ..default()
|
|
|
- },
|
|
|
- Ship,
|
|
|
- Name("Player".to_string()),
|
|
|
- Velocity {
|
|
|
- v: Vec3::new(0.0, 0.0, 0.0),
|
|
|
- },
|
|
|
- Rotation {
|
|
|
- r: f32::to_radians(0.),
|
|
|
- },
|
|
|
- ));
|
|
|
-}
|
|
|
-
|
|
|
-fn print_telemetry(
|
|
|
- time: Res<Time>,
|
|
|
- mut timer: ResMut<TelemetryTimer>,
|
|
|
- q: Query<(&Name, &Velocity, &Rotation, &Transform), With<Ship>>,
|
|
|
-) {
|
|
|
- if timer.0.tick(time.delta()).just_finished() {
|
|
|
- for (name, vel, rot, trans) in &q {
|
|
|
- println!(
|
|
|
- "{}: v={:.4?}, speed={:.4}, point={:.4?}, rot={:.4}",
|
|
|
- name.0,
|
|
|
- vel.v.xy(),
|
|
|
- vel.v.length(),
|
|
|
- (trans.rotation * Vec3::Y).xy(),
|
|
|
- rot.r
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-fn apply_thrust(
|
|
|
- time: Res<Time>,
|
|
|
- mut q: Query<(&mut Velocity, &mut Transform), With<Ship>>,
|
|
|
- keyboard: Res<Input<KeyCode>>,
|
|
|
-) {
|
|
|
- let mut thrust = Vec3::ZERO;
|
|
|
- for (mut vel, mut trans) in &mut q {
|
|
|
- for k in keyboard.get_pressed() {
|
|
|
- if k == &KeyCode::W {
|
|
|
- thrust = (trans.rotation * Vec3::Y) * MAIN_THRUST * time.delta_seconds();
|
|
|
- }
|
|
|
- }
|
|
|
- vel.v += thrust;
|
|
|
- trans.translation += vel.v;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-fn rotate_ship(
|
|
|
- time: Res<Time>,
|
|
|
- mut q: Query<(&mut Rotation, &mut Transform), With<Ship>>,
|
|
|
- keyboard: Res<Input<KeyCode>>,
|
|
|
-) {
|
|
|
- for (mut rot, mut transform) in &mut q {
|
|
|
- for k in keyboard.get_pressed() {
|
|
|
- match k {
|
|
|
- KeyCode::A => rot.r += f32::to_radians(RCS_THRUST) * time.delta_seconds(),
|
|
|
- KeyCode::D => rot.r -= f32::to_radians(RCS_THRUST) * time.delta_seconds(),
|
|
|
- _ => (),
|
|
|
- }
|
|
|
- }
|
|
|
- transform.rotate_z(rot.r);
|
|
|
- }
|
|
|
-}
|