Проектная работа - WIP
This commit is contained in:
@@ -76,7 +76,7 @@ pub fn spawn_snake(commands: &mut Commands) {
|
|||||||
End,
|
End,
|
||||||
Direction::default(),
|
Direction::default(),
|
||||||
Velocity(0.0),
|
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 {
|
Transform {
|
||||||
translation: vec3(0.0, 0.0, 0.0),
|
translation: vec3(0.0, 0.0, 0.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|||||||
@@ -49,10 +49,15 @@ pub fn head_moving(
|
|||||||
let _span = trace_span!("head_moving").entered();
|
let _span = trace_span!("head_moving").entered();
|
||||||
for (mut transform, mut direction, Velocity(velocity), head, entity) in query.into_iter() {
|
for (mut transform, mut direction, Velocity(velocity), head, entity) in query.into_iter() {
|
||||||
let next_dir = head.0.clone();
|
let next_dir = head.0.clone();
|
||||||
|
let dir = direction.clone();
|
||||||
let snake_path = &mut snake_path;
|
let snake_path = &mut snake_path;
|
||||||
let switch_direction = move |pos: Position| {
|
let switch_direction = move |pos: Position| {
|
||||||
snake_path.insert(pos.clone(), next_dir.clone());
|
if next_dir != dir {
|
||||||
Some(next_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);
|
moving(velocity, &time, &mut direction, &mut transform, entity, &mut commands, switch_direction);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,9 @@ pub fn clean_snake_path(event: On<GridBarier>, query: Query<&End>, mut snake_pat
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Обработка события [GrowUp]
|
/// Обработка события [GrowUp]
|
||||||
pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entity, &Direction, &Transform), (With<Tail>, With<End>)>) {
|
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) = query.into_inner();
|
let (entity, direction, transform, mut sprite) = query.into_inner();
|
||||||
|
sprite.color = GRAY_300.into();
|
||||||
commands.entity(entity).remove::<End>();
|
commands.entity(entity).remove::<End>();
|
||||||
let pos = Position::from(transform);
|
let pos = Position::from(transform);
|
||||||
let new_end = commands
|
let new_end = commands
|
||||||
@@ -34,7 +35,7 @@ pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entit
|
|||||||
End,
|
End,
|
||||||
direction.clone(),
|
direction.clone(),
|
||||||
Velocity(0.0),
|
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 {
|
Transform {
|
||||||
translation: pos.translation().extend(0.0),
|
translation: pos.translation().extend(0.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ pub fn startup(mut commands: Commands) {
|
|||||||
commands.insert_resource(ClearColor(SKY_600.into()));
|
commands.insert_resource(ClearColor(SKY_600.into()));
|
||||||
commands.insert_resource(SnakePath::default());
|
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::grow_up_snake));
|
||||||
commands.spawn(Observer::new(crate::observers::handle_collisions));
|
commands.spawn(Observer::new(crate::observers::handle_collisions));
|
||||||
|
|
||||||
|
|||||||
@@ -49,9 +49,7 @@ pub fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
|
|||||||
let mut src: f32 = src;
|
let mut src: f32 = src;
|
||||||
for step in old_grid_right as i32..new_grid_right as i32 {
|
for step in old_grid_right as i32..new_grid_right as i32 {
|
||||||
let step = step as f32 * grid;
|
let step = step as f32 * grid;
|
||||||
// if step != src {
|
|
||||||
output.push((step, step - src));
|
output.push((step, step - src));
|
||||||
// }
|
|
||||||
src = step;
|
src = step;
|
||||||
}
|
}
|
||||||
output.push((dst, dst - src));
|
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;
|
let mut dst: f32 = dst;
|
||||||
for step in new_grid_right as i32..old_grid_right as i32 {
|
for step in new_grid_right as i32..old_grid_right as i32 {
|
||||||
let step = step as f32 * grid;
|
let step = step as f32 * grid;
|
||||||
// if step != dst {
|
|
||||||
output.push((dst, dst - step));
|
output.push((dst, dst - step));
|
||||||
// }
|
|
||||||
dst = step;
|
dst = step;
|
||||||
}
|
}
|
||||||
output.push((dst, dst - src));
|
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(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)]);
|
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)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user