All current work
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+5751
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "bevy_test"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.17"
|
||||
rand = "0.9"
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 686 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.3 MiB |
@@ -0,0 +1,128 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::PrimaryWindow;
|
||||
|
||||
const ENEMY_SPEED: f32 = 100.0;
|
||||
const ENEMY_SIZE: f32 = 40.0;
|
||||
|
||||
const HOME_X: f32 = -400.0;
|
||||
const SPAWN_X: f32 = 400.0;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Enemy;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
spawn_enemies,
|
||||
move_enemies,
|
||||
click_enemies,
|
||||
),
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
// Дом
|
||||
commands.spawn((
|
||||
Sprite {
|
||||
color: Color::srgb(0.2, 0.8, 0.2),
|
||||
custom_size: Some(Vec2::new(20.0, 600.0)),
|
||||
..default()
|
||||
},
|
||||
Transform::from_xyz(HOME_X, 0.0, 0.0),
|
||||
));
|
||||
|
||||
// Спавн
|
||||
commands.spawn((
|
||||
Sprite {
|
||||
color: Color::srgb(0.8, 0.2, 0.2),
|
||||
custom_size: Some(Vec2::new(20.0, 600.0)),
|
||||
..default()
|
||||
},
|
||||
Transform::from_xyz(SPAWN_X, 0.0, 0.0),
|
||||
));
|
||||
}
|
||||
|
||||
fn spawn_enemies(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
// Пока просто спавним врага каждые 2 секунды.
|
||||
//
|
||||
// Это временный вариант.
|
||||
// Позже сделаем нормальный Timer как отдельный ресурс.
|
||||
|
||||
let seconds = time.elapsed_secs();
|
||||
|
||||
if (seconds % 2.0) < 0.02 {
|
||||
let y = rand::random_range(-250.0..250.0);
|
||||
|
||||
let texture = asset_server.load("test.png");
|
||||
|
||||
commands.spawn((
|
||||
Sprite {
|
||||
image: texture,
|
||||
custom_size: Some(Vec2::splat(ENEMY_SIZE)),
|
||||
..default()
|
||||
},
|
||||
Transform::from_xyz(SPAWN_X, y, 1.0),
|
||||
Enemy,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn move_enemies(
|
||||
mut commands: Commands,
|
||||
mut enemies: Query<(Entity, &mut Transform), With<Enemy>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
for (entity, mut transform) in &mut enemies {
|
||||
transform.translation.x -= ENEMY_SPEED * time.delta_secs();
|
||||
|
||||
// Враг дошёл до дома
|
||||
if transform.translation.x <= HOME_X {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn click_enemies(
|
||||
mouse: Res<ButtonInput<MouseButton>>,
|
||||
windows: Query<&Window, With<PrimaryWindow>>,
|
||||
enemies: Query<(Entity, &Transform), With<Enemy>>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
if !mouse.just_pressed(MouseButton::Left) {
|
||||
return;
|
||||
}
|
||||
|
||||
let window = windows.single().unwrap();
|
||||
|
||||
let Some(cursor) = window.cursor_position() else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Переводим координаты мыши из координат окна
|
||||
// в координаты игрового мира.
|
||||
let mouse_world = Vec2::new(
|
||||
cursor.x - window.width() / 2.0,
|
||||
window.height() / 2.0 - cursor.y,
|
||||
);
|
||||
|
||||
for (entity, transform) in &enemies {
|
||||
let enemy_position = transform.translation.truncate();
|
||||
|
||||
let distance = mouse_world.distance(enemy_position);
|
||||
|
||||
if distance < ENEMY_SIZE / 2.0 {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user