From 45502cceaf1d15369c8001f7ae449e82aeefafc7 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Sun, 7 Jun 2026 15:32:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20-=20WIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- snake-game/src/debug.rs | 9 ++++++++- snake-game/src/domain.rs | 2 +- snake-game/src/domain/events.rs | 3 +++ snake-game/src/domain/position.rs | 9 ++++++++- snake-game/src/gameplay.rs | 10 ++++++++++ snake-game/src/main.rs | 9 ++++++++- snake-game/src/observers.rs | 21 +++++++++++++++++++++ snake-game/src/startup.rs | 1 + 8 files changed, 60 insertions(+), 4 deletions(-) diff --git a/snake-game/src/debug.rs b/snake-game/src/debug.rs index fa0c855..e381a51 100644 --- a/snake-game/src/debug.rs +++ b/snake-game/src/debug.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; -use crate::domain::GridBarier; +use crate::domain::{GridBarier, GrowUp}; /// вывод всех сущностей мира pub fn print_all_entities(mut commands: Commands, query: Query) { @@ -102,3 +102,10 @@ pub fn trace_snake_moving(e: On) { let direction = e.direction; trace!("...<{entity:?}> is moving on {position} {direction:?}"); } + +/// событие [GrowUp] по нажатию Tab +pub fn grow_up_on_tab(input: Res>, mut commands: Commands) { + if input.just_pressed(KeyCode::Tab) { + commands.trigger(GrowUp); + } +} diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs index 827072d..d5bf5c4 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; +pub use events::{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 ae95599..77fc387 100644 --- a/snake-game/src/domain/events.rs +++ b/snake-game/src/domain/events.rs @@ -15,3 +15,6 @@ impl GridBarier { Self { entity, position, direction } } } + +#[derive(Event)] +pub struct GrowUp; diff --git a/snake-game/src/domain/position.rs b/snake-game/src/domain/position.rs index 1d63a9e..a0224da 100644 --- a/snake-game/src/domain/position.rs +++ b/snake-game/src/domain/position.rs @@ -1,6 +1,9 @@ use std::{fmt::Display, ops::Deref}; -use bevy::transform::components::Transform; +use bevy::{ + math::{Vec2, vec2}, + transform::components::Transform, +}; use crate::domain::{Direction, GRID_SIZE}; @@ -23,6 +26,10 @@ impl Position { Direction::Right => self.x += 1, } } + + pub fn translation(&self) -> Vec2 { + vec2(self.x as f32 * GRID_SIZE as f32, self.y as f32 * GRID_SIZE as f32) + } } impl Display for Position { diff --git a/snake-game/src/gameplay.rs b/snake-game/src/gameplay.rs index 5bac638..0ffd45a 100644 --- a/snake-game/src/gameplay.rs +++ b/snake-game/src/gameplay.rs @@ -29,9 +29,17 @@ pub fn head_rotation( head.0 = new_direction; transform.rotation = new_direction.orientation(); trace!("running {direction} at {cur_pos} {translation}: go {new_direction}"); + } else if input.just_pressed(KeyCode::ArrowUp) { + let (mut transform, mut head, direction) = query.into_inner(); + let translation = transform.translation.clone(); + let cur_pos = Position::from(&*transform); + head.0 = direction.clone(); + transform.rotation = direction.orientation(); + trace!("running {direction} at {cur_pos} {translation}: go {direction}"); } } +/// Общая механика движения fn moving( velocity: &f32, time: &Time, @@ -68,6 +76,7 @@ fn moving( } } +/// Система движения головы змеи pub fn head_moving( query: Query<(&mut Transform, &mut Direction, &Velocity, &Head, Entity)>, time: Res>, @@ -86,6 +95,7 @@ pub fn head_moving( } } +/// Система движения тела змеи pub fn snake_moving( query: Query<(&mut Transform, &mut Direction, &Velocity, Entity), (With, Without)>, time: Res>, diff --git a/snake-game/src/main.rs b/snake-game/src/main.rs index c3dd902..528b2e2 100644 --- a/snake-game/src/main.rs +++ b/snake-game/src/main.rs @@ -32,7 +32,14 @@ fn main() { // snake_game::debug::print_all_entities, ), ) - .add_systems(Update, (head_rotation,)) + .add_systems( + Update, + ( + || {}, // + head_rotation, + snake_game::debug::grow_up_on_tab, + ), + ) .add_systems(FixedUpdate, (head_moving, snake_moving)) .add_systems( PostUpdate, diff --git a/snake-game/src/observers.rs b/snake-game/src/observers.rs index b403660..d1942c5 100644 --- a/snake-game/src/observers.rs +++ b/snake-game/src/observers.rs @@ -21,3 +21,24 @@ pub fn clean_snake_path(event: On, query: Query<&End>, mut snake_pat snake_path.remove(&event.position); } } + +/// Обработка события [GrowUp] +pub fn grow_up_snake(_: On, mut commands: Commands, query: Single<(Entity, &Direction, &Transform), (With, With)>) { + let (entity, direction, transform) = query.into_inner(); + commands.entity(entity).remove::(); + let pos = Position::from(transform); + let new_end = commands + .spawn(( + Tail, + End, + direction.clone(), + Velocity(0.0), + Sprite::from_color(Color::srgb(0.75, 0.75, 0.75), vec2(HEAD_SIZE as f32 * 0.8, HEAD_SIZE as f32 * 0.8)), + Transform { + translation: pos.translation().extend(0.0), + ..Default::default() + }, + )) + .id(); + commands.entity(entity).observe(schedule_set_velocity(new_end)); +} diff --git a/snake-game/src/startup.rs b/snake-game/src/startup.rs index aa5b5c9..f71b9b4 100644 --- a/snake-game/src/startup.rs +++ b/snake-game/src/startup.rs @@ -20,6 +20,7 @@ fn spawn_snake(commands: &mut Commands) { commands.insert_resource(SnakePath::default()); 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_snake_moving));