Проектная работа - WIP

This commit is contained in:
8 changed files with 403 additions and 30 deletions
+46 -19
View File
@@ -1,8 +1,7 @@
use bevy::{ecs::observer::ObservedBy, prelude::*, winit::WinitSettings};
use std::time::Duration;
use bevy::{prelude::*, winit::WinitSettings};
use crate::domain::Tail;
use crate::domain::{End, Grid, GridBarier, SnakePath, Tail};
/// Настройка частоты кадров
pub fn setup_frame_rate(mut winit: ResMut<WinitSettings>) {
@@ -25,6 +24,8 @@ pub fn startup(mut commands: Commands) {
fn spawn_snake(commands: &mut Commands) {
use crate::domain::{Direction, HEAD_SIZE, Head, Inert, SNAKE_VELOCITY, Velocity};
commands.insert_resource(SnakePath::default());
let head = commands
.spawn((
Head,
@@ -64,43 +65,69 @@ fn spawn_snake(commands: &mut Commands) {
ChildOf(head),
));
commands.spawn((
Tail(0),
Direction::default(),
Inert::default(),
Velocity(0.0),
Sprite::from_color(
Color::srgb(0.7, 0.7, 0.7),
vec2(HEAD_SIZE as f32 * 0.8, HEAD_SIZE as f32 * 0.8),
),
Transform {
translation: vec3(0.0, 0.0, 0.0),
..Default::default()
let tail = commands
.spawn((
Tail,
End,
Direction::default(),
Inert::default(),
Velocity(0.0),
Sprite::from_color(
Color::srgb(0.7, 0.7, 0.7),
vec2(HEAD_SIZE as f32 * 0.8, HEAD_SIZE as f32 * 0.8),
),
Transform {
translation: vec3(0.0, 0.0, 0.0),
..Default::default()
},
))
.id();
commands.entity(head).observe(
move |event: On<GridBarier>,
mut commands: Commands,
mut query: Query<&mut Velocity, With<Tail>>| {
if let Ok(mut velocity) = query.get_mut(tail) {
velocity.0 = SNAKE_VELOCITY as f32;
}
commands.entity(event.0).remove::<ObservedBy>();
},
// ChildOf(head),
));
);
}
/// Создать координатную сетку
fn spawn_grid(commands: &mut Commands) {
use crate::domain::GRID_SIZE;
let grid = commands
.spawn((Grid, Transform::default(), Visibility::default()))
.id();
for x in -20..=20 {
commands.spawn((
Sprite::from_color(Color::srgb(1.0, 1.0, 0.5), vec2(1.0, 2000.0)),
Sprite::from_color(Color::srgb(1.0, 1.0, 0.5), vec2(0.5, 2000.0)),
Transform {
translation: vec3(x as f32 * GRID_SIZE as f32, 0.0, -1.0),
..Default::default()
},
ChildOf(grid),
));
}
for y in -20..=20 {
commands.spawn((
Sprite::from_color(Color::srgb(1.0, 1.0, 0.5), vec2(2000.0, 1.0)),
Sprite::from_color(Color::srgb(1.0, 1.0, 0.5), vec2(2000.0, 0.5)),
Transform {
translation: vec3(0.0, y as f32 * GRID_SIZE as f32, -1.0),
..Default::default()
},
ChildOf(grid),
));
}
}
/// ДЛЯ ОТЛАДКИ - вывод всех сущностей мира
pub fn print_all_entities(mut commands: Commands, query: Query<Entity>) {
for entity in query {
commands.entity(entity).log_components();
}
}