|
| 1 | +use bevy::core::FixedTimestep; |
1 | 2 | use bevy::prelude::*; |
| 3 | +use rand::prelude::random; |
2 | 4 |
|
3 | 5 | 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); |
4 | 7 |
|
5 | 8 | const ARENA_HEIGHT: u32 = 10; |
6 | 9 | const ARENA_WIDTH: u32 = 10; |
@@ -28,6 +31,9 @@ impl Size { |
28 | 31 | #[derive(Component)] |
29 | 32 | struct SnakeHead; |
30 | 33 |
|
| 34 | +#[derive(Component)] |
| 35 | +struct Food; |
| 36 | + |
31 | 37 | fn setup_camera(mut commands: Commands) { |
32 | 38 | commands.spawn_bundle(OrthographicCameraBundle::new_2d()); |
33 | 39 | } |
@@ -96,18 +102,40 @@ fn position_translation(windows: Res<Windows>, mut q: Query<(&Position, &mut Tra |
96 | 102 | } |
97 | 103 | } |
98 | 104 |
|
| 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 | + |
99 | 122 | fn main() { |
100 | 123 | App::new() |
101 | 124 | .insert_resource(ClearColor(Color::rgb(0.04, 0.04, 0.04))) |
102 | 125 | .insert_resource(WindowDescriptor { |
103 | 126 | title: "Snake!".to_string(), |
104 | 127 | width: 500.0, |
105 | 128 | height: 500.0, |
106 | | - ..Default::default() |
| 129 | + ..default() |
107 | 130 | }) |
108 | 131 | .add_startup_system(setup_camera) |
109 | 132 | .add_startup_system(spawn_snake) |
110 | 133 | .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 | + ) |
111 | 139 | .add_system_set_to_stage( |
112 | 140 | CoreStage::PostUpdate, |
113 | 141 | SystemSet::new() |
|
0 commit comments