diff --git a/snake-game/src/debug.rs b/snake-game/src/debug.rs index 880b1b9..fa0c855 100644 --- a/snake-game/src/debug.rs +++ b/snake-game/src/debug.rs @@ -71,8 +71,9 @@ pub fn spawn_grid(commands: &mut Commands) { for x in -20..=20 { let color = if x % 5 == 0 { Color::srgb(1.0, 0.2, 0.5) } else { Color::srgb(1.0, 1.0, 0.5) }; + let weight = if x % 5 == 0 { 1.0 } else { 0.5 }; commands.spawn(( - Sprite::from_color(color, vec2(0.5, 2000.0)), + Sprite::from_color(color, vec2(weight, 2000.0)), Transform { translation: vec3(x as f32 * GRID_SIZE as f32, 0.0, -1.0), ..Default::default() @@ -82,8 +83,9 @@ pub fn spawn_grid(commands: &mut Commands) { } for y in -20..=20 { let color = if y % 5 == 0 { Color::srgb(1.0, 0.2, 0.5) } else { Color::srgb(1.0, 1.0, 0.5) }; + let weight = if y % 5 == 0 { 1.0 } else { 0.5 }; commands.spawn(( - Sprite::from_color(color, vec2(2000.0, 0.5)), + Sprite::from_color(color, vec2(2000.0, weight)), Transform { translation: vec3(0.0, y as f32 * GRID_SIZE as f32, -1.0), ..Default::default() diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs index 05b6df1..542935b 100644 --- a/snake-game/src/domain.rs +++ b/snake-game/src/domain.rs @@ -88,13 +88,13 @@ impl Direction { } } - /// Получить следующую по направлению координатную позицию - pub fn next_pos(&self, transform: &Transform) -> Position { + /// Получить следующую позицию для текущего направления + pub fn dst_pos(&self, transform: &Transform) -> Position { let mut pos = Position::from(transform); match self { Direction::Up => pos.y += 1, - Direction::Left => pos.x -= 1, - Direction::Down => pos.y -= 1, + Direction::Left => (), + Direction::Down => (), Direction::Right => pos.x += 1, }; pos diff --git a/snake-game/src/gameplay.rs b/snake-game/src/gameplay.rs index fbf6b57..f71e13b 100644 --- a/snake-game/src/gameplay.rs +++ b/snake-game/src/gameplay.rs @@ -13,20 +13,24 @@ pub fn head_rotation( ) { 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(); - snake_path.insert(next_pos, direction); - transform.rotation = direction.orientation(); - trace!("go {direction} on {next_pos}"); + let translation = transform.translation.clone(); + let cur_pos = Position::from(&*transform); + let dst_pos = direction.dst_pos(&*transform); + let mut new_direction = direction.clone(); + new_direction.rotate_left(); + snake_path.insert(dst_pos, new_direction); + transform.rotation = new_direction.orientation(); + trace!("running {direction} at {cur_pos} {translation}: go {new_direction} on {dst_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(); - snake_path.insert(next_pos, direction); - transform.rotation = direction.orientation(); - trace!("go {direction} on {next_pos}"); + let translation = transform.translation.clone(); + let cur_pos = Position::from(&*transform); + let dst_pos = direction.dst_pos(&*transform); + let mut new_direction = direction.clone(); + new_direction.rotate_right(); + snake_path.insert(dst_pos, new_direction); + transform.rotation = new_direction.orientation(); + trace!("running {direction} at {cur_pos} {translation}: go {new_direction} on {dst_pos}"); } } @@ -52,7 +56,7 @@ pub fn snake_moving( let delta = second.1.abs(); let pos = Position::from(&*transform); if let Some(dir) = snake_path.get(&pos) { - trace!("set new direction in {pos:?} --> {dir:?}"); + trace!("set new direction for {entity} in {pos} --> {dir}"); *direction = dir.clone(); } let src = direction.get_coord(&*transform); diff --git a/snake-game/src/tools/position.rs b/snake-game/src/tools/position.rs index 7072836..9e41236 100644 --- a/snake-game/src/tools/position.rs +++ b/snake-game/src/tools/position.rs @@ -25,8 +25,8 @@ impl Display for Position { impl> From for Position { fn from(value: T) -> Self { Self { - x: (value.translation.x / GRID_SIZE as f32) as i8, - y: (value.translation.y / GRID_SIZE as f32) as i8, + x: (value.translation.x / GRID_SIZE as f32).floor() as i8, + y: (value.translation.y / GRID_SIZE as f32).floor() as i8, } } }