avoid-rs/src/enemy.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2025-01-02 22:15:03 +02:00
use bevy::prelude::*;
2025-01-01 18:52:19 +02:00
2025-01-02 22:15:03 +02:00
use crate::{common::animation::SpriteAnimation, level::Unit};
2025-01-01 18:52:19 +02:00
pub struct EnemyPlugin;
impl Plugin for EnemyPlugin {
fn build(&self, app: &mut App) {
// app.add_systems(Startup, setup);
app.add_systems(Update, debug_direction);
app.add_systems(FixedUpdate, outside_bounds);
}
}
#[derive(Component)]
2025-01-02 22:15:03 +02:00
#[require(Unit, SpriteAnimation)]
2025-01-01 18:52:19 +02:00
pub struct Enemy;
2025-01-02 22:15:03 +02:00
fn debug_direction(query: Query<&Transform, With<Enemy>>, gizmos: Gizmos) {
// for enemy in &query {
// let start = enemy.translation.truncate();
// let end = start + Vec2::new(10.0, 0.0);
// gizmos.arrow_2d(start, end, RED);
// }
2025-01-01 18:52:19 +02:00
}
fn outside_bounds(
enemies: Query<(Entity, &Transform), With<Enemy>>,
window: Query<&Window>,
mut commands: Commands,
) {
let window = window.single();
let screen_size: Vec3 = window.physical_size().as_vec2().extend(1.0);
for (entity, transform) in &enemies {
let pos = transform.translation.truncate();
if pos.x < -60.0
|| pos.y < -60.0
|| pos.x > screen_size.x + 60.0
|| pos.y > screen_size.y + 60.0
{
commands.entity(entity).despawn();
}
}
}