Проектная работа - WIP
This commit is contained in:
@@ -117,23 +117,3 @@ pub struct Inert(pub Direction);
|
|||||||
/// Путь змеи
|
/// Путь змеи
|
||||||
#[derive(Resource, Default, Debug, Deref, DerefMut)]
|
#[derive(Resource, Default, Debug, Deref, DerefMut)]
|
||||||
pub struct SnakePath(HashMap<Position, Direction>);
|
pub struct SnakePath(HashMap<Position, Direction>);
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use crate::domain::*;
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_snake_path() {
|
|
||||||
let mut snake_path = SnakePath(HashMap::default());
|
|
||||||
snake_path.push(&Transform::default(), Direction::default());
|
|
||||||
assert_eq!(snake_path.0.len(), 1);
|
|
||||||
|
|
||||||
assert!(snake_path.pick(&Transform::default()) == Some((Position::default(), Direction::default())));
|
|
||||||
assert_eq!(snake_path.0.len(), 1);
|
|
||||||
|
|
||||||
assert!(snake_path.drop(&Transform::default()) == Some((Position::default(), Direction::default())));
|
|
||||||
assert_eq!(snake_path.0.len(), 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ mod observers;
|
|||||||
mod startup;
|
mod startup;
|
||||||
mod tools;
|
mod tools;
|
||||||
|
|
||||||
pub use gameplay::{head_rotation, moving};
|
pub use gameplay::{head_rotation, snake_moving};
|
||||||
pub use startup::startup;
|
pub use startup::startup;
|
||||||
pub use tools::define_log_layer;
|
pub use tools::define_log_layer;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use snake_game::{head_rotation, moving, startup};
|
use snake_game::{head_rotation, snake_moving, startup};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
@@ -20,7 +20,7 @@ fn main() {
|
|||||||
.add_systems(Startup, (startup,))
|
.add_systems(Startup, (startup,))
|
||||||
// .add_systems(PostStartup, snake_game::debug::print_all_entities)
|
// .add_systems(PostStartup, snake_game::debug::print_all_entities)
|
||||||
.add_systems(Update, (head_rotation,))
|
.add_systems(Update, (head_rotation,))
|
||||||
.add_systems(FixedUpdate, (moving,))
|
.add_systems(FixedUpdate, (snake_moving,))
|
||||||
.add_systems(
|
.add_systems(
|
||||||
PostUpdate,
|
PostUpdate,
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ pub fn tail_observer(event: On<GridBarier>, mut query: Query<(&mut Direction, &T
|
|||||||
let entity = event.entity;
|
let entity = event.entity;
|
||||||
if event.observer() == entity {
|
if event.observer() == entity {
|
||||||
if let Ok((mut direction, transform)) = query.get_mut(event.entity) {
|
if let Ok((mut direction, transform)) = query.get_mut(event.entity) {
|
||||||
|
/*
|
||||||
if let Some((position, new_direction)) = snake_path.pick(transform) {
|
if let Some((position, new_direction)) = snake_path.pick(transform) {
|
||||||
trace!("change direction for {entity:?} in {position:?} to {new_direction:?}");
|
trace!("change direction for {entity:?} in {position:?} to {new_direction:?}");
|
||||||
*direction = new_direction;
|
*direction = new_direction;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,10 +25,12 @@ pub fn cleanup_snake_path(event: On<GridBarier>, mut commands: Commands, query:
|
|||||||
let transform = transform.clone();
|
let transform = transform.clone();
|
||||||
commands.queue(move |world: &mut World| {
|
commands.queue(move |world: &mut World| {
|
||||||
let mut snake_path = world.resource_mut::<SnakePath>();
|
let mut snake_path = world.resource_mut::<SnakePath>();
|
||||||
|
/*
|
||||||
if let Some((pos, dir)) = snake_path.drop(&transform) {
|
if let Some((pos, dir)) = snake_path.drop(&transform) {
|
||||||
let map = snake_path.debug();
|
let map = snake_path.debug();
|
||||||
trace!("removed entry from SnakePath: {pos:?} => {dir:?}\n\tsnake_path: {map:?}");
|
trace!("removed entry from SnakePath: {pos:?} => {dir:?}\n\tsnake_path: {map:?}");
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,10 +39,12 @@ pub fn cleanup_snake_path(event: On<GridBarier>, mut commands: Commands, query:
|
|||||||
pub fn schedule_set_velocity(tail: Entity) -> impl Fn(On<GridBarier>, Commands, Query<(&mut Velocity, &Transform)>) {
|
pub fn schedule_set_velocity(tail: Entity) -> impl Fn(On<GridBarier>, Commands, Query<(&mut Velocity, &Transform)>) {
|
||||||
move |event: On<GridBarier>, mut commands: Commands, mut query: Query<(&mut Velocity, &Transform)>| {
|
move |event: On<GridBarier>, mut commands: Commands, mut query: Query<(&mut Velocity, &Transform)>| {
|
||||||
if let Ok((mut velocity, transform)) = query.get_mut(tail) {
|
if let Ok((mut velocity, transform)) = query.get_mut(tail) {
|
||||||
|
/*
|
||||||
if event.dst_pos != transform.into() {
|
if event.dst_pos != transform.into() {
|
||||||
velocity.0 = SNAKE_VELOCITY as f32;
|
velocity.0 = SNAKE_VELOCITY as f32;
|
||||||
commands.entity(event.observer()).despawn();
|
commands.entity(event.observer()).despawn();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
domain::{End, GridBarier, SnakePath, Tail},
|
domain::{End, SnakePath, Tail},
|
||||||
observers::{cleanup_snake_path, schedule_set_velocity, tail_observer},
|
observers::{cleanup_snake_path, schedule_set_velocity, tail_observer},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -20,12 +20,14 @@ fn spawn_snake(commands: &mut Commands) {
|
|||||||
commands.insert_resource(SnakePath::default());
|
commands.insert_resource(SnakePath::default());
|
||||||
commands.spawn(Observer::new(cleanup_snake_path));
|
commands.spawn(Observer::new(cleanup_snake_path));
|
||||||
|
|
||||||
|
/*
|
||||||
commands.spawn(Observer::new(|e: On<GridBarier>| {
|
commands.spawn(Observer::new(|e: On<GridBarier>| {
|
||||||
let entity = e.entity;
|
let entity = e.entity;
|
||||||
let src = e.src_pos;
|
let src = e.src_pos;
|
||||||
let dst = e.dst_pos;
|
let dst = e.dst_pos;
|
||||||
trace!("...<{entity:?}> moving {src:>16} => {dst:>16}");
|
trace!("...<{entity:?}> moving {src:>16} => {dst:>16}");
|
||||||
}));
|
}));
|
||||||
|
*/
|
||||||
|
|
||||||
let head = commands
|
let head = commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
use bevy::transform::components::Transform;
|
use bevy::{math::Quat, transform::components::Transform};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_saturating_sub() {
|
fn test_saturating_sub() {
|
||||||
@@ -50,6 +50,12 @@ fn test_ranges() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transform() {
|
fn test_transform() {
|
||||||
dbg!(Transform::default());
|
let mut transform = Transform::default();
|
||||||
dbg!(Transform::default().rotate_z(PI / 2.0));
|
dbg!(transform);
|
||||||
|
transform.rotate_z(PI / 2.0);
|
||||||
|
transform.rotate_z(PI / 2.0);
|
||||||
|
transform.rotate_z(PI / 2.0);
|
||||||
|
transform.rotate_z(PI / 2.0);
|
||||||
|
dbg!(transform);
|
||||||
|
let mut quat = Quat::default();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user