@@ 69,3 69,16 @@ pub struct PlayerBundle {
pub player: Player,
pub attach: Attach,
}
+
+#[derive(Resource)]
+pub struct ModuleTimer(pub Timer);
+impl ModuleTimer {
+ pub fn new() -> Self {
+ Self(Timer::from_seconds(3.0, TimerMode::Repeating))
+ }
+}
+impl Default for ModuleTimer {
+ fn default() -> Self {
+ Self::new()
+ }
+}
@@ 54,11 54,12 @@ fn main() {
gravity: Vect { x: 0.0, y: 0.0 },
..Default::default()
})
+ .init_resource::<ModuleTimer>()
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(SCALE))
.add_plugins(TwiteServerPlugin)
.add_systems(Startup, setup_integration_parameters)
.add_systems(Startup, spawn_planets)
- .add_systems(Startup, module_spawn)
+ .add_systems(FixedUpdate, module_spawn)
.add_systems(Update, on_message)
.add_systems(Update, on_close)
.add_systems(FixedUpdate, on_position_change)
@@ 89,27 90,38 @@ fn spawn_planets(mut commands: Commands) {
.insert(ReadMassProperties::default())
.insert(RigidBody::Fixed);
}
-fn module_spawn(mut commands: Commands) {
- commands
- .spawn(PartBundle {
- part_type: PartType::Cargo,
- transform: TransformBundle::from(Transform::from_xyz(1100. / SCALE, 0., 0.)),
- })
- //.insert(Collider::cuboid(18.75 / SCALE, 23.4375 / SCALE))
- .insert(RigidBody::Dynamic)
- .with_children(|children| {
- children
- .spawn(Collider::cuboid(18.75 / SCALE, 23.4375 / SCALE))
- .insert(TransformBundle::from(Transform::from_xyz(
- 0.,
- 1.5625 / SCALE,
- 0.,
- )));
- })
- .insert(ExternalForce::default())
- .insert(ExternalImpulse::default())
- .insert(Velocity::default())
- .insert(ReadMassProperties::default());
+fn module_spawn(mut commands: Commands, time: Res<Time>, mut module_timer: ResMut<ModuleTimer>) {
+ if module_timer.0.tick(time.delta()).just_finished() {
+ let angle: f32 = {
+ let mut rng = rand::thread_rng();
+ rng.gen::<f32>() * std::f32::consts::PI * 2.
+ };
+ let mut transform = Transform::from_xyz(
+ angle.cos() * 1500.0 / SCALE,
+ angle.sin() * 1500.0 / SCALE,
+ 0.0,
+ );
+ transform.rotate_z(angle);
+ commands
+ .spawn(PartBundle {
+ part_type: PartType::Cargo,
+ transform: TransformBundle::from(transform),
+ })
+ .insert(RigidBody::Dynamic)
+ .with_children(|children| {
+ children
+ .spawn(Collider::cuboid(18.75 / SCALE, 23.4375 / SCALE))
+ .insert(TransformBundle::from(Transform::from_xyz(
+ 0.,
+ 1.5625 / SCALE,
+ 0.,
+ )));
+ })
+ .insert(ExternalForce::default())
+ .insert(ExternalImpulse::default())
+ .insert(Velocity::default())
+ .insert(ReadMassProperties::default());
+ }
}
fn on_message(