Проектная работа - WIP
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::domain::{GridBarier, GrowUp};
|
use crate::domain::{Collision, GridBarier, GrowUp};
|
||||||
|
|
||||||
/// вывод всех сущностей мира
|
/// вывод всех сущностей мира
|
||||||
pub fn print_all_entities(mut commands: Commands, query: Query<Entity>) {
|
pub fn print_all_entities(mut commands: Commands, query: Query<Entity>) {
|
||||||
@@ -109,3 +109,8 @@ pub fn grow_up_on_tab(input: Res<ButtonInput<KeyCode>>, mut commands: Commands)
|
|||||||
commands.trigger(GrowUp);
|
commands.trigger(GrowUp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Трасировка столкновений
|
||||||
|
pub fn trace_collisions(event: On<Collision>) {
|
||||||
|
trace!("collision detected: {event:?}")
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ mod direction;
|
|||||||
pub use direction::Direction;
|
pub use direction::Direction;
|
||||||
|
|
||||||
mod events;
|
mod events;
|
||||||
pub use events::{GridBarier, GrowUp};
|
pub use events::{Collision, GridBarier, GrowUp};
|
||||||
|
|
||||||
mod snake_path;
|
mod snake_path;
|
||||||
pub use snake_path::SnakePath;
|
pub use snake_path::SnakePath;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ impl GridBarier {
|
|||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
pub struct GrowUp;
|
pub struct GrowUp;
|
||||||
|
|
||||||
#[derive(Event)]
|
#[derive(Event, Debug)]
|
||||||
pub enum Collision {
|
pub enum Collision {
|
||||||
Bounds,
|
Bounds,
|
||||||
Entity(Entity),
|
Entity(Entity),
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
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,
|
tools::split_by_grid,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -109,3 +109,13 @@ pub fn snake_moving(
|
|||||||
moving(velocity, &time, &mut direction, &mut transform, entity, &mut commands, switch_direction);
|
moving(velocity, &time, &mut direction, &mut transform, entity, &mut commands, switch_direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Система отслеживания столкновений с границей
|
||||||
|
pub fn check_bounds_colisions(head: Single<(&Transform,), (With<Head>,)>, 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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ mod observers;
|
|||||||
mod startup;
|
mod startup;
|
||||||
mod tools;
|
mod tools;
|
||||||
|
|
||||||
pub use gameplay::{head_moving, head_rotation, snake_moving};
|
pub use gameplay::{check_bounds_colisions, head_moving, head_rotation, snake_moving};
|
||||||
pub use startup::startup;
|
pub use startup::{setup_window, startup};
|
||||||
pub use tools::define_log_layer;
|
pub use tools::define_log_layer;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use bevy::prelude::*;
|
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() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
@@ -21,6 +21,7 @@ fn main() {
|
|||||||
Startup,
|
Startup,
|
||||||
(
|
(
|
||||||
|| {}, //
|
|| {}, //
|
||||||
|
setup_window,
|
||||||
startup,
|
startup,
|
||||||
snake_game::debug::set_initial_time_speed_to_slow_and_pause,
|
snake_game::debug::set_initial_time_speed_to_slow_and_pause,
|
||||||
),
|
),
|
||||||
@@ -45,6 +46,7 @@ fn main() {
|
|||||||
PostUpdate,
|
PostUpdate,
|
||||||
(
|
(
|
||||||
|| {}, //
|
|| {}, //
|
||||||
|
check_bounds_colisions,
|
||||||
snake_game::debug::print_all_entities_by_press_enter,
|
snake_game::debug::print_all_entities_by_press_enter,
|
||||||
snake_game::debug::time_speed,
|
snake_game::debug::time_speed,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -5,6 +5,13 @@ use crate::{
|
|||||||
observers::schedule_set_velocity,
|
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<Fixed>>) {
|
pub fn startup(mut commands: Commands, mut time: ResMut<Time<Fixed>>) {
|
||||||
time.set_timestep_hz(32.0);
|
time.set_timestep_hz(32.0);
|
||||||
@@ -13,6 +20,7 @@ pub fn startup(mut commands: Commands, mut time: ResMut<Time<Fixed>>) {
|
|||||||
|
|
||||||
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::debug::trace_collisions));
|
||||||
|
|
||||||
spawn_bounds(&mut commands);
|
spawn_bounds(&mut commands);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user