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

This commit is contained in:
8 changed files with 403 additions and 30 deletions
+27 -7
View File
@@ -1,27 +1,36 @@
use bevy::prelude::*;
use crate::domain::{Direction, GRID_SIZE, Head, Inert, Velocity};
use crate::domain::{Direction, GRID_SIZE, GridBarier, Head, Inert, SnakePath, Velocity};
/// Система поворота головы змеи
pub fn head_rotation(
input: Res<ButtonInput<KeyCode>>,
query: Single<(&mut Transform, &mut Direction), With<Head>>,
mut snake_path: ResMut<SnakePath>,
) {
use core::f32::consts::PI;
let (mut transform, mut direction) = query.into_inner();
if input.just_pressed(KeyCode::ArrowLeft) {
transform.rotate_z(PI / 2.);
transform.rotate_z(PI / 2.0);
direction.rotate_left();
snake_path.push(&transform, direction.clone());
}
if input.just_pressed(KeyCode::ArrowRight) {
transform.rotate_z(-PI / 2.);
transform.rotate_z(-PI / 2.0);
direction.rotate_right();
snake_path.push(&transform, direction.clone());
}
}
pub fn moving(time: Res<Time>, query: Query<(&mut Transform, &mut Inert, &Direction, &Velocity)>) {
for (mut transform, mut inert, direction, Velocity(velocity)) in query.into_iter() {
/// Системе движения для всех движущихся сущностей
pub fn moving(
time: Res<Time>,
query: Query<(&mut Transform, &mut Inert, &Direction, &Velocity, Entity)>,
mut commands: Commands,
) {
for (mut transform, mut inert, direction, Velocity(velocity), entity) in query.into_iter() {
let delta = velocity * time.delta_secs();
let src = inert.0.get_coord(&transform);
let sign = inert.0.get_change_sign();
@@ -36,6 +45,7 @@ pub fn moving(time: Res<Time>, query: Query<(&mut Transform, &mut Inert, &Direct
let sign = inert.0.get_change_sign();
let dst = src + sign * delta;
inert.0.set_coord(&mut transform, dst);
commands.trigger(GridBarier(entity));
}
}
}
@@ -57,7 +67,9 @@ fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
let mut src: f32 = src;
for step in old_grid_right as i32..new_grid_right as i32 {
let step = step as f32 * grid;
output.push((step, step - src));
if step != src {
output.push((step, step - src));
}
src = step;
}
output.push((dst, dst - src));
@@ -65,7 +77,9 @@ fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
let mut dst: f32 = dst;
for step in new_grid_right as i32..old_grid_right as i32 {
let step = step as f32 * grid;
output.push((dst, dst - step));
if step != dst {
output.push((dst, dst - step));
}
dst = step;
}
output.push((dst, dst - src));
@@ -127,5 +141,11 @@ mod test {
// сценарии с нулевой сеткой
assert_eq!(split_by_grid(1.0, 4.0, 0.0), vec![(4.0, 3.0)]);
assert_eq!(split_by_grid(4.0, 1.0, 0.0), vec![(1.0, -3.0)]);
// Сценарии со стартом в узле сетки
assert_eq!(split_by_grid(0.0, 1.5, 5.0), vec![(1.5, 1.5)]);
assert_eq!(split_by_grid(5.0, 7.0, 5.0), vec![(7.0, 2.0)]);
assert_eq!(split_by_grid(0.0, -1.5, 5.0), vec![(-1.5, -1.5)]);
assert_eq!(split_by_grid(5.0, 3.0, 5.0), vec![(3.0, -2.0)]);
}
}