From 6a0c1349a8497e863297c627afa1b1755129a757 Mon Sep 17 00:00:00 2001 From: Alexander Baranov Date: Sat, 30 May 2026 13:20:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20-=20WIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit голова змеи и ее вращение --- snake-game/examples/bevy_learning.rs | 41 ++++++++++------------ snake-game/src/domain.rs | 12 +++++++ snake-game/src/gameplay.rs | 17 +++++++++ snake-game/src/lib.rs | 7 ++++ snake-game/src/main.rs | 13 +++---- snake-game/src/startup.rs | 52 ++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+), 29 deletions(-) create mode 100644 snake-game/src/domain.rs create mode 100644 snake-game/src/gameplay.rs create mode 100644 snake-game/src/startup.rs diff --git a/snake-game/examples/bevy_learning.rs b/snake-game/examples/bevy_learning.rs index ea3fbd3..8a51ba9 100644 --- a/snake-game/examples/bevy_learning.rs +++ b/snake-game/examples/bevy_learning.rs @@ -22,51 +22,44 @@ fn main() { .insert_resource(WinitSettings::desktop_app()) .insert_resource(ClearColor(Color::srgb(0.3, 0.6, 0.8))) .add_systems(Startup, setup) - .add_systems(PostStartup, print_all_entities) + .add_systems(PostStartup, (print_all_entities, inspect)) .run(); } -fn setup(mut commands: Commands, _window: Query<&Window>) { +fn setup(mut commands: Commands, _window: Query<&Window>, _world: &World) { let _window = _window.single().unwrap(); - // trace!("window: {:?}", window.single()); + let _root = commands + .spawn(( + Sprite::from_color(Color::srgb(1., 1., 1.), Vec2::new(100., 100.)), + Transform { + translation: vec3(0., 0., 0.), + // scale: Vec3::new(1., 1., 1.), + ..Default::default() + }, + )) + .id(); let _red = commands .spawn(( Sprite::from_color(Color::srgb(1., 0., 0.), Vec2::new(30., 30.)), Transform { - translation: vec3(-20., 30., 1.), + translation: vec3(-20., 30., 0.), ..Default::default() }, - // ChildOf(_root), + ChildOf(_root), )) .id(); let _blue = commands .spawn(( Sprite::from_color(Color::srgb(0., 0., 1.), Vec2::new(20., 20.)), Transform { - translation: vec3(20., 30., 1.), + translation: vec3(20., 30., 0.), ..Default::default() }, - // ChildOf(_root), + ChildOf(_root), )) .id(); - let x = -_window.resolution.width() / 2. + 52.; - let y = -_window.resolution.height() / 2. + 52.; - let _root = commands - .spawn(( - Sprite::from_color(Color::srgb(1., 1., 1.), Vec2::new(100., 100.)), - Transform { - translation: vec3(x, y, 1.), - // scale: Vec3::new(1., 1., 1.), - ..Default::default() - }, - )) - .id(); - - commands.entity(_red).insert(ChildOf(_root)); - commands.entity(_blue).insert(ChildOf(_root)); - commands.spawn(Camera2d); } @@ -75,3 +68,5 @@ fn print_all_entities(mut commands: Commands, query: Query) { commands.entity(entity).log_components(); } } + +fn inspect(mut _commands: Commands, _query: Query) {} diff --git a/snake-game/src/domain.rs b/snake-game/src/domain.rs new file mode 100644 index 0000000..17205fd --- /dev/null +++ b/snake-game/src/domain.rs @@ -0,0 +1,12 @@ +use bevy::ecs::component::Component; + +pub mod constants { + 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; +} + +#[derive(Component)] +pub struct Head; diff --git a/snake-game/src/gameplay.rs b/snake-game/src/gameplay.rs new file mode 100644 index 0000000..768e915 --- /dev/null +++ b/snake-game/src/gameplay.rs @@ -0,0 +1,17 @@ +use bevy::prelude::*; + +use crate::domain::Head; + +pub fn head_rotation( + input: Res>, + mut query: Single<&mut Transform, With>, +) { + use core::f32::consts::PI; + + if input.just_pressed(KeyCode::ArrowLeft) { + query.rotate_z(PI / 2.); + } + if input.just_pressed(KeyCode::ArrowRight) { + query.rotate_z(-PI / 2.); + } +} diff --git a/snake-game/src/lib.rs b/snake-game/src/lib.rs index e5dc8d6..9f62892 100644 --- a/snake-game/src/lib.rs +++ b/snake-game/src/lib.rs @@ -36,3 +36,10 @@ pub fn define_log_layer() -> Option { .boxed(); Some(Box::new(vec![stdout, log_file])) } + +mod domain; +mod gameplay; +mod startup; + +pub use gameplay::head_rotation; +pub use startup::startup; diff --git a/snake-game/src/main.rs b/snake-game/src/main.rs index ce2f6ad..ff737a7 100644 --- a/snake-game/src/main.rs +++ b/snake-game/src/main.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use snake_game::{head_rotation, startup}; fn main() { App::new() @@ -6,6 +7,8 @@ fn main() { DefaultPlugins .build() .set(bevy::log::LogPlugin { + level: tracing::Level::INFO, + filter: "wgpu_hal=warn,snake_game=trace".to_string(), fmt_layer: |_| snake_game::define_log_layer(), ..Default::default() }) @@ -13,11 +16,9 @@ fn main() { task_pool_options: TaskPoolOptions::with_num_threads(1), }), ) - .insert_resource(ClearColor(Color::srgb(0.0, 0.75, 1.0))) - .add_systems(Startup, setup) + .insert_resource(bevy::winit::WinitSettings::desktop_app()) + .insert_resource(ClearColor(Color::srgb(0.3, 0.6, 0.8))) + .add_systems(Startup, startup) + .add_systems(Update, head_rotation) .run(); } - -fn setup(mut commands: Commands) { - commands.spawn(Camera2d); -} diff --git a/snake-game/src/startup.rs b/snake-game/src/startup.rs new file mode 100644 index 0000000..26bbfda --- /dev/null +++ b/snake-game/src/startup.rs @@ -0,0 +1,52 @@ +use std::time::Duration; + +use bevy::{prelude::*, winit::WinitSettings}; + +use crate::domain::constants::FRAMES_PER_SECOND; + +/// Инициализация игрового мира +pub fn startup(mut commands: Commands, mut winit: ResMut) { + use crate::domain::{Head, constants::HEAD_SIZE}; + + winit.focused_mode = + bevy::winit::UpdateMode::reactive(Duration::from_secs_f32(1. / FRAMES_PER_SECOND as f32)); + winit.unfocused_mode = + bevy::winit::UpdateMode::reactive(Duration::from_secs_f32(1. / FRAMES_PER_SECOND as f32)); + + let head = commands + .spawn(( + Head, + Sprite::from_color( + Color::srgb(1., 1., 1.), + vec2(HEAD_SIZE as f32, HEAD_SIZE as f32), + ), + Transform { + translation: vec3(0., 0., 0.), + ..Default::default() + }, + )) + .id(); + commands.spawn(( + Sprite::from_color( + Color::srgb(1., 0., 0.), + vec2(HEAD_SIZE as f32 * 0.3, HEAD_SIZE as f32 * 0.3), + ), + Transform { + translation: vec3(HEAD_SIZE as f32 * -0.25, HEAD_SIZE as f32 * 0.25, 0.), + ..Default::default() + }, + ChildOf(head), + )); + commands.spawn(( + Sprite::from_color( + Color::srgb(0., 0., 1.), + vec2(HEAD_SIZE as f32 * 0.2, HEAD_SIZE as f32 * 0.2), + ), + Transform { + translation: vec3(HEAD_SIZE as f32 * 0.25, HEAD_SIZE as f32 * 0.25, 0.), + ..Default::default() + }, + ChildOf(head), + )); + commands.spawn(Camera2d); +}