Skip to content

Commit 4ca35b8

Browse files
committed
Changed trash text length to more likely be shorter
1 parent 0e5e02e commit 4ca35b8

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/game.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,29 @@ pub fn spawn_trash(
180180
mut previous_spawn_position: Local<f32>,
181181
) {
182182

183+
static TRASH_WORD_LENGTH_CHANCES: [u8; 20] = [
184+
2,
185+
2,
186+
3,
187+
3,
188+
3,
189+
3,
190+
4,
191+
4,
192+
4,
193+
4,
194+
4,
195+
4,
196+
4,
197+
4,
198+
5,
199+
5,
200+
6,
201+
6,
202+
7,
203+
8,
204+
];
205+
183206
static SPAWN_CHANCES: [TrashType; 9] = [
184207
TrashType::Bottle,
185208
TrashType::Pizza,
@@ -212,8 +235,10 @@ pub fn spawn_trash(
212235

213236
let random_x = get_random_coordinate(max_x, *previous_spawn_position);
214237
*previous_spawn_position = random_x;
238+
215239
let trash_type: TrashType = SPAWN_CHANCES[random.gen_range(0..SPAWN_CHANCES.len())].clone();
216240
let power_up: PowerUp = POWER_UP_CHANCES[random.gen_range(0..POWER_UP_CHANCES.len())].clone();
241+
let word_length = TRASH_WORD_LENGTH_CHANCES[random.gen_range(0..TRASH_WORD_LENGTH_CHANCES.len())];
217242

218243
// TODO: make sure the same word doesn't appear twice in a row
219244
// A solution might be to have search to search for a word as long as it's not in a list of
@@ -224,9 +249,8 @@ pub fn spawn_trash(
224249
trash.power_up = power_up;
225250
}
226251

227-
let word = get_random_word(&available_words);
228252
let trash_text = TrashBundle::create_text(
229-
word.clone(),
253+
get_random_word_with_length(&available_words, word_length),
230254
Anchor::Custom(Vec2::new(0.0, -2.0)),
231255
Color::GREEN,
232256
TextStyle {
@@ -369,6 +393,16 @@ fn delete_all_play_entities(
369393
}
370394
}
371395

396+
fn get_random_word_with_length(available_words: &Res<AvailableWords>, length: u8) -> String {
397+
let mut word = "".to_string();
398+
399+
while word.len() != length as usize {
400+
word = get_random_word(available_words);
401+
}
402+
403+
word
404+
}
405+
372406
fn get_random_word(available_words: &Res<AvailableWords>) -> String {
373407
let mut random = rand::thread_rng();
374408
let mut random_words: Vec<String> = Vec::new();

0 commit comments

Comments
 (0)