From 1d392198b9e5c27f1072e09c80e4fb8dc74497f8 Mon Sep 17 00:00:00 2001 From: mnmaita <47983254+mnmaita@users.noreply.github.com> Date: Thu, 16 Nov 2023 01:49:27 +0100 Subject: [PATCH] Adds projectile attacks to enemies --- src/game/enemy.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/game/enemy.rs b/src/game/enemy.rs index 6f61d2e..dca7bf2 100644 --- a/src/game/enemy.rs +++ b/src/game/enemy.rs @@ -3,7 +3,10 @@ use rand::seq::IteratorRandom; use crate::{physics::Speed, playing}; -use super::{combat::Range, BorderTile, Hitpoints, Player, TILE_SIZE}; +use super::{ + combat::{AttackDamage, AttackTimer, Range, SpawnProjectileEvent}, + BorderTile, Hitpoints, Player, TILE_SIZE, +}; pub(super) struct EnemyPlugin; @@ -11,14 +14,19 @@ impl Plugin for EnemyPlugin { fn build(&self, app: &mut App) { app.insert_resource(EnemySpawnTimer::new(3.)); - app.add_systems(FixedUpdate, (spawn_enemies, move_enemies).run_if(playing())); + app.add_systems( + FixedUpdate, + (spawn_enemies, move_enemies, attack_player).run_if(playing()), + ); } } #[derive(Bundle)] pub struct EnemyBundle { - pub marker: Enemy, + pub attack_damage: AttackDamage, + pub attack_timer: AttackTimer, pub hitpoints: Hitpoints, + pub marker: Enemy, pub range: Range, pub speed: Speed, pub sprite: SpriteBundle, @@ -48,6 +56,8 @@ fn spawn_enemies( let translation = tile_transform.translation.truncate().extend(1.); commands.spawn(EnemyBundle { + attack_damage: AttackDamage(5), + attack_timer: AttackTimer(Timer::from_seconds(5., TimerMode::Repeating)), hitpoints: Hitpoints::new(1), marker: Enemy, range: Range(TILE_SIZE.x * 3.), @@ -82,3 +92,37 @@ fn move_enemies( } } } + +fn attack_player( + mut spawn_projectile_event_writer: EventWriter, + mut enemy_query: Query< + (Entity, &Transform, &mut AttackTimer, &Range, &AttackDamage), + With, + >, + player_query: Query<&Transform, (With, Without)>, + time: Res