1
1
use bevy:: {
2
+ core:: FixedTimestep ,
2
3
diagnostic:: { Diagnostics , FrameTimeDiagnosticsPlugin , LogDiagnosticsPlugin } ,
3
4
prelude:: * ,
4
5
} ;
5
- use rand:: { random, Rng } ;
6
+ use rand:: random;
6
7
7
8
const BIRDS_PER_SECOND : u32 = 10000 ;
8
9
const _BASE_COLOR: Color = Color :: rgb ( 5.0 , 5.0 , 5.0 ) ;
@@ -43,30 +44,44 @@ fn main() {
43
44
. add_system ( movement_system)
44
45
. add_system ( collision_system)
45
46
. add_system ( counter_system)
47
+ . add_system_set (
48
+ SystemSet :: new ( )
49
+ . with_run_criteria ( FixedTimestep :: step ( 0.2 ) )
50
+ . with_system ( scheduled_spawner) ,
51
+ )
46
52
. run ( ) ;
47
53
}
48
54
49
- struct BirdTexture ( Handle < Image > ) ;
55
+ struct BirdScheduled {
56
+ wave : u128 ,
57
+ per_wave : u128 ,
58
+ }
50
59
51
- fn setup (
60
+ fn scheduled_spawner (
52
61
mut commands : Commands ,
53
62
windows : Res < Windows > ,
63
+ mut scheduled : ResMut < BirdScheduled > ,
54
64
mut counter : ResMut < BevyCounter > ,
55
- asset_server : Res < AssetServer > ,
65
+ bird_texture : Res < BirdTexture > ,
56
66
) {
57
- let texture = asset_server. load ( "branding/icon.png" ) ;
58
- if let Some ( initial_count) = std:: env:: args ( )
59
- . nth ( 1 )
60
- . and_then ( |arg| arg. parse :: < u128 > ( ) . ok ( ) )
61
- {
67
+ if scheduled. wave > 0 {
62
68
spawn_birds (
63
69
& mut commands,
64
70
& windows,
65
71
& mut counter,
66
- initial_count ,
67
- texture . clone_weak ( ) ,
72
+ scheduled . per_wave ,
73
+ bird_texture . 0 . clone_weak ( ) ,
68
74
) ;
75
+ counter. color = Color :: rgb_linear ( random ( ) , random ( ) , random ( ) ) ;
76
+ scheduled. wave -= 1 ;
69
77
}
78
+ }
79
+
80
+ struct BirdTexture ( Handle < Image > ) ;
81
+
82
+ fn setup ( mut commands : Commands , asset_server : Res < AssetServer > ) {
83
+ let texture = asset_server. load ( "branding/icon.png" ) ;
84
+
70
85
commands. spawn_bundle ( OrthographicCameraBundle :: new_2d ( ) ) ;
71
86
commands. spawn_bundle ( UiCameraBundle :: default ( ) ) ;
72
87
commands. spawn_bundle ( TextBundle {
@@ -120,6 +135,16 @@ fn setup(
120
135
} ) ;
121
136
122
137
commands. insert_resource ( BirdTexture ( texture) ) ;
138
+ commands. insert_resource ( BirdScheduled {
139
+ per_wave : std:: env:: args ( )
140
+ . nth ( 1 )
141
+ . and_then ( |arg| arg. parse :: < u128 > ( ) . ok ( ) )
142
+ . unwrap_or_default ( ) ,
143
+ wave : std:: env:: args ( )
144
+ . nth ( 2 )
145
+ . and_then ( |arg| arg. parse :: < u128 > ( ) . ok ( ) )
146
+ . unwrap_or ( 1 ) ,
147
+ } ) ;
123
148
}
124
149
125
150
fn mouse_handler (
@@ -131,7 +156,7 @@ fn mouse_handler(
131
156
mut counter : ResMut < BevyCounter > ,
132
157
) {
133
158
if mouse_button_input. just_released ( MouseButton :: Left ) {
134
- counter. color = Color :: rgb ( random ( ) , random ( ) , random ( ) ) ;
159
+ counter. color = Color :: rgb_linear ( random ( ) , random ( ) , random ( ) ) ;
135
160
}
136
161
137
162
if mouse_button_input. pressed ( MouseButton :: Left ) {
@@ -141,7 +166,7 @@ fn mouse_handler(
141
166
& windows,
142
167
& mut counter,
143
168
spawn_count,
144
- bird_texture. 0 . clone ( ) ,
169
+ bird_texture. 0 . clone_weak ( ) ,
145
170
) ;
146
171
}
147
172
}
@@ -210,6 +235,9 @@ fn collision_system(windows: Res<Windows>, mut bird_query: Query<(&mut Bird, &Tr
210
235
if y_vel < 0. && y_pos - HALF_BIRD_SIZE < -half_height {
211
236
bird. velocity . y = -y_vel;
212
237
}
238
+ if y_pos + HALF_BIRD_SIZE > half_height && y_vel > 0.0 {
239
+ bird. velocity . y = 0.0 ;
240
+ }
213
241
}
214
242
}
215
243
@@ -227,15 +255,3 @@ fn counter_system(
227
255
}
228
256
} ;
229
257
}
230
-
231
- /// Generate a color modulation
232
- ///
233
- /// Because there is no `Mul<Color> for Color` instead `[f32; 3]` is
234
- /// used.
235
- fn _gen_color ( rng : & mut impl Rng ) -> [ f32 ; 3 ] {
236
- let r = rng. gen_range ( 0.2 ..1.0 ) ;
237
- let g = rng. gen_range ( 0.2 ..1.0 ) ;
238
- let b = rng. gen_range ( 0.2 ..1.0 ) ;
239
- let v = Vec3 :: new ( r, g, b) ;
240
- v. normalize ( ) . into ( )
241
- }
0 commit comments