diff --git a/snake-game/src/debug.rs b/snake-game/src/debug.rs index e381a51..2446f6e 100644 --- a/snake-game/src/debug.rs +++ b/snake-game/src/debug.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; -use crate::domain::{GridBarier, GrowUp}; +use crate::domain::{Collision, GridBarier, GrowUp}; /// вывод всех сущностей мира pub fn print_all_entities(mut commands: Commands, query: Query) { @@ -109,3 +109,8 @@ pub fn grow_up_on_tab(input: Res>, mut commands: Commands) commands.trigger(GrowUp); } } + +/// Трасировка столкновений +pub fn trace_collisions(event: On) { + trace!("collision detected: {event:?}") +} diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs index ee21476..a5c7f37 100644 --- a/snake-game/src/domain.rs +++ b/snake-game/src/domain.rs @@ -4,7 +4,7 @@ mod direction; pub use direction::Direction; mod events; -pub use events::{GridBarier, GrowUp}; +pub use events::{Collision, GridBarier, GrowUp}; mod snake_path; pub use snake_path::SnakePath; diff --git a/snake-game/src/domain/events.rs b/snake-game/src/domain/events.rs index ef929c4..a63ce92 100644 --- a/snake-game/src/domain/events.rs +++ b/snake-game/src/domain/events.rs @@ -19,7 +19,7 @@ impl GridBarier { #[derive(Event)] pub struct GrowUp; -#[derive(Event)] +#[derive(Event, Debug)] pub enum Collision { Bounds, Entity(Entity), diff --git a/snake-game/src/gameplay.rs b/snake-game/src/gameplay.rs index 0ffd45a..52b2ae2 100644 --- a/snake-game/src/gameplay.rs +++ b/snake-game/src/gameplay.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use crate::{ - domain::{Direction, GRID_SIZE, GridBarier, Head, Position, SnakePath, Tail, Velocity}, + domain::{Collision, Direction, FIELD_SIZE, GRID_SIZE, GridBarier, Head, Position, SnakePath, Tail, Velocity}, tools::split_by_grid, }; @@ -109,3 +109,13 @@ pub fn snake_moving( moving(velocity, &time, &mut direction, &mut transform, entity, &mut commands, switch_direction); } } + +/// Система отслеживания столкновений с границей +pub fn check_bounds_colisions(head: Single<(&Transform,), (With,)>, mut commands: Commands) { + let (head_transform,) = head.into_inner(); + let (x, y) = (head_transform.translation.x, head_transform.translation.y); + const SIDE: f32 = (FIELD_SIZE as f32 / 2.0) * GRID_SIZE as f32; + if x < -SIDE || x > SIDE || y < -SIDE || y > SIDE { + commands.trigger(Collision::Bounds); + }; +} diff --git a/snake-game/src/lib.rs b/snake-game/src/lib.rs index 3d9b088..7da9fd5 100644 --- a/snake-game/src/lib.rs +++ b/snake-game/src/lib.rs @@ -5,6 +5,6 @@ mod observers; mod startup; mod tools; -pub use gameplay::{head_moving, head_rotation, snake_moving}; -pub use startup::startup; +pub use gameplay::{check_bounds_colisions, head_moving, head_rotation, snake_moving}; +pub use startup::{setup_window, startup}; pub use tools::define_log_layer; diff --git a/snake-game/src/main.rs b/snake-game/src/main.rs index 528b2e2..85ce8d5 100644 --- a/snake-game/src/main.rs +++ b/snake-game/src/main.rs @@ -1,5 +1,5 @@ use bevy::prelude::*; -use snake_game::{head_moving, head_rotation, snake_moving, startup}; +use snake_game::{check_bounds_colisions, head_moving, head_rotation, setup_window, snake_moving, startup}; fn main() { App::new() @@ -21,6 +21,7 @@ fn main() { Startup, ( || {}, // + setup_window, startup, snake_game::debug::set_initial_time_speed_to_slow_and_pause, ), @@ -45,6 +46,7 @@ fn main() { PostUpdate, ( || {}, // + check_bounds_colisions, snake_game::debug::print_all_entities_by_press_enter, snake_game::debug::time_speed, ), diff --git a/snake-game/src/startup.rs b/snake-game/src/startup.rs index 4ded960..b90859c 100644 --- a/snake-game/src/startup.rs +++ b/snake-game/src/startup.rs @@ -5,6 +5,13 @@ use crate::{ observers::schedule_set_velocity, }; +pub fn setup_window(mut window: Single<&mut Window>) { + window.resolution.set( + (FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32 + 20.0, + (FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32 + 20.0, + ); +} + /// Инициализация игрового мира pub fn startup(mut commands: Commands, mut time: ResMut>) { time.set_timestep_hz(32.0); @@ -13,6 +20,7 @@ pub fn startup(mut commands: Commands, mut time: ResMut>) { commands.spawn(Observer::new(crate::observers::clean_snake_path)); commands.spawn(Observer::new(crate::observers::grow_up_snake)); + commands.spawn(Observer::new(crate::debug::trace_collisions)); spawn_bounds(&mut commands);