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

This commit is contained in:
13 changed files with 149 additions and 225 deletions
+32 -21
View File
@@ -1,7 +1,9 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Debug};
use bevy::prelude::*;
use crate::tools::Position;
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;
@@ -20,7 +22,19 @@ pub struct End;
/// Событие прохождения границы сетки
#[derive(EntityEvent)]
pub struct GridBarier(pub Entity);
pub struct GridBarier {
pub entity: Entity,
#[allow(unused)]
pub src_pos: Position,
#[allow(unused)]
pub dst_pos: Position,
}
impl GridBarier {
pub fn new(entity: Entity, src_pos: Position, dst_pos: Position) -> Self {
Self { entity, src_pos, dst_pos }
}
}
/// Направление движения
#[derive(Component, PartialEq, Clone, Copy, Debug)]
@@ -93,30 +107,27 @@ pub struct Inert(pub Direction);
/// Путь змеи
#[derive(Resource, Default, Debug)]
pub struct SnakePath(HashMap<(i16, i16), Direction>);
pub struct SnakePath(HashMap<Position, 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) -> (i16, i16, Direction) {
let key = SnakePath::to_key(transform);
pub fn push(&mut self, transform: &Transform, direction: Direction) -> (Position, Direction) {
let key = transform.into();
self.0.insert(key, direction.clone());
(key.0, key.1, direction.clone())
(key, direction.clone())
}
pub fn pick(&self, transform: &Transform) -> Option<(i16, i16, Direction)> {
let key = SnakePath::to_key(transform);
self.0.get(&key).cloned().map(|d| (key.0, key.1, d))
pub fn pick(&self, transform: &Transform) -> Option<(Position, Direction)> {
let key = transform.into();
self.0.get(&key).cloned().map(|d| (key, d))
}
pub fn drop(&mut self, transform: &Transform) -> Option<(i16, i16, Direction)> {
let key = SnakePath::to_key(transform);
self.0.remove(&key).map(|d| (key.0, key.1, d))
pub fn drop(&mut self, transform: &Transform) -> Option<(Position, Direction)> {
let key = transform.into();
self.0.remove(&key).map(|d| (key, d))
}
pub fn debug(&self) -> impl Debug {
&self.0
}
}
@@ -132,10 +143,10 @@ mod test {
snake_path.push(&Transform::default(), Direction::default());
assert_eq!(snake_path.0.len(), 1);
assert!(snake_path.pick(&Transform::default()) == Some((0, 0, Direction::default())));
assert!(snake_path.pick(&Transform::default()) == Some((Position::default(), Direction::default())));
assert_eq!(snake_path.0.len(), 1);
assert!(snake_path.drop(&Transform::default()) == Some((0, 0, Direction::default())));
assert!(snake_path.drop(&Transform::default()) == Some((Position::default(), Direction::default())));
assert_eq!(snake_path.0.len(), 0);
}
}