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

This commit is contained in:
8 changed files with 403 additions and 30 deletions
+64 -2
View File
@@ -1,3 +1,5 @@
use std::collections::HashMap;
use bevy::prelude::*;
pub const FRAMES_PER_SECOND: u8 = 24;
@@ -5,16 +7,28 @@ 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 Grid;
/// Голова змеи
#[derive(Component)]
pub struct Head;
/// Сегмент змеиного хвоста
#[derive(Component)]
pub struct Tail(pub u16);
pub struct Tail;
/// Конец змеиного хвоста
#[derive(Component)]
pub struct End;
/// Событие прохождения границы сетки
#[derive(EntityEvent)]
pub struct GridBarier(pub Entity);
/// Направление движения
#[derive(Component, PartialEq, Clone, Copy)]
#[derive(Component, PartialEq, Clone, Copy, Debug)]
pub enum Direction {
Up,
Left,
@@ -81,3 +95,51 @@ pub struct Velocity(pub f32);
/// Инерция
#[derive(Component, Default)]
pub struct Inert(pub Direction);
/// Путь змеи
#[derive(Resource, Default, Debug)]
pub struct SnakePath(HashMap<(i16, i16), Direction>);
impl SnakePath {
fn to_key(transform: &Transform) -> (i16, i16) {
(
(transform.translation.x / GRID_SIZE as f32).floor() as i16,
(transform.translation.y / GRID_SIZE as f32).floor() as i16,
)
}
pub fn push(&mut self, transform: &Transform, direction: Direction) {
let key = SnakePath::to_key(transform);
self.0.insert(key, direction.clone());
}
pub fn pick(&self, transform: &Transform) -> Option<Direction> {
let key = SnakePath::to_key(transform);
self.0.get(&key).cloned()
}
pub fn pop(&mut self, transform: &Transform) -> Option<Direction> {
let key = SnakePath::to_key(transform);
self.0.remove(&key)
}
}
#[cfg(test)]
mod test {
use crate::domain::*;
use std::collections::HashMap;
#[test]
fn test_snake_path() {
let mut snake_path = SnakePath(HashMap::default());
snake_path.push(&Transform::default(), Direction::default());
assert_eq!(snake_path.0.len(), 1);
assert!(snake_path.pick(&Transform::default()) == Some(Direction::default()));
assert_eq!(snake_path.0.len(), 1);
assert!(snake_path.pop(&Transform::default()) == Some(Direction::default()));
assert_eq!(snake_path.0.len(), 0);
}
}