Проектная работа - WIP
This commit is contained in:
+13
-23
@@ -86,6 +86,18 @@ impl Direction {
|
||||
Direction::Left | Direction::Right => vec.translation.x = value,
|
||||
}
|
||||
}
|
||||
|
||||
/// Получить следующую по направлению координатную позицию
|
||||
pub fn next_pos(&self, transform: &Transform) -> Position {
|
||||
let mut pos = Position::from(transform);
|
||||
match self {
|
||||
Direction::Up => pos.y += 1,
|
||||
Direction::Left => pos.x -= 1,
|
||||
Direction::Down => pos.y -= 1,
|
||||
Direction::Right => pos.x += 1,
|
||||
};
|
||||
pos
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Direction {
|
||||
@@ -103,31 +115,9 @@ pub struct Velocity(pub f32);
|
||||
pub struct Inert(pub Direction);
|
||||
|
||||
/// Путь змеи
|
||||
#[derive(Resource, Default, Debug)]
|
||||
#[derive(Resource, Default, Debug, Deref, DerefMut)]
|
||||
pub struct SnakePath(HashMap<Position, Direction>);
|
||||
|
||||
impl SnakePath {
|
||||
pub fn push(&mut self, transform: &Transform, direction: Direction) -> (Position, Direction) {
|
||||
let key = transform.into();
|
||||
self.0.insert(key, direction.clone());
|
||||
(key, direction.clone())
|
||||
}
|
||||
|
||||
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<(Position, Direction)> {
|
||||
let key = transform.into();
|
||||
self.0.remove(&key).map(|d| (key, d))
|
||||
}
|
||||
|
||||
pub fn debug(&self) -> impl Debug {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::domain::*;
|
||||
|
||||
Reference in New Issue
Block a user