Spawning food

This commit is contained in:
Timothy Warren 2020-12-24 14:14:18 -05:00
parent e15e900425
commit 23e8713b6d

View File

@ -1,5 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy::render::pass::ClearColor; use bevy::render::pass::ClearColor;
use rand::prelude::random;
use std::time::Duration;
const ARENA_WIDTH: u32 = 10; const ARENA_WIDTH: u32 = 10;
const ARENA_HEIGHT: u32 = 10; const ARENA_HEIGHT: u32 = 10;
@ -26,12 +28,23 @@ impl Size {
struct SnakeHead; struct SnakeHead;
struct Materials { struct Materials {
head_material: Handle<ColorMaterial>, head_material: Handle<ColorMaterial>,
food_material: Handle<ColorMaterial>,
}
struct Food;
struct FoodSpawnTimer(Timer);
impl Default for FoodSpawnTimer {
fn default() -> Self {
Self(Timer::new(Duration::from_millis(1000), true))
}
} }
fn setup(commands: &mut Commands, mut materials: ResMut<Assets<ColorMaterial>>) { fn setup(commands: &mut Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
commands.spawn(Camera2dBundle::default()); commands.spawn(Camera2dBundle::default());
commands.insert_resource(Materials { commands.insert_resource(Materials {
head_material: materials.add(Color::rgb(0.7, 0.7, 0.7).into()), head_material: materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
food_material: materials.add(Color::rgb(1.0, 0.0, 1.0).into()),
}); });
} }
@ -93,6 +106,27 @@ fn position_translation(windows: Res<Windows>, mut q: Query<(&Position, &mut Tra
} }
} }
fn food_spawner(
commands: &mut Commands,
materials: Res<Materials>,
time: Res<Time>,
mut timer: Local<FoodSpawnTimer>,
) {
if timer.0.tick(time.delta_seconds().finished()) {
commands
.spawn(SpriteBundle {
material: materials.food_material.clone(),
..Default::default()
})
.with(Food)
.with(Position {
x: (random::<f32>() * ARENA_WIDTH as f32) as i32,
y: (random::<f32>() * ARENA_HEIGHT as f32) as i32,
})
.with(Size::square(0.8));
}
}
fn main() { fn main() {
App::build() App::build()
.add_resource(ClearColor(Color::rgb(0.04, 0.04, 0.04))) .add_resource(ClearColor(Color::rgb(0.04, 0.04, 0.04)))
@ -107,6 +141,7 @@ fn main() {
.add_system(snake_movement.system()) .add_system(snake_movement.system())
.add_system(position_translation.system()) .add_system(position_translation.system())
.add_system(size_scaling.system()) .add_system(size_scaling.system())
.add_system(food_spawner.system())
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.run(); .run();
} }