From 5a32449be463bf7419626e0fc26d47821a53c1bd Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Mon, 8 Jun 2026 09:41:36 +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/domain.rs | 1 + snake-game/src/domain/events.rs | 6 ++++ snake-game/src/startup.rs | 57 +++++++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs index d5bf5c4..ee21476 100644 --- a/snake-game/src/domain.rs +++ b/snake-game/src/domain.rs @@ -15,6 +15,7 @@ pub use position::Position; pub const GRID_SIZE: u8 = 48; pub const HEAD_SIZE: u8 = (GRID_SIZE as f32 * 1.1) as u8; pub const SNAKE_VELOCITY: u8 = (GRID_SIZE as f32 / 1.0) as u8; +pub const FIELD_SIZE: u8 = 20; /// Голова змеи #[derive(Component)] diff --git a/snake-game/src/domain/events.rs b/snake-game/src/domain/events.rs index 77fc387..ef929c4 100644 --- a/snake-game/src/domain/events.rs +++ b/snake-game/src/domain/events.rs @@ -18,3 +18,9 @@ impl GridBarier { #[derive(Event)] pub struct GrowUp; + +#[derive(Event)] +pub enum Collision { + Bounds, + Entity(Entity), +} diff --git a/snake-game/src/startup.rs b/snake-game/src/startup.rs index f71b9b4..4ded960 100644 --- a/snake-game/src/startup.rs +++ b/snake-game/src/startup.rs @@ -1,27 +1,64 @@ -use bevy::prelude::*; +use bevy::{color::palettes::tailwind::*, prelude::*}; use crate::{ - domain::{End, SnakePath, Tail}, + domain::{End, FIELD_SIZE, GRID_SIZE, SnakePath, Tail}, observers::schedule_set_velocity, }; /// Инициализация игрового мира pub fn startup(mut commands: Commands, mut time: ResMut>) { time.set_timestep_hz(32.0); - spawn_snake(&mut commands); - crate::debug::spawn_grid(&mut commands); - commands.spawn(Camera2d); -} - -/// Создать голову змеи -fn spawn_snake(commands: &mut Commands) { - use crate::domain::{Direction, HEAD_SIZE, Head, SNAKE_VELOCITY, Velocity}; commands.insert_resource(SnakePath::default()); commands.spawn(Observer::new(crate::observers::clean_snake_path)); commands.spawn(Observer::new(crate::observers::grow_up_snake)); + spawn_bounds(&mut commands); + + spawn_snake(&mut commands); + + crate::debug::spawn_grid(&mut commands); + + commands.spawn(Camera2d); +} + +/// Создать границу игрового поля +fn spawn_bounds(commands: &mut Commands) { + commands.spawn(( + Sprite::from_color(AMBER_500, vec2((FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32, 5.0)), // + Transform { + translation: vec3(0.0, (FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32 / 2.0, 0.0), + ..Default::default() + }, + )); + commands.spawn(( + Sprite::from_color(AMBER_500, vec2((FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32, 5.0)), // + Transform { + translation: vec3(0.0, -(FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32 / 2.0, 0.0), + ..Default::default() + }, + )); + commands.spawn(( + Sprite::from_color(AMBER_500, vec2(5.0, (FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32)), // + Transform { + translation: vec3((FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32 / 2.0, 0.0, 0.0), + ..Default::default() + }, + )); + commands.spawn(( + Sprite::from_color(AMBER_500, vec2(5.0, (FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32)), // + Transform { + translation: vec3(-(FIELD_SIZE as f32 + 1.0) * GRID_SIZE as f32 / 2.0, 0.0, 0.0), + ..Default::default() + }, + )); +} + +/// Создать голову змеи +fn spawn_snake(commands: &mut Commands) { + use crate::domain::{Direction, HEAD_SIZE, Head, SNAKE_VELOCITY, Velocity}; + commands.spawn(Observer::new(crate::debug::trace_snake_moving)); let head = commands