Проектная работа - WIP
This commit is contained in:
@@ -10,7 +10,7 @@ pub const SNAKE_VELOCITY: u8 = (GRID_SIZE as f32 / 1.0) as u8;
|
|||||||
pub struct Head;
|
pub struct Head;
|
||||||
|
|
||||||
/// Направление движения
|
/// Направление движения
|
||||||
#[derive(Component)]
|
#[derive(Component, PartialEq)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
Up,
|
Up,
|
||||||
Left,
|
Left,
|
||||||
@@ -18,10 +18,6 @@ pub enum Direction {
|
|||||||
Right,
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Скорость движения
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Velocity(f32);
|
|
||||||
|
|
||||||
impl Direction {
|
impl Direction {
|
||||||
pub fn rotate_left(&mut self) {
|
pub fn rotate_left(&mut self) {
|
||||||
*self = match self {
|
*self = match self {
|
||||||
@@ -40,6 +36,15 @@ impl Direction {
|
|||||||
Direction::Left => Direction::Up,
|
Direction::Left => Direction::Up,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn vector(&self) -> Vec2 {
|
||||||
|
match self {
|
||||||
|
Direction::Up => vec2(0., 1.),
|
||||||
|
Direction::Right => vec2(-1., 0.),
|
||||||
|
Direction::Down => vec2(0., -1.),
|
||||||
|
Direction::Left => vec2(1., 0.),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Direction {
|
impl Default for Direction {
|
||||||
@@ -47,3 +52,11 @@ impl Default for Direction {
|
|||||||
Direction::Up
|
Direction::Up
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Скорость движения
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Velocity(pub f32);
|
||||||
|
|
||||||
|
/// Инерция
|
||||||
|
#[derive(Component, Default)]
|
||||||
|
pub struct Inert(pub Direction);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::domain::{Direction, Head};
|
use crate::domain::{Direction, GRID_SIZE, Head, Inert, Velocity};
|
||||||
|
|
||||||
pub fn head_rotation(
|
pub fn head_rotation(
|
||||||
input: Res<ButtonInput<KeyCode>>,
|
input: Res<ButtonInput<KeyCode>>,
|
||||||
@@ -20,4 +20,45 @@ pub fn head_rotation(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn moving() {}
|
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() {
|
||||||
|
let delta = velocity * time.delta_secs();
|
||||||
|
if inert.0 == *direction {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
let inert_vec = inert.0.vector();
|
||||||
|
let delta_vec = inert_vec * delta;
|
||||||
|
let ovf_vec = vec2(
|
||||||
|
get_overflow(GRID_SIZE as f32, transform.translation.x, delta_vec.x),
|
||||||
|
get_overflow(GRID_SIZE as f32, transform.translation.y, delta_vec.y),
|
||||||
|
);
|
||||||
|
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_overflow(grid: f32, coord: f32, delta: f32) -> f32 {
|
||||||
|
let rem = coord % grid;
|
||||||
|
let rem = if rem < 0.0 { grid + rem } else { rem };
|
||||||
|
let new = rem + delta;
|
||||||
|
if new >= 0.0 && new <= grid {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
new % grid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::gameplay::get_overflow;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_overflow() {
|
||||||
|
assert_eq!(get_overflow(5.0, -6.0, 3.0), 2.0);
|
||||||
|
assert_eq!(get_overflow(5.0, 6.0, 3.0), 0.0);
|
||||||
|
assert_eq!(get_overflow(5.0, -6.0, -3.0), 0.0);
|
||||||
|
assert_eq!(get_overflow(5.0, 6.0, -3.0), -2.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use bevy::{prelude::*, winit::WinitSettings};
|
use bevy::{prelude::*, winit::WinitSettings};
|
||||||
|
|
||||||
use crate::domain::{Direction, FRAMES_PER_SECOND};
|
use crate::domain::{Direction, FRAMES_PER_SECOND, Inert, SNAKE_VELOCITY, Velocity};
|
||||||
|
|
||||||
/// Инициализация игрового мира
|
/// Инициализация игрового мира
|
||||||
pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
||||||
@@ -17,6 +17,8 @@ pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
|||||||
.spawn((
|
.spawn((
|
||||||
Head,
|
Head,
|
||||||
Direction::default(),
|
Direction::default(),
|
||||||
|
Inert::default(),
|
||||||
|
Velocity(SNAKE_VELOCITY as f32),
|
||||||
Sprite::from_color(
|
Sprite::from_color(
|
||||||
Color::srgb(1., 1., 1.),
|
Color::srgb(1., 1., 1.),
|
||||||
vec2(HEAD_SIZE as f32, HEAD_SIZE as f32),
|
vec2(HEAD_SIZE as f32, HEAD_SIZE as f32),
|
||||||
|
|||||||
@@ -5,3 +5,10 @@ fn test_saturating_sub() {
|
|||||||
dbg!(a.saturating_sub(10));
|
dbg!(a.saturating_sub(10));
|
||||||
dbg!(a.saturating_sub(15));
|
dbg!(a.saturating_sub(15));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_remainder() {
|
||||||
|
dbg!(-6 % 5);
|
||||||
|
dbg!(6 % 5);
|
||||||
|
dbg!(6.3 % 5.0);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user