diff --git a/snake-game/src/core.rs b/snake-game/src/core.rs index f797b14..a4ed236 100644 --- a/snake-game/src/core.rs +++ b/snake-game/src/core.rs @@ -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() diff --git a/snake-game/src/fixed_update.rs b/snake-game/src/fixed_update.rs index 202434f..7ade808 100644 --- a/snake-game/src/fixed_update.rs +++ b/snake-game/src/fixed_update.rs @@ -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); } diff --git a/snake-game/src/observers.rs b/snake-game/src/observers.rs index 1b43a3d..b27bacf 100644 --- a/snake-game/src/observers.rs +++ b/snake-game/src/observers.rs @@ -24,8 +24,9 @@ pub fn clean_snake_path(event: On, query: Query<&End>, mut snake_pat } /// Обработка события [GrowUp] -pub fn grow_up_snake(_: On, mut commands: Commands, query: Single<(Entity, &Direction, &Transform), (With, With)>) { - let (entity, direction, transform) = query.into_inner(); +pub fn grow_up_snake(_: On, mut commands: Commands, query: Single<(Entity, &Direction, &Transform, &mut Sprite), (With, With)>) { + let (entity, direction, transform, mut sprite) = query.into_inner(); + sprite.color = GRAY_300.into(); commands.entity(entity).remove::(); let pos = Position::from(transform); let new_end = commands @@ -34,7 +35,7 @@ pub fn grow_up_snake(_: On, 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() diff --git a/snake-game/src/startup.rs b/snake-game/src/startup.rs index e519891..df60d3a 100644 --- a/snake-game/src/startup.rs +++ b/snake-game/src/startup.rs @@ -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)); diff --git a/snake-game/src/tools.rs b/snake-game/src/tools.rs index 13ce61e..91564f0 100644 --- a/snake-game/src/tools.rs +++ b/snake-game/src/tools.rs @@ -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)]); + } }