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

This commit is contained in:
4 changed files with 64 additions and 48 deletions
+4 -4
View File
@@ -1,9 +1,9 @@
use bevy::prelude::*;
use crate::domain::{Direction, GRID_SIZE, GridBarier, Head, Inert, SnakePath, Velocity};
mod tools;
use tools::*;
use crate::{
domain::{Direction, GRID_SIZE, GridBarier, Head, Inert, SnakePath, Velocity},
tools::split_by_grid,
};
/// Система поворота головы змеи
pub fn head_rotation(
+1
View File
@@ -40,6 +40,7 @@ pub fn define_log_layer() -> Option<bevy::log::BoxedFmtLayer> {
mod domain;
mod gameplay;
mod startup;
mod tools;
pub use gameplay::{head_rotation, moving};
pub use startup::{print_all_entities, setup_frame_rate, startup};
+8 -43
View File
@@ -1,7 +1,10 @@
use bevy::{ecs::observer::ObservedBy, prelude::*, winit::WinitSettings};
use bevy::{prelude::*, winit::WinitSettings};
use std::time::Duration;
use crate::domain::{End, Grid, GridBarier, SnakePath, Tail};
use crate::{
domain::{End, Grid, SnakePath, Tail},
tools::{end_observer, schedule_set_velocity},
};
/// Настройка частоты кадров
pub fn setup_frame_rate(mut winit: ResMut<WinitSettings>) {
@@ -37,7 +40,7 @@ fn spawn_snake(commands: &mut Commands) {
vec2(HEAD_SIZE as f32, HEAD_SIZE as f32),
),
Transform {
translation: vec3(0.0, 0.0, 0.0),
translation: vec3(0.0, 0.0, 1.0),
..Default::default()
},
))
@@ -82,46 +85,8 @@ fn spawn_snake(commands: &mut Commands) {
},
))
.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>();
},
);
commands
.entity(tail)
.observe(
|event: On<GridBarier>,
mut query: Query<(&mut Direction, &Transform)>,
snake_path: Res<SnakePath>| {
if let Ok((mut direction, transform)) = query.get_mut(event.0) {
if let Some(new_direction) = snake_path.pick(transform) {
trace!("change direction for {:?} to {:?}", event.0, new_direction);
*direction = new_direction;
}
}
},
)
.observe(
|event: On<GridBarier>,
query: Query<(Entity, &Transform), With<End>>,
mut snake_path: ResMut<SnakePath>| {
if let Ok((entity, transform)) = query.get(event.0) {
if let Some(removed) = snake_path.pop(transform) {
trace!(
"removed direction {:?} from snake_path by End entity {:?}",
removed, entity
);
}
}
},
);
commands.entity(head).observe(schedule_set_velocity(tail));
commands.entity(tail).observe(end_observer);
}
/// Создать координатную сетку
@@ -1,3 +1,7 @@
use bevy::{ecs::observer::ObservedBy, prelude::*};
use crate::domain::*;
/// Разбиение изменения координаты по границам сетки
pub fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
if grid <= 0.0 {
@@ -36,9 +40,55 @@ pub fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
output
}
#[allow(unused)]
/// Слушатель событий [GridBarier] для хвоста, обновляющий направление его движения из ресурса [SnakePath]
pub fn tail_observer(
event: On<GridBarier>,
mut query: Query<(&mut Direction, &Transform), Without<End>>,
snake_path: Res<SnakePath>,
) {
if let Ok((mut direction, transform)) = query.get_mut(event.0) {
if let Some(new_direction) = snake_path.pick(transform) {
trace!("change direction for {:?} to {:?}", event.0, new_direction);
*direction = new_direction;
}
}
}
/// Слушатель событий [GridBarier] для конца змеи, обновляющий направление его движения из ресурса [SnakePath] и удаляющий полученные значения из [SnakePath]
pub fn end_observer(
event: On<GridBarier>,
mut query: Query<(&mut Direction, &Transform), With<End>>,
mut snake_path: ResMut<SnakePath>,
) {
if let Ok((mut direction, transform)) = query.get_mut(event.0) {
if let Some(new_direction) = snake_path.pop(transform) {
trace!(
"change direction for {:?} to {:?} and remove it from SnakePath",
event.0, new_direction
);
*direction = new_direction;
}
}
}
/// Запланировать установку скорости движения [Velocity] при следующем событии [GridBarier]
pub fn schedule_set_velocity(
tail: Entity,
) -> impl Fn(On<GridBarier>, Commands, Query<&mut Velocity, With<Tail>>) {
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>();
}
}
#[cfg(test)]
mod test {
use crate::gameplay::*;
use crate::tools::*;
#[test]
fn test_split_by_grid() {