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

This commit is contained in:
4 changed files with 27 additions and 21 deletions
+4 -2
View File
@@ -71,8 +71,9 @@ pub fn spawn_grid(commands: &mut Commands) {
for x in -20..=20 { 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 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(( commands.spawn((
Sprite::from_color(color, vec2(0.5, 2000.0)), Sprite::from_color(color, vec2(weight, 2000.0)),
Transform { Transform {
translation: vec3(x as f32 * GRID_SIZE as f32, 0.0, -1.0), translation: vec3(x as f32 * GRID_SIZE as f32, 0.0, -1.0),
..Default::default() ..Default::default()
@@ -82,8 +83,9 @@ pub fn spawn_grid(commands: &mut Commands) {
} }
for y in -20..=20 { 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 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(( commands.spawn((
Sprite::from_color(color, vec2(2000.0, 0.5)), Sprite::from_color(color, vec2(2000.0, weight)),
Transform { Transform {
translation: vec3(0.0, y as f32 * GRID_SIZE as f32, -1.0), translation: vec3(0.0, y as f32 * GRID_SIZE as f32, -1.0),
..Default::default() ..Default::default()
+4 -4
View File
@@ -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); let mut pos = Position::from(transform);
match self { match self {
Direction::Up => pos.y += 1, Direction::Up => pos.y += 1,
Direction::Left => pos.x -= 1, Direction::Left => (),
Direction::Down => pos.y -= 1, Direction::Down => (),
Direction::Right => pos.x += 1, Direction::Right => pos.x += 1,
}; };
pos pos
+17 -13
View File
@@ -13,20 +13,24 @@ pub fn head_rotation(
) { ) {
if input.just_pressed(KeyCode::ArrowLeft) { if input.just_pressed(KeyCode::ArrowLeft) {
let (mut transform, direction) = query.into_inner(); let (mut transform, direction) = query.into_inner();
let next_pos = direction.next_pos(&*transform); let translation = transform.translation.clone();
let mut direction = direction.clone(); let cur_pos = Position::from(&*transform);
direction.rotate_left(); let dst_pos = direction.dst_pos(&*transform);
snake_path.insert(next_pos, direction); let mut new_direction = direction.clone();
transform.rotation = direction.orientation(); new_direction.rotate_left();
trace!("go {direction} on {next_pos}"); 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) { } else if input.just_pressed(KeyCode::ArrowRight) {
let (mut transform, direction) = query.into_inner(); let (mut transform, direction) = query.into_inner();
let next_pos = direction.next_pos(&*transform); let translation = transform.translation.clone();
let mut direction = direction.clone(); let cur_pos = Position::from(&*transform);
direction.rotate_right(); let dst_pos = direction.dst_pos(&*transform);
snake_path.insert(next_pos, direction); let mut new_direction = direction.clone();
transform.rotation = direction.orientation(); new_direction.rotate_right();
trace!("go {direction} on {next_pos}"); 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 delta = second.1.abs();
let pos = Position::from(&*transform); let pos = Position::from(&*transform);
if let Some(dir) = snake_path.get(&pos) { 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(); *direction = dir.clone();
} }
let src = direction.get_coord(&*transform); let src = direction.get_coord(&*transform);
+2 -2
View File
@@ -25,8 +25,8 @@ impl Display for Position {
impl<T: Deref<Target = Transform>> From<T> for Position { impl<T: Deref<Target = Transform>> From<T> for Position {
fn from(value: T) -> Self { fn from(value: T) -> Self {
Self { Self {
x: (value.translation.x / 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) as i8, y: (value.translation.y / GRID_SIZE as f32).floor() as i8,
} }
} }
} }