Skip to content

Commit d7026c3

Browse files
committed
Food spawning
1 parent 3a3d707 commit d7026c3

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/main.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use bevy::core::FixedTimestep;
12
use bevy::prelude::*;
3+
use rand::prelude::random;
24

35
const SNAKE_HEAD_COLOR: Color = Color::rgb(0.7, 0.7, 0.7);
6+
const FOOD_COLOR: Color = Color::rgb(1.0, 0.0, 1.0);
47

58
const ARENA_HEIGHT: u32 = 10;
69
const ARENA_WIDTH: u32 = 10;
@@ -28,6 +31,9 @@ impl Size {
2831
#[derive(Component)]
2932
struct SnakeHead;
3033

34+
#[derive(Component)]
35+
struct Food;
36+
3137
fn setup_camera(mut commands: Commands) {
3238
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
3339
}
@@ -96,18 +102,40 @@ fn position_translation(windows: Res<Windows>, mut q: Query<(&Position, &mut Tra
96102
}
97103
}
98104

105+
fn food_spawner(mut commands: Commands) {
106+
commands
107+
.spawn_bundle(SpriteBundle {
108+
sprite: Sprite {
109+
color: FOOD_COLOR,
110+
..default()
111+
},
112+
..default()
113+
})
114+
.insert(Food)
115+
.insert(Position {
116+
x: (random::<f32>() * ARENA_WIDTH as f32) as i32,
117+
y: (random::<f32>() * ARENA_HEIGHT as f32) as i32,
118+
})
119+
.insert(Size::square(0.8));
120+
}
121+
99122
fn main() {
100123
App::new()
101124
.insert_resource(ClearColor(Color::rgb(0.04, 0.04, 0.04)))
102125
.insert_resource(WindowDescriptor {
103126
title: "Snake!".to_string(),
104127
width: 500.0,
105128
height: 500.0,
106-
..Default::default()
129+
..default()
107130
})
108131
.add_startup_system(setup_camera)
109132
.add_startup_system(spawn_snake)
110133
.add_system(snake_movement)
134+
.add_system_set(
135+
SystemSet::new()
136+
.with_run_criteria(FixedTimestep::step(1.0))
137+
.with_system(food_spawner),
138+
)
111139
.add_system_set_to_stage(
112140
CoreStage::PostUpdate,
113141
SystemSet::new()

0 commit comments

Comments
 (0)