Проектная работа - WIP
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::domain::GridBarier;
|
use crate::domain::{GridBarier, GrowUp};
|
||||||
|
|
||||||
/// вывод всех сущностей мира
|
/// вывод всех сущностей мира
|
||||||
pub fn print_all_entities(mut commands: Commands, query: Query<Entity>) {
|
pub fn print_all_entities(mut commands: Commands, query: Query<Entity>) {
|
||||||
@@ -102,3 +102,10 @@ pub fn trace_snake_moving(e: On<GridBarier>) {
|
|||||||
let direction = e.direction;
|
let direction = e.direction;
|
||||||
trace!("...<{entity:?}> is moving on {position} {direction:?}");
|
trace!("...<{entity:?}> is moving on {position} {direction:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// событие [GrowUp] по нажатию Tab
|
||||||
|
pub fn grow_up_on_tab(input: Res<ButtonInput<KeyCode>>, mut commands: Commands) {
|
||||||
|
if input.just_pressed(KeyCode::Tab) {
|
||||||
|
commands.trigger(GrowUp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ mod direction;
|
|||||||
pub use direction::Direction;
|
pub use direction::Direction;
|
||||||
|
|
||||||
mod events;
|
mod events;
|
||||||
pub use events::GridBarier;
|
pub use events::{GridBarier, GrowUp};
|
||||||
|
|
||||||
mod snake_path;
|
mod snake_path;
|
||||||
pub use snake_path::SnakePath;
|
pub use snake_path::SnakePath;
|
||||||
|
|||||||
@@ -15,3 +15,6 @@ impl GridBarier {
|
|||||||
Self { entity, position, direction }
|
Self { entity, position, direction }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Event)]
|
||||||
|
pub struct GrowUp;
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
use std::{fmt::Display, ops::Deref};
|
use std::{fmt::Display, ops::Deref};
|
||||||
|
|
||||||
use bevy::transform::components::Transform;
|
use bevy::{
|
||||||
|
math::{Vec2, vec2},
|
||||||
|
transform::components::Transform,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::domain::{Direction, GRID_SIZE};
|
use crate::domain::{Direction, GRID_SIZE};
|
||||||
|
|
||||||
@@ -23,6 +26,10 @@ impl Position {
|
|||||||
Direction::Right => self.x += 1,
|
Direction::Right => self.x += 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn translation(&self) -> Vec2 {
|
||||||
|
vec2(self.x as f32 * GRID_SIZE as f32, self.y as f32 * GRID_SIZE as f32)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Position {
|
impl Display for Position {
|
||||||
|
|||||||
@@ -29,9 +29,17 @@ pub fn head_rotation(
|
|||||||
head.0 = new_direction;
|
head.0 = new_direction;
|
||||||
transform.rotation = new_direction.orientation();
|
transform.rotation = new_direction.orientation();
|
||||||
trace!("running {direction} at {cur_pos} {translation}: go {new_direction}");
|
trace!("running {direction} at {cur_pos} {translation}: go {new_direction}");
|
||||||
|
} else if input.just_pressed(KeyCode::ArrowUp) {
|
||||||
|
let (mut transform, mut head, direction) = query.into_inner();
|
||||||
|
let translation = transform.translation.clone();
|
||||||
|
let cur_pos = Position::from(&*transform);
|
||||||
|
head.0 = direction.clone();
|
||||||
|
transform.rotation = direction.orientation();
|
||||||
|
trace!("running {direction} at {cur_pos} {translation}: go {direction}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Общая механика движения
|
||||||
fn moving(
|
fn moving(
|
||||||
velocity: &f32,
|
velocity: &f32,
|
||||||
time: &Time<Fixed>,
|
time: &Time<Fixed>,
|
||||||
@@ -68,6 +76,7 @@ fn moving(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Система движения головы змеи
|
||||||
pub fn head_moving(
|
pub fn head_moving(
|
||||||
query: Query<(&mut Transform, &mut Direction, &Velocity, &Head, Entity)>,
|
query: Query<(&mut Transform, &mut Direction, &Velocity, &Head, Entity)>,
|
||||||
time: Res<Time<Fixed>>,
|
time: Res<Time<Fixed>>,
|
||||||
@@ -86,6 +95,7 @@ pub fn head_moving(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Система движения тела змеи
|
||||||
pub fn snake_moving(
|
pub fn snake_moving(
|
||||||
query: Query<(&mut Transform, &mut Direction, &Velocity, Entity), (With<Tail>, Without<Head>)>,
|
query: Query<(&mut Transform, &mut Direction, &Velocity, Entity), (With<Tail>, Without<Head>)>,
|
||||||
time: Res<Time<Fixed>>,
|
time: Res<Time<Fixed>>,
|
||||||
|
|||||||
@@ -32,7 +32,14 @@ fn main() {
|
|||||||
// snake_game::debug::print_all_entities,
|
// snake_game::debug::print_all_entities,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.add_systems(Update, (head_rotation,))
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
(
|
||||||
|
|| {}, //
|
||||||
|
head_rotation,
|
||||||
|
snake_game::debug::grow_up_on_tab,
|
||||||
|
),
|
||||||
|
)
|
||||||
.add_systems(FixedUpdate, (head_moving, snake_moving))
|
.add_systems(FixedUpdate, (head_moving, snake_moving))
|
||||||
.add_systems(
|
.add_systems(
|
||||||
PostUpdate,
|
PostUpdate,
|
||||||
|
|||||||
@@ -21,3 +21,24 @@ pub fn clean_snake_path(event: On<GridBarier>, query: Query<&End>, mut snake_pat
|
|||||||
snake_path.remove(&event.position);
|
snake_path.remove(&event.position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Обработка события [GrowUp]
|
||||||
|
pub fn grow_up_snake(_: On<GrowUp>, mut commands: Commands, query: Single<(Entity, &Direction, &Transform), (With<Tail>, With<End>)>) {
|
||||||
|
let (entity, direction, transform) = query.into_inner();
|
||||||
|
commands.entity(entity).remove::<End>();
|
||||||
|
let pos = Position::from(transform);
|
||||||
|
let new_end = commands
|
||||||
|
.spawn((
|
||||||
|
Tail,
|
||||||
|
End,
|
||||||
|
direction.clone(),
|
||||||
|
Velocity(0.0),
|
||||||
|
Sprite::from_color(Color::srgb(0.75, 0.75, 0.75), vec2(HEAD_SIZE as f32 * 0.8, HEAD_SIZE as f32 * 0.8)),
|
||||||
|
Transform {
|
||||||
|
translation: pos.translation().extend(0.0),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.id();
|
||||||
|
commands.entity(entity).observe(schedule_set_velocity(new_end));
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ fn spawn_snake(commands: &mut Commands) {
|
|||||||
commands.insert_resource(SnakePath::default());
|
commands.insert_resource(SnakePath::default());
|
||||||
|
|
||||||
commands.spawn(Observer::new(crate::observers::clean_snake_path));
|
commands.spawn(Observer::new(crate::observers::clean_snake_path));
|
||||||
|
commands.spawn(Observer::new(crate::observers::grow_up_snake));
|
||||||
|
|
||||||
commands.spawn(Observer::new(crate::debug::trace_snake_moving));
|
commands.spawn(Observer::new(crate::debug::trace_snake_moving));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user