Проектная работа - WIP
This commit is contained in:
@@ -23,7 +23,7 @@ pub struct Head(pub Direction);
|
|||||||
|
|
||||||
/// Сегмент змеиного хвоста
|
/// Сегмент змеиного хвоста
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Tail;
|
pub struct Tail(pub Entity);
|
||||||
|
|
||||||
/// Конец змеиного хвоста
|
/// Конец змеиного хвоста
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
|||||||
@@ -22,5 +22,6 @@ pub struct GrowUp;
|
|||||||
#[derive(Event, Debug)]
|
#[derive(Event, Debug)]
|
||||||
pub enum Collision {
|
pub enum Collision {
|
||||||
Bounds,
|
Bounds,
|
||||||
Entity(Entity),
|
Tail,
|
||||||
|
Meal,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,3 +119,24 @@ pub fn check_bounds_colisions(head: Single<(&Transform,), (With<Head>,)>, mut co
|
|||||||
commands.trigger(Collision::Bounds);
|
commands.trigger(Collision::Bounds);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Система отслеживания столкновений c хвостом
|
||||||
|
pub fn check_tail_colisions(
|
||||||
|
head: Single<(&Transform, Entity), (With<Head>,)>,
|
||||||
|
query: Query<(&Transform, &Velocity, &Tail)>,
|
||||||
|
mut commands: Commands, //
|
||||||
|
) {
|
||||||
|
let (head_transform, head_entity) = head.into_inner();
|
||||||
|
for (tail_transform, velocity, tail) in query.into_iter() {
|
||||||
|
if velocity.0 == 0.0 || tail.0 == head_entity {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let distance = head_transform.translation.distance(tail_transform.translation);
|
||||||
|
if distance < GRID_SIZE as f32 {
|
||||||
|
commands.trigger(Collision::Tail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Система отслеживания столкновений с едой
|
||||||
|
pub fn check_meal_collisions() {}
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ mod observers;
|
|||||||
mod startup;
|
mod startup;
|
||||||
mod tools;
|
mod tools;
|
||||||
|
|
||||||
pub use gameplay::{check_bounds_colisions, head_moving, head_rotation, snake_moving};
|
pub use gameplay::{check_bounds_colisions, check_meal_collisions, check_tail_colisions, head_moving, head_rotation, snake_moving};
|
||||||
pub use startup::{setup_window, 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::{check_bounds_colisions, head_moving, head_rotation, setup_window, snake_moving, startup};
|
use snake_game::{check_bounds_colisions, check_meal_collisions, check_tail_colisions, head_moving, head_rotation, setup_window, snake_moving, startup};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
@@ -47,6 +47,8 @@ fn main() {
|
|||||||
(
|
(
|
||||||
|| {}, //
|
|| {}, //
|
||||||
check_bounds_colisions,
|
check_bounds_colisions,
|
||||||
|
check_tail_colisions,
|
||||||
|
check_meal_collisions,
|
||||||
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,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::domain::*;
|
use crate::domain::*;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::{color::palettes::tailwind::*, prelude::*};
|
||||||
|
|
||||||
/// Запланировать установку скорости движения [Velocity] при следующем событии [GridBarier]
|
/// Запланировать установку скорости движения [Velocity] при следующем событии [GridBarier]
|
||||||
pub fn schedule_set_velocity(tail: Entity) -> impl Fn(On<GridBarier>, Commands, Query<(&mut Velocity, &Transform)>) {
|
pub fn schedule_set_velocity(tail: Entity) -> impl Fn(On<GridBarier>, Commands, Query<(&mut Velocity, &Transform)>) {
|
||||||
@@ -29,7 +29,7 @@ pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entit
|
|||||||
let pos = Position::from(transform);
|
let pos = Position::from(transform);
|
||||||
let new_end = commands
|
let new_end = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
Tail,
|
Tail(entity),
|
||||||
End,
|
End,
|
||||||
direction.clone(),
|
direction.clone(),
|
||||||
Velocity(0.0),
|
Velocity(0.0),
|
||||||
@@ -42,3 +42,21 @@ pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entit
|
|||||||
.id();
|
.id();
|
||||||
commands.entity(entity).observe(schedule_set_velocity(new_end));
|
commands.entity(entity).observe(schedule_set_velocity(new_end));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Обработка столкновений
|
||||||
|
pub fn handle_collisions(
|
||||||
|
event: On<Collision>, //
|
||||||
|
query: Query<(&mut Velocity, &mut Sprite), Or<(With<Head>, With<Tail>, With<End>)>>,
|
||||||
|
) {
|
||||||
|
match event.event() {
|
||||||
|
Collision::Meal => {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
Collision::Bounds | Collision::Tail => {
|
||||||
|
for (mut velocity, mut sprite) in query.into_iter() {
|
||||||
|
velocity.0 = 0.0;
|
||||||
|
sprite.color = RED_300.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ fn spawn_snake(commands: &mut Commands) {
|
|||||||
use crate::domain::{Direction, HEAD_SIZE, Head, SNAKE_VELOCITY, Velocity};
|
use crate::domain::{Direction, HEAD_SIZE, Head, SNAKE_VELOCITY, Velocity};
|
||||||
|
|
||||||
commands.spawn(Observer::new(crate::debug::trace_snake_moving));
|
commands.spawn(Observer::new(crate::debug::trace_snake_moving));
|
||||||
|
commands.spawn(Observer::new(crate::observers::handle_collisions));
|
||||||
|
|
||||||
let head = commands
|
let head = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
@@ -100,7 +101,7 @@ fn spawn_snake(commands: &mut Commands) {
|
|||||||
|
|
||||||
let tail = commands
|
let tail = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
Tail,
|
Tail(head),
|
||||||
End,
|
End,
|
||||||
Direction::default(),
|
Direction::default(),
|
||||||
Velocity(0.0),
|
Velocity(0.0),
|
||||||
|
|||||||
Reference in New Issue
Block a user