Files
rust-otus/snake-game/src/debug.rs
T

105 lines
3.5 KiB
Rust

//! модуль с инструментами отладки
use bevy::prelude::*;
use crate::domain::GridBarier;
/// вывод всех сущностей мира
pub fn print_all_entities(mut commands: Commands, query: Query<Entity>) {
for entity in query {
commands.entity(entity).log_components();
}
}
/// вывод всех сущностей мира по нажатию Enter
pub fn print_all_entities_by_press_enter(input: Res<ButtonInput<KeyCode>>, mut commands: Commands, query: Query<Entity>) {
if input.just_pressed(KeyCode::Enter) {
for entity in query {
commands.entity(entity).log_components();
}
}
}
/// регулировка скорости игры
pub fn time_speed(input: Res<ButtonInput<KeyCode>>, mut time: ResMut<Time<Virtual>>) {
if input.just_pressed(KeyCode::Equal) {
let mut speed = time.relative_speed();
speed *= 2.0;
time.set_relative_speed(speed);
trace!("time speed set x2.0");
} else if input.just_pressed(KeyCode::Minus) {
let mut speed = time.relative_speed();
speed *= 0.5;
time.set_relative_speed(speed);
trace!("time speed set x0.5");
} else if input.just_pressed(KeyCode::Backspace) {
time.set_relative_speed(1.0);
trace!("time speed reset");
} else if input.just_pressed(KeyCode::Space) {
if time.is_paused() {
time.unpause();
trace!("unpause game");
} else {
time.pause();
trace!("pause game");
}
}
}
/// Замедлить время и поставить на паузу
pub fn set_initial_time_speed_to_slow_and_pause(mut time: ResMut<Time<Virtual>>) {
time.set_relative_speed(1.0 / 8.0);
time.pause();
}
/// обертка над функцией [crate::tools::split_by_grid] для ее отладки
pub fn _split_by_grid_tracing(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
let result = crate::tools::split_by_grid(src, dst, grid);
trace!("\tsplit_by_grid({:?}, {:?}, {:?}) -> {:?}", src, dst, grid, result);
result
}
/// Координатная сетка
#[derive(Component)]
pub struct Grid;
/// Создать координатную сетку
pub fn spawn_grid(commands: &mut Commands) {
use crate::domain::GRID_SIZE;
let grid = commands.spawn((Grid, Transform::default(), Visibility::default())).id();
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(weight, 2000.0)),
Transform {
translation: vec3(x as f32 * GRID_SIZE as f32, 0.0, -1.0),
..Default::default()
},
ChildOf(grid),
));
}
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, weight)),
Transform {
translation: vec3(0.0, y as f32 * GRID_SIZE as f32, -1.0),
..Default::default()
},
ChildOf(grid),
));
}
}
/// трассировка движения змеи
pub fn trace_snake_moving(e: On<GridBarier>) {
let entity = e.entity;
let position = e.position;
let direction = e.direction;
trace!("...<{entity:?}> is moving on {position} {direction:?}");
}