diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs index 542935b..e923dad 100644 --- a/snake-game/src/domain.rs +++ b/snake-game/src/domain.rs @@ -1,8 +1,16 @@ -use std::{collections::HashMap, f32::consts::PI, fmt::Debug}; - use bevy::prelude::*; -use crate::tools::Position; +mod direction; +pub use direction::Direction; + +mod events; +pub use events::GridBarier; + +mod snake_path; +pub use snake_path::SnakePath; + +mod position; +pub use position::Position; pub const GRID_SIZE: u8 = 48; pub const HEAD_SIZE: u8 = (GRID_SIZE as f32 * 1.1) as u8; @@ -20,118 +28,6 @@ pub struct Tail; #[derive(Component)] pub struct End; -/// Событие прохождения границы сетки -#[derive(EntityEvent)] -pub struct GridBarier { - pub entity: Entity, - pub position: Position, - pub direction: Direction, -} - -impl GridBarier { - pub fn new(entity: Entity, position: Position, direction: Direction) -> Self { - Self { entity, position, direction } - } -} - -/// Направление движения -#[derive(Component, PartialEq, Clone, Copy, Debug)] -pub enum Direction { - Up, - Left, - Down, - Right, -} - -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, - }; - } - - /// Получить значение кооринаты соответствующей направлению движения - pub fn get_coord(&self, vec: &Transform) -> f32 { - match self { - Direction::Up | Direction::Down => vec.translation.y, - Direction::Left | Direction::Right => vec.translation.x, - } - } - - /// Получить знак изменения соответствующий направлению движения - pub fn get_change_sign(&self) -> f32 { - match self { - Direction::Up | Direction::Right => 1.0, - Direction::Down | Direction::Left => -1.0, - } - } - - /// Установить значение кооринаты соответствующей направлению движения - pub fn set_coord(&self, vec: &mut Transform, value: f32) { - match self { - Direction::Up | Direction::Down => vec.translation.y = value, - Direction::Left | Direction::Right => vec.translation.x = value, - } - } - - /// Получить следующую позицию для текущего направления - pub fn dst_pos(&self, transform: &Transform) -> Position { - let mut pos = Position::from(transform); - match self { - Direction::Up => pos.y += 1, - Direction::Left => (), - Direction::Down => (), - Direction::Right => pos.x += 1, - }; - pos - } - - /// Получить ориентацию в пространстве - pub fn orientation(&self) -> Quat { - match self { - Direction::Up => Quat::default(), - Direction::Left => Quat::from_rotation_z(PI / 2.0), - Direction::Down => Quat::from_rotation_z(PI), - Direction::Right => Quat::from_rotation_z(-PI / 2.0), - } - } -} - -impl Default for Direction { - fn default() -> Self { - Direction::Up - } -} - -impl std::fmt::Display for Direction { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Direction::Up => write!(f, "Up"), - Direction::Left => write!(f, "Left"), - Direction::Down => write!(f, "Down"), - Direction::Right => write!(f, "Right"), - } - } -} - /// Скорость движения #[derive(Component)] pub struct Velocity(pub f32); - -/// Путь змеи -#[derive(Resource, Default, Debug, Deref, DerefMut)] -pub struct SnakePath(HashMap); diff --git a/snake-game/src/domain/direction.rs b/snake-game/src/domain/direction.rs new file mode 100644 index 0000000..fb9ff08 --- /dev/null +++ b/snake-game/src/domain/direction.rs @@ -0,0 +1,99 @@ +use bevy::prelude::*; + +use crate::domain::Position; + +/// Направление движения +#[derive(Component, PartialEq, Clone, Copy, Debug)] +pub enum Direction { + Up, + Left, + Down, + Right, +} + +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, + }; + } + + /// Получить значение кооринаты соответствующей направлению движения + pub fn get_coord(&self, vec: &Transform) -> f32 { + match self { + Direction::Up | Direction::Down => vec.translation.y, + Direction::Left | Direction::Right => vec.translation.x, + } + } + + /// Получить знак изменения соответствующий направлению движения + pub fn get_change_sign(&self) -> f32 { + match self { + Direction::Up | Direction::Right => 1.0, + Direction::Down | Direction::Left => -1.0, + } + } + + /// Установить значение кооринаты соответствующей направлению движения + pub fn set_coord(&self, vec: &mut Transform, value: f32) { + match self { + Direction::Up | Direction::Down => vec.translation.y = value, + Direction::Left | Direction::Right => vec.translation.x = value, + } + } + + /// Получить следующую позицию для текущего направления + pub fn dst_pos(&self, transform: &Transform) -> Position { + let mut pos = Position::from(transform); + match self { + Direction::Up => pos.y += 1, + Direction::Left => (), + Direction::Down => (), + Direction::Right => pos.x += 1, + }; + pos + } + + /// Получить ориентацию в пространстве + pub fn orientation(&self) -> Quat { + use std::f32::consts::PI; + + match self { + Direction::Up => Quat::default(), + Direction::Left => Quat::from_rotation_z(PI / 2.0), + Direction::Down => Quat::from_rotation_z(PI), + Direction::Right => Quat::from_rotation_z(-PI / 2.0), + } + } +} + +impl Default for Direction { + fn default() -> Self { + Direction::Up + } +} + +impl std::fmt::Display for Direction { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Direction::Up => write!(f, "Up"), + Direction::Left => write!(f, "Left"), + Direction::Down => write!(f, "Down"), + Direction::Right => write!(f, "Right"), + } + } +} diff --git a/snake-game/src/domain/events.rs b/snake-game/src/domain/events.rs new file mode 100644 index 0000000..ae95599 --- /dev/null +++ b/snake-game/src/domain/events.rs @@ -0,0 +1,17 @@ +use bevy::prelude::*; + +use crate::domain::{Direction, Position}; + +/// Событие прохождения границы сетки +#[derive(EntityEvent)] +pub struct GridBarier { + pub entity: Entity, + pub position: Position, + pub direction: Direction, +} + +impl GridBarier { + pub fn new(entity: Entity, position: Position, direction: Direction) -> Self { + Self { entity, position, direction } + } +} diff --git a/snake-game/src/tools/position.rs b/snake-game/src/domain/position.rs similarity index 100% rename from snake-game/src/tools/position.rs rename to snake-game/src/domain/position.rs diff --git a/snake-game/src/domain/snake_path.rs b/snake-game/src/domain/snake_path.rs new file mode 100644 index 0000000..8f053a3 --- /dev/null +++ b/snake-game/src/domain/snake_path.rs @@ -0,0 +1,8 @@ +use bevy::prelude::*; +use std::{collections::HashMap, fmt::Debug}; + +use crate::domain::{Direction, Position}; + +/// Путь змеи +#[derive(Resource, Default, Debug, Deref, DerefMut)] +pub struct SnakePath(HashMap); diff --git a/snake-game/src/gameplay.rs b/snake-game/src/gameplay.rs index f71e13b..7eeafc6 100644 --- a/snake-game/src/gameplay.rs +++ b/snake-game/src/gameplay.rs @@ -1,8 +1,8 @@ use bevy::prelude::*; use crate::{ - domain::{Direction, End, GRID_SIZE, GridBarier, Head, SnakePath, Tail, Velocity}, - tools::{Position, split_by_grid}, + domain::{Direction, End, GRID_SIZE, GridBarier, Head, Position, SnakePath, Tail, Velocity}, + tools::split_by_grid, }; /// Система поворота головы змеи diff --git a/snake-game/src/observers.rs b/snake-game/src/observers.rs index 82b7c3f..9cbf6a4 100644 --- a/snake-game/src/observers.rs +++ b/snake-game/src/observers.rs @@ -1,4 +1,4 @@ -use crate::{domain::*, tools::Position}; +use crate::domain::*; use bevy::prelude::*; diff --git a/snake-game/src/tools.rs b/snake-game/src/tools.rs index 8e9a4f0..13ce61e 100644 --- a/snake-game/src/tools.rs +++ b/snake-game/src/tools.rs @@ -107,6 +107,3 @@ mod test { assert_eq!(split_by_grid(5.0, 3.0, 5.0), vec![(5.0, 0.0), (3.0, -2.0)]); } } - -mod position; -pub use position::Position;