Проектная работа - WIP
This commit is contained in:
@@ -1,12 +1,49 @@
|
|||||||
use bevy::ecs::component::Component;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub mod constants {
|
|
||||||
pub const FRAMES_PER_SECOND: u8 = 24;
|
pub const FRAMES_PER_SECOND: u8 = 24;
|
||||||
|
|
||||||
pub const GRID_SIZE: u8 = 48;
|
pub const GRID_SIZE: u8 = 48;
|
||||||
|
|
||||||
pub const HEAD_SIZE: u8 = (GRID_SIZE as f32 * 1.1) as u8;
|
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;
|
||||||
|
|
||||||
|
/// Голова змеи
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Head;
|
pub struct Head;
|
||||||
|
|
||||||
|
/// Направление движения
|
||||||
|
#[derive(Component)]
|
||||||
|
pub enum Direction {
|
||||||
|
Up,
|
||||||
|
Left,
|
||||||
|
Down,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Скорость движения
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Velocity(f32);
|
||||||
|
|
||||||
|
impl Direction {
|
||||||
|
pub fn rotate_left(&mut self) {
|
||||||
|
*self = match self {
|
||||||
|
Direction::Up => Direction::Left,
|
||||||
|
Direction::Left => Direction::Down,
|
||||||
|
Direction::Down => Direction::Right,
|
||||||
|
Direction::Right => Direction::Up,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotate_right(&mut self) {
|
||||||
|
*self = match self {
|
||||||
|
Direction::Up => Direction::Right,
|
||||||
|
Direction::Right => Direction::Down,
|
||||||
|
Direction::Down => Direction::Left,
|
||||||
|
Direction::Left => Direction::Up,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Direction {
|
||||||
|
fn default() -> Self {
|
||||||
|
Direction::Up
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,17 +1,23 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::domain::Head;
|
use crate::domain::{Direction, Head};
|
||||||
|
|
||||||
pub fn head_rotation(
|
pub fn head_rotation(
|
||||||
input: Res<ButtonInput<KeyCode>>,
|
input: Res<ButtonInput<KeyCode>>,
|
||||||
mut query: Single<&mut Transform, With<Head>>,
|
query: Single<(&mut Transform, &mut Direction), With<Head>>,
|
||||||
) {
|
) {
|
||||||
use core::f32::consts::PI;
|
use core::f32::consts::PI;
|
||||||
|
|
||||||
|
let (mut transform, mut direction) = query.into_inner();
|
||||||
|
|
||||||
if input.just_pressed(KeyCode::ArrowLeft) {
|
if input.just_pressed(KeyCode::ArrowLeft) {
|
||||||
query.rotate_z(PI / 2.);
|
transform.rotate_z(PI / 2.);
|
||||||
|
direction.rotate_left();
|
||||||
}
|
}
|
||||||
if input.just_pressed(KeyCode::ArrowRight) {
|
if input.just_pressed(KeyCode::ArrowRight) {
|
||||||
query.rotate_z(-PI / 2.);
|
transform.rotate_z(-PI / 2.);
|
||||||
|
direction.rotate_right();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn moving() {}
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use bevy::{prelude::*, winit::WinitSettings};
|
use bevy::{prelude::*, winit::WinitSettings};
|
||||||
|
|
||||||
use crate::domain::constants::FRAMES_PER_SECOND;
|
use crate::domain::{Direction, FRAMES_PER_SECOND};
|
||||||
|
|
||||||
/// Инициализация игрового мира
|
/// Инициализация игрового мира
|
||||||
pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
||||||
use crate::domain::{Head, constants::HEAD_SIZE};
|
use crate::domain::{HEAD_SIZE, Head};
|
||||||
|
|
||||||
winit.focused_mode =
|
winit.focused_mode =
|
||||||
bevy::winit::UpdateMode::reactive(Duration::from_secs_f32(1. / FRAMES_PER_SECOND as f32));
|
bevy::winit::UpdateMode::reactive(Duration::from_secs_f32(1. / FRAMES_PER_SECOND as f32));
|
||||||
@@ -16,6 +16,7 @@ pub fn startup(mut commands: Commands, mut winit: ResMut<WinitSettings>) {
|
|||||||
let head = commands
|
let head = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
Head,
|
Head,
|
||||||
|
Direction::default(),
|
||||||
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),
|
||||||
|
|||||||
Reference in New Issue
Block a user