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

This commit is contained in:
5 changed files with 20 additions and 11 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ pub fn spawn_snake(commands: &mut Commands) {
End,
Direction::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)),
Sprite::from_color(YELLOW_500, vec2(HEAD_SIZE as f32 * 0.8, HEAD_SIZE as f32 * 0.8)),
Transform {
translation: vec3(0.0, 0.0, 0.0),
..Default::default()
+7 -2
View File
@@ -49,10 +49,15 @@ pub fn head_moving(
let _span = trace_span!("head_moving").entered();
for (mut transform, mut direction, Velocity(velocity), head, entity) in query.into_iter() {
let next_dir = head.0.clone();
let dir = direction.clone();
let snake_path = &mut snake_path;
let switch_direction = move |pos: Position| {
snake_path.insert(pos.clone(), next_dir.clone());
Some(next_dir)
if next_dir != dir {
snake_path.insert(pos.clone(), next_dir.clone());
Some(next_dir)
} else {
None
}
};
moving(velocity, &time, &mut direction, &mut transform, entity, &mut commands, switch_direction);
}
+4 -3
View File
@@ -24,8 +24,9 @@ pub fn clean_snake_path(event: On<GridBarier>, query: Query<&End>, mut snake_pat
}
/// Обработка события [GrowUp]
pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entity, &Direction, &Transform), (With<Tail>, With<End>)>) {
let (entity, direction, transform) = query.into_inner();
pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entity, &Direction, &Transform, &mut Sprite), (With<Tail>, With<End>)>) {
let (entity, direction, transform, mut sprite) = query.into_inner();
sprite.color = GRAY_300.into();
commands.entity(entity).remove::<End>();
let pos = Position::from(transform);
let new_end = commands
@@ -34,7 +35,7 @@ pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entit
End,
direction.clone(),
Velocity(0.0),
Sprite::from_color(Color::srgb(0.75, 0.75, 0.75), vec2(HEAD_SIZE as f32 * 0.8, HEAD_SIZE as f32 * 0.8)),
Sprite::from_color(YELLOW_500, vec2(HEAD_SIZE as f32 * 0.8, HEAD_SIZE as f32 * 0.8)),
Transform {
translation: pos.translation().extend(0.0),
..Default::default()
+1 -1
View File
@@ -20,7 +20,7 @@ pub fn startup(mut commands: Commands) {
commands.insert_resource(ClearColor(SKY_600.into()));
commands.insert_resource(SnakePath::default());
commands.spawn(Observer::new(crate::observers::clean_snake_path));
// commands.spawn(Observer::new(crate::observers::clean_snake_path));
commands.spawn(Observer::new(crate::observers::grow_up_snake));
commands.spawn(Observer::new(crate::observers::handle_collisions));
+7 -4
View File
@@ -49,9 +49,7 @@ pub 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;
// if step != src {
output.push((step, step - src));
// }
src = step;
}
output.push((dst, dst - src));
@@ -59,9 +57,7 @@ pub 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;
// if step != dst {
output.push((dst, dst - step));
// }
dst = step;
}
output.push((dst, dst - src));
@@ -106,4 +102,11 @@ mod test {
assert_eq!(split_by_grid(0.0, -1.5, 5.0), vec![(0.0, 0.0), (-1.5, -1.5)]);
assert_eq!(split_by_grid(5.0, 3.0, 5.0), vec![(5.0, 0.0), (3.0, -2.0)]);
}
#[test]
fn debug_split_by_grid() {
assert_eq!(split_by_grid(0.0, -1.5, 5.0), vec![(0.0, 0.0), (-1.5, -1.5)]);
// assert_eq!(split_by_grid(0.0, -1.0, 5.0), vec![(0.0, 0.0), (-1.0, -1.0)]);
// assert_eq!(split_by_grid(-5.0, -6.0, 5.0), vec![(-5.0, 0.0), (-6.0, -1.0)]);
}
}