Проектная работа - WIP
This commit is contained in:
@@ -19,6 +19,7 @@ pub enum Direction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Direction {
|
impl Direction {
|
||||||
|
/// Поворот налево
|
||||||
pub fn rotate_left(&mut self) {
|
pub fn rotate_left(&mut self) {
|
||||||
*self = match self {
|
*self = match self {
|
||||||
Direction::Up => Direction::Left,
|
Direction::Up => Direction::Left,
|
||||||
@@ -28,6 +29,7 @@ impl Direction {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Поворот направо
|
||||||
pub fn rotate_right(&mut self) {
|
pub fn rotate_right(&mut self) {
|
||||||
*self = match self {
|
*self = match self {
|
||||||
Direction::Up => Direction::Right,
|
Direction::Up => Direction::Right,
|
||||||
@@ -37,12 +39,27 @@ impl Direction {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn vector(&self) -> Vec2 {
|
/// Получить значение кооринаты соответствующей направлению движения
|
||||||
|
pub fn get_coord(&self, vec: &Transform) -> f32 {
|
||||||
match self {
|
match self {
|
||||||
Direction::Up => vec2(0., 1.),
|
Direction::Up | Direction::Down => vec.translation.y,
|
||||||
Direction::Right => vec2(1., 0.),
|
Direction::Left | Direction::Right => vec.translation.x,
|
||||||
Direction::Down => vec2(0., -1.),
|
}
|
||||||
Direction::Left => vec2(-1., 0.),
|
}
|
||||||
|
|
||||||
|
/// Получить знак изменения соответствующий направлению движения
|
||||||
|
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-20
@@ -23,26 +23,19 @@ pub fn head_rotation(
|
|||||||
pub fn moving(time: Res<Time>, query: Query<(&mut Transform, &mut Inert, &Direction, &Velocity)>) {
|
pub fn moving(time: Res<Time>, query: Query<(&mut Transform, &mut Inert, &Direction, &Velocity)>) {
|
||||||
for (mut transform, mut inert, direction, Velocity(velocity)) in query.into_iter() {
|
for (mut transform, mut inert, direction, Velocity(velocity)) in query.into_iter() {
|
||||||
let delta = velocity * time.delta_secs();
|
let delta = velocity * time.delta_secs();
|
||||||
if inert.0 == *direction {
|
let src = inert.0.get_coord(&transform);
|
||||||
let inert_vec = inert.0.vector();
|
let sign = inert.0.get_change_sign();
|
||||||
let delta_vec = inert_vec * delta;
|
let dst = src + sign * delta;
|
||||||
transform.translation += delta_vec.extend(0.0);
|
let mut splited = split_by_grid(src, dst, GRID_SIZE as f32).into_iter();
|
||||||
} else {
|
let first = splited.next().unwrap_or((0.0, 0.0));
|
||||||
let inert_vec = inert.0.vector();
|
inert.0.set_coord(&mut transform, first.0);
|
||||||
let delta_vec = inert_vec * delta;
|
if let Some(second) = splited.next() {
|
||||||
let ovf_vec = vec2(
|
let delta = second.1.abs();
|
||||||
get_overflow(GRID_SIZE as f32, transform.translation.x, delta_vec.x),
|
|
||||||
get_overflow(GRID_SIZE as f32, transform.translation.y, delta_vec.y),
|
|
||||||
);
|
|
||||||
transform.translation += delta_vec.extend(0.0);
|
|
||||||
if ovf_vec.x != 0. && ovf_vec.y != 0. {
|
|
||||||
transform.translation -= ovf_vec.extend(0.0);
|
|
||||||
let delta = ovf_vec.x + ovf_vec.y;
|
|
||||||
let inert_vec = direction.vector();
|
|
||||||
let delta_vec = inert_vec * delta;
|
|
||||||
transform.translation += delta_vec.extend(0.0);
|
|
||||||
}
|
|
||||||
inert.0 = *direction;
|
inert.0 = *direction;
|
||||||
|
let src = inert.0.get_coord(&transform);
|
||||||
|
let sign = inert.0.get_change_sign();
|
||||||
|
let dst = src + sign * delta;
|
||||||
|
inert.0.set_coord(&mut transform, dst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,7 +79,7 @@ fn find_grid_bound(old: f32, new: f32, grid: f32) -> f32 {
|
|||||||
/// Разбиение изменения координаты по границам сетки
|
/// Разбиение изменения координаты по границам сетки
|
||||||
fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
|
fn split_by_grid(src: f32, dst: f32, grid: f32) -> Vec<(f32, f32)> {
|
||||||
if grid <= 0.0 {
|
if grid <= 0.0 {
|
||||||
return Vec::default();
|
return vec![(dst, dst - src)];
|
||||||
};
|
};
|
||||||
|
|
||||||
let old_grid = src / grid;
|
let old_grid = src / grid;
|
||||||
@@ -206,5 +199,9 @@ mod test {
|
|||||||
split_by_grid(-2.0, -6.0, 5.0),
|
split_by_grid(-2.0, -6.0, 5.0),
|
||||||
vec![(-5.0, -3.0), (-6.0, -1.0)]
|
vec![(-5.0, -3.0), (-6.0, -1.0)]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// сценарии с нулевой сеткой
|
||||||
|
assert_eq!(split_by_grid(1.0, 4.0, 0.0), vec![(4.0, 3.0)]);
|
||||||
|
assert_eq!(split_by_grid(4.0, 1.0, 0.0), vec![(1.0, -3.0)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use bevy::{prelude::*, winit::WinitSettings};
|
use bevy::{ecs::spawn, prelude::*, winit::WinitSettings};
|
||||||
|
|
||||||
use crate::domain::{Direction, FRAMES_PER_SECOND, Inert, SNAKE_VELOCITY, Velocity};
|
use crate::domain::{Direction, FRAMES_PER_SECOND, GRID_SIZE, Inert, SNAKE_VELOCITY, Velocity};
|
||||||
|
|
||||||
/// Инициализация игрового мира
|
/// Инициализация игрового мира
|
||||||
pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
||||||
@@ -51,5 +51,25 @@ pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
|||||||
},
|
},
|
||||||
ChildOf(head),
|
ChildOf(head),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
for x in -20..=20 {
|
||||||
|
commands.spawn((
|
||||||
|
Sprite::from_color(Color::srgb(1.0, 1.0, 0.5), vec2(1.0, 2000.0)),
|
||||||
|
Transform {
|
||||||
|
translation: vec3(x as f32 * GRID_SIZE as f32, 0.0, -1.0),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
for y in -20..=20 {
|
||||||
|
commands.spawn((
|
||||||
|
Sprite::from_color(Color::srgb(1.0, 1.0, 0.5), vec2(2000.0, 1.0)),
|
||||||
|
Transform {
|
||||||
|
translation: vec3(0.0, y as f32 * GRID_SIZE as f32, -1.0),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
commands.spawn(Camera2d);
|
commands.spawn(Camera2d);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user