Don't spawn fruits in other elements on the grid

This commit is contained in:
2022-08-21 13:08:35 +02:00
parent 84ed6d1ec1
commit b97e279d71
4 changed files with 32 additions and 17 deletions

14
Cargo.lock generated
View File

@@ -428,7 +428,7 @@ dependencies = [
[[package]]
name = "bevy_editor_pls"
version = "0.1.1"
source = "git+https://github.com/jakobhellermann/bevy_editor_pls#f327365d9bcab3b6f88abd38a00320fbf1c89e7e"
source = "git+https://github.com/jakobhellermann/bevy_editor_pls#9aa28e3f1b6b804eefeb4b976143ca075b4e8697"
dependencies = [
"bevy",
"bevy_editor_pls_core",
@@ -439,7 +439,7 @@ dependencies = [
[[package]]
name = "bevy_editor_pls_core"
version = "0.1.1"
source = "git+https://github.com/jakobhellermann/bevy_editor_pls#f327365d9bcab3b6f88abd38a00320fbf1c89e7e"
source = "git+https://github.com/jakobhellermann/bevy_editor_pls#9aa28e3f1b6b804eefeb4b976143ca075b4e8697"
dependencies = [
"bevy",
"bevy-inspector-egui",
@@ -449,7 +449,7 @@ dependencies = [
[[package]]
name = "bevy_editor_pls_default_windows"
version = "0.1.1"
source = "git+https://github.com/jakobhellermann/bevy_editor_pls#f327365d9bcab3b6f88abd38a00320fbf1c89e7e"
source = "git+https://github.com/jakobhellermann/bevy_editor_pls#9aa28e3f1b6b804eefeb4b976143ca075b4e8697"
dependencies = [
"bevy",
"bevy-inspector-egui",
@@ -3007,18 +3007,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.143"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.143"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
"proc-macro2",
"quote",

View File

@@ -29,14 +29,29 @@ fn eaten_event_sent(mut eaten_event_reader: EventReader<EatenEvent>) -> bool {
eaten_event_reader.iter().count() != 0
}
fn spawn_fruit_system(mut commands: Commands) {
// TODO: not spawn in snake
let mut rng = rand::thread_rng();
let coordinate_range = 0..grid::SIZE;
let fruit_coordinate = grid::Coordinate(
rng.gen_range(coordinate_range.clone()),
rng.gen_range(coordinate_range),
);
fn spawn_fruit_system(
mut commands: Commands,
coordinate_query: Query<&grid::Coordinate>,
) {
fn random_coordinate() -> grid::Coordinate {
let mut rng = rand::thread_rng();
let coordinate_range = 0..grid::SIZE;
grid::Coordinate(
rng.gen_range(coordinate_range.clone()),
rng.gen_range(coordinate_range),
)
}
let coordinate_in_other_element = |coordinate: grid::Coordinate| -> bool {
coordinate_query
.iter()
.any(|&snake_coordinate| snake_coordinate == coordinate)
};
let fruit_coordinate = std::iter::repeat(random_coordinate())
.find(|&coordinate| !coordinate_in_other_element(coordinate))
.expect("Should always be found.");
commands
.spawn_bundle(SpriteBundle {

View File

@@ -24,7 +24,7 @@ enum AppState {
fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb_u8(64, 64, 64)))
.insert_resource(ClearColor(Color::rgb_u8(12, 12, 12)))
.insert_resource(WindowDescriptor {
title: "Bevy-Snake".into(),
resizable: true,

View File

@@ -56,7 +56,7 @@ impl Plugin for SnakePlugin {
pub const Z_HEIGHT: f32 = 10.;
#[derive(Component)]
struct SnakeSegment;
pub struct SnakeSegment;
#[derive(Component)]
pub struct SnakeHead;