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