Проектная работа - WIP
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub const FRAMES_PER_SECOND: u8 = 2;
|
||||
pub const FRAMES_PER_SECOND: u8 = 24;
|
||||
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;
|
||||
@@ -40,9 +40,9 @@ impl Direction {
|
||||
pub fn vector(&self) -> Vec2 {
|
||||
match self {
|
||||
Direction::Up => vec2(0., 1.),
|
||||
Direction::Right => vec2(-1., 0.),
|
||||
Direction::Right => vec2(1., 0.),
|
||||
Direction::Down => vec2(0., -1.),
|
||||
Direction::Left => vec2(1., 0.),
|
||||
Direction::Left => vec2(-1., 0.),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,11 +38,11 @@ pub fn moving(time: Res<Time>, query: Query<(&mut Transform, &mut Inert, &Direct
|
||||
if ovf_vec.x != 0. && ovf_vec.y != 0. {
|
||||
transform.translation -= ovf_vec.extend(0.0);
|
||||
let delta = ovf_vec.x + ovf_vec.y;
|
||||
inert.0 = *direction;
|
||||
let inert_vec = inert.0.vector();
|
||||
let inert_vec = direction.vector();
|
||||
let delta_vec = inert_vec * delta;
|
||||
transform.translation += delta_vec.extend(0.0);
|
||||
}
|
||||
inert.0 = *direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,9 +58,34 @@ fn get_overflow(grid: f32, coord: f32, delta: f32) -> f32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Найти значение значение на границе сетки, если она находится между значениями old и new
|
||||
fn find_grid_bound(old: f32, new: f32, grid: f32) -> f32 {
|
||||
use std::f32::NAN;
|
||||
|
||||
if grid <= 0.0 {
|
||||
return NAN;
|
||||
};
|
||||
|
||||
let old_grid = old / grid;
|
||||
let new_grid = new / grid;
|
||||
|
||||
let old_grid_left = old_grid.floor();
|
||||
let old_grid_right = old_grid.ceil();
|
||||
let new_grid_left = new_grid.floor();
|
||||
let new_grid_right = new_grid.ceil();
|
||||
|
||||
if old_grid_right == new_grid_left {
|
||||
old_grid_right * grid
|
||||
} else if old_grid_left == new_grid_right {
|
||||
old_grid_left * grid
|
||||
} else {
|
||||
NAN
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::gameplay::get_overflow;
|
||||
use crate::gameplay::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_overflow() {
|
||||
@@ -69,4 +94,36 @@ mod test {
|
||||
assert_eq!(get_overflow(5.0, -6.0, -3.0), 0.0);
|
||||
assert_eq!(get_overflow(5.0, 6.0, -3.0), -2.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_grid_bound() {
|
||||
// проверка с положительными значениями old и new
|
||||
assert!(find_grid_bound(1.0, 4.0, 5.0).is_nan());
|
||||
assert!(find_grid_bound(4.0, 1.0, 5.0).is_nan());
|
||||
assert_eq!(find_grid_bound(4.0, 6.0, 5.0), 5.0);
|
||||
assert_eq!(find_grid_bound(6.0, 4.0, 5.0), 5.0);
|
||||
assert!(find_grid_bound(1.0, 11.0, 5.0).is_nan());
|
||||
assert!(find_grid_bound(11.0, 1.0, 5.0).is_nan());
|
||||
|
||||
// проверка с отрицательными значениями old и new
|
||||
assert!(find_grid_bound(-1.0, -4.0, 5.0).is_nan());
|
||||
assert!(find_grid_bound(-4.0, -1.0, 5.0).is_nan());
|
||||
assert_eq!(find_grid_bound(-4.0, -6.0, 5.0), -5.0);
|
||||
assert_eq!(find_grid_bound(-6.0, -4.0, 5.0), -5.0);
|
||||
assert!(find_grid_bound(-1.0, -11.0, 5.0).is_nan());
|
||||
assert!(find_grid_bound(-11.0, -1.0, 5.0).is_nan());
|
||||
|
||||
// проверка перехода через 0
|
||||
assert_eq!(find_grid_bound(-1.0, 1.0, 5.0), 0.0);
|
||||
assert_eq!(find_grid_bound(1.0, -1.0, 5.0), 0.0);
|
||||
assert_eq!(find_grid_bound(-1.0, 1.0, 10.0), 0.0);
|
||||
assert_eq!(find_grid_bound(1.0, -1.0, 10.0), 0.0);
|
||||
assert!(find_grid_bound(-1.0, 10.0, 5.0).is_nan());
|
||||
assert!(find_grid_bound(-10.0, 1.0, 5.0).is_nan());
|
||||
|
||||
// проверка при grid <= 0
|
||||
assert!(find_grid_bound(1.0, 2.0, 0.0).is_nan());
|
||||
assert!(find_grid_bound(1.0, 2.0, -0.0).is_nan());
|
||||
assert!(find_grid_bound(1.0, 2.0, -0.1).is_nan());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,3 +12,27 @@ fn test_remainder() {
|
||||
dbg!(6 % 5);
|
||||
dbg!(6.3 % 5.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ceil() {
|
||||
dbg!(0.1_f32.ceil());
|
||||
dbg!(-0.1_f32.ceil());
|
||||
dbg!(5.9_f32.ceil());
|
||||
dbg!(-5.9_f32.ceil());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_floor() {
|
||||
dbg!(0.1_f32.floor());
|
||||
dbg!((-0.1_f32).floor());
|
||||
dbg!((-0.2_f32).floor());
|
||||
dbg!((-0.3_f32).floor());
|
||||
dbg!((-1.0_f32 / 5.0_f32).floor());
|
||||
dbg!(5.9_f32.floor());
|
||||
dbg!(-5.9_f32.floor());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero() {
|
||||
dbg!(-0.0_f32 == 0.0_f32);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user