Проектная работа - WIP
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::domain::{Direction, GRID_SIZE, GridBarier, Head, Inert, SnakePath, Velocity};
|
use crate::{
|
||||||
|
domain::{Direction, GRID_SIZE, GridBarier, Head, Inert, SnakePath, Velocity},
|
||||||
mod tools;
|
tools::split_by_grid,
|
||||||
use tools::*;
|
};
|
||||||
|
|
||||||
/// Система поворота головы змеи
|
/// Система поворота головы змеи
|
||||||
pub fn head_rotation(
|
pub fn head_rotation(
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ pub fn define_log_layer() -> Option<bevy::log::BoxedFmtLayer> {
|
|||||||
mod domain;
|
mod domain;
|
||||||
mod gameplay;
|
mod gameplay;
|
||||||
mod startup;
|
mod startup;
|
||||||
|
mod tools;
|
||||||
|
|
||||||
pub use gameplay::{head_rotation, moving};
|
pub use gameplay::{head_rotation, moving};
|
||||||
pub use startup::{print_all_entities, setup_frame_rate, startup};
|
pub use startup::{print_all_entities, setup_frame_rate, startup};
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
use bevy::{ecs::observer::ObservedBy, prelude::*, winit::WinitSettings};
|
use bevy::{prelude::*, winit::WinitSettings};
|
||||||
use std::time::Duration;
|
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>) {
|
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),
|
vec2(HEAD_SIZE as f32, HEAD_SIZE as f32),
|
||||||
),
|
),
|
||||||
Transform {
|
Transform {
|
||||||
translation: vec3(0.0, 0.0, 0.0),
|
translation: vec3(0.0, 0.0, 1.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
@@ -82,46 +85,8 @@ fn spawn_snake(commands: &mut Commands) {
|
|||||||
},
|
},
|
||||||
))
|
))
|
||||||
.id();
|
.id();
|
||||||
|
commands.entity(head).observe(schedule_set_velocity(tail));
|
||||||
commands.entity(head).observe(
|
commands.entity(tail).observe(end_observer);
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Создать координатную сетку
|
/// Создать координатную сетку
|
||||||
|
|||||||
@@ -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)> {
|
pub fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
|
||||||
if grid <= 0.0 {
|
if grid <= 0.0 {
|
||||||
@@ -36,9 +40,55 @@ pub fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
|
|||||||
output
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::gameplay::*;
|
use crate::tools::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_split_by_grid() {
|
fn test_split_by_grid() {
|
||||||
Reference in New Issue
Block a user