Проектная работа - WIP
голова змеи и ее вращение
This commit is contained in:
@@ -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<Entity>) {
|
||||
commands.entity(entity).log_components();
|
||||
}
|
||||
}
|
||||
|
||||
fn inspect(mut _commands: Commands, _query: Query<Entity>) {}
|
||||
|
||||
@@ -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;
|
||||
@@ -0,0 +1,17 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::domain::Head;
|
||||
|
||||
pub fn head_rotation(
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Single<&mut Transform, With<Head>>,
|
||||
) {
|
||||
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.);
|
||||
}
|
||||
}
|
||||
@@ -36,3 +36,10 @@ pub fn define_log_layer() -> Option<bevy::log::BoxedFmtLayer> {
|
||||
.boxed();
|
||||
Some(Box::new(vec![stdout, log_file]))
|
||||
}
|
||||
|
||||
mod domain;
|
||||
mod gameplay;
|
||||
mod startup;
|
||||
|
||||
pub use gameplay::head_rotation;
|
||||
pub use startup::startup;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<WinitSettings>) {
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user