Проектная работа - WIP

This commit is contained in:
8 changed files with 138 additions and 121 deletions
+11 -115
View File
@@ -1,8 +1,16 @@
use std::{collections::HashMap, f32::consts::PI, fmt::Debug};
use bevy::prelude::*; 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 GRID_SIZE: u8 = 48;
pub const HEAD_SIZE: u8 = (GRID_SIZE as f32 * 1.1) as u8; pub const HEAD_SIZE: u8 = (GRID_SIZE as f32 * 1.1) as u8;
@@ -20,118 +28,6 @@ pub struct Tail;
#[derive(Component)] #[derive(Component)]
pub struct End; 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)] #[derive(Component)]
pub struct Velocity(pub f32); pub struct Velocity(pub f32);
/// Путь змеи
#[derive(Resource, Default, Debug, Deref, DerefMut)]
pub struct SnakePath(HashMap<Position, Direction>);
+99
View File
@@ -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"),
}
}
}
+17
View File
@@ -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 }
}
}
+8
View File
@@ -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<Position, Direction>);
+2 -2
View File
@@ -1,8 +1,8 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::{ use crate::{
domain::{Direction, End, GRID_SIZE, GridBarier, Head, SnakePath, Tail, Velocity}, domain::{Direction, End, GRID_SIZE, GridBarier, Head, Position, SnakePath, Tail, Velocity},
tools::{Position, split_by_grid}, tools::split_by_grid,
}; };
/// Система поворота головы змеи /// Система поворота головы змеи
+1 -1
View File
@@ -1,4 +1,4 @@
use crate::{domain::*, tools::Position}; use crate::domain::*;
use bevy::prelude::*; use bevy::prelude::*;
-3
View File
@@ -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)]); 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;