From 6c322435418f7360bbb3e4205a48030acc29a0e8 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Sat, 30 May 2026 22:23:01 +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 | 23 ++++++++++++---- snake-game/src/gameplay.rs | 45 +++++++++++++++++++++++++++++-- snake-game/src/startup.rs | 4 ++- snake-game/tests/rust_learning.rs | 7 +++++ 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs index 2eaada2..15783ff 100644 --- a/snake-game/src/domain.rs +++ b/snake-game/src/domain.rs @@ -10,7 +10,7 @@ pub const SNAKE_VELOCITY: u8 = (GRID_SIZE as f32 / 1.0) as u8; pub struct Head; /// Направление движения -#[derive(Component)] +#[derive(Component, PartialEq)] pub enum Direction { Up, Left, @@ -18,10 +18,6 @@ pub enum Direction { Right, } -/// Скорость движения -#[derive(Component)] -pub struct Velocity(f32); - impl Direction { pub fn rotate_left(&mut self) { *self = match self { @@ -40,6 +36,15 @@ impl Direction { Direction::Left => Direction::Up, }; } + + pub fn vector(&self) -> Vec2 { + match self { + Direction::Up => vec2(0., 1.), + Direction::Right => vec2(-1., 0.), + Direction::Down => vec2(0., -1.), + Direction::Left => vec2(1., 0.), + } + } } impl Default for Direction { @@ -47,3 +52,11 @@ impl Default for Direction { Direction::Up } } + +/// Скорость движения +#[derive(Component)] +pub struct Velocity(pub f32); + +/// Инерция +#[derive(Component, Default)] +pub struct Inert(pub Direction); diff --git a/snake-game/src/gameplay.rs b/snake-game/src/gameplay.rs index 02136cb..d005a0d 100644 --- a/snake-game/src/gameplay.rs +++ b/snake-game/src/gameplay.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::domain::{Direction, Head}; +use crate::domain::{Direction, GRID_SIZE, Head, Inert, Velocity}; pub fn head_rotation( input: Res>, @@ -20,4 +20,45 @@ pub fn head_rotation( } } -pub fn moving() {} +pub fn moving(time: Res