Проектная работа - WIP
This commit is contained in:
+25
-19
@@ -1,7 +1,7 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
domain::{Direction, End, GRID_SIZE, Head, SnakePath, Tail, Velocity},
|
||||
domain::{Direction, End, GRID_SIZE, GridBarier, Head, SnakePath, Tail, Velocity},
|
||||
tools::{Position, split_by_grid},
|
||||
};
|
||||
|
||||
@@ -11,32 +11,22 @@ pub fn head_rotation(
|
||||
query: Single<(&mut Transform, &Direction), With<Head>>,
|
||||
mut snake_path: ResMut<SnakePath>,
|
||||
) {
|
||||
use core::f32::consts::PI;
|
||||
|
||||
if input.just_pressed(KeyCode::ArrowLeft) {
|
||||
let (mut transform, direction) = query.into_inner();
|
||||
let next_pos = direction.next_pos(&*transform);
|
||||
let mut direction = direction.clone();
|
||||
direction.rotate_left();
|
||||
if let Some(prev_direction) = snake_path.insert(next_pos, direction) {
|
||||
if direction != prev_direction {
|
||||
transform.rotate_z(PI);
|
||||
}
|
||||
} else {
|
||||
transform.rotate_z(PI / 2.0);
|
||||
}
|
||||
snake_path.insert(next_pos, direction);
|
||||
transform.rotation = direction.orientation();
|
||||
trace!("go {direction} on {next_pos}");
|
||||
} else if input.just_pressed(KeyCode::ArrowRight) {
|
||||
let (mut transform, direction) = query.into_inner();
|
||||
let next_pos = direction.next_pos(&*transform);
|
||||
let mut direction = direction.clone();
|
||||
direction.rotate_right();
|
||||
if let Some(prev_direction) = snake_path.insert(next_pos, direction) {
|
||||
if direction != prev_direction {
|
||||
transform.rotate_z(-PI);
|
||||
}
|
||||
} else {
|
||||
transform.rotate_z(-PI / 2.0);
|
||||
}
|
||||
snake_path.insert(next_pos, direction);
|
||||
transform.rotation = direction.orientation();
|
||||
trace!("go {direction} on {next_pos}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,9 +34,11 @@ pub fn head_rotation(
|
||||
pub fn snake_moving(
|
||||
time: Res<Time<Fixed>>,
|
||||
snake_path: Res<SnakePath>,
|
||||
query: Query<(&mut Transform, &mut Direction, &Velocity), Or<(With<Head>, With<Tail>, With<End>)>>,
|
||||
query: Query<(&mut Transform, &mut Direction, &Velocity, Entity, Has<End>), Or<(With<Head>, With<Tail>, With<End>)>>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for (mut transform, mut direction, Velocity(velocity)) in query.into_iter() {
|
||||
for (mut transform, mut direction, Velocity(velocity), entity, has_end) in query.into_iter() {
|
||||
// первая часть движения - до границы сетки
|
||||
let delta = velocity * time.delta_secs();
|
||||
let src = direction.get_coord(&transform);
|
||||
let sign = direction.get_change_sign();
|
||||
@@ -54,6 +46,8 @@ pub fn snake_moving(
|
||||
let mut splited = split_by_grid(src, dst, GRID_SIZE as f32).into_iter();
|
||||
let first = splited.next().unwrap_or((0.0, 0.0));
|
||||
direction.set_coord(&mut transform, first.0);
|
||||
|
||||
// вторая часть движения - после границы сетки
|
||||
if let Some(second) = splited.next() {
|
||||
let delta = second.1.abs();
|
||||
let pos = Position::from(&*transform);
|
||||
@@ -65,6 +59,18 @@ pub fn snake_moving(
|
||||
let sign = direction.get_change_sign();
|
||||
let dst = src + sign * delta;
|
||||
direction.set_coord(&mut transform, dst);
|
||||
|
||||
// создать событие прохождения границы сетки
|
||||
commands.entity(entity).trigger(|e| GridBarier::new(e, pos, (&*direction).clone()));
|
||||
|
||||
// если это кончик хвоста, то нужно очистить [SnakePath] за собой
|
||||
if has_end {
|
||||
commands.queue(move |world: &mut World| {
|
||||
if let Some(mut snake_path) = world.get_resource_mut::<SnakePath>() {
|
||||
snake_path.remove(&pos);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user