|
@@ -1,6 +1,7 @@
|
|
|
use bevy::prelude::*;
|
|
use bevy::prelude::*;
|
|
|
|
|
|
|
|
-const TIME_STEP: f32 = 1.0 / 60.0;
|
|
|
|
|
|
|
+const RCS_THRUST: f32 = 0.5;
|
|
|
|
|
+const MAIN_THRUST: f32 = 1.0;
|
|
|
|
|
|
|
|
fn main() {
|
|
fn main() {
|
|
|
App::new()
|
|
App::new()
|
|
@@ -10,9 +11,13 @@ fn main() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
#[derive(Component)]
|
|
|
-struct Position {
|
|
|
|
|
- x: f32,
|
|
|
|
|
- y: f32,
|
|
|
|
|
|
|
+struct Velocity {
|
|
|
|
|
+ v: Vec3,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Component)]
|
|
|
|
|
+struct Rotation {
|
|
|
|
|
+ r: f32, // radiants per second
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
#[derive(Component)]
|
|
@@ -29,13 +34,14 @@ pub struct ShipsPlugin;
|
|
|
impl Plugin for ShipsPlugin {
|
|
impl Plugin for ShipsPlugin {
|
|
|
fn build(&self, app: &mut App) {
|
|
fn build(&self, app: &mut App) {
|
|
|
app.insert_resource(TelemetryTimer(Timer::from_seconds(
|
|
app.insert_resource(TelemetryTimer(Timer::from_seconds(
|
|
|
- 2.0,
|
|
|
|
|
|
|
+ 1.0,
|
|
|
TimerMode::Repeating,
|
|
TimerMode::Repeating,
|
|
|
)))
|
|
)))
|
|
|
- .insert_resource(FixedTime::new_from_secs(TIME_STEP))
|
|
|
|
|
|
|
+ //.insert_resource(FixedTime::new_from_secs(TIME_STEP))
|
|
|
.add_systems(Startup, spawn_ships)
|
|
.add_systems(Startup, spawn_ships)
|
|
|
.add_systems(Update, print_telemetry)
|
|
.add_systems(Update, print_telemetry)
|
|
|
- .add_systems(Update, move_ship);
|
|
|
|
|
|
|
+ .add_systems(Update, apply_thrust)
|
|
|
|
|
+ .add_systems(Update, rotate_ship);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -45,43 +51,69 @@ fn spawn_ships(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
commands.spawn((
|
|
commands.spawn((
|
|
|
SpriteBundle {
|
|
SpriteBundle {
|
|
|
texture: ship_texture,
|
|
texture: ship_texture,
|
|
|
- transform: Transform::from_xyz(100., 100., 0.),
|
|
|
|
|
|
|
+ transform: Transform::from_xyz(100., 100., 0.).with_scale(Vec3::new(0.2, 0.2, 0.2)),
|
|
|
..default()
|
|
..default()
|
|
|
},
|
|
},
|
|
|
Ship,
|
|
Ship,
|
|
|
Name("Player".to_string()),
|
|
Name("Player".to_string()),
|
|
|
- Position { x: 100.0, y: 100.0 },
|
|
|
|
|
|
|
+ Velocity {
|
|
|
|
|
+ v: Vec3::new(0.0, 0.0, 0.0),
|
|
|
|
|
+ },
|
|
|
|
|
+ Rotation {
|
|
|
|
|
+ r: f32::to_radians(0.),
|
|
|
|
|
+ },
|
|
|
));
|
|
));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
fn print_telemetry(
|
|
fn print_telemetry(
|
|
|
time: Res<Time>,
|
|
time: Res<Time>,
|
|
|
mut timer: ResMut<TelemetryTimer>,
|
|
mut timer: ResMut<TelemetryTimer>,
|
|
|
- query: Query<(&Position, &Name), With<Ship>>,
|
|
|
|
|
|
|
+ q: Query<(&Name, &Velocity, &Rotation, &Transform), With<Ship>>,
|
|
|
) {
|
|
) {
|
|
|
if timer.0.tick(time.delta()).just_finished() {
|
|
if timer.0.tick(time.delta()).just_finished() {
|
|
|
- for (position, name) in &query {
|
|
|
|
|
- println!("{}: x = {}, y = {}", name.0, position.x, position.y);
|
|
|
|
|
|
|
+ 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 move_ship(
|
|
|
|
|
|
|
+fn rotate_ship(
|
|
|
time: Res<Time>,
|
|
time: Res<Time>,
|
|
|
- mut sprite_position: Query<(&mut Position, &mut Transform), With<Ship>>,
|
|
|
|
|
- input: Res<Input<KeyCode>>,
|
|
|
|
|
|
|
+ mut q: Query<(&mut Rotation, &mut Transform), With<Ship>>,
|
|
|
|
|
+ keyboard: Res<Input<KeyCode>>,
|
|
|
) {
|
|
) {
|
|
|
- for (mut pos, mut transform) in &mut sprite_position {
|
|
|
|
|
- for k in input.get_pressed() {
|
|
|
|
|
|
|
+ for (mut rot, mut transform) in &mut q {
|
|
|
|
|
+ for k in keyboard.get_pressed() {
|
|
|
match k {
|
|
match k {
|
|
|
- KeyCode::S => pos.y -= 150. * time.delta_seconds(),
|
|
|
|
|
- KeyCode::W => pos.y += 150. * time.delta_seconds(),
|
|
|
|
|
- KeyCode::A => pos.x -= 150. * time.delta_seconds(),
|
|
|
|
|
- KeyCode::D => pos.x += 150. * time.delta_seconds(),
|
|
|
|
|
|
|
+ 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.translation.x = pos.x;
|
|
|
|
|
- transform.translation.y = pos.y;
|
|
|
|
|
|
|
+ transform.rotate_z(rot.r);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|