From 60756ebc9c6c7473fb46f0a3e582e0b2ba29a112 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Sat, 30 May 2026 15:27:14 +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 | 53 ++++++++++++++++++++++++++++++++------ snake-game/src/gameplay.rs | 14 +++++++--- snake-game/src/startup.rs | 5 ++-- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs index 17205fd..2eaada2 100644 --- a/snake-game/src/domain.rs +++ b/snake-game/src/domain.rs @@ -1,12 +1,49 @@ -use bevy::ecs::component::Component; +use bevy::prelude::*; -pub mod constants { - pub const FRAMES_PER_SECOND: u8 = 24; - - pub const GRID_SIZE: u8 = 48; - - pub const HEAD_SIZE: u8 = (GRID_SIZE as f32 * 1.1) as u8; -} +pub const FRAMES_PER_SECOND: u8 = 24; +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; +/// Голова змеи #[derive(Component)] pub struct Head; + +/// Направление движения +#[derive(Component)] +pub enum Direction { + Up, + Left, + Down, + Right, +} + +/// Скорость движения +#[derive(Component)] +pub struct Velocity(f32); + +impl Direction { + pub fn rotate_left(&mut self) { + *self = match self { + Direction::Up => Direction::Left, + Direction::Left => Direction::Down, + Direction::Down => Direction::Right, + Direction::Right => Direction::Up, + }; + } + + pub fn rotate_right(&mut self) { + *self = match self { + Direction::Up => Direction::Right, + Direction::Right => Direction::Down, + Direction::Down => Direction::Left, + Direction::Left => Direction::Up, + }; + } +} + +impl Default for Direction { + fn default() -> Self { + Direction::Up + } +} diff --git a/snake-game/src/gameplay.rs b/snake-game/src/gameplay.rs index 768e915..02136cb 100644 --- a/snake-game/src/gameplay.rs +++ b/snake-game/src/gameplay.rs @@ -1,17 +1,23 @@ use bevy::prelude::*; -use crate::domain::Head; +use crate::domain::{Direction, Head}; pub fn head_rotation( input: Res>, - mut query: Single<&mut Transform, With>, + query: Single<(&mut Transform, &mut Direction), With>, ) { use core::f32::consts::PI; + let (mut transform, mut direction) = query.into_inner(); + if input.just_pressed(KeyCode::ArrowLeft) { - query.rotate_z(PI / 2.); + transform.rotate_z(PI / 2.); + direction.rotate_left(); } if input.just_pressed(KeyCode::ArrowRight) { - query.rotate_z(-PI / 2.); + transform.rotate_z(-PI / 2.); + direction.rotate_right(); } } + +pub fn moving() {} diff --git a/snake-game/src/startup.rs b/snake-game/src/startup.rs index 26bbfda..53fcb55 100644 --- a/snake-game/src/startup.rs +++ b/snake-game/src/startup.rs @@ -2,11 +2,11 @@ use std::time::Duration; use bevy::{prelude::*, winit::WinitSettings}; -use crate::domain::constants::FRAMES_PER_SECOND; +use crate::domain::{Direction, FRAMES_PER_SECOND}; /// Инициализация игрового мира pub fn startup(mut commands: Commands, mut winit: ResMut) { - use crate::domain::{Head, constants::HEAD_SIZE}; + use crate::domain::{HEAD_SIZE, Head}; winit.focused_mode = bevy::winit::UpdateMode::reactive(Duration::from_secs_f32(1. / FRAMES_PER_SECOND as f32)); @@ -16,6 +16,7 @@ pub fn startup(mut commands: Commands, mut winit: ResMut) { let head = commands .spawn(( Head, + Direction::default(), Sprite::from_color( Color::srgb(1., 1., 1.), vec2(HEAD_SIZE as f32, HEAD_SIZE as f32),