42 lines
1.2 KiB
Rust
42 lines
1.2 KiB
Rust
use bevy::prelude::*;
|
|
|
|
pub struct MovablePlugin;
|
|
|
|
impl Plugin for MovablePlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.insert_resource(Time::<Fixed>::from_hz(60.0))
|
|
.add_systems(FixedUpdate, movement_system);
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub struct Movable {
|
|
pub linear_speed: f32,
|
|
pub rotation_speed: f32,
|
|
pub max_linear_speed: f32,
|
|
pub max_rotation_speed: f32,
|
|
}
|
|
|
|
impl Movable {
|
|
pub fn new(max_linear_speed: f32, max_rotation_speed: f32) -> Movable {
|
|
Movable {
|
|
linear_speed: 0.,
|
|
rotation_speed: 0.,
|
|
max_linear_speed: max_linear_speed,
|
|
max_rotation_speed: max_rotation_speed,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn movement_system(time: Res<Time>, query: Query<(&Movable, &mut Transform)>) {
|
|
for (movable, mut transform) in query {
|
|
transform.rotate_z(movable.rotation_speed * time.delta_secs());
|
|
let movement_direction = transform.rotation * Vec3::X;
|
|
let movement_distance = movable.linear_speed * time.delta_secs();
|
|
let translation_delta = movement_direction * movement_distance;
|
|
|
|
transform.translation += translation_delta;
|
|
|
|
//TODO: Loop on bounds
|
|
}
|
|
}
|