Проектная работа - WIP
This commit is contained in:
+19
-11
@@ -15,20 +15,32 @@ pub fn head_rotation(
|
||||
let (mut transform, direction) = query.into_inner();
|
||||
let translation = transform.translation.clone();
|
||||
let cur_pos = Position::from(&*transform);
|
||||
let dst_pos = direction.dst_pos(&*transform);
|
||||
let mut dst_pos = direction.dst_pos(&*transform);
|
||||
let mut new_direction = direction.clone();
|
||||
new_direction.rotate_left();
|
||||
snake_path.insert(dst_pos, new_direction);
|
||||
if !snake_path.push(dst_pos, new_direction) {
|
||||
trace!("try 1 failed: cannot push [{dst_pos} => {new_direction}] into SnakePath");
|
||||
dst_pos.change(&direction);
|
||||
if !snake_path.push(dst_pos, new_direction) {
|
||||
trace!("try 2 failed: cannot push [{dst_pos} => {new_direction}] into SnakePath");
|
||||
}
|
||||
}
|
||||
transform.rotation = new_direction.orientation();
|
||||
trace!("running {direction} at {cur_pos} {translation}: go {new_direction} on {dst_pos}");
|
||||
} else if input.just_pressed(KeyCode::ArrowRight) {
|
||||
let (mut transform, direction) = query.into_inner();
|
||||
let translation = transform.translation.clone();
|
||||
let cur_pos = Position::from(&*transform);
|
||||
let dst_pos = direction.dst_pos(&*transform);
|
||||
let mut dst_pos = direction.dst_pos(&*transform);
|
||||
let mut new_direction = direction.clone();
|
||||
new_direction.rotate_right();
|
||||
snake_path.insert(dst_pos, new_direction);
|
||||
if !snake_path.push(dst_pos, new_direction) {
|
||||
trace!("try 1 failed: cannot push [{dst_pos} => {new_direction}] into SnakePath");
|
||||
dst_pos.change(&direction);
|
||||
if !snake_path.push(dst_pos, new_direction) {
|
||||
trace!("try 2 failed: cannot push [{dst_pos} => {new_direction}] into SnakePath");
|
||||
}
|
||||
}
|
||||
transform.rotation = new_direction.orientation();
|
||||
trace!("running {direction} at {cur_pos} {translation}: go {new_direction} on {dst_pos}");
|
||||
}
|
||||
@@ -37,7 +49,7 @@ pub fn head_rotation(
|
||||
/// Система движения змеи
|
||||
pub fn snake_moving(
|
||||
time: Res<Time<Fixed>>,
|
||||
snake_path: Res<SnakePath>,
|
||||
mut snake_path: ResMut<SnakePath>,
|
||||
query: Query<(&mut Transform, &mut Direction, &Velocity, Entity, Has<End>), Or<(With<Head>, With<Tail>, With<End>)>>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
@@ -55,7 +67,7 @@ pub fn snake_moving(
|
||||
if let Some(second) = splited.next() {
|
||||
let delta = second.1.abs();
|
||||
let pos = Position::from(&*transform);
|
||||
if let Some(dir) = snake_path.get(&pos) {
|
||||
if let Some(dir) = snake_path.peek(&pos) {
|
||||
trace!("set new direction for {entity} in {pos} --> {dir}");
|
||||
*direction = dir.clone();
|
||||
}
|
||||
@@ -69,11 +81,7 @@ pub fn snake_moving(
|
||||
|
||||
// если это кончик хвоста, то нужно очистить [SnakePath] за собой
|
||||
if has_end {
|
||||
commands.queue(move |world: &mut World| {
|
||||
if let Some(mut snake_path) = world.get_resource_mut::<SnakePath>() {
|
||||
snake_path.remove(&pos);
|
||||
}
|
||||
});
|
||||
snake_path.drop(&pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user